def initialize_handler_vector_table(self): try: self.handler_vector_table[self.reg_msg_handler_no] = self.reg_msg_handler self.handler_vector_table[self.data_msg_handler_no] = self.data_msg_handler logger.debug("Handler vector table initialized.") except Exception as inst: logger.critical("ERROR: Exception in initialize_handler_vector_table: " + str(inst) )
def in_expected_range(self, new_id, old_id): try: # self.error_scope shows the range of session_id which may be old\ # and should be discarded return (new_id > old_id) or self.is_wrap_up(new_id, old_id) except Exception as inst: logger.critical("ERROR: Exception in in_expected_range: " + str(inst))
def in_expected_subseq_range(self, new_subseq_no): try: upper_limit = self.highest_nc_subseq_no + self.nc_window_size # 1. Ex: highest_nc_subseq_no == ackd_nc_subseq_no so acceptable msgs can fall in range # highest_nc_subseq_no < *HERE* <= highest_nc_subseq_no + nc_window_size # 2. Ex: ackd_nc_subseq_no < highest_nc_subseq_no so acceptable msgs can fall in range # ackd_nc_subseq_no < *HERE* <= highest_nc_subseq_no + nc_window_size # 3. Ex: ackd_nc_subseq_no > highest_nc_subseq_no so acceptable msgs can fall in range # ackd_nc_subseq_no < *HERE* <= upper_seq_bytes_limit OR 0 < *HERE* <= (highest_nc_subseq_no + nc_window_size) # Explanation for limit_due_to_wrap_up: suppose upper_seq_bytes_limit=255 and nc_window_size=2 # and highest_nc_subseq_no=254 then upper_limit(calculated above)=256 which is invalid # so set another limit_due_to_wrap_up which will be (in this case): 1 # Wrap-up can occur in both 1 & 2 cases above and 3rd is essentially wrap up if upper_limit > self.upper_seq_bytes_limit: limit_due_to_wrap_up = (self.highest_nc_subseq_no + \ self.nc_window_size) - (self.upper_seq_bytes_limit) upper_limit = self.upper_seq_bytes_limit else: # placeholder value limit_due_to_wrap_up = 0 if self.highest_nc_subseq_no >= self.ackd_nc_subseq_no: return (self.ackd_nc_subseq_no < new_subseq_no <= upper_limit) or\ (0 < new_subseq_no <= limit_due_to_wrap_up) return (self.ackd_nc_subseq_no < new_subseq_no <= \ self.upper_seq_bytes_limit) or (0 < new_subseq_no <= upper_limit) except Exception as inst: logger.critical("ERROR: Exception in in_expected_subseq_range: " + str(inst))
def save_session_id(self, tag_name, session_id): try: config = ConfigObj(self.log_file_name) config[tag_name] = session_id config.write() except Exception as inst: logger.critical("ERROR: Exception in save_session_id: " + str(inst) )
def close(self): try: self.external_communicator.shutdown = 1 self.external_communicator.handle_close() self.external_communicator.join(1) except Exception as inst: logger.critical("ERROR: Exception in close: " + str(inst))
def pass_thread_address(self, main_thread, sensor_controller): try: self.main_thread = main_thread self.sensor_controller = sensor_controller logger.debug("Addresses of main_thread and sensor_controller saved.") except Exception as inst: logger.critical("ERROR: Exception in pass_thread_address: " + str(inst))
def plugin_sensors(self): try: imported_sensor_modules, sensor_class_names = self.import_new_sensor_modules() logger.debug("Module extracted from package."+"\n\n") if sensor_class_names: sensor_class_objcts = [] for module_name, sensor_class_name in zip(imported_sensor_modules, sensor_class_names): if sensor_class_name != '__init__': try: # Loads the class dynamically from the imported module # So essentially the sensor_class_name (class name)\ # and the module_name (file name) must match sensor_class = getattr(module_name, sensor_class_name) # create an object of the above class sensor_class_obj = sensor_class() sensor_class_objcts.append(sensor_class_obj) except Exception as inst: logger.critical("Exception in plugin loop: " + str(inst)+"\n\n") self.register_modules(sensor_class_objcts, sensor_class_names) for sensor_class_name in sensor_class_names: self.sensorBoard_input_queue[sensor_class_name] = Queue.Queue() self.sensorBoard_output_queue[sensor_class_name] = Queue.Queue() logger.debug("Sensors' info added to the config file and their buffers created.") self.start_sensors(sensor_class_objcts, sensor_class_names) # Notify to the other threads that the sensors' info has been \ # saved in config file. Used to avoid race conditions. if not sensors_info_saved_event.is_set(): sensors_info_saved_event.set() logger.debug("Sensors_info_saved_event set."+"\n\n") return except Exception as inst: logger.critical("Exception in plugin_sensors: " + str(inst)+"\n\n")
def get_new_no(self, new_no, old_no): try: if (new_no != old_no) and self.in_expected_range(new_no, old_no): return new_no return old_no except Exception as inst: logger.critical("ERROR: Exception in get_new_no: " + str(inst))
def gen_gn_seq_no(self): try: self.highest_gn_subseq_no = self.increment_no(self.highest_gn_subseq_no) logger.debug("\tSUBSEQUENCE NO. gen:" + str(self.highest_gn_subseq_no)) return str(self.convert_to_bytearray(self.gn_session_id)) +\ str(self.convert_to_bytearray(self.highest_gn_subseq_no)) except Exception as inst: logger.critical("ERROR: Exception in gen_gn_seq_no: " + str(inst))
def valid_new_session_id(self, old_session_id, new_session_id): try: if old_session_id: return self.in_expected_sessionseq_range(new_session_id, old_session_id) # if this GN is contacting the NC for the first time then accept any session_id from NC return True except Exception as inst: logger.critical("ERROR: Exception in valid_new_session_id: " + str(inst))
def send_timed_out_msg(self): try: if self.sent_gnMsgBfr: timed_out_msg_info = self.get_timed_out_msg_info() if timed_out_msg_info: self.handler_vector_table[timed_out_msg_info[4]](timed_out_msg_info, None) except Exception as inst: logger.critical("ERROR: Exception in send_timed_out_msg: " + str(inst) )
def collect_incoming_data(self, data): try: if self.shutdown == 0: """Buffer the data""" self.input_buffer.append(data) logger.debug("Data received from NC.\n\n") except Exception as inst: logger.critical("Exception in collect_incoming_data: " + str(inst) + "\n\n")
def save_registered_sensors(self): try: config = ConfigObj(config_file_name) for sensor_name in config['Sensors Info']: if config["Sensors Info"][sensor_name]["Registered"] == 'YES': self.registered_sensors.append(sensor_name) except Exception as inst: logger.critical("Exception in is_sensor_registered: " + str(inst)+"\n\n")
def is_new_sensor(self, sensor_file_name): try: config = ConfigObj(config_file_name) if "last_sensors_registration_time" in config: return (config["last_sensors_registration_time"] < \ time.ctime(os.path.getmtime(self.watchdir+'/'+str(sensor_file_name)+".py"))) return True except Exception as inst: logger.critical("Exception in is_new_sensor: " + str(inst)+"\n\n")
def get_timed_out_msg_info(self): try: msg_handler_info = self.sent_gnMsgBfr[0] if msg_handler_info[2] < time.time(): self.sent_gnMsgBfr.remove(msg_handler_info) return msg_handler_info return None except Exception as inst: logger.critical("ERROR: Exception in get_timed_out_msg_info: " + str(inst))
def convert_to_int(self, byte_seq): try: if byte_seq != None: byte_seq = bytearray(byte_seq) int_id = sum(byte_seq[i] << ((len(byte_seq)-1-i) * 8) for i in range(len(byte_seq))) return int_id return None except Exception as inst: logger.critical("ERROR: Exception in convert_to_int: " + str(inst))
def register_modules(self, sensor_class_objcts, sensor_file_names): try: for sensor_class_obj, sensor_file_name in zip(sensor_class_objcts, sensor_file_names): if self.is_new_sensor(sensor_file_name): sensor_class_obj.register() self.update_last_sensors_registration_time() self.save_registered_sensors() except Exception as inst: logger.critical("Exception in register_modules: " + str(inst)+"\n\n")
def get_nc_ip(self): #while ip == '127.0.0.1': #logger.info("Waiting to get NC's IP..") #time.sleep(1) try: ip = open('nc_ip','r').read() return ip except Exception as inst: logger.critical("Exception in get_nc_ip: " + str(inst)+ "\n\n")
def get_node_info(config_file_name): config_object = ConfigObj(config_file_name) config = config_object["Systems Info"] list_of_keywords = ["hostname", "noOfProcessors", "cpuModelName", "cpuVendorID", "extensions", "hardware", "interfaces", "memTotal", "osName", "osVersion", "osID", "osPrettyName", "osVersionID", "osKernelRelease", "machineName", "kernelVersion", "diskStorage"] list_of_cmds = [ "hostname", "cat /proc/cpuinfo | grep -i processor | wc -l", "cat /proc/cpuinfo | grep -i model\ name | uniq | tr -s ' ' | cut -d ':' -f 2", "cat /proc/cpuinfo | grep -i vendor_id | uniq | cut -d ':' -f 2", ["cat /proc/cpuinfo | grep -i flags | tr ' ' '\n' | sort | uniq | grep -i -v flag | tr '\n' ' '", "cat /proc/cpuinfo | grep -i features | tr ' ' '\n' | sort | uniq | grep -i -v features | tr '\n' ' '"], "cat /proc/cpuinfo | grep -i hardware | cut -d ':' -f 2", ["ls /sys/class/net/", "cat /sys/class/net/"], 'cat /proc/meminfo | tr -s " " | grep -i MemTotal | cut -d ":" -f 2', 'cat /etc/os-release | grep -i NAME | grep -vi "pretty_name" | cut -d "=" -f 2 ', 'cat /etc/os-release | grep -i VERSION | grep -vi "version_id" |cut -d "=" -f 2', 'cat /etc/os-release | grep -i ID=ubuntu | cut -d "=" -f 2', 'cat /etc/os-release | grep -i PRETTY_NAME | cut -d "=" -f 2', 'cat /etc/os-release | grep -i VERSION_ID | cut -d "=" -f 2', 'uname -r', 'uname -m', 'cat /proc/version', "df -h | tr -s ' ' | cut -d ' ' -f 1,2,5,6 | tr '\n' ';' | sed 's/;/\ ;\ /g'" ] for key, cmd in zip(list_of_keywords, list_of_cmds): try: if not isinstance(cmd, list): config[key] = bashit(cmd) else: if key == "interfaces": interfaces = bashit(cmd[0]) interfaces=interfaces.split('\n') config[key] = interfaces if interfaces: interface_details = '' for i in interfaces: if i <> 'lo': interface_details += str(i) + ',' + bashit(cmd[1]+str(i)+"/address") + '\n' interface_details = interface_details.split('\n') config[key] = interface_details elif key == 'extensions': config[key] = bashit(cmd[0]) if not config[key]: config[key] = bashit(cmd[1]) if not config[key]: del config[key] except Exception as inst: logger.critical("Exception in get_system_info: " + str(inst) + "while executing command " + str(cmd) +"\n\n") return 1 config_object.write() return 0
def calculate_expiration_time(self, msg_type, msg): try: if msg_type == registration_type: wait_time = gn_registration_ack_wait_time elif msg_type == data_type: wait_time = data_ack_wait_time logger.debug("Calculated expiration_time based on msg_type and msg.") return (time.time() + wait_time) except Exception as inst: logger.critical("ERROR: Exception in calculate_expiration_time: " + str(inst))
def get_old_session_id(self, tag_name): try: if tag_name == 'NC Session ID' and self.nc_session_id: return self.nc_session_id config = ConfigObj(self.log_file_name) if tag_name in config: return int(config[tag_name]) return None except Exception as inst: logger.critical("ERROR: Exception in get_old_session_id: " + str(inst))
def get_msg_handler_no(self, msg_type): try: if msg_type == registration_type: logger.debug("Fetching registration msg handler no.") return self.reg_msg_handler_no elif msg_type == data_type: logger.debug("Fetching data msg handler no.") return self.data_msg_handler_no except Exception as inst: logger.critical("ERROR: Exception in get_msg_handler_no: " + str(inst))
def add_to_sent_msgs_bfr(self, msg_handler_info): try: logger.debug("Buffer size of GN_msgs_buffer_mngr's output buffer \ before adding item: " + str(len(self.sent_gnMsgBfr))) self.sent_gnMsgBfr.append(msg_handler_info) # sorted based on time so retrieval for expired msgs is in FIFO order sorted(self.sent_gnMsgBfr, key=lambda x: x[2]) logger.debug("Msg waiting for ACK inserted in sorted buffer.") except Exception as inst: logger.critical("ERROR: Exception in add_to_sent_msgs_bfr: " + str(inst) )
def convert_to_bytearray(self, int_no): try: if int_no != None: byte_seq = bytearray([0,0,0]) for i in range(self.seq_no_partition_size): byte_seq[i] = (int_no >> ((self.seq_no_partition_size-1-i)*8)) & 0xff return byte_seq return None except Exception as inst: logger.critical("ERROR: Exception in convert_to_bytearray: " + str(inst))
def start_sensors(self, sensor_class_objcts, sensor_class_names): try: config = ConfigObj(config_file_name) for sensor_class_obj, sensor_class_name in zip(sensor_class_objcts, sensor_class_names): t = Thread(target=self.start_sensor, args = (sensor_class_obj, \ self.sensorBoard_input_queue[sensor_class_name], self.sensorBoard_output_queue[sensor_class_name])) self.update_sensor_thread_list(t) t.start() logger.debug("New sensors started."+"\n\n") except Exception as inst: logger.critical("Exception in start_sensors: " + str(inst)+"\n\n")
def send_msg_to_nc(self, msg): try: self.external_communicator.push(msg) # logger.critical("Msg Sent to NC:"+str('%0.4f' % time.time())+ \ # "\tcount:" + str(self.sent_msg_count) + "\t" + str(encoded_msg)) # # logger.critical("Msg Sent to NC:"+str('%0.4f' % time.time())+":"\ # +str(self.highest_gn_subseq_no)+ ":"+str(self.ackd_gn_subseq_no)) logger.critical("Msg "+str(self.highest_gn_subseq_no)+" Sent to NC:"+str('%0.4f' % time.time())+">>>>>>>>>>>>>>>>>>>>") except Exception as inst: logger.critical("ERROR: Exception in send_msg_to_nc: " + str(inst)) self.send_msg_to_nc()
def increment_no(self, int_no): integer_no = copy.copy(int_no) try: if integer_no == self.upper_seq_bytes_limit: # reset it to 1 integer_no = self.default_seq_no + 1 else: integer_no += 1 except Exception as inst: logger.critical("ERROR: Exception in increment_no: " + str(inst)) return integer_no
def extract_module_names(self, module_list): try: module_names = [] for module in module_list: if not os.path.isdir(module): module = module.split('.') if self.is_source_module(module) and self.is_new_module(module[0]): module_names.append(module[0]) logger.debug("New module names extracted."+"\n\n") return module_names except Exception as inst: logger.critical("Exception in extract_module_names: " + str(inst)+"\n\n")
def initialize_gn_session_id(self): try: session_id = self.get_old_session_id('GN Session ID') if not session_id: session_id = self.initial_session_id session_id = int (session_id) session_id = self.increment_no(session_id) self.save_session_id("GN Session ID", session_id) logger.debug("Seq_no. initialized.") return session_id except Exception as inst: logger.critical("ERROR: Exception in initialize_gn_session_id: " + str(inst) )
def handle_request(self): try: msg = '' if self.shutdown == 0: # recreates msg by concatenating list's elements for single_msg in self.input_buffer: msg = msg + single_msg msg = buffered_msg(None, None, None, msg) # Sends msg to the buffer_mngr's buffer add_to_thread_buffer(self.buffer_mngr.incoming_ncAckBfr, msg, "Buffer Mngr") logger.debug("Msg forwarded to buffer_mngr.\n\n") except Exception as inst: logger.critical("Exception in handle_request: " + str(inst) + "\n\n")