def run(self): print "[Search] Started" if os.path.isabs(self.searchInfo.query) or '../' in self.searchInfo.query or not os.path.isfile(os.path.join(cfg.SHARING_FOLDER, self.searchInfo.query)): # if path is absolut or relative to parent (contains ..) or is not a valid file inside shared folder print "[Search] File not found or provided absolute path or provided path outside shared folder: " + self.searchInfo.query #No files matched self.searchInfo.ttl = self.searchInfo.ttl - 1 if self.searchInfo.ttl > 0: #add my IP at the end of routeIp self.searchInfo.routeIp.append(self.sock.localIp()) #forward message to neighbours not in the route for neighbour in self.neighbourManager.neighbourList: if neighbour.sock.remoteIp() not in self.searchInfo.routeIp: print "[Search] Broadcasting to " + neighbour.sock.remoteIp() # convert search -> string strSearch = mapper.searchToStr(self.searchInfo) # send it to the neighbour neighbour.sock.sendStr(strSearch) #else -> dont resend the message else: # File matched, generate result info and send it back print "[Search] File found: " + self.searchInfo.query resultInfo = model.resultinfo.resultInfo(cfg.SEARCH_CODE_RETURN, self.searchInfo.routeIp, self.sock.localIp(), self.searchInfo.query) resultStr = resMapper.resultToStr(resultInfo) # Send result to the requester (should be the last in the ip list) self.sock.sendStr(resultStr)
def run(self): print "[ResultSearch] started" if self.searchResult.routeIp[-1] == self.sock.localIp(): # If the last ip is my ip if (len(self.searchResult.routeIp) == 1): # I'm the one that started the search, show result to the user. print "[ResultSearch] Found file " + self.searchResult.fileFound + " at " + self.searchResult.senderIp+ ". Use 'download' or 'd' command to fetch it." self.downloadManager.save(self.searchResult.fileFound, self.searchResult.senderIp) # Save the result to download it later with 'd' command else: # I'm an intermediate node, forward the result # Remove myself from the route self.searchResult.routeIp.remove(self.sock.localIp()) # It MUST be there to remove. # Search the neighbour with the route's last IP lastIp = self.searchResult.routeIp[-1] nextNeighbour = self.neighbourManager.findNeighbourWithIp(lastIp) if (nextNeighbour != None): # Convert search info to a string an send it to that neighbour resultAsStr = resMapper.resultToStr(self.searchResult) nextNeighbour.sock.sendStr(resultAsStr) else: print "[ResultSearch] error: didn't find neighbour to send the search result" else: #error print "[ResultSearch] error: something weird happened, I'm not in the IP route"