本文整理汇总了Python中dht.protocol.KademliaProtocol.callStore方法的典型用法代码示例。如果您正苦于以下问题:Python KademliaProtocol.callStore方法的具体用法?Python KademliaProtocol.callStore怎么用?Python KademliaProtocol.callStore使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类dht.protocol.KademliaProtocol
的用法示例。
在下文中一共展示了KademliaProtocol.callStore方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: Server
# 需要导入模块: from dht.protocol import KademliaProtocol [as 别名]
# 或者: from dht.protocol.KademliaProtocol import callStore [as 别名]
#.........这里部分代码省略.........
dkey = digest(keyword)
if self.storage.get(dkey) is not None:
return defer.succeed(self.storage.get(dkey))
node = Node(dkey)
nearest = self.protocol.router.findNeighbors(node)
if len(nearest) == 0:
self.log.warning("There are no known neighbors to get key %s" % keyword)
return None
spider = ValueSpiderCrawl(self.protocol, node, nearest, self.ksize, self.alpha)
return spider.find()
def set(self, keyword, key, value):
"""
Set the given key/value tuple at the hash of the given keyword.
All values stored in the DHT are stored as dictionaries of key/value
pairs. If a value already exists for a given keyword, the new key/value
pair will be appended to the dictionary.
Args:
keyword: a `string` keyword. The SHA1 hash of which will be used as
the key when inserting in the DHT.
key: the 20 byte hash of the data.
value: a serialized `protos.objects.Node` object which serves as a
pointer to the node storing the data.
Return: True if at least one peer responded. False if the store rpc
completely failed.
"""
self.log.debug("setting '%s' = '%s':'%s' on network" % (keyword, hexlify(key), hexlify(value)))
dkey = digest(keyword)
def store(nodes):
self.log.info("setting '%s' on %s" % (keyword, map(str, nodes)))
ds = [self.protocol.callStore(node, dkey, key, value) for node in nodes]
keynode = Node(dkey)
if self.node.distanceTo(keynode) < max([n.distanceTo(keynode) for n in nodes]):
self.storage[dkey] = (key, value)
self.log.debug("got a store request from %s, storing value" % str(self.node))
return defer.DeferredList(ds).addCallback(self._anyRespondSuccess)
node = Node(dkey)
nearest = self.protocol.router.findNeighbors(node)
if len(nearest) == 0:
self.log.warning("There are no known neighbors to set key %s" % key)
return defer.succeed(False)
spider = NodeSpiderCrawl(self.protocol, node, nearest, self.ksize, self.alpha)
return spider.find().addCallback(store)
def delete(self, keyword, key, signature):
"""
Delete the given key/value pair from the keyword dictionary on the network.
To delete you must provide a signature covering the key that you wish to
delete. It will be verified against the public key stored in the value. We
use our ksize as alpha to make sure we reach as many nodes storing our value
as possible.
Args:
keyword: the `string` keyword where the data being deleted is stored.
key: the 20 byte hash of the data.
signature: a signature covering the key.
"""
self.log.debug("deleting '%s':'%s' from the network" % (keyword, hexlify(key)))
dkey = digest(keyword)
示例2: KademliaProtocolTest
# 需要导入模块: from dht.protocol import KademliaProtocol [as 别名]
# 或者: from dht.protocol.KademliaProtocol import callStore [as 别名]
#.........这里部分代码省略.........
expected_message = m.SerializeToString()
self.clock.advance(100 * constants.PACKET_TIMEOUT)
connection.REACTOR.runUntilCurrent()
m_calls = self.proto_mock.send_datagram.call_args_list
sent_packet = packet.Packet.from_bytes(self.proto_mock.send_datagram.call_args_list[0][0][0])
received_message = sent_packet.payload
m = message.Message()
m.ParseFromString(received_message)
self.assertEqual(received_message, expected_message)
self.assertEqual(len(m_calls), 2)
def test_callPing(self):
self._connecting_to_connected()
n = Node(digest("S"), self.addr1[0], self.addr1[1])
self.wire_protocol[self.addr1] = self.con
self.protocol.callPing(n)
self.clock.advance(100 * constants.PACKET_TIMEOUT)
connection.REACTOR.runUntilCurrent()
sent_packet = packet.Packet.from_bytes(self.proto_mock.send_datagram.call_args_list[0][0][0])
sent_message = sent_packet.payload
m = message.Message()
m.ParseFromString(sent_message)
self.assertTrue(len(m.messageID) == 20)
self.assertEqual(self.protocol.sourceNode.getProto().guid, m.sender.guid)
self.assertEqual(self.protocol.sourceNode.getProto().signedPublicKey, m.sender.signedPublicKey)
self.assertTrue(m.command == message.PING)
self.assertEqual(self.proto_mock.send_datagram.call_args_list[0][0][1], self.addr1)
def test_callStore(self):
self._connecting_to_connected()
n = Node(digest("S"), self.addr1[0], self.addr1[1])
self.wire_protocol[self.addr1] = self.con
self.protocol.callStore(n, digest("Keyword"), digest("Key"),
self.protocol.sourceNode.getProto().SerializeToString(), 10)
self.clock.advance(100 * constants.PACKET_TIMEOUT)
connection.REACTOR.runUntilCurrent()
sent_packet = packet.Packet.from_bytes(self.proto_mock.send_datagram.call_args_list[0][0][0])
sent_message = sent_packet.payload
m = message.Message()
m.ParseFromString(sent_message)
self.assertTrue(len(m.messageID) == 20)
self.assertEqual(self.protocol.sourceNode.getProto().guid, m.sender.guid)
self.assertEqual(self.protocol.sourceNode.getProto().signedPublicKey, m.sender.signedPublicKey)
self.assertTrue(m.command == message.STORE)
self.assertEqual(self.proto_mock.send_datagram.call_args_list[0][0][1], self.addr1)
self.assertEqual(m.arguments[0], digest("Keyword"))
self.assertEqual(m.arguments[1], digest("Key"))
self.assertEqual(m.arguments[2], self.protocol.sourceNode.getProto().SerializeToString())
def test_callFindValue(self):
self._connecting_to_connected()
n = Node(digest("S"), self.addr1[0], self.addr1[1])
self.wire_protocol[self.addr1] = self.con
keyword = Node(digest("Keyword"))
self.protocol.callFindValue(n, keyword)
self.clock.advance(100 * constants.PACKET_TIMEOUT)
示例3: KademliaProtocolTest
# 需要导入模块: from dht.protocol import KademliaProtocol [as 别名]
# 或者: from dht.protocol.KademliaProtocol import callStore [as 别名]
#.........这里部分代码省略.........
self.clock.advance(100 * constants.PACKET_TIMEOUT)
connection.REACTOR.runUntilCurrent()
m_calls = self.proto_mock.send_datagram.call_args_list
sent_packet = packet.Packet.from_bytes(self.proto_mock.send_datagram.call_args_list[0][0][0])
received_message = sent_packet.payload
m = message.Message()
m.ParseFromString(received_message)
self.assertEqual(received_message, expected_message)
self.assertEqual(len(m_calls), 2)
def test_callPing(self):
self._connecting_to_connected()
n = Node(digest("S"), self.addr1[0], self.addr1[1])
self.protocol[self.addr1] = self.con
self.protocol.callPing(n)
self.clock.advance(100 * constants.PACKET_TIMEOUT)
connection.REACTOR.runUntilCurrent()
sent_packet = packet.Packet.from_bytes(self.proto_mock.send_datagram.call_args_list[0][0][0])
sent_message = sent_packet.payload
m = message.Message()
m.ParseFromString(sent_message)
self.assertTrue(len(m.messageID) == 20)
self.assertEqual(self.protocol.sourceNode.getProto().guid, m.sender.guid)
self.assertEqual(self.protocol.sourceNode.getProto().signedPublicKey, m.sender.signedPublicKey)
self.assertTrue(m.command == message.PING)
self.assertEqual(self.proto_mock.send_datagram.call_args_list[0][0][1], self.addr1)
def test_callStore(self):
self._connecting_to_connected()
n = Node(digest("S"), self.addr1[0], self.addr1[1])
self.protocol[self.addr1] = self.con
self.protocol.callStore(n, digest("Keyword"), digest("Key"), self.protocol.sourceNode.getProto().SerializeToString())
self.clock.advance(100 * constants.PACKET_TIMEOUT)
connection.REACTOR.runUntilCurrent()
sent_packet = packet.Packet.from_bytes(self.proto_mock.send_datagram.call_args_list[0][0][0])
sent_message = sent_packet.payload
m = message.Message()
m.ParseFromString(sent_message)
self.assertTrue(len(m.messageID) == 20)
self.assertEqual(self.protocol.sourceNode.getProto().guid, m.sender.guid)
self.assertEqual(self.protocol.sourceNode.getProto().signedPublicKey, m.sender.signedPublicKey)
self.assertTrue(m.command == message.STORE)
self.assertEqual(self.proto_mock.send_datagram.call_args_list[0][0][1], self.addr1)
self.assertEqual(m.arguments[0], digest("Keyword"))
self.assertEqual(m.arguments[1], digest("Key"))
self.assertEqual(m.arguments[2], self.protocol.sourceNode.getProto().SerializeToString())
def test_callFindValue(self):
self._connecting_to_connected()
n = Node(digest("S"), self.addr1[0], self.addr1[1])
self.protocol[self.addr1] = self.con
keyword = Node(digest("Keyword"))
self.protocol.callFindValue(n, keyword)
self.clock.advance(100 * constants.PACKET_TIMEOUT)
connection.REACTOR.runUntilCurrent()