def __init__(self, path, receiver_func=None, background=False, use_threading=True): self.path = path self.receiver_func_callback = receiver_func self.background = background if use_threading: self.unixconnection = ThreadingUnixDatagramServer(self.path, self.__receiver_thread) else: self.unixconnection = UnixDatagramServer(self.path, self.__receiver_thread)
class ALPSUNIXConnection: def __receiver_thread(self, request, client_path, server): message = request[0] if callable(self.receiver_func_callback): self.receiver_func_callback(self, client_path, message) # print client_path def __init__(self, path, receiver_func=None, background=False, use_threading=True): self.path = path self.receiver_func_callback = receiver_func self.background = background if use_threading: self.unixconnection = ThreadingUnixDatagramServer(self.path, self.__receiver_thread) else: self.unixconnection = UnixDatagramServer(self.path, self.__receiver_thread) # self.__objname = inspect.stack()[1][-2][0].split('=')[0].strip() def __serve_forever(self): try: self.unixconnection.serve_forever() except: pass def bind(self, thread_name="UNIXConnection Thread"): self.unixsock_thread = ALPSThread(threadfunc=self.__serve_forever, threadname=thread_name) self.unixsock_thread.setDaemon(not self.background) self.unixsock_thread.start() def send(self, path, message, error_except=True): if not error_except: return self.unixconnection.socket.sendto(message, path) try: self.unixconnection.socket.sendto(message, path) except Exception as e: ALPSDebug.alps_error(e, '\n', ALPSDebug.format_inspect_traceback(inspect.currentframe().f_back)) return False return True def close(self): os.unlink(self.path) self.unixconnection.server_close()
def main(): args = parse_args() config = Config.from_file(args.config) global log # pylint: disable=W0603 log = configure_logger(config.log_debug, config.log_datetime) log.debug("load configuration from %s, %d commands loaded", args.config, len(config.commands)) signal.signal(signal.SIGTERM, signal_handler) global cache # pylint: disable=W0603 cache = Cache(mode=config.cache_delete_mode, maxsize=config.queue_size, ttl=config.cache_expire) global queue # pylint: disable=W0603 queue = Queue.Queue(maxsize=config.queue_size) for i in range(1, config.workers + 1): worker = Worker(name="Worker-%d" % i, queue=queue, cache=cache, cmd_list=config.commands) worker.start() log.info("listen unix socket on %s", config.socket) server = UnixDatagramServer(config.socket, RequestHandler) try: server.serve_forever() except KeyboardInterrupt: log.info("shutting down server") exit(0) finally: os.unlink(config.socket)