def connect_learner(self, data): s = socket.socket() ip = "127.0.0.1" port = 9000 s.connect((ip, port)) retval = encode_decode.recvfrom(s) encode_decode.sendto(s, data) retval = encode_decode.recvfrom(s) print(retval)
def connect_acceptor(self, data): acceptor_socket = [] ipport = [] global base global flag valuepair = data base = time.time() data = "PREPARE " + str(base) result = get_acceptors() acceptor_socket2 = [] for i in result: s = socket.socket() ipport.append(i) acceptor_socket.append(s) acceptor_count = len(acceptor_socket) for j in range(len(acceptor_socket)): i = ipport.pop() try: acceptor_socket[j].connect((i[0], i[1])) print(i) acceptor_socket2.append(acceptor_socket[j]) except ConnectionRefusedError: output.print_failure("Acceptor not running: " + str(i[0]) + ":" + str(i[1])) continue retval = encode_decode.recvfrom(acceptor_socket[j]) # print(len(acceptor_socket2)) if len(acceptor_socket2) / acceptor_count < 0.5: encode_decode.sendto(self.conn, "No acceptor running") output.print_failure("Majority acceptor not running") for fd in acceptor_socket2: encode_decode.sendto(fd, "EXIT") return for fd in acceptor_socket2: encode_decode.sendto(fd, data) for fd in acceptor_socket2: retval = encode_decode.recvfrom(fd) output.print_running(retval) retval = retval.split() if retval[0] == 'PROMISE': output.print_success(retval) data = "ACCEPT-REQUEST " + str( base) + " " + valuepair[1] + " " + valuepair[2] for fd in acceptor_socket2: encode_decode.sendto(fd, data) for fd in acceptor_socket2: retval = encode_decode.recvfrom(fd) print(retval) print("sent success to client") encode_decode.sendto(self.conn, "SUCCESS") elif retval[0] == 'ACCEPT': base = float(retval[1]) encode_decode.sendto(self.conn, "FAILED") data1 = " STORE " + valuepair[1] + " " + valuepair[2]
def connect_Proposer(self,port): s = socket.socket() ip = "127.0.0.1" s.connect((ip,port)) retval = encode_decode.recvfrom(s) print(retval) return s
def run(self): print("qdwqe") try: data = encode_decode.recvfrom(self.conn) except ConnectionResetError: print("Connection closed by client") return output.print_primary("Server received data: " + data) self.connect_acceptor(data.split())
def find_min(): global proposer_fds global rcvd_msgs global lock if len(rcvd_msgs) == 0: return lock.acquire() a = rcvd_msgs.index(min(rcvd_msgs)) lock.release() data = "PROMISE " + str(max(rcvd_msgs)) encode_decode.sendto(proposer_fds[a], data) output.print_success(data) data = encode_decode.recvfrom(proposer_fds[a]) output.print_success(data) data = data.split() if data[0] == "ACCEPT-REQUEST": data = "ACCEPT " + str(min(rcvd_msgs)) + " " + data[2] + " " + data[3] for fd in proposer_fds: encode_decode.sendto(fd, data) result = get_learners() ipport = [] learner_socket = [] for i in result: s = socket.socket() ipport.append(i) learner_socket.append(s) for fd in learner_socket: i = ipport.pop() try: fd.connect((i[0], i[1])) except ConnectionRefusedError: output.print_failure("Learner not running") return retval = encode_decode.recvfrom(fd) output.print_running(retval) for fd in learner_socket: encode_decode.sendto(fd, data) for fd in learner_socket: retval = encode_decode.recvfrom(fd) output.print_running(retval)
def run(self): global rcvd_msgs try: data = encode_decode.recvfrom(conn) except ConnectionResetError: print("Connection closed by client") return print("Server received data:", data) data = data.split() if data[0] == "EXIT": return lock.acquire() rcvd_msgs.append(float(data[1])) lock.release()
TCP_PORT = int(input()) insert_learner(TCP_IP, TCP_PORT) tcpServer = socket.socket(socket.AF_INET, socket.SOCK_STREAM) tcpServer.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) tcpServer.bind((TCP_IP, TCP_PORT)) threads = [] while True: tcpServer.listen(5) output.print_running("Listening ...") (conn, (ip, port)) = tcpServer.accept() print("got connection from ", ip, port) conn.send(b"SUCCESS") try: data = encode_decode.recvfrom(conn) except ConnectionResetError: print("Connection closed by client") print("Server received data:", data) print(data) data = data.split() if data[0] == "ACCEPT": data_store[data[2]] = data[3] encode_decode.sendto(conn, "STORED") if data[0] == "READ": try: retval = data_store[data[1]] output.print_failure("Key found") encode_decode.sendto(conn, retval) except KeyError: output.print_failure("Key not found")
def connect_Learner(self,port): s = socket.socket() s.connect(('127.0.0.1', port)) retval = encode_decode.recvfrom(s) print(retval) return s
except ConnectionRefusedError: continue if choice == '2': if connected_to_proposer == 0: print("FIRST CONNECT TO PROPOSER!!") else: print("insert a key value pair") key = input("key:: ") value = input("value:: ") string = "STORE "+key+" "+value for fd in Proposer_socket: encode_decode.sendto(fd,string) retval=False for fd in Proposer_socket: val = encode_decode.recvfrom(fd) if val == "SUCCESS": retval=retval or True if retval: output.print_success("Stored: "+key+" "+value) else: output.print_failure("FAILED") connected_to_proposer = 0 Proposer_socket = [] if choice == '3': valuepairlist=[] if connected_to_proposer == 0: print("FIRST CONNECT TO PROPOSER!!") else: for i in Proposer_socket: