Ejemplo n.º 1
0
 def checkLivePeers( self ):
 
 	""" Attempts to ping all currently known peers in order to ensure that
 	they are still active. Removes any from the peer list that do
 	not reply. This function can be used as a simple stabilizer.
 
 	"""
 	todelete = []
 	for pid in self.getPeers():
 	    isconnected = False
 	    try:
     		
     		host,port,super = self.getPeers()[pid]
     		peerConn = PeerConnection( pid, host, port)
     		peerConn.sendData( 'PING', '' )
     		isconnected = True
 	    except:
             todelete.append( pid )
 	    if isconnected:
             peerConn.close()
 
 	self.getPeerLock().acquire()
 	try:
 	    for pid in todelete: 
             if pid in self.getPeers():
                 del self.getPeers()[pid]
 	finally:
 	    self.getPeerLock().release()
Ejemplo n.º 2
0
    def connectandsend( self, host, port, msgtype, msgdata, 
			pid=None, waitreply=True ):
   
    	"""
    	connectandsend( host, port, message type, message data, peer id,
    	wait for a reply ) -> [ ( reply type, reply data ), ... ]
    
    	Connects and sends a message to the specified host:port. The host's
    	reply, if expected, will be returned as a list of tuples.
    
    	"""
        
    	msgreply = []
    	try:
            
    	    peerconn = PeerConnection( pid, host, port )
            
    	    peerconn.sendData( msgtype, msgdata )
    	
    	    
    	    if waitreply:
        		onereply = peerconn.recvData()
        		while (onereply != (None,None)):
        		    msgreply.append( onereply )
        		    print ( 'Got reply %s: %s' 
        				  % ( pid, str(msgreply) ) )
        		    onereply = peerconn.recvData()
    	    peerconn.close()
    	except KeyboardInterrupt:
    	    raise
    	except:
    	    if False:
    		      traceback.print_exc()
    	
    	return msgreply
Ejemplo n.º 3
0
    def connectAndSend( self, host, port, msgType, msgData, 
			pid=None, waitreply=True ):
   
    	msgreply = []
        num = self.getAttemptedConnectionNumber()
        
        while  num != Peer.NUMBER:
            
            print "ConnectAndSend peers from (%s,%s) %s number %d" % (host,port,msgType,num)
            try:
                peerConn = PeerConnection( pid, host, port)
                peerConn.sendData( msgType, msgData )
                if waitreply:
                    onereply = peerConn.recvData()
                    while (onereply != (None,None)):
                        msgreply.append( onereply )
                        print 'Got reply %s: %s' % ( pid, str(msgreply) )
                        onereply = peerConn.recvData()
                peerConn.close()
                break
            except KeyboardInterrupt:
                raise
            except:
                num += 1
                print "Erro de Connecao peers from (%s,%s) %s %d" % (host,port,msgType, num)
        
        if num == Peer.NUMBER:
            self.setMySuperPeer(self.getMyID())
            self.setPeerType(Peer.SUPER)
            
    	return msgreply