示例#1
0
文件: __init__.py 项目: rosalesr/krpc
def connect(address=DEFAULT_ADDRESS, rpc_port=DEFAULT_RPC_PORT, stream_port=DEFAULT_STREAM_PORT, name=None):
    """
    Connect to a kRPC server on the specified IP address and port numbers. If
    stream_port is None, does not connect to the stream server.
    Optionally give the kRPC server the supplied name to identify the client (up
    to 32 bytes of UTF-8 encoded text).
    """
    assert rpc_port != stream_port

    # Connect to RPC server
    rpc_connection = Connection(address, rpc_port)
    rpc_connection.connect(retries=10, timeout=0.1)
    rpc_connection.send(Encoder.RPC_HELLO_MESSAGE)
    rpc_connection.send(Encoder.client_name(name))
    client_identifier = rpc_connection.receive(Decoder.GUID_LENGTH)

    # Connect to Stream server
    if stream_port is not None:
        stream_connection = Connection(address, stream_port)
        stream_connection.connect(retries=10, timeout=0.1)
        stream_connection.send(Encoder.STREAM_HELLO_MESSAGE)
        stream_connection.send(client_identifier)
        ok_message = stream_connection.receive(Decoder.OK_LENGTH)
        assert ok_message == Decoder.OK_MESSAGE
    else:
        stream_connection = None

    return Client(rpc_connection, stream_connection)
示例#2
0
文件: __init__.py 项目: pipi1226/krpc
def connect(address=DEFAULT_ADDRESS,
            rpc_port=DEFAULT_RPC_PORT,
            stream_port=DEFAULT_STREAM_PORT,
            name=None):
    """
    Connect to a kRPC server on the specified IP address and port numbers. If
    stream_port is None, does not connect to the stream server.
    Optionally give the kRPC server the supplied name to identify the client (up
    to 32 bytes of UTF-8 encoded text).
    """
    assert rpc_port != stream_port

    # Connect to RPC server
    rpc_connection = Connection(address, rpc_port)
    rpc_connection.connect(retries=10, timeout=0.1)
    rpc_connection.send(Encoder.RPC_HELLO_MESSAGE)
    rpc_connection.send(Encoder.client_name(name))
    client_identifier = rpc_connection.receive(Decoder.GUID_LENGTH)

    # Connect to Stream server
    if stream_port is not None:
        stream_connection = Connection(address, stream_port)
        stream_connection.connect(retries=10, timeout=0.1)
        stream_connection.send(Encoder.STREAM_HELLO_MESSAGE)
        stream_connection.send(client_identifier)
        ok_message = stream_connection.receive(Decoder.OK_LENGTH)
        assert ok_message == Decoder.OK_MESSAGE
    else:
        stream_connection = None

    return Client(rpc_connection, stream_connection)
示例#3
0
 def test_long_client_name(self):
     message = Encoder.client_name('a'*33)
     self.assertEqual (32, len(message))
     self.assertEqual ('61'*32, hexlify(message))
示例#4
0
 def test_empty_client_name(self):
     message = Encoder.client_name()
     self.assertEqual (32, len(message))
     self.assertEqual ('00'*32, hexlify(message))
示例#5
0
 def test_client_name(self):
     message = Encoder.client_name('foo')
     self.assertEqual (32, len(message))
     self.assertEqual ('666f6f'+'00'*29, hexlify(message))