def inBound_onUnbind( self, event ): self.state = self.STATE_UNBINDING self.socket.close() self.socket = None event.binding = self self.state = self.STATE_UNBOUND Dispatcher.notify( StateEvent( State.EVENT_STATE_CHANGE, self.state, self.STATE_BOUND ) )
def inConnected_onUnbind( self, event ): self.state = self.STATE_DISCONNECTING self.socket.shutdown( SHUT_RDWR ) self.state = self.STATE_DISCONNECTED newEvent = NetEvent( NetEvent.EVENT_DISCONNECTED, self.transport, self.localAddress, self.localPort, self.remoteAddress, self.remotePort, connection=self ) Dispatcher.notify( newEvent )
def inInitial_onRequest( self, event ): self.request = event self.state.changeState( self.STATE_TRYING ) # Send request to server. Dispatcher.getInstance().notify( Event( self.NOTIFICATION_STARTED ), messageEvent = event ) #FIXME: This notify is also below... # Inform the TU. Dispatcher.getInstance().notify( Event( self.NOTIFICATION_STARTED ) )
def close( self, event ): self.state = self.STATE_DISCONNECTING self.socket.close() self.socket = None self.state = self.STATE_DISCONNECTED newEvent = NetEvent( NetEvent.EVENT_DISCONNECTED, self.transport, self.localAddress, self.localPort, self.remoteAddress, self.remotePort, connection=self ) Dispatcher.notify( newEvent )
def inInitial_onRequest( self, event ): self.lastRequest = event # Pass request to TU. if self.auto100: # Send 100 response to client. e = createResponseEvent( event, 100 ) Dispatcher.getInstance().notify( e ) self.state.changeState( self.STATE_TRYING )
def inInitial_onBind( self, event ): self.state = self.STATE_BINDING self.socket = socket( AF_INET, SOCK_DGRAM ) self.socket.setblocking( 0 ) self.socket.bind( ( self.localAddress, self.localPort ) ) self.socket.setsockopt( SOL_SOCKET, SO_REUSEADDR, 1 ) event.binding = self self.state = self.STATE_BOUND Dispatcher.notify( StateEvent( State.EVENT_STATE_CHANGE, self.state, self.STATE_INITIAL ) )
def inBound_onRead( self, event ): if self.socket: try: buffer, source = self.socket.recvfrom( self.query( 'network.maxmessage' ), self.query( 'network.recvfromflags' ) ) self.log.info( 'recvfrom:\n%s(%s:%s %s:%s)\n%s.' % ( str(self.transport), str(self.localAddress), str(self.localPort), str(source[0]), str(source[1]), str(buffer) ) ) newEvent = NetEvent( NetEvent.EVENT_RX_PACKET, self.transport, self.localAddress, self.localPort, remoteAddress=source[0], remotePort=source[1], packet=str(buffer) ) Dispatcher.notify( newEvent ) except error, e: newEvent = NetError( e[0], e[1], self.transport, self.localAddress, self.localPort ) Dispatcher.notify( newEvent )
def inConnected_onRead( self, event ): if self.socket: buffer = self.socket.recv( self.query( 'network.maxmessage' ), self.query( 'network.recvflags' ) ) if buffer: self.log.info( 'recv:\n%s(%s:%s %s:%s)\n%s.' % ( str(self.transport), str(self.localAddress), str(self.localPort), str(self.remoteAddress), str(self.remotePort), str(buffer) ) ) newEvent = NetEvent( NetEvent.EVENT_RX_PACKET, self.transport, self.localAddress, self.localPort, self.remoteAddress, self.remotePort, packet=str(buffer), connection=self ) Dispatcher.notify( newEvent ) else: # Remote disconnect. self.close( event )
def onTxPacket( self, event ): event.id = BindingBase.EVENT_QUEUE name = Network.getBindingName( event.transport, event.localAddress, event.localPort ) binding = self.bindings[name] if binding: Dispatcher.send( event, srcObj=self, dstObj=binding ) else: for binding in self.bindings.itervalues(): result = Dispatcher.send( event, srcObj=self, dstObj=binding ) if result: break
def inInitial_onBind( self, event ): self.state = self.STATE_BINDING #Dispatcher.notify( StateEvent( State.EVENT_STATE_CHANGE, self.state, self.STATE_INITIAL ) ) self.socket = socket( AF_INET, SOCK_STREAM ) self.socket.setblocking( 0 ) self.socket.bind( ( self.localAddress, self.localPort ) ) self.socket.setsockopt( SOL_SOCKET, SO_REUSEADDR, 1 ) self.socket.listen( self.query( 'network.maxconnections' ) ) event.binding = self self.state = self.STATE_BOUND Dispatcher.notify( StateEvent( State.EVENT_STATE_CHANGE, self.state, self.STATE_INITIAL ) )
def runTest( self ): d = Dispatcher.getInstance() c = Context( 'Root' ) c.start() p = TestParent() d.add( obj=p, parentObj=None, context=c ) t = UasTransaction() d.add( obj=t, parentObj=None, context=c ) d.addListener( t, p ) s = 'SUBSCRIBE sip:chloe@cave;treats SIP/2.0\r\nTo: "Matt"<sip:matthew@cave>\r\nContact: "RileyMan"<sip:riley@cave>\r\nFrom: "Josh"<sip:joshua@cave>\r\n\r\n' m = Message( s ) e = MessageEvent( MessageEvent.EVENT_RX, message=m, transport='udp', localAddress='127.0.0.1', localPort=9000, remoteAddress='127.0.0.1', remotePort=9001 ) d.send( e, srcObj = p, dstObj = t ) s = 'SIP/2.0 100 Trying\r\nTo: "Matt"<sip:matthew@cave>\r\nContact: "RileyMan"<sip:riley@cave>\r\nFrom: "Josh"<sip:joshua@cave>\r\n\r\n' m = Message( s ) e = MessageEvent( MessageEvent.EVENT_TX, message=m, transport='udp', localAddress='127.0.0.1', localPort=9000, remoteAddress='127.0.0.1', remotePort=9001 ) d.send( e, srcObj = p, dstObj = t ) s = 'SIP/2.0 200 OK\r\nTo: "Matt"<sip:matthew@cave>\r\nContact: "RileyMan"<sip:riley@cave>\r\nFrom: "Josh"<sip:joshua@cave>\r\n\r\n' m = Message( s ) e = MessageEvent( MessageEvent.EVENT_TX, message=m, transport='udp', localAddress='127.0.0.1', localPort=9000, remoteAddress='127.0.0.1', remotePort=9001 ) d.send( e, srcObj = p, dstObj = t ) d.send( Event( t.EVENT_TIMER_J ), srcObj = p, dstObj = t ) c.stop()
def inBound_onQueue( self, event ): self.__queue.put( event ) self.remoteAddress = event.remoteAddress self.remotePort = event.remotePort #FIXME: self.socket.settimeout( self.query( 'network.connecttimeout' ) ) try: self.socket.connect( (event.remoteAddress, event.remotePort) ) self.state = self.STATE_CONNECTING return True except error, e: newEvent = NetError( e[0], e[1], self.transport, self.localAddress, self.localPort, connection=self ) Dispatcher.notify( newEvent ) self.close( event ) return False
def run( self ): while True: timeout = self.query( 'network.selecttimeout' ) readSet = set() writeSet = set() errorSet = set() #FIXME: There needs to be locking here since a socket can # be killed while we are enumerating. for binding in self.bindings.itervalues(): binding.getReadyBindings( readSet, writeSet, errorSet ) read = [ binding for binding in readSet ] write = [ binding for binding in writeSet ] error = [ binding for binding in errorSet ] if len(errorSet) or len(readSet) or len(writeSet): read, write, error = select( read, write, error, timeout ) for binding in read: event = Event( binding.EVENT_READ ) Dispatcher.send( event, srcObj=self, dstObj=binding ) for binding in write: event = Event( binding.EVENT_WRITE ) Dispatcher.send( event, srcObj=self, dstObj=binding ) for binding in error: event = Event( binding.EVENT_ERROR ) Dispatcher.send( event, srcObj=self, dstObj=binding ) else: sleep( timeout )
def changeState( self, newState, notify = False ): """ Transition to the new state and optionally notify listeners. The state transition will also generate and process internal leave and enter events. """ if self.currentState == None: raise Exception( 'This state does not have a current state.' ) oldState = self.currentState # Only execute block if the state is really changing. if oldState != newState: # Stop state timer. self.stopStateTimer() # Leave pseudo-event. Dispatcher.getInstance().send( StateEvent( self.EVENT_LEAVE, newState = newState, oldState = oldState ), self.obj, self.obj ) # Change state. self.log.info( 'Changing state from %s to %s.' % ( str(oldState), str(newState) ) ) self.currentState = newState # Enter pseudo-event. Dispatcher.getInstance().send( StateEvent( self.EVENT_ENTER, newState = newState, oldState = oldState ), self.obj, self.obj ) # Notify listeners. if notify: self.log.info( 'Notify listeners of the state change.' ) Dispatcher.getInstance().notify( StateEvent( self.EVENT_STATE_CHANGE, newState = newState, oldState = oldState ), self.obj ) # Start state timer. self.startStateTimer()
def startStateTimer( self ): """ If the current state is configured with a state timeout, then start the state timer. """ stateTimeout = self.stateTimeouts.get( self.currentState, None ) if stateTimeout: self.log.info( 'Start state timer with a timeout of ' + str(stateTimeout) + '.' ) if self.stateTimer: self.stopStateTimer() self.stateTimer = Dispatcher.getInstance().schedule( stateTimeout, StateEvent( self.EVENT_TIMEOUT, newState = self.currentState ), self.obj, self.obj )
def onBind( self, event ): name = str(event.transport) + ':' + str(event.localAddress) + ':' + str(event.localPort) binding = Binding( event ) Dispatcher.add( binding, parentObj=self ) Dispatcher.addListener( binding, self ) name = Network.getBindingName( event.transport, event.localAddress, event.localPort ) self.bindings[name] = binding Dispatcher.send( event, srcObj=self, dstObj=binding ) if not self.__thread: self.__thread = Thread( None, self.run ) self.__thread.start()
def runTest( self ): d = Dispatcher.getInstance() c = Context( 'Root' ) c.start() p = TestParent() d.add( obj=p, parentObj=None, context=c ) t = UacTransactionInvite() d.add( obj=t, parentObj=None, context=c ) d.addListener( t, p ) d.send( Event( 'INVITE' ), srcObj = p, dstObj = t ) d.send( Event( 'TimerA' ), srcObj = p, dstObj = t ) d.send( Event( '1xx' ), srcObj = p, dstObj = t ) d.send( Event( '3xx' ), srcObj = p, dstObj = t ) d.send( Event( 'TimerD' ), srcObj = p, dstObj = t ) c.stop()
def runTest( self ): d = Dispatcher.getInstance() c = Context( 'Root' ) c.start() o = self.TestObj() d.add( obj=o, parentObj=None, context=c ) o.Start() assert( str(o.state.currentState) == o.STATE_STARTED ) assert( str(o.lastHandler) == 'inStarted_onEnter' ) o.Pause() assert( str(o.state.currentState) == 'Paused' ) assert( str(o.lastHandler) == 'inPaused_onEnter' ) o.Stop() assert( str(o.state.currentState) == 'Stopped' ) assert( str(o.lastHandler) == 'inStopped_onEnter' ) c.stop()
def accept( self ): if self.socket: (newSocket, remote) = self.socket.accept() connection = TcpConnection( self.transport, self.localAddress, self.localPort, remoteAddress=remote[0], remotePort=remote[1], csocket=newSocket ) Dispatcher.add( connection, parentObj=self ) Dispatcher.addListener( connection, self ) name = Network.getBindingName( connection.transport, connection.remoteAddress, connection.remotePort ) self.connections[name] = connection # Send the TCP connection's connected notification to ourselves. # This is a work-around, since we are constructing the connection in the connected state and # we do not want to send the event in the TCP connection's constructor. newEvent = NetEvent( NetEvent.EVENT_CONNECTED, connection.transport, connection.localAddress, connection.localPort, connection.remoteAddress, connection.remotePort, connection=connection ) Dispatcher.send( newEvent, srcObj=self, dstObj=self )
def inBound_onQueue( self, event ): if not event.connection: if ( event.localAddress != self.localAddress ) or ( event.localPort != self.localPort ): # This is not our binding. return # Let the network layer choose the local port. event.localPort = 0 # Create a new connection and queue the event in the connection. connection = TcpConnection( event.transport, event.localAddress, event.localPort, event.remoteAddress, event.remotePort ) Dispatcher.add( connection, parentObj=self ) Dispatcher.addListener( connection, self ) name = Network.getBindingName( connection.transport, connection.remoteAddress, connection.remotePort ) self.connections[name] = connection Dispatcher.send( event, srcObj=self, dstObj=connection )
def runTest( self ): d = Dispatcher.getInstance() c = Context( 'Root' ) c.start() dialog = UasDialogSubscribe() #FIXME: pass event? d.add( obj=dialog, parentObj=None, context=c ) s = 'SUBSCRIBE sip:chloe@cave;treats SIP/2.0\r\nTo: "Matt"<sip:matthew@cave>\r\nContact: "RileyMan"<sip:riley@cave>\r\nFrom: "Josh"<sip:joshua@cave>\r\nExpires: 3600\r\nCall-ID: abcd\r\n\r\n' m = Message( s ) e = MessageEvent( MessageEvent.EVENT_RX, message=m, transport='udp', localAddress='127.0.0.1', localPort=9000, remoteAddress='127.0.0.1', remotePort=9001 ) d.send( e, srcObj = dialog, dstObj = dialog ) s = 'SIP/2.0 100 Trying\r\nTo: "Matt"<sip:matthew@cave>\r\nContact: "RileyMan"<sip:riley@cave>\r\nFrom: "Josh"<sip:joshua@cave>\r\nCall-ID: abcd\r\n\r\n' m = Message( s ) e = MessageEvent( MessageEvent.EVENT_RX, message=m, transport='udp', localAddress='127.0.0.1', localPort=9000, remoteAddress='127.0.0.1', remotePort=9001 ) d.send( e, srcObj = dialog, dstObj = dialog ) s = 'SIP/2.0 200 OK\r\nTo: "Matt"<sip:matthew@cave>\r\nContact: "RileyMan"<sip:riley@cave>\r\nFrom: "Josh"<sip:joshua@cave>\r\nCall-ID: abcd\r\n\r\n' m = Message( s ) e = MessageEvent( MessageEvent.EVENT_RX, message=m, transport='udp', localAddress='127.0.0.1', localPort=9000, remoteAddress='127.0.0.1', remotePort=9001 ) d.send( e, srcObj = dialog, dstObj = dialog ) s = 'SUBSCRIBE sip:chloe@cave;treats SIP/2.0\r\nTo: "Matt"<sip:matthew@cave>\r\nContact: "RileyMan"<sip:riley@cave>\r\nFrom: "Josh"<sip:joshua@cave>\r\nCall-ID: abcd\r\nExpires: 0\r\n\r\n' m = Message( s ) e = MessageEvent( MessageEvent.EVENT_RX, message=m, transport='udp', localAddress='127.0.0.1', localPort=9000, remoteAddress='127.0.0.1', remotePort=9001 ) d.send( e, srcObj = dialog, dstObj = dialog ) s = 'SIP/2.0 200 OK\r\nTo: "Matt"<sip:matthew@cave>\r\nContact: "RileyMan"<sip:riley@cave>\r\nFrom: "Josh"<sip:joshua@cave>\r\n\r\n' m = Message( s ) e = MessageEvent( MessageEvent.EVENT_RX, message=m, transport='udp', localAddress='127.0.0.1', localPort=9000, remoteAddress='127.0.0.1', remotePort=9001 ) d.send( e, srcObj = dialog, dstObj = dialog ) sleep( 4 ) assert 1
def onRxResponse( self, event ): Dispatcher.getInstance().send( event, srcObj = self, dstObj = self.transaction )
def inConfirmed_onSUBSCRIBE( self, event ): self.transaction = Transaction( event.message['Call-ID'], self, event ) # Send request to server. Dispatcher.getInstance().send( event, srcObj = self, dstObj = self.transaction )
def onTxResponse( self, event ): Dispatcher.getInstance().notify( event )
def onTxRequest( self, event ): Dispatcher.getInstance().notify( event )
def inTrying_onTimerE( self, event ): # Resend request to server. Dispatcher.getInstance().notify( self.request ) # Restart Timer E. Dispatcher.getInstance().schedule( self.CONFIG_TIMER_E, Event( self.EVENT_TIMER_E ), srcObj = self, dstObj = self )
def inTrying_onEnter( self, event ): # Only start timer E if the transport is unreliable. if self.request.transport == 'udp': self.timerE = Dispatcher.getInstance().schedule( self.CONFIG_TIMER_E, Event( self.EVENT_TIMER_E ), srcObj = self, dstObj = self )
def inTerminated_onEnter( self, event ): # Inform the TU. Dispatcher.getInstance().notify( Event( self.NOTIFICATION_TERMINATED ), result = self.result )
def inCompleted_onEnter( self, event ): # Inform the TU. Dispatcher.getInstance().notify( Event( self.NOTIFICATION_COMPLETED ) )
def inProceeding_onRequest( self, event ): # Resend response to client. Dispatcher.getInstance().notify( self.lastResponse )