def processMessage(self, msg, fromAddr): '''processes the received messages''' if isinstance(msg, message.HeloMessage): # set your own ip if you dont know it if self.ip == "null": self.ip = msg.recipientIP self.key = Host.constructKey(self.ip, self.port) logging.info("You're now connected to a Hachat-network. Your key is " + self.key) senderIP = fromAddr[0] key = Host.constructKey(senderIP, msg.senderPort) logging.debug("received: HELO from " + str(key)) if key in self.hosts: # if you know Host set status that Host had contact host = self.hosts[key] host.lastSeen = 1 logging.debug(key + " already in hostlist - refreshing lastSeen") else: # add new host to hostlist self.addToHosts(key) # only accept Messages from Peers in self.hosts else: try: sender = msg.origin except Exception, e: logging.debug("Msg needs origin, but doesn't have one " + str(msg)) try: # overriding sender with lastHop sender = msg.lastHop except Exception, e: pass
def addToHosts(self, addr): '''check if already in hostlist otherwise add''' # construct key and tuple if isinstance(addr, str): key = addr (hostIP, hostPort) = re.split(':', addr, 1) elif isinstance(addr, tuple): (hostIP, hostPort) = addr key = Host.constructKey(hostIP, hostPort) # if host is not the peer itself if hostIP != self.ip or int(hostPort) != int(self.port): if not (key in self.hosts): # insert in host dict logging.debug("adding " + key + " to hostlist") Host(self, hostIP, hostPort) logging.debug("Now %d Hosts in HostList and %d Hosts in knownHosts"%(len(self.hosts), len(self.knownPeers)))
def forwardMsg(self, msg, Oneneigbour=None): '''forwarding TextMessage, but not to initial sender if host is set, it will only forward to this single host''' msgSender = msg.origin # rewrite lastHop oldLastHop = msg.lastHop msg.lastHop = self.key if Oneneigbour == None: for h in self.hosts.values(): hostAddr = Host.constructKey(h.hostIP, h.hostPort) # don't forward to origin or lastHop if msgSender != hostAddr and oldLastHop != hostAddr: #logging.debug("Message " + msg.text + " from " + msgSender + " will be forwarded to " + hostAddr ) h.addToMsgQueue(msg) else: logging.debug("Message " + msg.text + " will not be forwarded to initial sender " + msgSender + " and lastHop " + oldLastHop) else: host = self.hosts[Oneneigbour] host.addToMsgQueue(msg) logging.debug("Will forward msg to " + Oneneigbour)
def __init__(self, firstHost = None, port = None, name = "temp", ip = None, testmode = False): self.name = name # set peer name self.inSocket = None # Socket für eingehende Verbindungen self.hosts = {} # Dict. der bekannten Hosts self.knownPeers = {} # Dict (ip:port) : name self.msgParts = {} # set own ip if you already know it if ip != None: self.ip = ip else: self.ip = "null" # open socket self.inSocket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) if port == None: # bind on random port self.inSocket.bind(('', 0)) else: # bind on given port self.inSocket.bind(('', int(port))) self.port = int(self.inSocket.getsockname()[1]) # port where peer listens on logging.info("Listening on port " + str(self.port)) self.history = message.History(const.HISTORY_SAVEDMSGSLIMIT, const.HISTORY_SAVEDHASHESLIMIT) self.gui = gui.gui(self) # lock for hostlist (not needed atm) # self.hostlock = threading.RLock() # start receiveLoop self.rThread = threading.Thread(target=self.startRecvLoop) self.rThread.daemon = True self.rThread.start() # start sendLoop if testmode == True: #if testmode is true, sendLoop will drop random parts of msgs self.sThread = threading.Thread(target=self.sendLoop, args=(True,)) self.sThread.daemon = True self.sThread.start() else: self.sThread = threading.Thread(target=self.sendLoop, args=(False,)) self.sThread.daemon = True self.sThread.start() # start maintenance Loop self.counter = 0 self.mThread = threading.Thread(target=self.maintenanceLoop) self.mThread.daemon = True self.mThread.start() # send HELO to first host if you know one if firstHost != None: self.key = None (hostIP, hostPort) = firstHost h = Host(self, hostIP, hostPort) h.bootstrap = True # wait until you're connected to the network while self.key == None: time.sleep(0.5) #Initial Request for some more hosts from firstHost key = h.constructKey(hostIP, hostPort) logging.debug("Initial Request for some peers from " + key) self.requestHosts(key, const.MIN_PEERLIMIT) # and get some more hosts #Initial Request for History logging.debug("Initial Request for History from " + key) self.getHistory(key, initial=True) else: self.key = Host.constructKey(self.ip, self.port) logging.info("You created a new Hachat-network. Your key is " + self.key) if testmode == True: self.generateMsgParts(10, 3000) #generates Randome Text-Msgs #start gui self.gui.run()