本文整理汇总了Python中dht.protocol.KademliaProtocol.transferKeyValues方法的典型用法代码示例。如果您正苦于以下问题:Python KademliaProtocol.transferKeyValues方法的具体用法?Python KademliaProtocol.transferKeyValues怎么用?Python KademliaProtocol.transferKeyValues使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类dht.protocol.KademliaProtocol
的用法示例。
在下文中一共展示了KademliaProtocol.transferKeyValues方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: Server
# 需要导入模块: from dht.protocol import KademliaProtocol [as 别名]
# 或者: from dht.protocol.KademliaProtocol import transferKeyValues [as 别名]
class Server(object):
"""
High level view of a node instance. This is the object that should be created
to start listening as an active node on the network.
"""
def __init__(self, node, db, signing_key, ksize=20, alpha=3, storage=None):
"""
Create a server instance. This will start listening on the given port.
Args:
node: The node instance for this peer. It must contain (at minimum) an ID,
public key, ip address, and port.
ksize (int): The k parameter from the paper
alpha (int): The alpha parameter from the paper
storage: An instance that implements :interface:`~dht.storage.IStorage`
"""
self.ksize = ksize
self.alpha = alpha
self.log = Logger(system=self)
self.storage = storage or ForgetfulStorage()
self.node = node
self.protocol = KademliaProtocol(self.node, self.storage, ksize, db, signing_key)
self.refreshLoop = LoopingCall(self.refreshTable)
reactor.callLater(1800, self.refreshLoop.start, 3600)
def listen(self, port):
"""
Start listening on the given port.
This is the same as calling::
reactor.listenUDP(port, server.protocol)
"""
return reactor.listenUDP(port, self.protocol)
def refreshTable(self):
"""
Refresh buckets that haven't had any lookups in the last hour
(per section 2.3 of the paper).
"""
ds = []
refresh_ids = self.protocol.getRefreshIDs()
refresh_ids.append(digest(random.getrandbits(255))) # random node so we get more diversity
for rid in refresh_ids:
node = Node(rid)
nearest = self.protocol.router.findNeighbors(node, self.alpha)
spider = NodeSpiderCrawl(self.protocol, node, nearest, self.ksize, self.alpha)
ds.append(spider.find())
def republishKeys(_):
self.log.debug("Republishing key/values...")
neighbors = self.protocol.router.findNeighbors(self.node, exclude=self.node)
for node in neighbors:
self.protocol.transferKeyValues(node)
return defer.gatherResults(ds).addCallback(republishKeys)
def querySeed(self, list_seed_pubkey):
"""
Query an HTTP seed and return a `list` if (ip, port) `tuple` pairs.
Args:
Receives a list of one or more tuples Example [(seed, pubkey)]
seed: A `string` consisting of "ip:port" or "hostname:port"
pubkey: The hex encoded public key to verify the signature on the response
"""
nodes = []
if not list_seed_pubkey:
self.log.error('failed to query seed {0} from ob.cfg'.format(list_seed_pubkey))
return nodes
else:
for sp in list_seed_pubkey:
seed, pubkey = sp
try:
self.log.info("querying %s for peers" % seed)
c = httplib.HTTPConnection(seed)
c.request("GET", "/")
response = c.getresponse()
self.log.debug("Http response from %s: %s, %s" % (seed, response.status, response.reason))
data = response.read()
reread_data = data.decode("zlib")
proto = peers.PeerSeeds()
proto.ParseFromString(reread_data)
for peer in proto.serializedNode:
n = objects.Node()
n.ParseFromString(peer)
tup = (str(n.nodeAddress.ip), n.nodeAddress.port)
nodes.append(tup)
verify_key = nacl.signing.VerifyKey(pubkey, encoder=nacl.encoding.HexEncoder)
verify_key.verify("".join(proto.serializedNode), proto.signature)
self.log.info("%s returned %s addresses" % (seed, len(nodes)))
except Exception, e:
self.log.error("failed to query seed: %s" % str(e))
return nodes
示例2: KademliaProtocolTest
# 需要导入模块: from dht.protocol import KademliaProtocol [as 别名]
# 或者: from dht.protocol.KademliaProtocol import transferKeyValues [as 别名]
#.........这里部分代码省略.........
def test_acceptResponse(self):
self._connecting_to_connected()
def handle_response(resp):
self.assertTrue(resp[0])
self.assertEqual(resp[1][0], "test")
self.assertTrue(message_id not in self.protocol._outstanding)
message_id = digest("msgid")
n = Node(digest("S"), self.addr1[0], self.addr1[1])
d = defer.Deferred()
self.protocol._outstanding[message_id] = (d, self.addr1, reactor.callLater(5, handle_response))
self.protocol._acceptResponse(message_id, ["test"], n)
return d.addCallback(handle_response)
def test_unknownRPC(self):
self.assertFalse(self.handler.receive_message(str(random.getrandbits(1400))))
def test_timeout(self):
def handle_response(resp, n):
self.assertFalse(resp[0])
self.assertIsNone(resp[1])
self.assertTrue(self.protocol.router.isNewNode(n))
n = Node(digest("S"), self.addr1[0], self.addr1[1])
d = defer.Deferred().addCallback(handle_response, n)
self.protocol._outstanding["msgID"] = [d, self.addr1]
self.protocol.router.addContact(n)
self.protocol.timeout(self.addr1, n)
def test_transferKeyValues(self):
self._connecting_to_connected()
self.wire_protocol[self.addr1] = self.con
self.protocol.router.addContact(mknode())
self.protocol.storage[digest("keyword")] = (
digest("key"), self.protocol.sourceNode.getProto().SerializeToString(), 10)
self.protocol.storage[digest("keyword")] = (
digest("key2"), self.protocol.sourceNode.getProto().SerializeToString(), 10)
self.protocol.transferKeyValues(Node(digest("id"), self.addr1[0], self.addr1[1]))
self.clock.advance(1)
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
x = message.Message()
x.ParseFromString(sent_message)
i = objects.Inv()
i.keyword = digest("keyword")
i.valueKey = digest("key")
i2 = objects.Inv()
i2.keyword = digest("keyword")
i2.valueKey = digest("key2")
m = message.Message()
m.sender.MergeFrom(self.protocol.sourceNode.getProto())
m.command = message.Command.Value("INV")
m.protoVer = self.version
m.arguments.append(i.SerializeToString())
示例3: KademliaProtocolTest
# 需要导入模块: from dht.protocol import KademliaProtocol [as 别名]
# 或者: from dht.protocol.KademliaProtocol import transferKeyValues [as 别名]
#.........这里部分代码省略.........
self.assertEqual(resp[1][0], self.protocol.sourceNode.id)
n = Node(digest("S"), self.addr1[0], self.addr1[1])
self.protocol[self.addr1] = self.con
d = self.protocol.callPing(n)
self.clock.advance(1)
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)
timeout = reactor.callLater(5, self.protocol._timeout, m.messageID)
self.handler._outstanding[m.messageID] = (d, timeout)
m.arguments.append(self.protocol.sourceNode.id)
self.handler.receive_message(m.SerializeToString())
return d.addCallback(handle_response)
def test_unknownRPC(self):
self.assertFalse(self.handler._acceptRequest(digest("msgid"), "unknown", [digest("argument")], Node(digest("nodeid"))))
def test_timeout(self):
self._connecting_to_connected()
self.protocol[self.addr1] = self.con
def test_remove_outstanding():
self.assertTrue(len(self.protocol._outstanding) == 0)
def test_deffered(d):
self.assertFalse(d[0])
test_remove_outstanding()
n = Node(digest("S"), self.addr1[0], self.addr1[1])
d = self.protocol.callPing(n)
self.clock.advance(6)
return d.addCallback(test_deffered)
def test_transferKeyValues(self):
self._connecting_to_connected()
self.protocol[self.addr1] = self.con
self.protocol.storage[digest("keyword")] = (digest("key"), self.protocol.sourceNode.getProto().SerializeToString())
self.protocol.transferKeyValues(Node(digest("id"), self.addr1[0], self.addr1[1]))
self.clock.advance(1)
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
x = message.Message()
x.ParseFromString(sent_message)
m = message.Message()
m.sender.MergeFrom(self.protocol.sourceNode.getProto())
m.command = message.Command.Value("STORE")
m.arguments.append(digest("keyword"))
m.arguments.append(digest("key"))
m.arguments.append(self.protocol.sourceNode.getProto().SerializeToString())
self.assertEqual(x.sender, m.sender)
self.assertEqual(x.command, m.command)
self.assertEqual(x.arguments[0], m.arguments[0])
self.assertEqual(x.arguments[1], m.arguments[1])
self.assertEqual(x.arguments[2], m.arguments[2])
def test_refreshIDs(self):
node1 = Node(digest("id1"), "127.0.0.1", 12345, signed_pubkey=digest("key1"))
node2 = Node(digest("id2"), "127.0.0.1", 22222, signed_pubkey=digest("key2"))
node3 = Node(digest("id3"), "127.0.0.1", 77777, signed_pubkey=digest("key3"))
self.protocol.router.addContact(node1)
self.protocol.router.addContact(node2)
self.protocol.router.addContact(node3)
for b in self.protocol.router.buckets:
b.lastUpdated = (time.time() - 5000)
ids = self.protocol.getRefreshIDs()
self.assertTrue(len(ids) == 1)
def _connecting_to_connected(self):
remote_synack_packet = packet.Packet.from_data(
42,
self.con.own_addr,
self.con.dest_addr,
ack=0,
syn=True
)
self.con.receive_packet(remote_synack_packet)
self.clock.advance(0)
connection.REACTOR.runUntilCurrent()
self.next_remote_seqnum = 43
m_calls = self.proto_mock.send_datagram.call_args_list
sent_syn_packet = packet.Packet.from_bytes(m_calls[0][0][0])
seqnum = sent_syn_packet.sequence_number
self.handler_mock.reset_mock()
self.proto_mock.reset_mock()
self.next_seqnum = seqnum + 1