aboutsummaryrefslogtreecommitdiff
path: root/gnunet/dht.py
diff options
context:
space:
mode:
Diffstat (limited to 'gnunet/dht.py')
-rw-r--r--gnunet/dht.py105
1 files changed, 105 insertions, 0 deletions
diff --git a/gnunet/dht.py b/gnunet/dht.py
new file mode 100644
index 0000000..d26b7f2
--- /dev/null
+++ b/gnunet/dht.py
@@ -0,0 +1,105 @@
1import dbus
2
3import datetime
4
5from gnunet import *
6from gnunet._dbus_utils import *
7
8import gnunet.block as block
9
10get_requests = {}
11requests_lock = threading.Lock()
12
13class GetResult(threading.Thread):
14 def __init__(self, expiry, key, get_path, put_path, block_type, data, path):
15 threading.Thread.__init__(self)
16 self.expiry = expiry
17 self.key = key
18 self.get_path = get_path
19 self.put_path = put_path
20 self.block_type = block_type
21 self.data = data
22 self.path = path
23 self.daemon = True
24 self.start()
25
26 def run(self):
27 request = None
28 with requests_lock:
29 request = get_requests[self.path]
30
31 if request:
32 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 else:
35 request.callback(self.block_type, self.key, self.data, self.expiry)
36
37def _result(expiry, key, get_path, put_path, block_type, data, path):
38 expiry = pythonize(expiry, datetime.datetime)
39 key = HashCode(key)
40 get_path = list(get_path)
41 put_path = list(put_path)
42 block_type = str(block_type)
43 data = bytearray(data)
44 GetResult(expiry, key, get_path, put_path, block_type, data, path)
45
46sysbus.add_signal_receiver(_result, "result", "gnu.gnunet.dht.get", "gnu.gnunet.dht", path_keyword="path")
47
48class GetRequest:
49 def __init__(self, path, callback, record_route):
50 self._path = path
51 self.callback = callback
52 self.record_route = record_route
53
54def put(key, desired_replication_level, block_type, data, expiry=None, demultiplex_everywhere=False, record_route=False, bart=False):
55 key = dbusize(HashCode(key), True)
56 desired_replication_level = dbus.UInt32(desired_replication_level)
57 if block_type not in block.types:
58 raise ValueError("'block_type' must be one of %s" % block.types)
59 block_type = dbus.String(block_type, variant_level=1)
60 if expiry is not None:
61 if not isinstance(expiry, datetime.datetime):
62 raise TypeError("'expiry' must be a datetime.datetime")
63 expiry = dbusize(expiry)
64 else:
65 expiry = dbus.String("end of time", variant_level=1)
66 options = dbus.Array([], variant_level=1, signature="s")
67 if demultiplex_everywhere:
68 options += ["demultiplex_everywhere"]
69 if record_route:
70 options += ["record_route"]
71 if bart:
72 options += ["bart"]
73 data = dbus.Array(bytearray(data), signature="y")
74
75 try:
76 sysbus.get_object("gnu.gnunet.dht", "/").put(key, desired_replication_level, options, block_type, data, expiry)
77 except dbus.DBusException as e:
78 handle_exception(e, "dht", "gnu.gnunet.dht")
79
80def get_start(callback, block_type, key, desired_replication_level, demultiplex_everywhere=False, record_route=False, bart=False):
81 if block_type not in block.types:
82 raise ValueError("'block_type' must be one of %s" % block.types)
83 block_type = dbus.String(block_type, variant_level=1)
84 key = dbusize(HashCode(key), True)
85 desired_replication_level = dbus.UInt32(desired_replication_level)
86 options = dbus.Array([], variant_level=1, signature="s")
87 if demultiplex_everywhere:
88 options += ["demultiplex_everywhere"]
89 if record_route:
90 options += ["record_route"]
91 if bart:
92 options += ["bart"]
93
94 ret = None
95 try:
96 with requests_lock:
97 path = sysbus.get_object("gnu.gnunet.dht", "/").get_start(block_type, key, desired_replication_level, options)
98 ret = GetRequest(path, callback, record_route)
99 get_requests[path] = ret
100 except dbus.DBusException as e:
101 handle_exception(e, "dht", "gnu.gnunet.dht")
102
103 return ret
104
105