示例#1
0
 def p2preceiver(self, p2p):
     '''Receive packets or connections from p2p socket server.'''
     def p2phandler(self, sock): # Handle the messages on the given P2P connection.
         while True: 
             data = yield sock.recv()
     while True:
         sock = yield p2p.accept()
         if hasattr(self, 'identity') and self.identity: multitask.add(p2phandler(sock))
示例#2
0
 def send(self, data, remote, stack):
     '''Send a given data to remote for the SIP stack.'''
     def _send(self, data, remote, stack): # a generator function that does the sending
         if _debug: print '%r=>%r on type=%r\n%s'%(stack.sock.getsockname(), remote, stack.sock.type, data)
         if stack.sock.type == socket.SOCK_STREAM: # for TCP send only if a connection exists to the remote.
             if remote in self.conn: 
                 yield multitask.send(self.conn[remote], data) # and send using that connected TCP socket.
         else: # for UDP send using the stack's UDP socket.
             yield multitask.sendto(stack.sock, data, remote)
     multitask.add(_send(self, data, remote, stack))
示例#3
0
 def receivedRequest(self, ua, request, stack): 
     if _debug: print 'received request from stack', request.method
     handlerName = 'on' + request.method
     try:
         if hasattr(self, handlerName) and callable(eval('self.' + handlerName)): # user has defined onINVITE, onREGISTER, etc
             result = getattr(self, handlerName)(ua, request, stack)
         else:
             result = self.onRequest(ua, request, stack)
         if result is not None and type(result) == types.GeneratorType:
             if _debug: print 'result type', type(result)
             multitask.add(result)
     except:
         if _debug: print 'exception in', handlerName
         traceback and traceback.print_exc()
         ua.sendResponse(500, 'Internal server error')
示例#4
0
 def _sipreceiver(self, stack, maxsize=16386):
     '''Handle the messages or connections on the given SIP stack's socket, and pass it to the stack
     so that stack can invoke appropriate callback on this object such as receivedRequest.'''
     sock = stack.sock
     def tcpreceiver(sock, remote): # handle the messages on the given TCP connection.
         while True:
             data = yield multitask.recv(sock, maxsize)
             if _debug: print '%r=>%r on type=%r\n%s'%(remote, sock.getsockname(), sock.type, data)
             if data: stack.received(data, remote)
     while True:
         if sock.type == socket.SOCK_DGRAM:
             data, remote = yield multitask.recvfrom(sock, maxsize)
             if _debug: print '%r=>%r on type=%r\n%s'%(remote, sock.getsockname(), sock.type, data)
             if data: stack.received(data, remote)
         elif sock.type == socket.SOCK_STREAM:
             conn, remote = yield multitask.accept(sock)
             if conn:
                 self.conn[remote] = conn
                 multitask.add(tcpreceiver(conn, remote))
         else: raise ValueError, 'invalid socket type'
示例#5
0
 def start(self):
     '''Start the agent.'''
     for gen in [self._sipreceiver(self.udp), self._sipreceiver(self.tcp)]: self._gens.append(gen); multitask.add(gen)
     return self