示例#1
0
def _query_encoder(query):
    """@see encode"""
    query_dict = {
        "q": query.rpctype,
        "a": {
            "id": basic_coder.encode_network_id(query._from)
        }
    }
    # Perform specific rpc encoding
    if query.rpctype == 'ping':
        pass
    elif query.rpctype == 'find_node':
        query_dict['a']['target'] = (basic_coder.encode_network_id(
            query.target_id))
    elif query.rpctype == 'get_peers':
        query_dict['a']['info_hash'] = (basic_coder.encode_network_id(
            query.target_id))
    elif query.rpctype == 'announce_peer':
        query_dict['a']['token'] = basic_coder.ltob(query.token)
        # Try encoding the port, to see if it is within range
        basic_coder.encode_port(query.port)
        query_dict['a']['port'] = query.port
        query_dict['a']['info_hash'] = (basic_coder.encode_network_id(
            query.target_id))
    else:
        raise _ProtocolFormatError()
    return query_dict
示例#2
0
def _query_decoder(rpc_dict):
    """
    Decode the given KRPC dictionary into a valid Query
    
    @see decode
    @return krpc_types.Query

    """
    q = Query()
    q._from = basic_coder.decode_network_id(rpc_dict['a']['id'])
    q.rpctype = rpctype = rpc_dict['q']

    if rpctype == 'ping':
        pass
    elif rpctype == 'find_node':
        q.target_id = basic_coder.decode_network_id(rpc_dict['a']['target'])
    elif rpctype == 'get_peers':
        q.target_id = basic_coder.decode_network_id(rpc_dict['a']['info_hash'])
    elif rpctype == 'announce_peer':
        q.target_id = basic_coder.decode_network_id(rpc_dict['a']['info_hash'])
        # Try encoding the port (to ensure it is within range)
        basic_coder.encode_port(rpc_dict['a']['port'])
        q.port = rpc_dict['a']['port']
        q.token = basic_coder.btol(rpc_dict['a']['token'])
    else:
        raise _ProtocolFormatError()
    return q
示例#3
0
def _query_decoder(rpc_dict):
    """
    Decode the given KRPC dictionary into a valid Query
    
    @see decode
    @return krpc_types.Query

    """
    q = Query()
    q._from = basic_coder.decode_network_id(rpc_dict['a']['id'])
    q.rpctype = rpctype = rpc_dict['q']

    if rpctype == 'ping':
        pass
    elif rpctype == 'find_node':
        q.target_id = basic_coder.decode_network_id(rpc_dict['a']['target'])
    elif rpctype == 'get_peers':
        q.target_id = basic_coder.decode_network_id(rpc_dict['a']['info_hash'])
    elif rpctype == 'announce_peer':
        q.target_id = basic_coder.decode_network_id(rpc_dict['a']['info_hash'])
        # Try encoding the port (to ensure it is within range)
        basic_coder.encode_port(rpc_dict['a']['port'])
        q.port = rpc_dict['a']['port']
        q.token = basic_coder.btol(rpc_dict['a']['token'])
    else:
        raise _ProtocolFormatError()
    return q
示例#4
0
 def test_encode_port_validPorts(self):
     expected_encodings = {
         0: "\x00\x00",
         255: "\x00\xff",
         65535: "\xff\xff"
     }
     for port, expected_port_string in expected_encodings.iteritems():
         self.assertEquals(expected_port_string, encode_port(port))
示例#5
0
def _query_encoder(query):
    """@see encode"""
    query_dict = {"q": query.rpctype,
                  "a": { "id": basic_coder.encode_network_id(query._from)}}
    # Perform specific rpc encoding
    if query.rpctype == 'ping':
        pass
    elif query.rpctype == 'find_node':
        query_dict['a']['target'] = (
                basic_coder.encode_network_id(query.target_id))
    elif query.rpctype == 'get_peers':
        query_dict['a']['info_hash'] = (
                basic_coder.encode_network_id(query.target_id))
    elif query.rpctype == 'announce_peer':
        query_dict['a']['token'] = basic_coder.ltob(query.token)
        # Try encoding the port, to see if it is within range
        basic_coder.encode_port(query.port)
        query_dict['a']['port'] = query.port
        query_dict['a']['info_hash'] = (
                basic_coder.encode_network_id(query.target_id))
    else:
        raise _ProtocolFormatError()
    return query_dict
示例#6
0
 def test_encode_port_validPorts(self):
     expected_encodings = {0 : "\x00\x00", 255 : "\x00\xff",
                             65535 : "\xff\xff"}
     for port, expected_port_string in expected_encodings.iteritems():
         self.assertEquals(expected_port_string, encode_port(port))