From eb90fffb5e8c9d4fd7ffecb4dab2c1b35c2796c1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adrien=20B=C3=A9raud?= <adrien.beraud@savoirfairelinux.com> Date: Tue, 7 Jun 2016 12:13:03 -0400 Subject: [PATCH] update for API changes --- include/opendht/callbacks.h | 3 ++- include/opendht/indexation/pht.h | 6 +++--- python/opendht.pyx | 18 +++++++++--------- python/opendht_cpp.pxd | 10 ++++++---- src/callbacks.cpp | 8 ++++++++ src/indexation/pht.cpp | 6 +++--- tools/dhtnode.cpp | 12 +++++------- 7 files changed, 36 insertions(+), 27 deletions(-) diff --git a/include/opendht/callbacks.h b/include/opendht/callbacks.h index ed75c0d6..ed05c3d0 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 c4013c5a..c2b2e5c2 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 8e846efb..7518b13b 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 4b283ee9..95b95500 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 6a84b5d7..bcdaf6fb 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 2e0b4673..5bbfe437 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 0a0b27c8..2dabb2cb 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()) { -- GitLab