def action(path=None): stub_file = abspath( join(dirname(realpath(__file__)), '..', 'settings.pyi')) # Process is run from current executable to ensure that Savannah is installed try: out = subprocess.check_output([sys.executable, stub_file], stderr=subprocess.STDOUT) CONFIG_PATH = join(path, 'settings.json') if path else join( environ['SAVANNAH_BASEDIR'], 'settings.json') # We can't use settings import to load the config path at this point # since we are trying to create the settings. # An import fix could enable config path loading even if settings.json does not exist # However, this could have repercussions in the way the rest of the modules # are used (expecting that settings are configured when they are not), # for this reason it is wiser to just define the variable at this scope. with open(CONFIG_PATH, "wb") as file: file.write(out) logger.info("Settings have been created at {}".format(CONFIG_PATH)) except subprocess.CalledProcessError as err: logger.error( "Could not create settings. Creation file returned error code {0} and message: \n {1}" .format(err.returncode, err.output)) sys.exit(err.returncode)
def init(self): if not IOUtils.socket_available(self.host, self.port): raise RuntimeError('Socket is occupied.') # We initialize the SamplingUnit so it can be passed to the interpreter self.sampling_unit = SamplingUnit() command_interpreter = environ.load_interpreter().Interpreter( self.sampling_unit.manager) self.server = CPUServer(self.host, self.port, command_interpreter) # We initialize the UnitManager self.unit_manager = UnitManager() # We create a space to store the queue proxies to the sensors self.unit_manager.sampling_proxies = \ {k: self.unit_manager.ioserver.Queue() for k in self.sampling_unit.sensor_dict.keys()} # Now we start the threads self.sampling_unit.init(self.unit_manager.sampling_proxies) self.server.run() logger.info( "IOUnit has been initialized. CPUServer now running at //{0}:{1}". format(self.host, self.port))
def _loop_target(self, queue_proxy): self.reader.queue = queue_proxy logger.info( "Sensor {name} has started sampling at a frequency {freq}".format( name=self.reader.sensor.name(), freq=self.sampling_frequency)) super()._loop_target()
def task(self): while not self._mother.close_flag: # The following statement blocks when there are no connections in the queue. # If the server flag to stop has been changed, to make the loop break we only # need to send a connection with some empty message. self._mother.curr_conn, self._mother.curr_addr = curr_conn, curr_addr = self._mother.socket.accept( ) logger.info( "[CPUServer]: New incoming connection at {addr}".format( addr=curr_addr)) try: raw_message = Utils.recv_message(curr_conn) # The empty connection to close the thread can contain anything, but using # a conventional word saves up time since the interpreter is not involved. if raw_message and raw_message != b'NEXT': message = raw_message.decode() logger.info( "[CPUServer]: {addr} sent: \"{msg}\"".format( addr=curr_addr, msg=message)) response = self._mother.interpret_and_execute(message) response_type = type(response) if response_type not in (bytes, str): response = pickle.dumps(response) elif response_type is str: response = response.encode() Utils.send_message(curr_conn, b'exec_ok:1') logger.info( "[CPUServer]: {addr} request [EXEC_OK]".format( addr=curr_addr)) Utils.send_message( curr_conn, 'data_type:{}'.format( response_type.__name__).encode()) Utils.send_message(curr_conn, response) logger.info( "[CPUServer]: {addr} request was successfully responded with data_type {dt}" .format(addr=curr_addr, dt=response_type.__name__)) except (ConnectionError, OSError) as e: # TODO: This should be carefully tested in the future. # Current tests indicate that this exception is due to a finalised # connection at the other side. logger.warning("[CPUServer]: {addr} [ERROR]: {msg}".format( addr=curr_addr, msg=Utils.exception_message(e))) except EvaluationException as e: Utils.send_message(curr_conn, b'exec_ok:0') Utils.send_message(curr_conn, Utils.exception_message(e).encode()) logger.warning( "[CPUServer]: {addr} [EVALUATION_EXCEPTION]: {msg}". format(addr=curr_addr, msg=Utils.exception_message(e))) finally: self._mother.curr_conn.close() logger.info( "[CPUServer]: {addr} connection closed.".format( addr=curr_addr)) del curr_addr, curr_conn
def init(self, sampling_proxies): sampler_list = SamplingUtils.make_samplers(self.sensor_dict.values()) self.manager.propagate(sampler_list) self.manager.start_all(sampling_proxies) logger.info("SamplingUnit has been initialized")