diff options
author | dec05eba <0xdec05eba@gmail.com> | 2018-05-15 06:06:02 +0200 |
---|---|---|
committer | dec05eba <0xdec05eba@gmail.com> | 2018-05-15 06:06:06 +0200 |
commit | 4b656c5600a28f05665e849715af3d08f29dff2f (patch) | |
tree | 88c96a5f90c52fbc5f1670f607a7dcbf63fd9659 /src/Database.cpp | |
parent | 4a0d1ff39ec150d8e8c04ca846b3116884e1774e (diff) |
Store known remote nodes and connect to them next time
Diffstat (limited to 'src/Database.cpp')
-rw-r--r-- | src/Database.cpp | 28 |
1 files changed, 26 insertions, 2 deletions
diff --git a/src/Database.cpp b/src/Database.cpp index 93dec99..ac5a941 100644 --- a/src/Database.cpp +++ b/src/Database.cpp @@ -92,14 +92,15 @@ namespace odhtdb onCreateNodeCallbackFunc(callbackFuncs.createNodeCallbackFunc), onAddNodeCallbackFunc(callbackFuncs.addNodeCallbackFunc), onAddUserCallbackFunc(callbackFuncs.addUserCallbackFunc), - databaseStorage(this, storageDir) + databaseStorage(this, storageDir), + shuttingDown(false) { node.run(port , { /*.dht_config = */{ /*.node_config = */{ /*.node_id = */{}, /*.network = */0, - /*.is_bootstrap = */false, + /*.is_bootstrap = */true, /*.maintain_storage*/false }, /*.id = */databaseStorage.getIdentity() @@ -110,6 +111,10 @@ namespace odhtdb }); auto portStr = to_string(port); node.bootstrap(bootstrapNodeAddr, portStr.c_str()); + const auto &remoteNodes = databaseStorage.getRemoteNodes(); + if(!remoteNodes.empty()) + node.bootstrap(remoteNodes); + Log::debug("Connecting to bootstrap node (%s) and %u other known nodes that we have connected to previously", bootstrapNodeAddr, remoteNodes.size()); // TODO: Make this work for multiple threads initializing database at same time ++databaseCount; @@ -155,12 +160,31 @@ namespace odhtdb }); ntpThread->detach(); } + + remoteNodesSaveThread = thread([this]() + { + int saveIntervalMs = 5000; // 5 sec + const int sleepDurationMs = 200; + while(!shuttingDown) + { + for(int i = 0; i < saveIntervalMs / sleepDurationMs; ++i) + { + this_thread::sleep_for(chrono::milliseconds(sleepDurationMs)); + if(shuttingDown) + return; + } + databaseStorage.setRemoteNodes(node.exportNodes()); + saveIntervalMs = 30000; // 30 sec + } + }); } Database::~Database() { // TODO: Make this work for multiple threads removing database object at same time --databaseCount; + shuttingDown = true; + remoteNodesSaveThread.join(); node.join(); } |