Ejemplo n.º 1
0
    def _received_find_value_rpc(self, data_packet):
        '''Find value rpc callback'''

        _logger.debug('Find value %s←%s', self.address,
            data_packet.address)

        key = KeyBytes.new_silent(data_packet.dict_obj.get(JSONKeys.KEY,
            'fake'))
        index = KeyBytes.new_silent(data_packet.dict_obj.get(JSONKeys.INDEX))

        if not key:
            _logger.debug('Find value %s←%s bad key', self.address,
            data_packet.address)
            return

        _logger.debug('Find value %s←%s k=%s i=%s', self.address,
            data_packet.address, key, index)

        kvpid = KVPID(key, index)

        if index and kvpid in self._kvp_table:
            kvp_record = self._kvp_table.record(kvpid)

            d = self._template_dict()
            d[JSONKeys.VALUES] = KVPExchangeInfoList([
                KVPExchangeInfo.from_kvp_record(kvp_record)
            ]).to_json_dumpable()

            self._network.send_answer_reply(data_packet, d)
        elif self._kvp_table.indices(key):
            kvp_record_list = self._kvp_table.records_by_key(key)

            d = self._template_dict()
            d[JSONKeys.VALUES] = KVPExchangeInfoList.from_kvp_record_list(
                kvp_record_list).to_json_dumpable()

            self._network.send_answer_reply(data_packet, d)
        else:
            self._reply_find_node(data_packet, key)
Ejemplo n.º 2
0
    def _received_find_node_rpc(self, data_packet):
        '''Find node RPC callback'''

        _logger.debug('Find node %s←%s', self.address,
            data_packet.address)

        key_obj = KeyBytes.new_silent(data_packet.dict_obj.get(JSONKeys.KEY))

        if not key_obj:
            _logger.debug('Find node %s←%s bad key', self.address,
            data_packet.address)
            return

        self._reply_find_node(data_packet, key_obj)
Ejemplo n.º 3
0
    def _received_get_value_rpc(self, data_packet):
        '''Get value rpc calllback'''

        _logger.debug('Get value %s←%s', self.address,
            data_packet.address)

        self._update_routing_table_from_data_packet(data_packet)

        key = KeyBytes.new_silent(data_packet.dict_obj[JSONKeys.KEY])
        index = KeyBytes.new_silent(data_packet.dict_obj[JSONKeys.INDEX])
        transfer_id = data_packet.dict_obj.get(JSONKeys.TRANSFER_ID)

        if not transfer_id:
            _logger.debug('Missing transfer id')
            return

        try:
            offset = data_packet.dict_obj.get(JSONKeys.VALUE_OFFSET, 0)
        except TypeError as e:
            _logger.debug('Offset parse error %s', e)

            return

        kvpid = KVPID(key, index)

        if not kvpid in self._kvp_table:
            _logger.debug('KeyBytes not in cache')
            return

        data = self._kvp_table[kvpid]

        task = self._network.send_bytes(data_packet.address,
            transfer_id, data[offset:])
        bytes_sent = task.result()

        _logger.debug('Sent %d bytes', bytes_sent)