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
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
def __handlepeer( self, clientSock ): """ handlepeer( new socket connection ) -> () Dispatches messages from the socket connection """ print 'New child ' + str(threading.currentThread().getName()) print 'Connected ' + str(clientSock.getpeername()) host, port = clientSock.getpeername() peerConn = PeerConnection( None, host, port, clientSock) try: msgType, msgData = peerConn.recvData() if msgType: msgType = msgType.upper() if msgType not in self.__handlers: print 'Not handled: %s: %s' % (msgType, msgData) else: print 'Handling peer msg: %s: %s' % (msgType, msgData) self.__handlers[ msgType ]( peerConn, msgData ) except KeyboardInterrupt: raise except: traceback.print_exc() print 'Disconnecting ' + str(clientSock.getpeername()) peerConn.close()