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 do_search(self, message): if self.neighbourManager.currentNeighbours() > 0: if (message != None and message != ""): print "Starting search, please wait..." searchInfo = searchinfo.SearchInfo(cfg.SEARCH_CODE_REQUEST, [], cfg.SEARCH_TTL, message) self.neighbourManager.lock() for neighbour in self.neighbourManager.neighbourList: searchInfo.routeIp = [neighbour.sock.localIp()] # Put myself as the only one in the route strSearch = mapper.searchToStr(searchInfo) # convert search -> string neighbour.sock.sendStr(strSearch) # send it to the neighbour self.neighbourManager.unlock() else: print "You should provide a filename" print "Example: search batman.txt" else: print "There are no connected neighbours (use connect command), nothing to do :("