def connectAndSend( self, host, port, msgType, msgData, pid=None, waitreply=True ): msgreply = [] num = self.getAttemptedConnectionNumber() while num != AbstractPeer.NUMBER: #PeerLogger().resgiterLoggingInfo("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 ) #PeerLogger().resgiterLoggingInfo('Got reply %s: %s' % ( pid, str(msgreply) )) onereply = peerConn.recvData() peerConn.close() break except KeyboardInterrupt: raise except: print "Erro de Connecao peers from (%s,%s) %s %d" % (host,port,msgType, num) num += 1 #PeerLogger().resgiterLoggingInfo("Erro de Connecao peers from (%s,%s) %s %d" % (host,port,msgType, num)) #if num == AbstractPeer.NUMBER: # self.setMySuperPeer(self.getPID()) # self.setPeerType(AbstractPeer.SUPER) #print msgreply return msgreply
def __handlePeer( self, clientSock ): """ handlepeer( new socket connection ) -> () Dispatches messages from the socket connection """ #Logger().resgiterLoggingInfo('New child ' + str(threading.currentThread().getName())) #Logger().resgiterLoggingInfo('Connected ' + str(clientSock.getpeername())) #PeerLogger().resgiterLoggingInfo('New child ' + str(threading.currentThread().getName())) #PeerLogger().resgiterLoggingInfo('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 not self.__dispatcher.hasTypeMessage(msgType): #if msgType not in self.__dispatcher.getMessageHandlers(): pass#print "msg",msgType #PeerLogger().resgiterLoggingInfo('Not handled: %s: %s' % (msgType, msgData)) else: #PeerLogger().resgiterLoggingInfo('Handling peer msg: %s: %s' % (msgType, msgData)) self.__dispatcher.executeHandleMessage(msgType, peerConn, msgData) #self.__dispatcher.getMessageHandlers()[ msgType ].e( peerConn, msgData ) except KeyboardInterrupt: raise except: traceback.print_exc() #PeerLogger().resgiterLoggingInfo('Disconnecting ' + str(clientSock.getpeername())) peerConn.close()