def run(self): try: from p2psip.external import multitask yield multitask.sleep(self.delay / 1000.0) if self.running: self.app.timedout(self) except: pass # probably stopped before timeout
def _testRelay(): try: sock1 = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) sock1.bind(("0.0.0.0", 0)) multitask.add(server(sock1)) sockaddr = getlocaladdr(sock1) sock2 = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) sock2.bind(("0.0.0.0", 0)) yield multitask.sleep(2) response, mapped = request(sock2, sockaddr, method=Message.ALLOCATE) print "mapped=", mapped sock1.close() sock2.close() yield multitask.sleep(6) except: print "exception", sys.exc_info(), traceback.print_exc(file=sys.stdout)
def _testTcpRequest(): try: sock1 = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock1.bind(("0.0.0.0", 0)) # should use any port for testing multitask.add(server(sock1)) sockaddr = getlocaladdr(sock1) sock2 = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock2.bind(("0.0.0.0", 0)) yield multitask.sleep(2) # wait for server to be started. response, external = (yield request(sock2, sockaddr)) print "external=", external sock1.close() yield multitask.sleep(6) print "_testTcpRequest() exiting" except (ValueError, multitask.Timeout), E: print "exception - ValueError or Timeout", E
def _testServer(): sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, 0) sock.bind(("0.0.0.0", 0)) # should use any port for testing multitask.add(server(sock)) sockaddr = getlocaladdr(sock) multitask.add(_testDiscoverBehavior([sockaddr, defaultServers[0]])) yield multitask.sleep(5) sock.close()
def _testRelay(): try: sock1 = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) sock1.bind(('0.0.0.0', 0)) multitask.add(server(sock1)) sockaddr = getlocaladdr(sock1) sock2 = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) sock2.bind(('0.0.0.0', 0)) yield multitask.sleep(2) response, mapped = request(sock2, sockaddr, method=Message.ALLOCATE) print 'mapped=', mapped sock1.close() sock2.close() yield multitask.sleep(6) except: print 'exception', sys.exc_info(), traceback.print_exc(file=sys.stdout)
def _testTcpRequest(): try: sock1 = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock1.bind(('0.0.0.0', 0)) # should use any port for testing multitask.add(server(sock1)) sockaddr = getlocaladdr(sock1) sock2 = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock2.bind(('0.0.0.0', 0)) yield multitask.sleep(2) # wait for server to be started. response, external = (yield request(sock2, sockaddr)) print 'external=', external sock1.close() yield multitask.sleep(6) print '_testTcpRequest() exiting' except (ValueError, multitask.Timeout), E: print 'exception - ValueError or Timeout', E
def _testServer(): sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, 0) sock.bind(('0.0.0.0', 0)) # should use any port for testing multitask.add(server(sock)) sockaddr = getlocaladdr(sock) multitask.add(_testDiscoverBehavior([sockaddr, defaultServers[0]])) yield multitask.sleep(5) sock.close()
def send(self, msg, node, timeout=None): '''Send some msg to dest node (Node), and if timeout is specified then return a success (True) or failure (False) within that timeout. Otherwise, the function may return immediately.''' try: start = time.time() if node.type == socket.SOCK_DGRAM and timeout is not None: # no ack required for tcp msg['ack'] = True # require a NetworkAck data = dht.int2bin(self.node.guid) + str( msg) # TODO: this assumes guid is same for all transports. if _debug and msg.name[:4] != 'Hash': print self.name, 'sending %d bytes %s=>%s: %r' % ( len(data), self.node.hostport, node.hostport, msg) if node.type == socket.SOCK_DGRAM: self.udp.sendto(data, (node.ip, node.port)) else: if node in self.tcpc: sock = self.tcpc[node] else: sock = socket.socket(type=socket.SOCK_STREAM) sock.setblocking(0) try: if _debug: print 'connecting to %s' % (node.hostport, ) sock.connect((node.ip, node.port)) except (socket.timeout, socket.error): yield multitask.sleep(2.0) ret = select.select((), (sock, ), (), 0) if len(ret[1]) == 0: if _debug: print 'connection timedout to %s' % ( node.hostport, ) raise multitask.Timeout, 'Cannot connect to the destination' self.tcpc[node] = sock # yield multitask.sleep() multitask.add(self.tcphandler(sock, (node.ip, node.port))) data = struct.pack('!H', len(data)) + data # put a length first. sock.send(data) if msg.ack: hash = H( data ) # hash property to associate the ack to the data request. ack = yield self.get( lambda x: x.name == 'Ack:Indication' and x.hash == hash, timeout=(timeout - (time.time() - start))) if _debug: 'received ack %r' % (ack) if ack is None: raise StopIteration(False) # no ack received raise StopIteration(True) except (multitask.Timeout, socket.error): raise StopIteration(False) # timeout in sendto or get
def testPresence(): # TODO: rename this with prefix _ to enable testing # TODO: change the following to your account and password u1 = User(server='gmail.com', username='******', password='******') result, error = yield u1.login() yield multitask.sleep(1) u1.roster.presence = Presence(show='dnd', status='Online') h1 = u1.chat('*****@*****.**') yield h1.send(Message(body='Hello How are you?')) count = 5 for i in xrange(5): try: msg = yield h1.recv(timeout=120) print msg print '%s: %s' % (msg.frm, msg.body.cdata) yield h1.send(Message(body='You said "%s"' % (msg.body.cdata))) except Exception, e: print str(type(e)), e break
def testPresence(): # TODO: rename this with prefix _ to enable testing # TODO: change the following to your account and password u1 = User(server='gmail.com', username='******', password='******') result, error = yield u1.login() yield multitask.sleep(1) u1.roster.presence = Presence(show='dnd', status='Online') h1 = u1.chat('*****@*****.**') yield h1.send(Message(body='Hello How are you?')) count = 5 for i in xrange(5): try: msg = yield h1.recv(timeout=120) print msg print '%s: %s'%(msg.frm, msg.body.cdata) yield h1.send(Message(body='You said "%s"'%(msg.body.cdata))) except Exception, e: print str(type(e)), e break
def send(self, msg, node, timeout=None): '''Send some msg to dest node (Node), and if timeout is specified then return a success (True) or failure (False) within that timeout. Otherwise, the function may return immediately.''' try: start = time.time() if node.type==socket.SOCK_DGRAM and timeout is not None: # no ack required for tcp msg['ack'] = True # require a NetworkAck data = dht.int2bin(self.node.guid) + str(msg) # TODO: this assumes guid is same for all transports. if _debug and msg.name[:4] != 'Hash': print self.name, 'sending %d bytes %s=>%s: %r'%(len(data), self.node.hostport, node.hostport, msg) if node.type == socket.SOCK_DGRAM: self.udp.sendto(data, (node.ip, node.port)) else: if node in self.tcpc: sock = self.tcpc[node] else: sock = socket.socket(type=socket.SOCK_STREAM) sock.setblocking(0) try: if _debug: print 'connecting to %s'%(node.hostport,) sock.connect((node.ip, node.port)) except (socket.timeout, socket.error): yield multitask.sleep(2.0) ret = select.select((), (sock,), (), 0) if len(ret[1]) == 0: if _debug: print 'connection timedout to %s'%(node.hostport,) raise multitask.Timeout, 'Cannot connect to the destination' self.tcpc[node] = sock # yield multitask.sleep() multitask.add(self.tcphandler(sock, (node.ip, node.port))) data = struct.pack('!H', len(data)) + data # put a length first. sock.send(data) if msg.ack: hash = H(data) # hash property to associate the ack to the data request. ack = yield self.get(lambda x: x.name=='Ack:Indication' and x.hash==hash, timeout=(timeout - (time.time() - start))) if _debug: 'received ack %r'%(ack) if ack is None: raise StopIteration(False) # no ack received raise StopIteration(True) except (multitask.Timeout, socket.error): raise StopIteration(False) # timeout in sendto or get
def testClose(): yield multitask.sleep(25) exit()
def testClose(): yield multitask.sleep(25); exit() if __name__ == '__main__':