def connectToClients(): global otherClients for i in range(6, 9): if (i != int(serverPID)): sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) sock.connect((socket.gethostname(), configData[str(i)])) msg_send = 'server ' + serverPID msg_send = message(msg_send, serverPID).getReadyToSend() sock.sendall(msg_send) otherClients.append([sock, str(i)])
def main(server, port): print "Enter your name" myname = raw_input() s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.connect((server, port)) msg = utility.message(myname, myname, ['server']) utility.send_message(s, msg) thread1 = threading.Thread(target=input_listener, args=[s, myname]) thread2 = threading.Thread(target=server_listner, args=[s]) thread3 = threading.Thread(target=msg_dispatcher, args=[s]) thread1.start() thread2.start() thread3.start()
def handleLeaderCommand(): global BallotNum global lock lock.acquire() BallotNum[0] += AcceptNum[0] + 1 lock.release() time.sleep(delay) print("***** Starting Phase 1 *****") prepare = message("prepare", serverPID) prepare.BallotNum = BallotNum broadcastToOtherServers(prepare.getReadyToSend())
def receiveMajorityPromises(): global hintedLeader global myVal global myId global AcceptVal global AcceptNum global BallotNum global receivedPromises global numReceivedPromises global phaseTwoAlreadyInProcess notAllBottom = False # think about logic for setting myVal for promise in receivedPromises: if promise.AcceptVal != None: notAllBottom = True if (notAllBottom): highestBallotMsg = receivedPromises[0] for promise in receivedPromises: if (compareBallots(promise.AcceptNum, highestBallotMsg.AcceptNum)): highestBallotMsg = promise AcceptNum = highestBallotMsg.AcceptNum myVal = highestBallotMsg.AcceptVal myId = highestBallotMsg.AcceptVal[3] else: AcceptNum = BallotNum # compare ballots function # keep trying hintedLeader = serverPID numReceivedPromises = 0 msg = message("hintedLeader", serverPID).getReadyToSend() time.sleep(delay) broadcastToOtherServers(msg) for sock in otherClients: try: sock[0].sendall(msg) except socket.error: sock[0].close() # start Phase 2 if myVal != None # start a thread if (myVal != None or not OPqueue.empty() and not phaseTwoAlreadyInProcess): phaseTwoAlreadyInProcess = True print("**** Starting Phase 2 ****") # print("val", myVal) # print("myId", myId) threading.Thread(target=sendAcceptMessages, args=(False, )).start()
def sendACK(pid): time.sleep(delay) msg = message("ack", serverPID).getReadyToSend() for sock in otherServers: if sock[1] == pid: try: sock[0].sendall(msg) except socket.error: sock[0].close() for sock in otherClients: if sock[1] == pid: try: sock[0].sendall(msg) except socket.error: sock[0].close()
def handleAcceptCommand(newBallotNum, newVal, uid): global BallotNum global AcceptVal global AcceptNum global myId if (compareBallots(newBallotNum, BallotNum)): AcceptNum = newBallotNum AcceptVal = newVal myId = uid for sock in otherServers: if sock[1] not in failedLinks and sock[1] == hintedLeader: msg = message("accepted", serverPID, uid) msg.val = AcceptVal msg.BallotNum = BallotNum time.sleep(delay) try: sock[0].sendall(msg.getReadyToSend()) except: sock[0].close() break
def handlePrepareCommand(recBallot): global BallotNum global AcceptNum global AcceptVal if (compareBallots(recBallot, BallotNum)): time.sleep(delay) for sock in otherServers: if ((int(sock[1]) == int(recBallot[1])) and (str(recBallot[1]) not in failedLinks)): lock.acquire() promise = "promise" promise = message(promise, serverPID, myId) promise.BallotNum = BallotNum promise.AcceptNum = AcceptNum promise.AcceptVal = AcceptVal try: sock[0].sendall(promise.getReadyToSend()) except socket.error: sock[0].close() lock.release()
def sendAcceptMessages(startFromPhaseTwo): global BallotNum global myVal global requestingClient global requestingServer global myId if startFromPhaseTwo: BallotNum[0] += 1 if myVal == None: op = OPqueue.get() # print("op is", op) requestingClient = op[1] requestingServer = op[2] myVal = bc.mine(op[0], op[3]) myId = op[3] # PUT IN CHECK HERE? msg = message("accept", serverPID) msg.val = myVal msg.BallotNum = BallotNum time.sleep(delay) broadcastToOtherServers(msg.getReadyToSend())
def input_listener(socket, myname): while True: text = raw_input() recipients, text = parse_text(text) new_message = utility.message(text, myname, recipients) message_queue.push(new_message)
def userInput(): global bc while True: x = input() commandList = x.split(" ") command = commandList[0].strip() if (command == 'connect'): threading.Thread(target=connectToServers).start() threading.Thread(target=connectToClients).start() elif (command == 'sendall'): test = "testing from server " + str(serverPID) send = message(test, serverPID).getReadyToSend() broadcastToOtherServers(send) for sock in otherClients: if (sock[1] not in failedLinks): try: sock[0].sendall(send) except socket.error: sock[0].close() elif (command == 'send'): pid = commandList[1] test = "testing individual from server " + str(serverPID) test = message(test, serverPID).getReadyToSend() for sock in otherServers: if (sock[1] == str(pid) and sock[1] not in failedLinks): try: sock[0].sendall(test) except socket.error: sock[0].close() for sock in otherClients: if (sock[1] == str(pid) and sock[1] not in failedLinks): try: sock[0].sendall(test) except socket.error: sock[0].close() elif (command == 'hintedLeader'): print(hintedLeader) elif (command == 'failLink'): # example: failLink 1 2 if (commandList[1] == serverPID): failedLinks.add(commandList[2]) print("failedLinks:", failedLinks) else: print("please enter valid source for server {s}".format( s=serverPID)) elif (command == 'fixLink'): # example: fixLink 1 2 if (commandList[1] == serverPID): failedLinks.remove(commandList[2]) print("Current failedLinks: ", failedLinks) else: print("please enter valid source for server {s}".format( s=serverPID)) elif (command == 'printBlockchain' or command == 'bc'): bc.print() elif (command == 'printKVStore' or command == 'kv'): print(keyvalue) elif (command == 'printQueue' or command == 'q'): print(OPqueue.queue) elif (command == 'failProcess' or command == 'exit'): doExit()
def receiveMajorityAccepted(): global numReceivedAccepted global bc global keyvalue global alreadySentAccepted global requestingClient global requestingServer global phaseTwoAlreadyInProcess numReceivedAccepted = 0 msg = message("decide", serverPID, myId) msg.val = myVal msg.BallotNum = BallotNum alreadySentAccepted = True # Add block to block chain # update KV store bc.add(myVal, BallotNum[2], myId) keyvalue = bc.recreateKV() if myVal != None: operation = myVal[0].split(" ") else: operation = "dummy" # print("operation", operation) opCommand = operation[0] # send decide to all other servers broadcastToOtherServers(msg.getReadyToSend()) # Reset paxos vars resetPaxosVars() print("**** End Decide Phase ****") time.sleep(delay) msg = message("ack", serverPID) for sock in otherClients: if (sock[1] == requestingClient): try: sock[0].sendall(msg.getReadyToSend()) except socket.error: sock[0].close() infoMsg = message("info", serverPID) if (opCommand == "get" and operation[1] in keyvalue.keys()): infoMsg.val = keyvalue[operation[1]] try: sock[0].sendall(infoMsg.getReadyToSend()) except socket.error: sock[0].close() elif (opCommand == "get" and operation[1] not in keyvalue.keys()): infoMsg.val = "key not found" try: sock[0].sendall(infoMsg.getReadyToSend()) except socket.error: sock[0].close() for sock in otherServers: if (sock[1] == requestingServer): try: sock[0].sendall(msg.getReadyToSend()) except socket.error: sock[0].close() requestingClient = None requestingServer = None phaseTwoAlreadyInProcess = False # restart paxos if more operations in queue if (not OPqueue.empty() and myVal == None): print("**** Starting Phase 2 ****") phaseTwoAlreadyInProcess = True sendAcceptMessages(True)