def main_loop(self, container_id): """ The 'internal' loop. Listen to SBus, receive datagram, dispatch command, report back. :param container_id: container id :returns: exit status (SUCCESS/FAILURE) """ # Create SBus. Listen and process requests sbus = SBus() fd = sbus.create(self.pipe_path) if fd < 0: self.logger.error("Failed to create SBus. exiting.") return EXIT_FAILURE while True: rc = sbus.listen(fd) if rc < 0: self.logger.error("Failed to wait on SBus. exiting.") return EXIT_FAILURE self.logger.debug("Wait returned") dtg = sbus.receive(fd) # TODO(eranr): # Should we really be exitting here. # If so should we exit the container altogether, so # that it gets restarted? if dtg is None: self.logger.error("Failed to receive message. Exitting.") return EXIT_FAILURE outfd = dtg.get_service_out_fd() if outfd is None: self.logger.error("Received message does not have outfd." " continuing.") continue self.logger.debug("Received outfd %d" % outfd) with os.fdopen(outfd, 'w') as outfile: resp = self.dispatch_command(dtg, container_id) self.log_and_report(outfile, resp) if not resp.iterable: break # We left the main loop for some reason. Terminating. self.logger.debug('Leaving main loop') return EXIT_SUCCESS
def main_loop(self, container_id): """ The 'internal' loop. Listen to SBus, receive datagram, dispatch command, report back. """ # Create SBus. Listen and process requests sbus = SBus() fd = sbus.create(self.pipe_path) if fd < 0: self.logger.error("Failed to create SBus. exiting.") return b_iterate = True b_status = True error_text = '' while b_iterate: rc = sbus.listen(fd) if rc < 0: self.logger.error("Failed to wait on SBus. exiting.") return self.logger.debug("Wait returned") dtg = sbus.receive(fd) if not dtg: self.logger.error("Failed to receive message. exiting.") return try: # TODO(takashi): We had better use contextmanager outfd = dtg.get_first_file_of_type(SBUS_FD_OUTPUT_OBJECT) except Exception: self.logger.error("Received message does not have outfd." " continuing.") continue else: self.logger.debug("Received outfd %d" % outfd.fileno()) b_status, error_text, b_iterate = \ self.dispatch_command(dtg, container_id) self.log_and_report(outfd, b_status, error_text) outfd.close() # We left the main loop for some reason. Terminating. self.logger.debug('Leaving main loop')