<s> '''<STR_LIT>''' <EOL> import sys <EOL> from diesel import ( <EOL> Application , Service , until_eol , fire , first , send , Client , call , thread , <EOL> fork , Loop , <EOL> ) <EOL> from diesel . util . queue import Queue <EOL> def chat_server ( addr ) : <EOL> my_nick = until_eol ( ) . strip ( ) <EOL> while True : <EOL> evt , data = first ( until_eol = True , waits = [ '<STR_LIT>' ] ) <EOL> if evt == '<STR_LIT>' : <EOL> fire ( '<STR_LIT>' , ( my_nick , data . strip ( ) ) ) <EOL> else : <EOL> nick , message = data <EOL> send ( "<STR_LIT>" % ( nick , message ) ) <EOL> class ChatClient ( Client ) : <EOL> def __init__ ( self , * args , ** kw ) : <EOL> Client . __init__ ( self , * args , ** kw ) <EOL> self . input = Queue ( ) <EOL> def read_chat_message ( self , prompt ) : <EOL> msg = raw_input ( prompt ) <EOL> return msg <EOL> def input_handler ( self ) : <EOL> nick = thread ( self . read_chat_message , "<STR_LIT>" ) . strip ( ) <EOL> self . nick = nick <EOL> self . input . put ( nick ) <EOL> while True : <EOL> msg = thread ( self . read_chat_message , "<STR_LIT>" ) . strip ( ) <EOL> self . input . put ( msg ) <EOL> @ call <EOL> def chat ( self ) : <EOL> fork ( self . input_handler ) <EOL> nick = self . input . get ( ) <EOL> send ( "<STR_LIT>" % nick ) <EOL> while True : <EOL> evt , data = first ( until_eol = True , waits = [ self . input ] ) <EOL> if evt == "<STR_LIT>" : <EOL> print data . strip ( ) <EOL> else : <EOL> send ( "<STR_LIT>" % data ) <EOL> def chat_client ( ) : <EOL> with ChatClient ( '<STR_LIT:localhost>' , <NUM_LIT> ) as c : <EOL> c . chat ( ) <EOL> app = Application ( ) <EOL> if sys . argv [ <NUM_LIT:1> ] == "<STR_LIT>" : <EOL> app . add_service ( Service ( chat_server , <NUM_LIT> ) ) <EOL> elif sys . argv [ <NUM_LIT:1> ] == "<STR_LIT>" : <EOL> app . add_loop ( Loop ( chat_client ) ) <EOL> else : <EOL> print "<STR_LIT>" % sys . argv [ <NUM_LIT:0> ] <EOL> raise SystemExit ( <NUM_LIT:1> ) <EOL> app . run ( ) </s>
