mirror of
https://github.com/bigchaindb/bigchaindb.git
synced 2024-10-13 13:34:05 +00:00
Code cleanup, rename some vars
This commit is contained in:
parent
a92c091eeb
commit
64a033b17a
@ -29,15 +29,15 @@ class Dispatcher:
|
|||||||
self.event_source = event_source
|
self.event_source = event_source
|
||||||
self.subscribers = {}
|
self.subscribers = {}
|
||||||
|
|
||||||
def subscribe(self, uuid, ws):
|
def subscribe(self, uuid, websocket):
|
||||||
"""Add a websocket to the list of subscribers.
|
"""Add a websocket to the list of subscribers.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
uuid (str): a unique identifier for the websocket.
|
uuid (str): a unique identifier for the websocket.
|
||||||
ws: the websocket to publish information.
|
websocket: the websocket to publish information.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
self.subscribers[uuid] = ws
|
self.subscribers[uuid] = websocket
|
||||||
|
|
||||||
@asyncio.coroutine
|
@asyncio.coroutine
|
||||||
def publish(self):
|
def publish(self):
|
||||||
@ -47,8 +47,8 @@ class Dispatcher:
|
|||||||
event = yield from self.event_source.get()
|
event = yield from self.event_source.get()
|
||||||
if event == POISON_PILL:
|
if event == POISON_PILL:
|
||||||
return
|
return
|
||||||
for uuid, ws in self.subscribers.items():
|
for uuid, websocket in self.subscribers.items():
|
||||||
ws.send_str(event)
|
websocket.send_str(event)
|
||||||
|
|
||||||
|
|
||||||
@asyncio.coroutine
|
@asyncio.coroutine
|
||||||
@ -56,20 +56,20 @@ def websocket_handler(request):
|
|||||||
"""Handle a new socket connection."""
|
"""Handle a new socket connection."""
|
||||||
|
|
||||||
logger.debug('New websocket connection.')
|
logger.debug('New websocket connection.')
|
||||||
ws = web.WebSocketResponse()
|
websocket = web.WebSocketResponse()
|
||||||
yield from ws.prepare(request)
|
yield from websocket.prepare(request)
|
||||||
uuid = uuid4()
|
uuid = uuid4()
|
||||||
request.app['dispatcher'].subscribe(uuid, ws)
|
request.app['dispatcher'].subscribe(uuid, websocket)
|
||||||
|
|
||||||
while True:
|
while True:
|
||||||
# Consume input buffer
|
# Consume input buffer
|
||||||
msg = yield from ws.receive()
|
msg = yield from websocket.receive()
|
||||||
if msg.type == aiohttp.WSMsgType.ERROR:
|
if msg.type == aiohttp.WSMsgType.ERROR:
|
||||||
logger.debug('Websocket exception: {}'.format(ws.exception()))
|
logger.debug('Websocket exception: %s', websocket.exception())
|
||||||
return
|
return
|
||||||
|
|
||||||
|
|
||||||
def init_app(event_source, loop=None):
|
def init_app(event_source, *, loop=None):
|
||||||
"""Init the application server.
|
"""Init the application server.
|
||||||
|
|
||||||
Return:
|
Return:
|
||||||
@ -87,17 +87,33 @@ def init_app(event_source, loop=None):
|
|||||||
return app
|
return app
|
||||||
|
|
||||||
|
|
||||||
@asyncio.coroutine
|
def start(event_source, *, loop=None):
|
||||||
def constant_event_source(event_source):
|
"""Create and start the WebSocket server."""
|
||||||
while True:
|
|
||||||
yield from asyncio.sleep(1)
|
if not loop:
|
||||||
yield from event_source.put('meow')
|
loop = asyncio.get_event_loop()
|
||||||
|
|
||||||
|
app = init_app(event_source, loop=loop)
|
||||||
|
aiohttp.web.run_app(app, port=9985)
|
||||||
|
|
||||||
|
|
||||||
|
def test_websocket_server():
|
||||||
|
"""Set up a server and output a message every second.
|
||||||
|
Used for testing purposes."""
|
||||||
|
|
||||||
|
@asyncio.coroutine
|
||||||
|
def constant_event_source(event_source):
|
||||||
|
"""Put a message in ``event_source`` every second."""
|
||||||
|
|
||||||
|
while True:
|
||||||
|
yield from asyncio.sleep(1)
|
||||||
|
yield from event_source.put('meow')
|
||||||
|
|
||||||
|
loop = asyncio.get_event_loop()
|
||||||
|
event_source = asyncio.Queue()
|
||||||
|
loop.create_task(constant_event_source(event_source))
|
||||||
|
start(event_source, loop=loop)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
loop = asyncio.get_event_loop()
|
test_websocket_server()
|
||||||
event_source = asyncio.Queue()
|
|
||||||
|
|
||||||
loop.create_task(constant_event_source(event_source))
|
|
||||||
app = init_app(event_source, loop=loop)
|
|
||||||
aiohttp.web.run_app(app, port=9985)
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user