示例#1
0
	def __init__( self, name, parent ):
		#super( MessageAdapter, self ).__init__( name, parent )
		self.log = logging.getLogger( self.__class__.__name__ )

		self.network = Network( 'net', self )
		self.network.addListener( self )

		self.coder = MessageCoder( self.query( 'network.encoding' ) )

		self.default = DatagramReassembler()
		self.connections = {}
示例#2
0
	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 )
示例#3
0
	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 )
示例#4
0
class MessageAdapter(object):
	"""Adapts between NetEvents and MessageEvents.
	   Routes outgoing MessageEvents to the appropriate destination using the
	   the appropriate source.
	"""
	def __init__( self, name, parent ):
		#super( MessageAdapter, self ).__init__( name, parent )
		self.log = logging.getLogger( self.__class__.__name__ )

		self.network = Network( 'net', self )
		self.network.addListener( self )

		self.coder = MessageCoder( self.query( 'network.encoding' ) )

		self.default = DatagramReassembler()
		self.connections = {}

	def identifyEvent( self, event ):
		self.log.info( str(event) )

		if isinstance( event, MessageEvent ):
			return event.id
		elif isinstance( event, NetEvent ):
			return event.id
		elif isinstance( event, NetError ):
			return event.id

		raise SipException( '[' + str(self.name) + '] ' + 'Ignoring event ' + str(event) + '.' )

	def onBind( self, event ):
		# Pass through to the underlying network implementation.
		self.send( event, self.network, queued=False )

	def onUnbind( self, event ):
		# Pass through to the underlying network implementation.
		self.send( event, self.network, queued=False )

	def onRxPacket( self, event ):
		# Decode the message and, if the decoding succeeded, pass the MessageEvent up.
		text = self.coder.decode( event.packet )

		if not event.connection:
			message = self.default.parse( text )
		else:
			#FIXME: handle KeyError.
			message = self.connections[event.connection].parse( text )

		if message != None:
			newEvent = MessageEvent( MessageEvent.EVENT_RX, message, transport=event.transport, localAddress=event.localAddress, localPort=event.localPort, remoteAddress=event.remoteAddress, remotePort=event.remotePort, useragent=self )
			self.notify( newEvent, queued=False )

		event.handled = True

	def __onTxPacket( self, event ):
		# Determine the transport, addresses, and ports to use and adjust the
		# SIP message as necessary.
		self.routeMessage( event )

		# Encode the message, and if the encoding succeeded, pass the NetEvent down.
		text = self.coder.encode( event.message )
		newEvent = NetEvent( NetEvent.EVENT_TX_PACKET, event.transport, event.localAddress, event.localPort, event.remoteAddress, event.remotePort, packet=text )

		if newEvent:
			self.send( newEvent, self.network, queued=False )

		event.handled = True

	def onTxRequest( self, event ):
		self.__onTxPacket( event )

	def onTxResponse( self, event ):
		self.__onTxPacket( event )

	def onConnected( self, event ):
		print 'ccc', str(event.connection)
		self.connections[event.connection] = StreamReassembler()
		event.handled = True

	def onDisconnected( self, event ):
		#FIXME: handle KeyError.
		print 'ddd', str(event.connection)
		#print self.connections
		del self.connections[event.connection]
		event.handled = True

	def onNetError( self, event ):
		self.log.error( str(event) )

		#FIXME: Not sure what to do with these events.  Should they be sent up as-is?
		#       Or converted to MessageEvents?
		self.notify( event, queued=False )

		event.handled = True

	def routeMessage( self, event ):
		"""This function determines the address and port to send the message to.
		   For requests, this function also determines the transport, address, and port to
		   send the message from.

		   Request:
		      Look up Request-URI host and get remote transport, address and port.
		      Modify/set Contact.
		      Modify/set Via to local transport, address, and port.

		   Response:
		      Get the destination from the Via.
		"""

		#FIXME:IMPLEMENT.
		pass