def __init__(self, client, server, options=None): """It receives two :class:`SAPNIStreamSocket`s objects and creates the worker for processing data. Thread is started as daemon. :param client: client Stream Socket :type client: :class:`SAPNIStreamSocket` :param server: server Stream Socket :type server: :class:`SAPNIStreamSocket` :param options: options received from the proxy :type options: ``dict`` """ self.client = client self.server = server self.poll_interval = 0.5 self.processor = Worker(self, self._handle) self.processor.daemon = True self.processor.start()
def __init__(self, client, server, options=None): """It receives two L{SAPNIStreamSocket}s objects and creates the worker for processing data. Thread is started as daemon. @param client: client Stream Socket @type client: L{SAPNIStreamSocket} @param server: server Stream Socket @type server: L{SAPNIStreamSocket} @param options: options received from the proxy @type options: C{dict} """ self.client = client self.server = server self.poll_interval = 0.5 self.processor = Worker(self, self._handle) self.processor.daemon = True self.processor.start()
def __init__(self, client, server, options=None): """ It receives two L{SAPNIStreamSocket}s objects and creates the upload and download workers for processing data. Threads are started as daemons. @param client: client Stream Socket @type client: L{SAPNIStreamSocket} @param server: server Stream Socket @type server: L{SAPNIStreamSocket} @param options: options received from the proxy @type options: C{dict} """ self.client = client self.server = server upload_processor = Worker(self, self._upload) download_processor = Worker(self, self._download) upload_processor.daemon = True download_processor.daemon = True upload_processor.start() download_processor.start()
class SAPNIProxyHandler(object): """SAP NI Proxy Handler Handles NI packets. Works spawning one thread for processing data coming from each pair of client/server. """ def __init__(self, client, server, options=None): """It receives two :class:`SAPNIStreamSocket`s objects and creates the worker for processing data. Thread is started as daemon. :param client: client Stream Socket :type client: :class:`SAPNIStreamSocket` :param server: server Stream Socket :type server: :class:`SAPNIStreamSocket` :param options: options received from the proxy :type options: ``dict`` """ self.client = client self.server = server self.poll_interval = 0.5 self.processor = Worker(self, self._handle) self.processor.daemon = True self.processor.start() def recv_send(self, local, remote, process): """Receives data from one socket connection, process it and send to the remote connection. :param local: the local socket :type local: :class:`SAPNIStreamSocket` :param remote: the remote socket :type remote: :class:`SAPNIStreamSocket` :param process: the function that process the incoming data :type process: function """ # Receive a SAP NI packet packet = local.recv() log_sapni.debug("SAPNIProxyHandler: Received %d bytes", len(packet)) # Process the packet using the given function packet = process(packet) # Send the packet to the remote peer remote.send(packet.payload) log_sapni.debug("SAPNIProxyHandler: Sent %d bytes", len(packet)) def _handle(self): """Handles data coming from either the client or the server""" r, __, __ = select([self.client, self.server], [], [], self.poll_interval) if self.client in r: try: log_sapni.debug( "SAPNIProxyHandler: Client --> Server connection") self.recv_send(self.client, self.server, self.process_client) except socket.error: log_sapni.error("SAPNIProxyHandler: Client connection down") self.stop_workers() if self.server in r: try: log_sapni.debug( "SAPNIProxyHandler: Client <-- Server connection") self.recv_send(self.server, self.client, self.process_server) except socket.error: log_sapni.error("SAPNIProxyHandler: Server connection down") self.stop_workers() def process_client(self, packet): """This method is called each time a packet arrives from the client. It must return a packet in the same layer (:class:`SAPNI`). Stub method to be overloaded in subclasses. :param packet: the packet to be processed :type packet: Packet """ return packet def process_server(self, packet): """This method is called each time a packet arrives from the server. It must return a packet in the same layer (:class:`SAPNI`). Stub method to be overloaded in subclasses. :param packet: the packet to be processed :type packet: Packet """ return packet def stop_workers(self): """Stop the processor workers""" self.client.close() self.server.close() self.processor.stop()
class SAPNIProxyHandler(object): """SAP NI Proxy Handler Handles NI packets. Works spawning one thread for processing data coming from each pair of client/server. """ def __init__(self, client, server, options=None): """It receives two L{SAPNIStreamSocket}s objects and creates the worker for processing data. Thread is started as daemon. @param client: client Stream Socket @type client: L{SAPNIStreamSocket} @param server: server Stream Socket @type server: L{SAPNIStreamSocket} @param options: options received from the proxy @type options: C{dict} """ self.client = client self.server = server self.poll_interval = 0.5 self.processor = Worker(self, self._handle) self.processor.daemon = True self.processor.start() def recv_send(self, local, remote, process): """Receives data from one socket connection, process it and send to the remote connection. @param local: the local socket @type local: L{SAPNIStreamSocket} @param remote: the remote socket @type remote: L{SAPNIStreamSocket} @param process: the function that process the incoming data @type process: function """ # Receive a SAP NI packet packet = local.recv() log_sapni.debug("SAPNIProxyHandler: Received %d bytes", len(packet)) # Process the packet using the given function packet = process(packet) # Send the packet to the remote peer remote.send(packet.payload) log_sapni.debug("SAPNIProxyHandler: Sent %d bytes", len(packet)) def _handle(self): """Handles data coming from either the client or the server""" r, __, __ = select([self.client, self.server], [], [], self.poll_interval) if self.client in r: try: log_sapni.debug("SAPNIProxyHandler: Client --> Server connection") self.recv_send(self.client, self.server, self.process_client) except socket.error: log_sapni.error("SAPNIProxyHandler: Client connection down") self.stop_workers() if self.server in r: try: log_sapni.debug("SAPNIProxyHandler: Client <-- Server connection") self.recv_send(self.server, self.client, self.process_server) except socket.error: log_sapni.error("SAPNIProxyHandler: Server connection down") self.stop_workers() def process_client(self, packet): """This method is called each time a packet arrives from the client. It must return a packet in the same layer (L{SAPNI}). Stub method to be overloaded in subclasses. @param packet: the packet to be processed @type packet: Packet """ return packet def process_server(self, packet): """This method is called each time a packet arrives from the server. It must return a packet in the same layer (L{SAPNI}). Stub method to be overloaded in subclasses. @param packet: the packet to be processed @type packet: Packet """ return packet def stop_workers(self): """Stop the processor workers""" self.client.close() self.server.close() self.processor.stop()