def close_gn_socket(self): # Lock acquired to access global list variable and released when the 'with' block ends with gn_socket_list_lock: for socket in gn_socket_list: socket.shutdown = 1 gn_socket_list.remove(socket) socket.close() logger.info("Individual GN socket closed.") logger.critical("NC's individual GN sockets closed."+"\n\n")
def register_gn(self, gn_info): try: for single_gn_info in gn_info: config = ConfigObj(config_file_name) config["GN Info"][single_gn_info.instance_id] = single_gn_info.sys_info config["GN Info"][single_gn_info.instance_id]["Registered"] = 'NO' config.write() logger.info("GN registration info saved in config file.") except Exception as inst: logger.critical("Exception in register_gn:" + str(inst))
def gen_nc_seq_no(self, inst_id): try: if inst_id in self.highest_nc_subseq_no: self.highest_nc_subseq_no[inst_id] = self.increment_no(self.highest_nc_subseq_no[inst_id]) logger.info("Sequence no. generated: " + str(self.highest_nc_subseq_no[inst_id]) +\ "for Node:"+str(inst_id)) return str(self.convert_to_bytearray(self.nc_session_id)) +\ str(self.convert_to_bytearray(self.highest_nc_subseq_no[inst_id])) return None except Exception as inst: logger.critical("Exception in gen_nc_seq_no: " + str(inst))
def handle_accept(self): try: logger.info("New GN connection available." + "\n\n") gn_socket_conn, gn_addr = self.accept() socket = internal_communicator(gn_socket_conn, gn_addr, self.buffer_mngr) # Lock acquired to access global list variable and released when the 'with' block ends with gn_socket_list_lock: # Pass GN's details and new socket's details to a new socket object created for that GN gn_socket_list.append(socket) #logger.debug("Socket object corresponding to new GN created and running.") except: logger.critical("Error in handling the GN connection request."+ "\n\n") pass
def run(self): try: self.bind(("127.0.0.1", self.port_for_gn)) # Backlog argument: 5 specifies the maximum number of queued connections\ # and should be at least 1; the maximum value is system-dependent (usually 5) self.listen(5) logger.info("Listening on port_for_gn: " + str(self.port_for_gn)+"\n\n") # starts the loop which constantly checks whether the socket is readable or writable while True: # Starts the loop which polls all the open sockets one by one, # comes out of the asyncore loop after polling for *count* # times and then repeats after 0.01s. # Performance widely varies based on sleep time and count's value # When count=0/None control will never come out of the asyncore loop asyncore.loop(timeout=0.01, use_poll=True, map=None) # , count=n) can be added. n>=1 time.sleep(0.01) except Exception as inst: logger.critical("Exception in nc_server run(): " + str(inst)+"\n\n") time.sleep(1) self.run()