aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authordec05eba <dec05eba@protonmail.com>2018-02-10 19:36:35 +0100
committerdec05eba <dec05eba@protonmail.com>2020-08-18 23:25:12 +0200
commita9a7ecaa25e6bc11062e21affd458e2de78747ff (patch)
treec14a4e269cbf46163cf334781a5925d052e72aa1
parent64df3de50aa88f15e1a46f21a0f62281eb582603 (diff)
Fix put not working because used time discards message before received
-rw-r--r--include/Database.hpp4
-rw-r--r--src/Database.cpp48
-rw-r--r--tests/main.cpp20
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<dht::Value> &value);
StagedAddObject deserializeAddRequest(const std::shared_ptr<dht::Value> &value);
- bool listenCreateData(const std::vector<std::shared_ptr<dht::Value>> &values);
- bool listenAddData(const std::vector<std::shared_ptr<dht::Value>> &values);
+ bool listenCreateData(std::shared_ptr<dht::Value> value);
+ bool listenAddData(std::shared_ptr<dht::Value> value);
private:
dht::DhtRunner node;
std::vector<StagedCreateObject> 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<std::shared_ptr<dht::Value>> &values)
+ bool Database::listenCreateData(std::shared_ptr<dht::Value> value)
{
- for(const shared_ptr<Value> &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<std::shared_ptr<dht::Value>> &values)
+ bool Database::listenAddData(std::shared_ptr<dht::Value> value)
{
- for(const shared_ptr<Value> &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 <chrono>
+#include <thread>
+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;
}