Example #1
0
class Scanner(object):
    def __init__(self, event_loop, sid, server_addr):
        self._sid = sid
        self._codec = ZLengthHeadCodec(self._on_string_message)
        self._tcp_client = ZTcpClient(event_loop, server_addr)
        self._tcp_client.set_connection_callback(self._on_connection)
        self._tcp_client.set_connection_error_callback(self._on_error_connection)
        self._tcp_client.set_message_callback(self._codec.on_message)
        self._guard = Condition(Lock())
        self._connection_callback = None 
        self._connection_error_callback = None
        self._message_callback = None 

    def connect(self):
        self._tcp_client.connect()

    def disconnect(self):
        self._tcp_client.disconnect()
    
    def set_connection_callback(self, cb):
        self._connection_callback = cb

    def set_connection_error_callback(self, cb):
        self._connection_error_callback = cb

    def set_message_callback(self, cb):
        self._message_callback = cb

    def send(self, tcp_conn, msg):
        self._codec.send(tcp_conn, msg)

    def get_id(self):
        return self._sid

    def _on_connection(self, tcp_conn):
        if self._connection_callback:
            self._connection_callback(self, tcp_conn)
       
    def _on_error_connection(self, errno):
        if self._connection_error_callback:
            self._connection_error_callback(self, errno)
        else:
            logging.error('failed to connect to %s, errno=(%d)' %str(server_addr), errno)

    def _on_string_message(self, tcp_conn, message, receive_time):
        if self._message_callback:
            self._message_callback(self, tcp_conn, message, receive_time)
Example #2
0
class TestTcpClient(object):
    def __init__(self, server_addr):
        self._event_loop_thread = ZEventLoopThread()
        self._tcp_client = ZTcpClient(self._event_loop_thread.start_loop(), server_addr)
        self._codec = ZLengthHeadCodec(self._on_string_message)
        self._tcp_client.set_connection_callback(self._on_connection)
        self._tcp_client.set_message_callback(self._codec.on_message)
        self._tcp_conn = None
        self._guard = Condition(Lock())

    def connect(self):
        self._tcp_client.connect()

    def send_message_until_quit(self):
        while True:
            try:
                s = input()
                if s.find("bye") < 0:
                    self._write(s)
                else:
                    self._tcp_client.disconnect()
                    return
            except Exception as e:
                logging.error("input error %s" % (str(e)))

    def _write(self, s):
        with self._guard:
            if self._tcp_conn is not None:
                self._codec.send(self._tcp_conn, s)

    def _on_connection(self, tcp_conn):
        logging.info(tcp_conn.name() + " is " + (tcp_conn.connected() and "UP" or "DOWN"))
        with self._guard:
            if tcp_conn.connected():
                self._tcp_conn = tcp_conn
            else:
                self._tcp_conn = None

    def _on_string_message(self, tcp_conn, message, recieve_time):
        logging.info(message)