def initiate(main_port, tracker_addr, uname, pwd): sock = create_sock(main_port) sock.connect((tracker_addr["ip"], tracker_addr["port"])) sock.send(pickle.dumps({"cat" : "get", "item" : "peerlist"})) r = sock.recv(1024) peerlist = pickle.loads(r) sock.close() print(peerlist) modpath = input("Enter path of module : ") if os.path.isfile(modpath) == True: command = "python3 " + modpath + " initiate" threading.Thread(target = caller, args = (command, ), daemon = True).start() else: initiate(main_port, tracker_addr, uname, pwd) time.sleep(3) sock = create_sock(main_port) sock.connect(("127.0.0.1", 54321)) sock.send(pickle.dumps("data")) data = pickle.loads(transfer.receiver(sock)) sock.close() print("Data received : ") print(data) results = initiate2(main_port, tracker_addr, uname, pwd, modpath, data) sock = create_sock() sock.connect(("127.0.0.1", 54321)) transfer.sender(sock, pickle.dumps(results)) sock.close()
def volunteer(main_port, tracker_addr, uname, pwd): print("You have selected to volunteer.") while True: sock = create_sock(main_port) sock.listen(4) print("Listening") nsock, naddr = sock.accept() sock.close() print(naddr) r = nsock.recv(1024) data = pickle.loads(r) modpath = data["module"] choice = input("Do you want to participate? y/n, any other key to go back : ") if choice == "y": nsock.send(pickle.dumps("yes")) sock = create_sock(main_port) sock.listen(4) nsock, naddr = sock.accept() r = transfer.receiver(nsock) print(pickle.loads(r)) sock.close() command = "python3 " + modpath + " solve" t = threading.Thread(target = caller, args = (command, ), daemon = True) t.start() time.sleep(3) print("Sending data to module") msock = create_sock(main_port) msock.connect(("127.0.0.1", 54321)) transfer.sender(msock, r) msock.close() print("waiting for data from module..") msock = create_sock(main_port) msock.listen(5) newmsock, newmaddr = msock.accept() r = transfer.receiver(newmsock) newmsock.close() msock.close() t.join() transfer.sender(nsock, pickle.dumps({"content" : r})) nsock.close() elif choice == "n": nsock.send(pickle.dumps("no")) nsock.close() continue else: break
def solver(): print("Solve mode") modsock = create_sock(54321) modsock.listen(5) peersock, peer_addr = modsock.accept() r = transfer.receiver(peersock) peersock.close() modsock.close() dat = pickle.loads(r) print("Job Received: ") print(dat) result = 0 for num in dat: result = result + num print("Result Calculated: ", result) msock = create_sock(54321) msock.connect((peer_addr[0], peer_addr[1])) transfer.sender(msock, pickle.dumps(result)) msock.close()
def initiator(): print("Initiating....") data = filereader("rand") lines = data.splitlines() to_send = [] count = 0 sub = [] for line in lines: sub.append(int(line)) count += 1 if count == 100: to_send.append(sub) count = 0 sub = [] print(to_send) modsock = create_sock(54321) modsock.listen(5) peersock, peer_addr = modsock.accept() msg = pickle.loads(peersock.recv(1024)) if msg == "data": transfer.sender(peersock, pickle.dumps(to_send)) peersock.close() modsock.close() modsock = create_sock(54321) modsock.listen(5) peersock, peer_addr = modsock.accept() result = pickle.loads(transfer.receiver(peersock)) print("Result received from peers: ", result) final_result = 0 for ans in result: final_result += ans print("final result", final_result) modsock.close() peersock.close()
def solve_job (host, job, q): print((host["ip"], host["port"])) sock = create_sock() sock.connect((host["ip"], host["port"])) # checking if the peer is online - redundant # sock.send(pickle.dumps({"cat" : "request"})) # r = sock.recv(1024) # response = pickle.loads(r) # sending job transfer.sender(sock, pickle.dumps(job)) response = pickle.loads(transfer.receiver(sock)) if response["content"] == False: ret = [False, None] else: ret = [True, response["content"]] # sock.send(pickle.dumps(job)) # r = sock.recv(1024) # result = pickle.loads(r) q.put(ret) return ret