<s> from twisted . internet . protocol import Protocol , ServerFactory <EOL> from twisted . python import components <EOL> from zope . interface import implements <EOL> import stomper , simplejson , traceback <EOL> from service import IPyGoWaveService , IStompProtocolFactory <EOL> __all__ = [ "<STR_LIT>" ] <EOL> class IStompServerProtocolFactory ( IStompProtocolFactory ) : <EOL> """<STR_LIT>""" <EOL> class StompServerProtocol ( Protocol ) : <EOL> id = <NUM_LIT:0> <EOL> def __init__ ( self ) : <EOL> self . state = '<STR_LIT>' <EOL> self . stompBuffer = stomper . stompbuffer . StompBuffer ( ) <EOL> StompServerProtocol . id += <NUM_LIT:1> <EOL> self . id = StompServerProtocol . id <EOL> def dataReceived ( self , data ) : <EOL> self . stompBuffer . appendData ( data . replace ( '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> while True : <EOL> msg = self . stompBuffer . getOneMessage ( ) <EOL> if self . stompBuffer . buffer . startswith ( '<STR_LIT:\n>' ) : <EOL> self . stompBuffer . buffer = self . stompBuffer . buffer [ <NUM_LIT:1> : ] <EOL> if msg is None or ( not msg [ '<STR_LIT>' ] and not msg [ '<STR_LIT:body>' ] and not msg [ '<STR_LIT>' ] ) : <EOL> break <EOL> msg [ '<STR_LIT>' ] = msg [ '<STR_LIT>' ] . lower ( ) <EOL> getattr ( self , '<STR_LIT>' % self . state ) ( ** msg ) <EOL> def read_initial ( self , cmd , headers , body ) : <EOL> assert cmd == '<STR_LIT>' , "<STR_LIT>" <EOL> self . state = '<STR_LIT>' <EOL> self . sendFrame ( '<STR_LIT>' , { "<STR_LIT>" : self . id } , "<STR_LIT>" ) <EOL> self . factory . connected ( self ) <EOL> def sendError ( self , e ) : <EOL> exception , instance , tb = traceback . sys . exc_info ( ) <EOL> tbOutput = "<STR_LIT>" . join ( traceback . format_tb ( tb ) ) <EOL> self . sendFrame ( '<STR_LIT>' , { '<STR_LIT:message>' : str ( e ) } , tbOutput ) <EOL> def sendFrame ( self , cmd , headers , body ) : <EOL> f = stomper . Frame ( ) <EOL> f . cmd = cmd <EOL> f . headers . update ( headers ) <EOL> f . body = body <EOL> self . transport . write ( f . pack ( ) ) <EOL> def read_connected ( self , cmd , headers , body ) : <EOL> return getattr ( self , '<STR_LIT>' % cmd ) ( headers , body ) <EOL> def frame_subscribe ( self , headers , body ) : <EOL> self . factory . subscribe ( self , headers [ '<STR_LIT>' ] ) <EOL> def frame_unsubscribe ( self , headers , body ) : <EOL> self . factory . unsubscribe ( self , headers [ '<STR_LIT>' ] ) <EOL> def frame_send ( self , headers , body ) : <EOL> self . factory . send ( headers [ '<STR_LIT>' ] , body , headers ) <EOL> def frame_disconnect ( self , headers , body ) : <EOL> self . transport . loseConnection ( ) <EOL> def connectionLost ( self , reason ) : <EOL> self . factory . disconnected ( self ) <EOL> class StompServerFactoryFromService ( ServerFactory ) : <EOL> protocol = StompServerProtocol <EOL> implements ( IStompServerProtocolFactory ) <EOL> def __init__ ( self , service ) : <EOL> self . service = service <EOL> self . subscriptions = { } <EOL> self . destinations = { } <EOL> def subscribe ( self , proto , name ) : <EOL> self . subscriptions [ proto . id ] . append ( name ) <EOL> self . destinations [ name ] = proto <EOL> def unsubscribe ( self , proto , name ) : <EOL> self . subscriptions [ proto . id ] . remove ( name ) <EOL> del self . destinations [ name ] <EOL> def connected ( self , proto ) : <EOL> self . subscriptions [ proto . id ] = [ ] <EOL> def disconnected ( self , proto ) : <EOL> for sub in self . subscriptions [ proto . id ] : <EOL> self . unsubscribe ( proto , sub ) <EOL> del self . subscriptions [ proto . id ] <EOL> def send ( self , dest_name , body , headers = { } ) : <EOL> msg_dict = self . service . process ( dest_name , simplejson . loads ( body . decode ( "<STR_LIT:utf-8>" ) ) ) <EOL> for out_rkey , messages in msg_dict . iteritems ( ) : <EOL> self . sendMessagesTo ( out_rkey , messages ) <EOL> def sendMessagesTo ( self , rkey , messages ) : <EOL> if self . destinations . has_key ( rkey ) : <EOL> self . destinations [ rkey ] . sendFrame ( '<STR_LIT>' , { '<STR_LIT>' : str ( rkey ) } , simplejson . dumps ( messages ) . encode ( "<STR_LIT:utf-8>" ) ) <EOL> def startFactory ( self ) : <EOL> ServerFactory . startFactory ( self ) <EOL> self . service . factoryReady ( self ) <EOL> def __repr__ ( self ) : <EOL> return "<STR_LIT>" <EOL> components . registerAdapter ( StompServerFactoryFromService , IPyGoWaveService , IStompServerProtocolFactory ) </s>
