def join(): try: msg = Message(constants.message_type["join"], address, address, address) msg_parser = MessageParser() msg = msg_parser.pack_msg(msg) tcp.socket.sendto(msg, root_address) response, addr = tcp.socket.recvfrom(constants.BUFFER_SIZE) response = msg_parser.unpack_msg(response) request_handler(response) response, addr = tcp.socket.recvfrom(constants.BUFFER_SIZE) response = msg_parser.unpack_msg(response) request_handler(response) print "Node joined successfully at: ", successor, predecessor successor_thread.start() predecessor_thread.start() msg = Message(constants.message_type["distribute_keys"], (address[0], tcp.predecessor_port), str(address[1])) msg = msg_parser.pack_msg(msg) tcp.predecessor_socket.sendto(msg, predecessor) heartbeat.start() heartbeat_listen.start() except Exception as e: print str(e)
def run(self): global predecessor_dead_flag global successor_dead_flag msg_parser = MessageParser() timer_p = 0 timer_s = 0 while not self.stopped(): time.sleep(constants.SOCKET_TIMEOUT - 2) msg, addr = tcp.predecessor_socket.recvfrom(constants.BUFFER_SIZE) msg = msg_parser.unpack_msg(msg) msg_parser.validate_message_type(msg) if msg.type == constants.message_type["heartbeat"]: timer_p = 0 pass if timer_p >= 2: predecessor_dead_flag = True predecessor_dead() msg, addr = tcp.successor_socket.recvfrom(constants.BUFFER_SIZE) msg = msg_parser.unpack_msg(msg) msg_parser.validate_message_type(msg) if msg.type == constants.message_type["heartbeat"]: timer_s = 0 pass if timer_s >= 2: successor_dead_flag = True timer_p += 1 timer_s += 1 pass
def show_menu(self): msg_parser = MessageParser() while True: print "Enter a command:\ns --> Store\ni --> Iterative Search\n" \ "r --> Recursive Search\ne --> exit\n" command = raw_input() if command == "s": print "Enter key:" key = raw_input() print "Enter value:" val = raw_input() data = (key, val) msg = Message(constants.message_type["store"], self.client.address, str(data), self.root) msg = msg_parser.pack_msg(msg) self.client.tcp.socket.sendto(msg, self.root) pass elif command == "i": print "You have selected Iterative retrieve. Enter the key:" key = raw_input() msg = Message(constants.message_type["iterative_retrieve"], self.client.address, str(key), self.client.address) msg = msg_parser.pack_msg(msg) self.client.tcp.socket.sendto(msg, self.root) response, addr = self.client.tcp.socket.recvfrom( constants.BUFFER_SIZE) response = msg_parser.unpack_msg(response) msg_parser.validate_message_type(response) print response.data pass elif command == "r": print "You have selected Iterative retrieve. Enter the key:" key = raw_input() msg = Message(constants.message_type["recursive_retrieve"], self.client.address, str(key), self.client.address) msg = msg_parser.pack_msg(msg) self.client.tcp.socket.sendto(msg, self.root) response, addr = self.client.tcp.socket.recvfrom( constants.BUFFER_SIZE) response = msg_parser.unpack_msg(response) msg_parser.validate_message_type(response) print response.data pass elif command == "e": sys.exit(0) else: print "Enter correct instruction"
def run(self): try: while not self.stopped(): request, addr = tcp.socket.recvfrom(constants.BUFFER_SIZE) msg_parser = MessageParser() request = msg_parser.unpack_msg(request) request_handler(request) except Exception as e: print str(e)
def connect_to_root(self): self.client.tcp.start(self.client.address) msg = Message(msg_type=constants.message_type["client_connect"], destination=self.client.address) msg_parser = MessageParser() msg = msg_parser.pack_msg(msg) self.client.tcp.socket.sendto(msg, self.root) response, addr = self.client.tcp.socket.recvfrom(constants.BUFFER_SIZE) response = msg_parser.unpack_msg(response) msg_parser.validate_message_type(response) print response.data
def recursive_retrieve(request): global db data = request.data data_id = sha1(data).hexdigest() if successor is None: with open(constants.DB_FILE, "r") as db: for line in db: key = line.split(',')[0].lstrip('(').strip("'") key_id = sha1(key).hexdigest() if data_id == key_id: db.close() return line db.close() return -1 else: successor_id = sha1(str(successor[1])).hexdigest() msg_parser = MessageParser() if id < data_id < successor_id or successor == (root_address[0], root_address[1] + 2): with open(constants.DB_FILE, "r") as db: for line in db: key = line.split(',')[0].lstrip('(').strip("'") key_id = sha1(key).hexdigest() if data_id == key_id: if root: db.close() return line else: msg = Message(constants.message_type["ack"], address, line) msg = msg_parser.pack_msg(msg) tcp.predecessor_socket.sendto(msg, request.sender) db.close() if root: return -1 else: msg = Message(constants.message_type["not_found"]) msg = msg_parser.pack_msg(msg) tcp.predocessor_socket.sendto(msg, request.sender) else: msg = Message(constants.message_type["recursive_retrieve"], address, data) msg = msg_parser.pack_msg(msg) tcp.successor_socket.sendto(msg, successor) response, addr = tcp.socket.recvfrom(constants.BUFFER_SIZE) response = msg_parser.unpack_msg(response) msg_parser.validate_message_type(response) if root: return response.data else: tcp.predecessor_socket.sendto(response, request.sender)
def iterative_retrieve(request): global db data = request.data print data data_id = sha1(data).hexdigest() if successor is None: with open(constants.DB_FILE, "r") as db: for line in db: key = line.split(',')[0].lstrip('(').strip("'") key_id = sha1(key).hexdigest() if data_id == key_id: db.close() return line db.close() return -1 else: successor_id = sha1(str(successor[1])).hexdigest() msg_parser = MessageParser() if id < data_id < successor_id or successor == (root_address[0], root_address[1] + 2): with open(constants.DB_FILE, "r") as db: for line in db: key = line.split(',')[0].lstrip('(').strip("'") key_id = sha1(key).hexdigest() if data_id == key_id: db.close() return line db.close() return -1 else: temp_successor = successor print temp_successor while temp_successor != (address[0], address[1] + 2): msg = Message(constants.message_type["exist"], address, data) msg = msg_parser.pack_msg(msg) tcp.successor_socket.sendto( msg, (temp_successor[0], temp_successor[1])) response, addr = tcp.socket.recvfrom(constants.BUFFER_SIZE) response = msg_parser.unpack_msg(response) msg_parser.validate_message_type(response) print constants.message_dictionary[response.type] if response.type == constants.message_type["ack"]: return response.data pass elif response.type == constants.message_type["nack"]: temp_successor = response.data elif response.type == constants.message_type["not_found"]: print "Not Found" return -1 pass
def run(self): print "Predecessor thread started" while not self.stopped(): try: print "Waiting for new predecessor at: ", tcp.predecessor_port while True: request, addr = tcp.predecessor_socket.recvfrom( constants.BUFFER_SIZE) print "recd from ", addr msg_parser = MessageParser() request = msg_parser.unpack_msg(request) if request.type != constants.message_type["heartbeat"]: request_handler(request) except Exception as e: print str(e)
def run(self): while not self.stopped(): try: # tcp.successor_socket.listen(constants.BACKLOG_FOR_TCP_SOCKET) print "Waiting for new successor at: ", tcp.successor_port # tcp.successor_connection, tcp.useless = tcp.successor_socket.accept() # print "New successor connected: ", successor while True: request, addr = tcp.successor_socket.recvfrom( constants.BUFFER_SIZE) print "recd from ", addr msg_parser = MessageParser() request = msg_parser.unpack_msg(request) if request.type != constants.message_type["heartbeat"]: request_handler(request) except Exception as e: print str(e)