def packetReceived( self, packet ): debug( "Going to deal with %s packet.", packet.__class__.__name__ ) if not self.__packets: self.__packets = PacketFactory()[ packet._version ] Fail = self.__packets.use( 'Fail' ) if self.lastSeq != None and self.lastSeq >= packet._sequence: response = Fail( packet._sequence, "Frame", "Wrong sequence number!", [] ) else: self.lastSeq = packet._sequence try: handler = CommandDispatcher()[ packet.__class__.__name__ ]( self.__packets, self.__context ) except KeyError: error( "No handler for %s command!", packet._name ) response = Fail( packet._sequence, "UnavailablePermanently", "Command '%s' not supported!" % packet._name ) else: debug( "Calling %s handler method.", handler.__class__.__name__ ) try: response = handler( packet ) except Exception as ex: exception( "Handler for command %s failed:\n%s", packet.__class__.__name__, ex ) response = None if not response: response = Fail( packet._sequence, "UnavailablePermanently", "Internal server error!", []) self.sendResponse( response )
class ClientSessionHandler( object ): def __init__( self ): self.__packets = None self.__context = ClientSessionContext() self.lastSeq = None def sessionStarted( self, protocol ): self.protocol = protocol def packetReceived( self, packet ): debug( "Going to deal with %s packet.", packet.__class__.__name__ ) if not self.__packets: self.__packets = PacketFactory()[ packet._version ] Fail = self.__packets.use( 'Fail' ) if self.lastSeq != None and self.lastSeq >= packet._sequence: response = Fail( packet._sequence, "Frame", "Wrong sequence number!", [] ) else: self.lastSeq = packet._sequence try: handler = CommandDispatcher()[ packet.__class__.__name__ ]( self.__packets, self.__context ) except KeyError: error( "No handler for %s command!", packet._name ) response = Fail( packet._sequence, "UnavailablePermanently", "Command '%s' not supported!" % packet._name ) else: debug( "Calling %s handler method.", handler.__class__.__name__ ) try: response = handler( packet ) except Exception as ex: exception( "Handler for command %s failed:\n%s", packet.__class__.__name__, ex ) response = None if not response: response = Fail( packet._sequence, "UnavailablePermanently", "Internal server error!", []) self.sendResponse( response ) def sendResponse( self, response ): if isinstance( response, list ): for r in response: self.protocol.sendPacket( r ) else: self.protocol.sendPacket( response ) def connectionLost( self, reason ): pass