aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorng0 <ng0@n0.is>2017-12-01 21:10:30 +0000
committerng0 <ng0@n0.is>2017-12-01 21:10:30 +0000
commitb285c08b871c4b28fcf46a7437ab2713f78b2891 (patch)
tree998d6b6d7a7e17da7a53af282395ac4ce4379724
parent77db157a3e5325d6d6d282beafd51f875a9ab4a5 (diff)
downloadgnunet-python-b285c08b871c4b28fcf46a7437ab2713f78b2891.tar.gz
gnunet-python-b285c08b871c4b28fcf46a7437ab2713f78b2891.zip
PEP
-rwxr-xr-xexamples/example-dht.py17
-rwxr-xr-xexamples/example-gns.py16
-rw-r--r--gnunet/dht.py183
3 files changed, 110 insertions, 106 deletions
diff --git a/examples/example-dht.py b/examples/example-dht.py
index 64dd1cb..76f345b 100755
--- a/examples/example-dht.py
+++ b/examples/example-dht.py
@@ -7,18 +7,19 @@ key = gnunet.HashCode("RMKN0U1JNA3PVCL148D6JI0STVG94A8A65INOK849CF1RT6BGF26AMMT1
7 7
8gnunet.dht.put(key, 1, "test", b"hello") 8gnunet.dht.put(key, 1, "test", b"hello")
9 9
10
10def result_callback(block_type, key, data, expiry, get_path, put_path): 11def result_callback(block_type, key, data, expiry, get_path, put_path):
11 print("Got result from DHT") 12 print("Got result from DHT")
12 print(" block_type == %s" % repr(block_type)) 13 print(" block_type == %s" % repr(block_type))
13 print(" key == %s" % repr(key)) 14 print(" key == %s" % repr(key))
14 print(" expiry == %s" % repr(expiry)) 15 print(" expiry == %s" % repr(expiry))
15 print(" get_path == %s" % repr(get_path)) 16 print(" get_path == %s" % repr(get_path))
16 print(" put_path == %s" % repr(put_path)) 17 print(" put_path == %s" % repr(put_path))
17 print(" data == %s" % repr(data)) 18 print(" data == %s" % repr(data))
19
18 20
19req = gnunet.dht.get_start(result_callback, "test", key, 1, record_route=True) 21req = gnunet.dht.get_start(result_callback, "test", key, 1, record_route=True)
20req.filter_known_results([]) 22req.filter_known_results([])
21req.stop() 23req.stop()
22 24
23time.sleep(1) 25time.sleep(1)
24
diff --git a/examples/example-gns.py b/examples/example-gns.py
index 4b3ea10..efbea27 100755
--- a/examples/example-gns.py
+++ b/examples/example-gns.py
@@ -2,14 +2,14 @@
2 2
3import gnunet.gns 3import gnunet.gns
4 4
5
5results = gnunet.gns.lookup("www.gnu", "JK55QA8JLAL64MBO8UM209KE93M9JBBO7M2UB8M3M03FKRFSUOMG", "A", True) 6results = gnunet.gns.lookup("www.gnu", "JK55QA8JLAL64MBO8UM209KE93M9JBBO7M2UB8M3M03FKRFSUOMG", "A", True)
6 7
7for r in results: 8for r in results:
8 print("Got result from gns") 9 print("Got result from gns")
9 print(" record_type == %s" % repr(r.record_type)) 10 print(" record_type == %s" % repr(r.record_type))
10 print(" data == %s" % repr(r.data)) 11 print(" data == %s" % repr(r.data))
11 print(" expiration_time == %s" % repr(r.expiration_time)) 12 print(" expiration_time == %s" % repr(r.expiration_time))
12 print(" private == %s" % repr(r.private)) 13 print(" private == %s" % repr(r.private))
13 print(" pending == %s" % repr(r.pending)) 14 print(" pending == %s" % repr(r.pending))
14 print(" shadow == %s" % repr(r.shadow)) 15 print(" shadow == %s" % repr(r.shadow))
15
diff --git a/gnunet/dht.py b/gnunet/dht.py
index 849f07a..8f310a9 100644
--- a/gnunet/dht.py
+++ b/gnunet/dht.py
@@ -10,109 +10,112 @@ import gnunet.block as block
10get_requests = {} 10get_requests = {}
11requests_lock = threading.Lock() 11requests_lock = threading.Lock()
12 12
13
13class GetResult(threading.Thread): 14class GetResult(threading.Thread):
14 def __init__(self, expiry, key, get_path, put_path, block_type, data, path): 15 def __init__(self, expiry, key, get_path, put_path, block_type, data, path):
15 threading.Thread.__init__(self) 16 threading.Thread.__init__(self)
16 self.expiry = expiry 17 self.expiry = expiry
17 self.key = key 18 self.key = key
18 self.get_path = get_path 19 self.get_path = get_path
19 self.put_path = put_path 20 self.put_path = put_path
20 self.block_type = block_type 21 self.block_type = block_type
21 self.data = data 22 self.data = data
22 self.path = path 23 self.path = path
23 self.daemon = True 24 self.daemon = True
24 self.start() 25 self.start()
25 26
26 def run(self): 27 def run(self):
27 request = None 28 request = None
28 with requests_lock: 29 with requests_lock:
29 request = get_requests[self.path] 30 request = get_requests[self.path]
30 31
31 if request: 32 if request:
32 if request.record_route: 33 if request.record_route:
33 request.callback(self.block_type, self.key, self.data, self.expiry, get_path=self.get_path, put_path=self.put_path) 34 request.callback(self.block_type, self.key, self.data, self.expiry, get_path=self.get_path, put_path=self.put_path)
34 else: 35 else:
35 request.callback(self.block_type, self.key, self.data, self.expiry) 36 request.callback(self.block_type, self.key, self.data, self.expiry)
37
36 38
37def _result(expiry, key, get_path, put_path, block_type, data, path): 39def _result(expiry, key, get_path, put_path, block_type, data, path):
38 expiry = pythonize(expiry, datetime.datetime) 40 expiry = pythonize(expiry, datetime.datetime)
39 key = HashCode(key) 41 key = HashCode(key)
40 get_path = list(get_path) 42 get_path = list(get_path)
41 put_path = list(put_path) 43 put_path = list(put_path)
42 block_type = str(block_type) 44 block_type = str(block_type)
43 data = bytearray(data) 45 data = bytearray(data)
44 GetResult(expiry, key, get_path, put_path, block_type, data, path) 46 GetResult(expiry, key, get_path, put_path, block_type, data, path)
45 47
46sysbus.add_signal_receiver(_result, "result", "gnu.gnunet.dht.get", "gnu.gnunet.dht", path_keyword="path") 48sysbus.add_signal_receiver(_result, "result", "gnu.gnunet.dht.get", "gnu.gnunet.dht", path_keyword="path")
47 49
50
48class GetRequest: 51class GetRequest:
49 def __init__(self, path, callback, record_route): 52 def __init__(self, path, callback, record_route):
50 self._path = path 53 self._path = path
51 self.callback = callback 54 self.callback = callback
52 self.record_route = record_route 55 self.record_route = record_route
56
57 def filter_known_results(self, keys):
58 keys = dbus.Array([dbusize(HashCode(key)) for key in list(keys)], signature="v")
59 try:
60 sysbus.get_object("gnu.gnunet.dht", self._path).filter_known_results(keys, dbus_interface="gnu.gnunet.dht.get")
61 except dbus.DBusException as e:
62 handle_exception(e, "dht", "gnu.gnunet.dht")
63
64 def stop(self):
65 try:
66 sysbus.get_object("gnu.gnunet.dht", self._path).stop(dbus_interface="gnu.gnunet.dht.get")
67 except dbus.DBusException as e:
68 handle_exception(e, "dht", "gnu.gnunet.dht")
53 69
54 def filter_known_results(self, keys):
55 keys = dbus.Array([dbusize(HashCode(key)) for key in list(keys)], signature="v")
56 try:
57 sysbus.get_object("gnu.gnunet.dht", self._path).filter_known_results(keys, dbus_interface="gnu.gnunet.dht.get")
58 except dbus.DBusException as e:
59 handle_exception(e, "dht", "gnu.gnunet.dht")
60 70
61 def stop(self): 71def put(key, desired_replication_level, block_type, data, expiry=None, demultiplex_everywhere=False, record_route=False, bart=False):
72 key = dbusize(HashCode(key), True)
73 desired_replication_level = dbus.UInt32(desired_replication_level)
74 if block_type not in block.types:
75 raise ValueError("'block_type' must be one of %s" % block.types)
76 block_type = dbus.String(block_type, variant_level=1)
77 if expiry is not None:
78 if not isinstance(expiry, datetime.datetime):
79 raise TypeError("'expiry' must be a datetime.datetime")
80 expiry = dbusize(expiry)
81 else:
82 expiry = dbus.String("end of time", variant_level=1)
83 options = dbus.Array([], variant_level=1, signature="s")
84 if demultiplex_everywhere:
85 options += ["demultiplex_everywhere"]
86 if record_route:
87 options += ["record_route"]
88 if bart:
89 options += ["bart"]
90 data = dbus.Array(bytearray(data), signature="y")
91
62 try: 92 try:
63 sysbus.get_object("gnu.gnunet.dht", self._path).stop(dbus_interface="gnu.gnunet.dht.get") 93 sysbus.get_object("gnu.gnunet.dht", "/").put(key, desired_replication_level, options, block_type, data, expiry)
64 except dbus.DBusException as e: 94 except dbus.DBusException as e:
65 handle_exception(e, "dht", "gnu.gnunet.dht") 95 handle_exception(e, "dht", "gnu.gnunet.dht")
66 96
67def put(key, desired_replication_level, block_type, data, expiry=None, demultiplex_everywhere=False, record_route=False, bart=False):
68 key = dbusize(HashCode(key), True)
69 desired_replication_level = dbus.UInt32(desired_replication_level)
70 if block_type not in block.types:
71 raise ValueError("'block_type' must be one of %s" % block.types)
72 block_type = dbus.String(block_type, variant_level=1)
73 if expiry is not None:
74 if not isinstance(expiry, datetime.datetime):
75 raise TypeError("'expiry' must be a datetime.datetime")
76 expiry = dbusize(expiry)
77 else:
78 expiry = dbus.String("end of time", variant_level=1)
79 options = dbus.Array([], variant_level=1, signature="s")
80 if demultiplex_everywhere:
81 options += ["demultiplex_everywhere"]
82 if record_route:
83 options += ["record_route"]
84 if bart:
85 options += ["bart"]
86 data = dbus.Array(bytearray(data), signature="y")
87
88 try:
89 sysbus.get_object("gnu.gnunet.dht", "/").put(key, desired_replication_level, options, block_type, data, expiry)
90 except dbus.DBusException as e:
91 handle_exception(e, "dht", "gnu.gnunet.dht")
92 97
93def get_start(callback, block_type, key, desired_replication_level, demultiplex_everywhere=False, record_route=False, bart=False): 98def get_start(callback, block_type, key, desired_replication_level, demultiplex_everywhere=False, record_route=False, bart=False):
94 if block_type not in block.types: 99 if block_type not in block.types:
95 raise ValueError("'block_type' must be one of %s" % block.types) 100 raise ValueError("'block_type' must be one of %s" % block.types)
96 block_type = dbus.String(block_type, variant_level=1) 101 block_type = dbus.String(block_type, variant_level=1)
97 key = dbusize(HashCode(key), True) 102 key = dbusize(HashCode(key), True)
98 desired_replication_level = dbus.UInt32(desired_replication_level) 103 desired_replication_level = dbus.UInt32(desired_replication_level)
99 options = dbus.Array([], variant_level=1, signature="s") 104 options = dbus.Array([], variant_level=1, signature="s")
100 if demultiplex_everywhere: 105 if demultiplex_everywhere:
101 options += ["demultiplex_everywhere"] 106 options += ["demultiplex_everywhere"]
102 if record_route: 107 if record_route:
103 options += ["record_route"] 108 options += ["record_route"]
104 if bart: 109 if bart:
105 options += ["bart"] 110 options += ["bart"]
106 111
107 ret = None 112 ret = None
108 try: 113 try:
109 with requests_lock: 114 with requests_lock:
110 path = sysbus.get_object("gnu.gnunet.dht", "/").get_start(block_type, key, desired_replication_level, options) 115 path = sysbus.get_object("gnu.gnunet.dht", "/").get_start(block_type, key, desired_replication_level, options)
111 ret = GetRequest(path, callback, record_route) 116 ret = GetRequest(path, callback, record_route)
112 get_requests[path] = ret 117 get_requests[path] = ret
113 except dbus.DBusException as e: 118 except dbus.DBusException as e:
114 handle_exception(e, "dht", "gnu.gnunet.dht") 119 handle_exception(e, "dht", "gnu.gnunet.dht")
115
116 return ret
117
118 120
121 return ret