diff --git a/python/opendht.pyx b/python/opendht.pyx index e9170bd17..39f3d8efd 100644 --- a/python/opendht.pyx +++ b/python/opendht.pyx @@ -609,6 +609,7 @@ cdef class DhtRunner(_WithID): while pending > 0: lock.wait() return res + def put(self, InfoHash key, Value val, done_cb=None, permanent=False): """Publish a new value on the DHT at key. @@ -637,6 +638,72 @@ cdef class DhtRunner(_WithID): lock.wait() return ok + def putSigned(self, InfoHash key, Value val, done_cb=None, permanent=False): + if done_cb: + cb_obj = {'done':done_cb} + ref.Py_INCREF(cb_obj) + self.thisptr.get().putSigned(key._infohash, val._value, cpp.bindDoneCb(done_callback, cb_obj), permanent) + else: + lock = threading.Condition() + pending = 0 + ok = False + def tmp_done(ok_ret, nodes): + nonlocal pending, ok, lock + with lock: + ok = ok_ret + pending -= 1 + lock.notify() + with lock: + pending += 1 + self.putSigned(key, val, done_cb=tmp_done) + while pending > 0: + lock.wait() + return ok + + def putEncrypted(self, InfoHash key, InfoHash to, Value val, done_cb=None, bool permanent=False): + if done_cb: + cb_obj = {'done':done_cb} + ref.Py_INCREF(cb_obj) + self.thisptr.get().putEncrypted(key._infohash, to._infohash, val._value, cpp.bindDoneCb(done_callback, cb_obj), permanent) + else: + lock = threading.Condition() + pending = 0 + ok = False + def tmp_done(ok_ret, nodes): + nonlocal pending, ok, lock + with lock: + ok = ok_ret + pending -= 1 + lock.notify() + with lock: + pending += 1 + self.putEncrypted(key, to, val, done_cb=tmp_done, permanent=permanent) + while pending > 0: + lock.wait() + return ok + + def putEncrypted(self, InfoHash key, PublicKey to, Value val, done_cb=None, bool permanent=False): + if done_cb: + cb_obj = {'done':done_cb} + ref.Py_INCREF(cb_obj) + self.thisptr.get().putEncrypted(key._infohash, to._key, val._value, cpp.bindDoneCb(done_callback, cb_obj), permanent) + else: + lock = threading.Condition() + pending = 0 + ok = False + def tmp_done(ok_ret, nodes): + nonlocal pending, ok, lock + with lock: + ok = ok_ret + pending -= 1 + lock.notify() + with lock: + pending += 1 + self.putEncrypted(key, to, val, done_cb=tmp_done, permanent=permanent) + while pending > 0: + lock.wait() + return ok + def cancelPut(self, InfoHash key, Value val): self.thisptr.get().cancelPut(key._infohash, val._value) diff --git a/python/opendht_cpp.pxd b/python/opendht_cpp.pxd index e07084a1f..2d374d149 100644 --- a/python/opendht_cpp.pxd +++ b/python/opendht_cpp.pxd @@ -267,6 +267,9 @@ cdef extern from "opendht/dhtrunner.h" namespace "dht": string getSearchesLog(sa_family_t af) const void get(InfoHash key, GetCallback get_cb, DoneCallback done_cb, nullptr_t f, Where w) void put(InfoHash key, shared_ptr[Value] val, DoneCallback done_cb, time_point created, bool permanent) + void putSigned(InfoHash key, shared_ptr[Value] val, DoneCallback done_cb, bool permanent) + void putEncrypted(InfoHash key, InfoHash to, shared_ptr[Value] val, DoneCallback done_cb, bool permanent) + void putEncrypted(InfoHash key, shared_ptr[PublicKey] to, shared_ptr[Value] val, DoneCallback done_cb, bool permanent) void cancelPut(InfoHash key, shared_ptr[Value] val) ListenToken listen(InfoHash key, ValueCallback get_cb) void cancelListen(InfoHash key, SharedListenToken token)