From a9a7ecaa25e6bc11062e21affd458e2de78747ff Mon Sep 17 00:00:00 2001 From: dec05eba Date: Sat, 10 Feb 2018 19:36:35 +0100 Subject: Fix put not working because used time discards message before received --- include/Database.hpp | 4 ++-- src/Database.cpp | 48 ++++++++++++++++++++++++------------------------ tests/main.cpp | 20 ++++++++++++++------ 3 files changed, 40 insertions(+), 32 deletions(-) diff --git a/include/Database.hpp b/include/Database.hpp index bde4d5a..bfc3021 100644 --- a/include/Database.hpp +++ b/include/Database.hpp @@ -29,8 +29,8 @@ namespace odhtdb ntp::NtpTimestamp getSyncedTimestampUtc() const; StagedCreateObject deserializeCreateRequest(const std::shared_ptr &value); StagedAddObject deserializeAddRequest(const std::shared_ptr &value); - bool listenCreateData(const std::vector> &values); - bool listenAddData(const std::vector> &values); + bool listenCreateData(std::shared_ptr value); + bool listenAddData(std::shared_ptr value); private: dht::DhtRunner node; std::vector stagedCreateObjects; diff --git a/src/Database.cpp b/src/Database.cpp index f0c7b04..cd87845 100644 --- a/src/Database.cpp +++ b/src/Database.cpp @@ -104,17 +104,21 @@ namespace odhtdb // TODO: Combine staged objects into one object for efficiency. // TODO: Add rollback + printf("Num objects to create: %d\n", stagedCreateObjects.size()); for(StagedCreateObject &stagedObject : stagedCreateObjects) { commitStagedCreateObject(stagedObject); } stagedCreateObjects.clear(); + printf("Num objects to add: %d\n", stagedAddObjects.size()); for(StagedAddObject &stagedObject : stagedAddObjects) { commitStagedAddObject(stagedObject); } stagedAddObjects.clear(); + + // TODO: Add node.listen here to get notified when remote peers got the commit, then we can say we can return } void Database::commitStagedCreateObject(const StagedCreateObject &stagedObject) @@ -143,7 +147,7 @@ namespace odhtdb // TODO: Handle failure to put data if(!ok) fprintf(stderr, "Failed to put: %s, what to do?\n", "commitStagedCreateObject"); - }, time_point(), false); + }/* TODO: How to make this work?, time_point(), false*/); // Post data for listeners of this key Value putKeyValue(serializer.getBuffer().data() + OPENDHT_INFOHASH_LEN, serializer.getBuffer().size() - OPENDHT_INFOHASH_LEN); @@ -152,7 +156,7 @@ namespace odhtdb // TODO: Handle failure to put data if(!ok) fprintf(stderr, "Failed to put for listeners: %s, what to do?\n", "commitStagedCreateObject"); - }, time_point(), false); + }); } void Database::commitStagedAddObject(const StagedAddObject &stagedObject) @@ -242,36 +246,32 @@ namespace odhtdb return result; } - bool Database::listenCreateData(const std::vector> &values) + bool Database::listenCreateData(std::shared_ptr value) { - for(const shared_ptr &value : values) + printf("Got create data\n"); + try { - try - { - // TODO: Verify createObject timestamp is not in the future - StagedCreateObject createObject = deserializeCreateRequest(value); - delete createObject.primaryAdminGroup; - } - catch (sibs::DeserializeException &e) - { - fprintf(stderr, "Warning: Failed to deserialize 'create' request: %s\n", e.what()); - } + // TODO: Verify createObject timestamp is not in the future + StagedCreateObject createObject = deserializeCreateRequest(value); + delete createObject.primaryAdminGroup; + } + catch (sibs::DeserializeException &e) + { + fprintf(stderr, "Warning: Failed to deserialize 'create' request: %s\n", e.what()); } return true; } - bool Database::listenAddData(const std::vector> &values) + bool Database::listenAddData(std::shared_ptr value) { - for(const shared_ptr &value : values) + printf("Got add data\n"); + try { - try - { - StagedAddObject addObject = deserializeAddRequest(value); - } - catch (sibs::DeserializeException &e) - { - fprintf(stderr, "Warning: Failed to deserialize 'add' request: %s\n", e.what()); - } + StagedAddObject addObject = deserializeAddRequest(value); + } + catch (sibs::DeserializeException &e) + { + fprintf(stderr, "Warning: Failed to deserialize 'add' request: %s\n", e.what()); } return true; } diff --git a/tests/main.cpp b/tests/main.cpp index a685ee6..5e53dc8 100644 --- a/tests/main.cpp +++ b/tests/main.cpp @@ -2,7 +2,11 @@ #include "../include/Database.hpp" #include "../include/Group.hpp" #include "../include/LocalUser.hpp" +#include +#include +using namespace std; +using namespace chrono_literals; using namespace odhtdb; int main() @@ -14,6 +18,7 @@ int main() std::string privateKeyStr = localUser->getPrivateKey().toString(); printf("Local user private key: %s\n", privateKeyStr.c_str()); + /* char hex_ed_pk[65]; unsigned char seed[crypto_sign_ed25519_SEEDBYTES]; @@ -27,21 +32,24 @@ int main() */ - //crypto_sign_ed25519_sk_to_seed // TODO: For tests, dont run against bootstrap.ring.cx. // Run against a bootstrap node made only for testing which doesn't persist added data. - /* + Database database("bootstrap.ring.cx", 4222, "storage"); database.seed(); - LocalUser *localUser = LocalUser::create("dec05eba"); Group group("admin"); group.addUser(localUser); database.create("galax.channel.latenight.chat", &group); - const char *data = "hello, world!"; - database.add("galax.channel.latenight.chat", DataView{ (void*)data, strlen(data) }); + //const char *data = "hello, world!"; + //database.add("galax.channel.latenight.chat", DataView{ (void*)data, strlen(data) }); database.commit(); - */ + auto start = chrono::high_resolution_clock::now(); + while(chrono::high_resolution_clock::now() - start < 5s) + { + this_thread::sleep_for(10ms); + } + return 0; } -- cgit v1.2.3