Exemplo n.º 1
0
 def __init__(self):
     self._event_loop_thread = ZEventLoopThread() 
     self._event_loop = self._event_loop_thread.start_loop()
     self._guard = Condition(Lock())
     self._request_senders = {}
     self._client_msg_callback = None
     self._client_req_done_callback = None 
     self._sid = 0
Exemplo n.º 2
0
class FLRRequestSender(object):
    PORT = 9876
    def __init__(self):
        self._event_loop_thread = ZEventLoopThread() 
        self._event_loop = self._event_loop_thread.start_loop()
        self._guard = Condition(Lock())
        self._request_senders = {}
        self._client_msg_callback = None
        self._client_req_done_callback = None 
        self._sid = 0

    def send_requests(self, reqs):
        for req in reqs:
            self._request_senders.clear()
            logging.info('handling request %s %s' %(req[0], req[1]))
            sid = 0
            with self._guard:
                sid = self._sid
                self._sid += 1
            scanner_client = RequestSender(self._event_loop, sid, (req[0], self.PORT), req[-1])
            scanner_client.set_connection_callback(self._on_connection)
            scanner_client.set_connection_error_callback(self._on_error_connection)
            scanner_client.set_message_callback(self._on_message)
            scanner_client.connect()
            with self._guard:
                self._request_senders[sid] = [scanner_client, req]

    def set_message_callback(self, callback):
        self._client_msg_callback = callback

    def set_request_done_callback(self, callback):
        self._client_req_done_callback = callback

    def _on_connection(self, scanner_client, tcp_conn):
        if tcp_conn.connected():
            with self._guard:
                req = self._request_senders[scanner_client.get_id()][1]
            scanner_client.send(tcp_conn, req[1])
        else:
            done = False
            with self._guard:
                self._request_senders.pop(scanner_client.get_id())
                if not self._request_senders:
                    done = True
            if done and self._client_req_done_callback:
                self._client_req_done_callback(self)
    
    def _on_message(self, scanner_client, tcp_conn, message, receive_time):
        with self._guard:
            req = self._request_senders[scanner_client.get_id()][1]
        logging.info('receive message: %s for request: %s' %(message, req[0]))
        #scanner_client.disconnect()
        if self._client_msg_callback:
            self._client_msg_callback(self, scanner_client.get_client_data(), tcp_conn, message, receive_time)

    def _on_error_connection(self, server_addr, errno):
        logging.error('failed to connect to %s, errno=(%d)' %str(server_addr), errno)
        # FIXME Propergate this error to up layer
    
    def quit(self):
        with self._guard:
            if not self._request_senders:
                self._event_loop.quit()
            else:
                # todo...
                logging.warn('We are waiting for the reply from the scan servers')