def create_and_run_server(server_class, handler_class, port, *args): ''' Create and Run server in another thread. Returns the (server object, thread) ''' httpd = create_server(server_class, handler_class, port, *args) th = utils.run_thread(HTTPServer.serve_forever, httpd) return (httpd, th)
def multicastMsg(self, logical_id, pid, msg_id): ''' API that receives the update message :return: ''' with self.print_mutex: print "recevied multicast message at", self.pid, msg_id, pid, threading.currentThread( ) with self.__rwlock.writer_lock(): self.logical_id = max(int(logical_id), self.logical_id) + 1 self.compute_info() idx = self.in_queue(msg_id, pid) if idx == -1: self.queue.append((int(msg_id), int(pid), set())) self.queue = sorted(self.queue, key=(lambda x: (x[0], x[1]))) if self.greater_pids[self.pid] == 0: #Server with highest PID. Request has been processed and #send ack to all previous PIDs for server in self.servers: if server != self.pid2server(self.pid): with self.__rwlock.writer_lock(): self.logical_id += 1 q = 'http://' + server + '/multicastAck/%d/%d/%d/%s' % ( self.logical_id, int(pid), int(msg_id), self.pid2server(self.pid)) r = utils.run_thread(requests.get, q) return json.dumps({"response": "success"})
def __init__(self, server_id): ''' :param id: Server Address ''' self.id = server_id self.servers = [] self.__do_leader_election = True self.__leader_election_thread = utils.run_thread(self.perpetual_election)
def set_leader(self, start_clock_sync=True): ''' Set the clock to be master clock. :return: ''' self._is_leader = True if start_clock_sync: self.__thread_lock.acquire() if self.clock_sync_thread is None: self.clock_sync_thread = utils.run_thread( self.perform_clock_sync) self.__thread_lock.release()
def multicast_ordering(self): ''' Every request to Front-end server is passed to this method. Implements the multicast total ordering. :return: ???? ''' with self.__rwlock.writer_lock(): self.compute_info() # Step 1: set logical timestamp msg_id = self.logical_id self.logical_id += 1 print self.servers # Step 2: Multicast acknowledgement of message to all front end for server in self.servers: # call API with self.__rwlock.writer_lock(): self.logical_id += 1 s = 'http://' + server + '/multicastMsg/%d/%d/%d' % ( self.logical_id, self.pid, msg_id) r = utils.run_thread(requests.get, s)
def multicastAck(self, logical_id, pid, msg_id, server_pid): ''' :param logical_id: :param pid: :param server_pid: :return: ''' idx = -1 is_set_full = False with self.__rwlock.writer_lock(): self.compute_info() idx = self.in_queue(msg_id, pid) if idx == -1: self.queue.append((int(msg_id), int(pid), set([server_pid]))) else: self.logical_id = max(int(logical_id), self.logical_id) + 1 self.queue[idx][2].add( server_pid) # received acknowledgment from server self.queue = sorted(self.queue, key=(lambda x: (x[0], x[1]))) if len(self.queue[0][2]) == self.greater_pids[self.pid]: is_set_full = True if is_set_full: for pid in self.pids: if pid < self.pid: with self.__rwlock.writer_lock(): self.logical_id += 1 w = 'http://' + self.pid2server( pid) + '/multicastAck/%d/%d/%d/%s' % ( int(self.logical_id), int(pid), int(msg_id), "127.0.0.1:%d" % self.pid2server(self.pid)) r = utils.run_thread(requests.get, w) return json.dumps({"response": "success"})
def start_raffle_thread(self): ''' Separate thread to run raffle ''' self.__raffle_running = True self.raffle_thread = utils.run_thread(self.__raffle_thread_func)
def start_periodic_do(self, to_print=True): self.periodic_running = True self.to_print = to_print self.sleeping_time = config.SLEEP_TIME self.periodic_thread = utils.run_thread(Client._periodic_do_fun, self)