class EntryPoint: """Portal receives data from router, sends to service and then receives results from service, sends results to router. Portal interact with service using unix socket while interacting with router through input and output plugins. """ def __init__(self, tuple_input, tuple_output, tuple_exchange): """:parameter: tuple_input - a tuple object contains plugin name and arguments of that plugin. Plugin name is required and arguments are optional. :parameter: tuple_output - a tuple object contains plugin name and arguments of that plugin. Plugin name is required and arguments are optional. :parameter: tuple_exchange - a tuple object contains plugin name and arguments of that plugin. Plugin name is required and arguments are optional. """ assert isinstance(tuple_input, tuple) assert isinstance(tuple_output, tuple) assert isinstance(tuple_exchange, tuple) self.plugin_input = tuple_input[0] self.plugin_output = tuple_output[0] self.plugin_exchange = tuple_exchange[0] if len(tuple_input) is 2: self.input_args = tuple_input[1] if len(tuple_input) is 2: self.output_args = tuple_output[1] if len(tuple_input) is 2: self.exchange_args = tuple_exchange[1] # Get input, output and exchange classes. # Instantiation will be performed after arguments are verified. self.manager = PluginManager() self.PluginInput = self.manager.get_plugin(self.plugin_input) self.PluginOutput = self.manager.get_plugin(self.plugin_output) self.PluginExchange = self.manager.get_plugin(self.plugin_exchange) def start(self): """start to work, transport data to services and get all result""" # verify arguments first self.input_plugin = self.PluginInput(**self.input_args) self.output = self.PluginOutput(**self.output_args) self.exchanger = self.PluginExchange(**self.exchange_args) # stream_in = self.input_plugin.run() self.output.run(self.exchanger.run(self.input_plugin.run())) def _verify_args(self): """Verify all input arguments""" json.loads(self.input_args) # arguments received from agent json.loads(self.PluginInput.__doc__) # plugin's doc string json.loads(self.output_args) # arguments received from agent json.loads(self.PluginOutput.__doc__) # plugin's doc string json.loads(self.exchange_args) # arguments received from agent json.loads(self.PluginExchange.__doc__) # plugin's doc string
class Portal: """Portal receives data from router, sends to service and then receives results from service, sends results to router. Portal interact with service using unix socket while interacting with router through input and output plugins. """ def __init__(self, plugin_input, plugin_output, service_name, sock, max_conns): """input: input plugin name to load output: output plugin name to load service_name: service name which Portal interact with """ self.manager = PluginManager() self.input = self.manager.get_plugin(plugin_input)() self.output = self.manager.get_plugin(plugin_output)() self.service_name = service_name # self.portal_server = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) #self.portal_server = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self.sock_file = sock #self.max_conns = max_conns #if os.path.exists(self.sock_file): # os.unlink(self.sock_file) #self.portal_server.setblocking(0) #self.portal_server.bind(self.sock_file) #self.portal_server.listen(self.max_conns) def get_connection(self,sockfile): while True: #if os.path.exists(sockfile): if True: sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) try: sock.connect(sockfile) return sock except socket.error as e: logger.drs_log.debug("Connection Failed(%s), waiting..." %e) sleep(10) else: logger.drs_log.debug("Unix socket file %s not found. Waiting for sockets..." % sockfile) sleep(10) def run(self): x = 1 while x >= 1: stream_in = self.input.run() logger.drs_log.debug("read data: %d" % len(stream_in)) connection = self.get_connection(self.sock_file) logger.drs_log.debug("connecting from (%s,%d)" % connection.getsockname()) client = multiprocessing.Process(target=self.exchange, args=(connection, stream_in)) client.daemon = True client.start() logger.drs_log.debug("Portal thread:%s" % client.ident) client.join() connection.shutdown(2) connection.close() x -= 1 def exchange(self, connection, stream_in): logger.drs_log.info("Start data exchange") exchange = InteractWithService(connection, stream_in, buffer_size=1024) stream_out = exchange.exchange() logger.drs_log.info("Finish data exchange.") self.output.run(stream_out)
class Portal: """Portal receives data from router, sends to service and then receives results from service, sends results to router. Portal interact with service using unix socket while interacting with router through input and output plugins. """ def __init__(self, plugin_input, plugin_output, service_name, sock, max_conns): """input: input plugin name to load output: output plugin name to load service_name: service name which Portal interact with """ self.manager = PluginManager() self.input = self.manager.get_plugin(plugin_input)() self.output = self.manager.get_plugin(plugin_output)() self.service_name = service_name # self.portal_server = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) #self.portal_server = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self.sock_file = sock #self.max_conns = max_conns #if os.path.exists(self.sock_file): # os.unlink(self.sock_file) #self.portal_server.setblocking(0) #self.portal_server.bind(self.sock_file) #self.portal_server.listen(self.max_conns) def get_connection(self, sockfile): while True: #if os.path.exists(sockfile): if True: sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) try: sock.connect(sockfile) return sock except socket.error as e: logger.drs_log.debug("Connection Failed(%s), waiting..." % e) sleep(10) else: logger.drs_log.debug( "Unix socket file %s not found. Waiting for sockets..." % sockfile) sleep(10) def run(self): x = 1 while x >= 1: stream_in = self.input.run() logger.drs_log.debug("read data: %d" % len(stream_in)) connection = self.get_connection(self.sock_file) logger.drs_log.debug("connecting from (%s,%d)" % connection.getsockname()) client = multiprocessing.Process(target=self.exchange, args=(connection, stream_in)) client.daemon = True client.start() logger.drs_log.debug("Portal thread:%s" % client.ident) client.join() connection.shutdown(2) connection.close() x -= 1 def exchange(self, connection, stream_in): logger.drs_log.info("Start data exchange") exchange = InteractWithService(connection, stream_in, buffer_size=1024) stream_out = exchange.exchange() logger.drs_log.info("Finish data exchange.") self.output.run(stream_out)