Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import asyncio
- from .utility import Next
- __all__ = [
- "ChannelHandler", "InboundHandler", "OutboundHandler",
- "Pipeline", "Channel"
- ]
- class ChannelHandler:
- async def connected(self, channel):
- raise NotImplementedError()
- async def raised(self, channel, exception):
- raise NotImplementedError()
- async def disconnected(self, channel):
- raise NotImplementedError()
- class InboundHandler:
- async def received(self, channel, data, next):
- raise NotImplementedError()
- class OutboundHandler:
- async def sent(self, channel, message, next):
- raise NotImplementedError()
- class Pipeline:
- def __init__(self, channel, channel_handler, inbound_handlers, outbound_handlers):
- self.channel = channel
- self.channel_handler = channel_handler
- self.inbound_handlers = inbound_handlers
- self.outbound_handlers = outbound_handlers
- async def handle_raise_event(self, exception):
- await self.channel_handler.raised(channel, exception)
- async def handle_receive_event(self, data):
- await Next(self.inbound_handlers).next(self.channel, data)
- async def handle_send_event(self, message):
- await Next(self.outbound_handlers).next(self.channel, message)
- class Channel:
- def __init__(self, url, channel_handler, inbound_handlers, outbound_handlers, options={}):
- self.url = url
- self.pipeline = Pipeline(self, channel_handler, inbound_handlers, outbound_handlers)
- self.options = options
- async def connect(self):
- pass
- async def send(self, message):
- pass
- async def disconnect(self):
- pass
- async def connect(url, channel_handler, inbound_handlers, outbound_handlers, options={}):
- pass
- import transport
- class DummyChannelHandler:
- async def connected(self, channel):
- print("Connected.")
- async def raised(self, channel, exception):
- print("Error:", exception)
- await channel.disconnect()
- async def disconnected(self, channel):
- print("Disconected.")
- class DummyInboundHandler:
- async def received(self, channel, data, next):
- print("Received:", data)
- class DummyOutboundHandler:
- async def sent(self, channel, message, next):
- await channel.send(message.encode())
- async def main():
- channel = await transport.connect(
- "udp://192.168.20.244:55300",
- DummyChannelHandler(),
- [DummyInboundHandler()],
- [DummyOutboundHandler()]
- )
- await channel.send("Hi!")
- if __name__ == "__main__":
- loop = asyncio.get_event_loop()
- loop.run_until_complete(main())
- loop.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement