def run(self): global TCP_PORT output.print_running(":::::::::::::PAXOS PROPOSER:::::::::::::::") print("Insert I.P. Address of Proposer Node") TCP_IP = input() print("Insert Port number of Proposer Node") TCP_PORT = int(input()) insert_proposer(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 = [] client_fd = [] a = 0 while True: tcpServer.listen(5) output.print_running("Listening ...") (conn, (ip, port)) = tcpServer.accept() output.print_success("got connected from " + str(ip) + ":" + str(port)) conn.send(b"SUCCESS") client_fd.append(conn) newthread = Proposer_Thread(ip, port, client_fd[a]) a = a + 1 newthread.start() threads.append(newthread)
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 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)
import socket from threading import Thread import output import encode_decode from test import * data_store = {} output.print_running(":::::::::::::PAXOS LEARNER:::::::::::::::") print("Insert I.P. Address of Learner Node") TCP_IP = input() print("Insert Port number of Learner Node") 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)
lock.acquire() rcvd_msgs.append(float(data[1])) lock.release() 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) output.print_running(":::::::::::::PAXOS ACCEPTOR:::::::::::::::") print("Insert I.P. Address of Acceptor Node") TCP_IP = input() print("Insert Port number of Acceptor Node") TCP_PORT = int(input()) insert_acceptor(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 = [] proposer_fds = [] newthread = update_n() newthread.start() threads.append(newthread)