diff --git a/include/opendht/callbacks.h b/include/opendht/callbacks.h index ed75c0d6e830926007243087e9673f70c95ecee0..ed05c3d01a0d0d4916f56f2dd50889aeba32e104 100644 --- a/include/opendht/callbacks.h +++ b/include/opendht/callbacks.h @@ -85,12 +85,13 @@ GetCallback bindGetCb(GetCallbackSimple cb); using DoneCallback = std::function<void(bool success, const std::vector<std::shared_ptr<Node>>& nodes)>; typedef void (*DoneCallbackRaw)(bool, std::vector<std::shared_ptr<Node>>*, void *user_data); typedef void (*ShutdownCallbackRaw)(void *user_data); +typedef void (*DoneCallbackSimpleRaw)(bool, void *user_data); using DoneCallbackSimple = std::function<void(bool success)>; ShutdownCallback bindShutdownCb(ShutdownCallbackRaw shutdown_cb_raw, void* user_data); DoneCallback bindDoneCb(DoneCallbackSimple donecb); DoneCallback bindDoneCb(DoneCallbackRaw raw_cb, void* user_data); - +DoneCallbackSimple bindDoneCbSimple(DoneCallbackSimpleRaw raw_cb, void* user_data); } diff --git a/include/opendht/indexation/pht.h b/include/opendht/indexation/pht.h index c4013c5a1a612fc1caf65207fca62e9e436a9907..c2b2e5c29a4db16e0d7db4b2ef8ae65ad25014a6 100644 --- a/include/opendht/indexation/pht.h +++ b/include/opendht/indexation/pht.h @@ -169,11 +169,11 @@ public: /** * Lookup a key for a value. */ - void lookup(Key k, LookupCallback cb = {}, Dht::DoneCallbackSimple doneCb = {}, bool exact_match = true); + void lookup(Key k, LookupCallback cb = {}, DoneCallbackSimple doneCb = {}, bool exact_match = true); /** * Adds an entry into the index. */ - void insert(Key k, Value v, Dht::DoneCallbackSimple cb = {}); + void insert(Key k, Value v, DoneCallbackSimple cb = {}); private: class Cache { @@ -314,7 +314,7 @@ private: */ void lookupStep(Prefix k, std::shared_ptr<int> lo, std::shared_ptr<int> hi, std::shared_ptr<std::vector<std::shared_ptr<Value>>> vals, LookupCallback cb, - Dht::DoneCallbackSimple done_cb, std::shared_ptr<unsigned> max_common_prefix_len, + DoneCallbackSimple done_cb, std::shared_ptr<unsigned> max_common_prefix_len, int start = -1, bool all_values = false); /** diff --git a/python/opendht.pyx b/python/opendht.pyx index 8e846efb656095b1c68058137584dbaa33212261..7518b13bc5c78e10443f695e7592c70fc32174f5 100644 --- a/python/opendht.pyx +++ b/python/opendht.pyx @@ -296,13 +296,13 @@ cdef class DhtRunner(_WithID): def shutdown(self, shutdown_cb=None): cb_obj = {'shutdown':shutdown_cb} ref.Py_INCREF(cb_obj) - self.thisptr.shutdown(cpp.bindShutdownCb(shutdown_callback, <void*>cb_obj)) + self.thisptr.get().shutdown(cpp.bindShutdownCb(shutdown_callback, <void*>cb_obj)) def enableLogging(self): - cpp.enableLogging(self.thisptr[0]) + cpp.enableLogging(self.thisptr.get()[0]) def disableLogging(self): - cpp.disableLogging(self.thisptr[0]) + cpp.disableLogging(self.thisptr.get()[0]) def enableFileLogging(self, str path): - cpp.enableFileLogging(self.thisptr[0], path) + cpp.enableFileLogging(self.thisptr.get()[0], path) def isRunning(self): return self.thisptr.get().isRunning() def getStorageLog(self): @@ -330,7 +330,7 @@ cdef class DhtRunner(_WithID): if get_cb: cb_obj = {'get':get_cb, 'done':done_cb} ref.Py_INCREF(cb_obj) - self.thisptr.get(key._infohash, cpp.bindGetCb(get_callback, <void*>cb_obj), cpp.bindDoneCb(done_callback, <void*>cb_obj)) + self.thisptr.get().get(key._infohash, cpp.bindGetCb(get_callback, <void*>cb_obj), cpp.bindDoneCb(done_callback, <void*>cb_obj)) else: lock = threading.Condition() pending = 0 @@ -360,7 +360,7 @@ cdef class DhtRunner(_WithID): if done_cb: cb_obj = {'done':done_cb} ref.Py_INCREF(cb_obj) - self.thisptr.put(key._infohash, val._value, cpp.bindDoneCb(done_callback, <void*>cb_obj)) + self.thisptr.get().put(key._infohash, val._value, cpp.bindDoneCb(done_callback, <void*>cb_obj)) else: lock = threading.Condition() pending = 0 @@ -384,7 +384,7 @@ cdef class DhtRunner(_WithID): t._cb['cb'] = cb_obj # avoid the callback being destructed if the token is destroyed ref.Py_INCREF(cb_obj) - t._t = self.thisptr.listen(t._h, cpp.bindGetCb(get_callback, <void*>cb_obj)).share() + t._t = self.thisptr.get().listen(t._h, cpp.bindGetCb(get_callback, <void*>cb_obj)).share() return t def cancelListen(self, ListenToken token): self.thisptr.get().cancelListen(token._h, token._t) @@ -429,7 +429,7 @@ cdef class Pht(object): self.thisptr.lookup( cppk, cpp.Pht.bindLookupCb(lookup_callback, <void*>cb_obj), - cpp.Dht.bindDoneCbSimple(done_callback_simple, <void*>cb_obj) + cpp.bindDoneCbSimple(done_callback_simple, <void*>cb_obj) ) def insert(self, key, IndexValue value, done_cb=None): """Add an index entry to the Index. @@ -449,6 +449,6 @@ cdef class Pht(object): self.thisptr.insert( cppk, val, - cpp.Dht.bindDoneCbSimple(done_callback_simple, <void*>cb_obj) + cpp.bindDoneCbSimple(done_callback_simple, <void*>cb_obj) ) diff --git a/python/opendht_cpp.pxd b/python/opendht_cpp.pxd index 4b283ee9b33f2a3f9c7c893a5dc6c929c9a7b074..95b95500e7ba1bce1aeb3bacaf4592fa59b95307 100644 --- a/python/opendht_cpp.pxd +++ b/python/opendht_cpp.pxd @@ -105,19 +105,21 @@ cdef extern from "opendht/callbacks.h" namespace "dht": ctypedef void (*ShutdownCallbackRaw)(void *user_data) ctypedef bool (*GetCallbackRaw)(shared_ptr[Value] values, void *user_data) ctypedef void (*DoneCallbackRaw)(bool done, vector[shared_ptr[Node]]* nodes, void *user_data) + ctypedef void (*DoneCallbackSimpleRaw)(bool done, void *user_data) cppclass ShutdownCallback: ShutdownCallback() except + cppclass GetCallback: GetCallback() except + - #GetCallback(GetCallbackRaw cb, void *user_data) except + cppclass DoneCallback: DoneCallback() except + - #DoneCallback(DoneCallbackRaw, void *user_data) except + + cppclass DoneCallbackSimple: + DoneCallbackSimple() except + cdef ShutdownCallback bindShutdownCb(ShutdownCallbackRaw cb, void *user_data) cdef GetCallback bindGetCb(GetCallbackRaw cb, void *user_data) cdef DoneCallback bindDoneCb(DoneCallbackRaw cb, void *user_data) + cdef DoneCallbackSimple bindDoneCbSimple(DoneCallbackSimpleRaw cb, void *user_data) cppclass Config: InfoHash node_id @@ -171,8 +173,8 @@ cdef extern from "opendht/indexation/pht.h" namespace "dht::indexation": cppclass LookupCallback: LookupCallback() except + Pht(string, shared_ptr[DhtRunner]) except + - void lookup(IndexKey k, LookupCallback cb, Dht.DoneCallbackSimple doneCb); - void insert(IndexKey k, IndexValue v, Dht.DoneCallbackSimple cb) + void lookup(IndexKey k, LookupCallback cb, DoneCallbackSimple doneCb); + void insert(IndexKey k, IndexValue v, DoneCallbackSimple cb) @staticmethod LookupCallback bindLookupCb(LookupCallbackRaw cb, void *user_data) diff --git a/src/callbacks.cpp b/src/callbacks.cpp index 6a84b5d7c3748aad3b04f43fd23a3d30f0e0cbca..bcdaf6fb7d1577c8602797aa59d7ddd9cf532c06 100644 --- a/src/callbacks.cpp +++ b/src/callbacks.cpp @@ -47,4 +47,12 @@ bindDoneCb(DoneCallbackRaw raw_cb, void* user_data) }; } +DoneCallbackSimple +bindDoneCbSimple(DoneCallbackSimpleRaw raw_cb, void* user_data) { + if (not raw_cb) return {}; + return [=](bool success) { + raw_cb(success, user_data); + }; +} + } \ No newline at end of file diff --git a/src/indexation/pht.cpp b/src/indexation/pht.cpp index 2e0b467319f3393a1e25f9173cc2ba93a8104856..5bbfe43749fc5b6de277cff41ccf9dba55b641d5 100644 --- a/src/indexation/pht.cpp +++ b/src/indexation/pht.cpp @@ -9,7 +9,7 @@ constexpr std::chrono::minutes Pht::Cache::NODE_EXPIRE_TIME; void Pht::lookupStep(Prefix p, std::shared_ptr<int> lo, std::shared_ptr<int> hi, std::shared_ptr<std::vector<std::shared_ptr<Value>>> vals, - LookupCallback cb, Dht::DoneCallbackSimple done_cb, + LookupCallback cb, DoneCallbackSimple done_cb, std::shared_ptr<unsigned> max_common_prefix_len, int start, bool all_values) { struct node_lookup_result { @@ -135,7 +135,7 @@ void Pht::lookupStep(Prefix p, std::shared_ptr<int> lo, std::shared_ptr<int> hi, } } -void Pht::lookup(Key k, Pht::LookupCallback cb, Dht::DoneCallbackSimple done_cb, bool exact_match) { +void Pht::lookup(Key k, Pht::LookupCallback cb, DoneCallbackSimple done_cb, bool exact_match) { auto values = std::make_shared<std::vector<std::shared_ptr<Value>>>(); auto prefix = linearize(k); auto lo = std::make_shared<int>(0); @@ -165,7 +165,7 @@ void Pht::updateCanary(Prefix p) { } } -void Pht::insert(Key k, Value v, Dht::DoneCallbackSimple done_cb) { +void Pht::insert(Key k, Value v, DoneCallbackSimple done_cb) { Prefix kp = linearize(k); auto lo = std::make_shared<int>(0); diff --git a/tools/dhtnode.cpp b/tools/dhtnode.cpp index 0a0b27c8b0b86c778c84dac758c7a11e83279d5e..2dabb2cbca6fc9545d8d86f87cf7abc1b829634e 100644 --- a/tools/dhtnode.cpp +++ b/tools/dhtnode.cpp @@ -142,9 +142,9 @@ void cmd_loop(std::shared_ptr<DhtRunner>& dht, std::map<std::string, dht::indexa } else if (op == "log") { params.log = !params.log; if (params.log) - log::enableLogging(dht); + log::enableLogging(*dht); else - log::disableLogging(dht); + log::disableLogging(*dht); continue; } @@ -206,9 +206,7 @@ void cmd_loop(std::shared_ptr<DhtRunner>& dht, std::map<std::string, dht::indexa }, dht::Query {std::move(rem)}); } else if (op == "l") { - std::string rem; - std::getline(iss, rem); - dht.listen(id, [](std::shared_ptr<Value> value) { + dht->listen(id, [](std::shared_ptr<Value> value) { std::cout << "Listen: found value:" << std::endl; std::cout << "\t" << *value << std::endl; return true; @@ -338,9 +336,9 @@ main(int argc, char **argv) if (params.log) { if (not params.logfile.empty()) - log::enableFileLogging(dht, params.logfile); + log::enableFileLogging(*dht, params.logfile); else - log::enableLogging(dht); + log::enableLogging(*dht); } if (not params.bootstrap.first.empty()) {