Advertisement
Guest User

Untitled

a guest
Jun 25th, 2016
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.60 KB | None | 0 0
  1. import asyncio
  2.  
  3. from .utility import Next
  4.  
  5. __all__ = [
  6. "ChannelHandler", "InboundHandler", "OutboundHandler",
  7. "Pipeline", "Channel"
  8. ]
  9.  
  10. class ChannelHandler:
  11.  
  12. async def connected(self, channel):
  13. raise NotImplementedError()
  14.  
  15. async def raised(self, channel, exception):
  16. raise NotImplementedError()
  17.  
  18. async def disconnected(self, channel):
  19. raise NotImplementedError()
  20.  
  21. class InboundHandler:
  22.  
  23. async def received(self, channel, data, next):
  24. raise NotImplementedError()
  25.  
  26. class OutboundHandler:
  27.  
  28. async def sent(self, channel, message, next):
  29. raise NotImplementedError()
  30.  
  31. class Pipeline:
  32.  
  33. def __init__(self, channel, channel_handler, inbound_handlers, outbound_handlers):
  34. self.channel = channel
  35. self.channel_handler = channel_handler
  36. self.inbound_handlers = inbound_handlers
  37. self.outbound_handlers = outbound_handlers
  38.  
  39. async def handle_raise_event(self, exception):
  40. await self.channel_handler.raised(channel, exception)
  41.  
  42. async def handle_receive_event(self, data):
  43. await Next(self.inbound_handlers).next(self.channel, data)
  44.  
  45. async def handle_send_event(self, message):
  46. await Next(self.outbound_handlers).next(self.channel, message)
  47.  
  48. class Channel:
  49.  
  50. def __init__(self, url, channel_handler, inbound_handlers, outbound_handlers, options={}):
  51. self.url = url
  52. self.pipeline = Pipeline(self, channel_handler, inbound_handlers, outbound_handlers)
  53. self.options = options
  54.  
  55. async def connect(self):
  56. pass
  57.  
  58. async def send(self, message):
  59. pass
  60.  
  61. async def disconnect(self):
  62. pass
  63.  
  64. async def connect(url, channel_handler, inbound_handlers, outbound_handlers, options={}):
  65. pass
  66.  
  67. import transport
  68.  
  69. class DummyChannelHandler:
  70.  
  71. async def connected(self, channel):
  72. print("Connected.")
  73.  
  74. async def raised(self, channel, exception):
  75. print("Error:", exception)
  76. await channel.disconnect()
  77.  
  78. async def disconnected(self, channel):
  79. print("Disconected.")
  80.  
  81. class DummyInboundHandler:
  82.  
  83. async def received(self, channel, data, next):
  84. print("Received:", data)
  85.  
  86. class DummyOutboundHandler:
  87.  
  88. async def sent(self, channel, message, next):
  89. await channel.send(message.encode())
  90.  
  91. async def main():
  92. channel = await transport.connect(
  93. "udp://192.168.20.244:55300",
  94. DummyChannelHandler(),
  95. [DummyInboundHandler()],
  96. [DummyOutboundHandler()]
  97. )
  98.  
  99. await channel.send("Hi!")
  100.  
  101. if __name__ == "__main__":
  102. loop = asyncio.get_event_loop()
  103. loop.run_until_complete(main())
  104. loop.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement