<s> """<STR_LIT>""" <EOL> import os <EOL> import sys <EOL> import eventlet <EOL> from collections import defaultdict <EOL> from eventlet import spawn_n , sleep <EOL> from eventlet import wsgi <EOL> from eventlet import websocket <EOL> from eventlet . green import zmq <EOL> from eventlet . hubs import get_hub , use_hub <EOL> from uuid import uuid1 <EOL> use_hub ( '<STR_LIT>' ) <EOL> ctx = zmq . Context ( ) <EOL> class IDName ( object ) : <EOL> def __init__ ( self ) : <EOL> self . id = uuid1 ( ) <EOL> self . name = None <EOL> def __str__ ( self ) : <EOL> if self . name : <EOL> return self . name <EOL> else : <EOL> return str ( self . id ) <EOL> def pack_message ( self , msg ) : <EOL> return self , msg <EOL> def unpack_message ( self , msg ) : <EOL> sender , message = msg <EOL> sender_name = '<STR_LIT>' if sender . id == self . id else '<STR_LIT>' % sender <EOL> return "<STR_LIT>" % ( sender_name , message ) <EOL> participants = defaultdict ( IDName ) <EOL> def subscribe_and_distribute ( sub_socket ) : <EOL> global participants <EOL> while True : <EOL> msg = sub_socket . recv_pyobj ( ) <EOL> for ws , name_id in participants . items ( ) : <EOL> to_send = name_id . unpack_message ( msg ) <EOL> if to_send : <EOL> try : <EOL> ws . send ( to_send ) <EOL> except : <EOL> del participants [ ws ] <EOL> @ websocket . WebSocketWSGI <EOL> def handle ( ws ) : <EOL> global pub_socket <EOL> name_id = participants [ ws ] <EOL> ws . send ( "<STR_LIT>" % name_id ) <EOL> try : <EOL> while True : <EOL> m = ws . wait ( ) <EOL> if m is None : <EOL> break <EOL> if m . startswith ( '<STR_LIT>' ) : <EOL> old_name = str ( name_id ) <EOL> new_name = m . split ( '<STR_LIT::>' , <NUM_LIT:1> ) [ <NUM_LIT:1> ] . strip ( ) <EOL> name_id . name = new_name <EOL> m = '<STR_LIT>' % old_name <EOL> pub_socket . send_pyobj ( name_id . pack_message ( m ) ) <EOL> sleep ( ) <EOL> finally : <EOL> del participants [ ws ] <EOL> def dispatch ( environ , start_response ) : <EOL> """<STR_LIT>""" <EOL> global port <EOL> if environ [ '<STR_LIT>' ] == '<STR_LIT>' : <EOL> return handle ( environ , start_response ) <EOL> else : <EOL> start_response ( '<STR_LIT>' , [ ( '<STR_LIT>' , '<STR_LIT>' ) ] ) <EOL> return [ open ( os . path . join ( <EOL> os . path . dirname ( __file__ ) , <EOL> '<STR_LIT>' ) ) . read ( ) % dict ( port = port ) ] <EOL> port = None <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> usage = '<STR_LIT>' <EOL> if len ( sys . argv ) != <NUM_LIT:6> : <EOL> print ( usage ) <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> pub_addr = sys . argv [ <NUM_LIT:2> ] <EOL> sub_addr = sys . argv [ <NUM_LIT:4> ] <EOL> try : <EOL> port = int ( sys . argv [ <NUM_LIT:5> ] ) <EOL> except ValueError : <EOL> print ( "<STR_LIT>" , usage ) <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> try : <EOL> pub_socket = ctx . socket ( zmq . PUB ) <EOL> pub_socket . connect ( pub_addr ) <EOL> print ( "<STR_LIT>" % pub_addr ) <EOL> sub_socket = ctx . socket ( zmq . SUB ) <EOL> sub_socket . connect ( sub_addr ) <EOL> sub_socket . setsockopt ( zmq . SUBSCRIBE , "<STR_LIT>" ) <EOL> print ( "<STR_LIT>" % sub_addr ) <EOL> except : <EOL> print ( "<STR_LIT>" , usage ) <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> spawn_n ( subscribe_and_distribute , sub_socket ) <EOL> listener = eventlet . listen ( ( '<STR_LIT:127.0.0.1>' , port ) ) <EOL> print ( "<STR_LIT>" % port ) <EOL> wsgi . server ( listener , dispatch ) </s>
