From 9c22be3516d5067b98b06271e2f3545713ff6099 Mon Sep 17 00:00:00 2001 From: Aleksi Lindeman <0xdec05eba@gmail.com> Date: Sat, 17 Mar 2018 14:18:14 +0100 Subject: Add callback functions for operations --- .vscode/c_cpp_properties.json | 1 + compile_commands.json | 62 ++++++++++++++++++++++++ include/odhtdb/Database.hpp | 75 +++++++++++++++++------------ src/Database.cpp | 107 ++++++++++++++++++++++++++++++------------ src/Log.cpp | 3 ++ tests/main.cpp | 24 ++++++++-- 6 files changed, 209 insertions(+), 63 deletions(-) create mode 100644 compile_commands.json diff --git a/.vscode/c_cpp_properties.json b/.vscode/c_cpp_properties.json index aff2d2c..9c1a147 100644 --- a/.vscode/c_cpp_properties.json +++ b/.vscode/c_cpp_properties.json @@ -40,6 +40,7 @@ ], "defines": [], "intelliSenseMode": "clang-x64", + "compileCommands": "${workspaceRoot}/compile_commands.json", "browse": { "path": [ "/usr/lib/gcc/x86_64-pc-linux-gnu/7.2.1/../../../../include/c++/7.2.1", diff --git a/compile_commands.json b/compile_commands.json new file mode 100644 index 0000000..8e9346e --- /dev/null +++ b/compile_commands.json @@ -0,0 +1,62 @@ +[ + { + "directory": "/home/dec05eba/git/odhtdb/sibs-build/debug", + "command": "ccache c++ '-I/home/dec05eba/.sibs/lib' '-I/home/dec05eba/.sibs/lib/fmt/4.1.0/.' '-I/home/dec05eba/.sibs/lib/ntpclient/0.2.0/include' '-I/home/dec05eba/.sibs/lib/sibs-serializer/0.2.0/.' '-I/home/dec05eba/.sibs/lib/boost-filesystem/1.66.0/include' '-I/home/dec05eba/.sibs/lib/boost-system/1.66.0/include' '-I/home/dec05eba/.sibs/lib/boost-uuid/1.66.0/include' -std=c++14 -pedantic -MMD -MP '-Iodhtdb@exe' -I/usr/include/p11-kit-1 '-I..' -Wall -Wextra -Werror=return-type -fdiagnostics-show-option '-fdiagnostics-color=always' '-pipe' '-D_FILE_OFFSET_BITS=64' '-Winvalid-pch' -fstack-protector '-O0' -fexceptions -Wnon-virtual-dtor -g3 -c -fPIC ../../src/Database.cpp -o 'odhtdb@exe/src/Database.cpp.o'", + "file": "../../src/Database.cpp" + }, + { + "directory": "/home/dec05eba/git/odhtdb/sibs-build/debug", + "command": "ccache c++ '-I/home/dec05eba/.sibs/lib' '-I/home/dec05eba/.sibs/lib/fmt/4.1.0/.' '-I/home/dec05eba/.sibs/lib/ntpclient/0.2.0/include' '-I/home/dec05eba/.sibs/lib/sibs-serializer/0.2.0/.' '-I/home/dec05eba/.sibs/lib/boost-filesystem/1.66.0/include' '-I/home/dec05eba/.sibs/lib/boost-system/1.66.0/include' '-I/home/dec05eba/.sibs/lib/boost-uuid/1.66.0/include' -std=c++14 -pedantic -MMD -MP '-Iodhtdb@exe' -I/usr/include/p11-kit-1 '-I..' -Wall -Wextra -Werror=return-type -fdiagnostics-show-option '-fdiagnostics-color=always' '-pipe' '-D_FILE_OFFSET_BITS=64' '-Winvalid-pch' -fstack-protector '-O0' -fexceptions -Wnon-virtual-dtor -g3 -c -fPIC ../../src/Encryption.cpp -o 'odhtdb@exe/src/Encryption.cpp.o'", + "file": "../../src/Encryption.cpp" + }, + { + "directory": "/home/dec05eba/git/odhtdb/sibs-build/debug", + "command": "ccache c++ '-I/home/dec05eba/.sibs/lib' '-I/home/dec05eba/.sibs/lib/fmt/4.1.0/.' '-I/home/dec05eba/.sibs/lib/ntpclient/0.2.0/include' '-I/home/dec05eba/.sibs/lib/sibs-serializer/0.2.0/.' '-I/home/dec05eba/.sibs/lib/boost-filesystem/1.66.0/include' '-I/home/dec05eba/.sibs/lib/boost-system/1.66.0/include' '-I/home/dec05eba/.sibs/lib/boost-uuid/1.66.0/include' -std=c++14 -pedantic -MMD -MP '-Iodhtdb@exe' -I/usr/include/p11-kit-1 '-I..' -Wall -Wextra -Werror=return-type -fdiagnostics-show-option '-fdiagnostics-color=always' '-pipe' '-D_FILE_OFFSET_BITS=64' '-Winvalid-pch' -fstack-protector '-O0' -fexceptions -Wnon-virtual-dtor -g3 -c -fPIC ../../src/Group.cpp -o 'odhtdb@exe/src/Group.cpp.o'", + "file": "../../src/Group.cpp" + }, + { + "directory": "/home/dec05eba/git/odhtdb/sibs-build/debug", + "command": "ccache c++ '-I/home/dec05eba/.sibs/lib' '-I/home/dec05eba/.sibs/lib/fmt/4.1.0/.' '-I/home/dec05eba/.sibs/lib/ntpclient/0.2.0/include' '-I/home/dec05eba/.sibs/lib/sibs-serializer/0.2.0/.' '-I/home/dec05eba/.sibs/lib/boost-filesystem/1.66.0/include' '-I/home/dec05eba/.sibs/lib/boost-system/1.66.0/include' '-I/home/dec05eba/.sibs/lib/boost-uuid/1.66.0/include' -std=c++14 -pedantic -MMD -MP '-Iodhtdb@exe' -I/usr/include/p11-kit-1 '-I..' -Wall -Wextra -Werror=return-type -fdiagnostics-show-option '-fdiagnostics-color=always' '-pipe' '-D_FILE_OFFSET_BITS=64' '-Winvalid-pch' -fstack-protector '-O0' -fexceptions -Wnon-virtual-dtor -g3 -c -fPIC ../../src/Signature.cpp -o 'odhtdb@exe/src/Signature.cpp.o'", + "file": "../../src/Signature.cpp" + }, + { + "directory": "/home/dec05eba/git/odhtdb/sibs-build/debug", + "command": "ccache c++ '-I/home/dec05eba/.sibs/lib' '-I/home/dec05eba/.sibs/lib/fmt/4.1.0/.' '-I/home/dec05eba/.sibs/lib/ntpclient/0.2.0/include' '-I/home/dec05eba/.sibs/lib/sibs-serializer/0.2.0/.' '-I/home/dec05eba/.sibs/lib/boost-filesystem/1.66.0/include' '-I/home/dec05eba/.sibs/lib/boost-system/1.66.0/include' '-I/home/dec05eba/.sibs/lib/boost-uuid/1.66.0/include' -std=c++14 -pedantic -MMD -MP '-Iodhtdb@exe' -I/usr/include/p11-kit-1 '-I..' -Wall -Wextra -Werror=return-type -fdiagnostics-show-option '-fdiagnostics-color=always' '-pipe' '-D_FILE_OFFSET_BITS=64' '-Winvalid-pch' -fstack-protector '-O0' -fexceptions -Wnon-virtual-dtor -g3 -c -fPIC ../../src/Permission.cpp -o 'odhtdb@exe/src/Permission.cpp.o'", + "file": "../../src/Permission.cpp" + }, + { + "directory": "/home/dec05eba/git/odhtdb/sibs-build/debug", + "command": "ccache c++ '-I/home/dec05eba/.sibs/lib' '-I/home/dec05eba/.sibs/lib/fmt/4.1.0/.' '-I/home/dec05eba/.sibs/lib/ntpclient/0.2.0/include' '-I/home/dec05eba/.sibs/lib/sibs-serializer/0.2.0/.' '-I/home/dec05eba/.sibs/lib/boost-filesystem/1.66.0/include' '-I/home/dec05eba/.sibs/lib/boost-system/1.66.0/include' '-I/home/dec05eba/.sibs/lib/boost-uuid/1.66.0/include' -std=c++14 -pedantic -MMD -MP '-Iodhtdb@exe' -I/usr/include/p11-kit-1 '-I..' -Wall -Wextra -Werror=return-type -fdiagnostics-show-option '-fdiagnostics-color=always' '-pipe' '-D_FILE_OFFSET_BITS=64' '-Winvalid-pch' -fstack-protector '-O0' -fexceptions -Wnon-virtual-dtor -g3 -c -fPIC ../../src/Hash.cpp -o 'odhtdb@exe/src/Hash.cpp.o'", + "file": "../../src/Hash.cpp" + }, + { + "directory": "/home/dec05eba/git/odhtdb/sibs-build/debug", + "command": "ccache c++ '-I/home/dec05eba/.sibs/lib' '-I/home/dec05eba/.sibs/lib/fmt/4.1.0/.' '-I/home/dec05eba/.sibs/lib/ntpclient/0.2.0/include' '-I/home/dec05eba/.sibs/lib/sibs-serializer/0.2.0/.' '-I/home/dec05eba/.sibs/lib/boost-filesystem/1.66.0/include' '-I/home/dec05eba/.sibs/lib/boost-system/1.66.0/include' '-I/home/dec05eba/.sibs/lib/boost-uuid/1.66.0/include' -std=c++14 -pedantic -MMD -MP '-Iodhtdb@exe' -I/usr/include/p11-kit-1 '-I..' -Wall -Wextra -Werror=return-type -fdiagnostics-show-option '-fdiagnostics-color=always' '-pipe' '-D_FILE_OFFSET_BITS=64' '-Winvalid-pch' -fstack-protector '-O0' -fexceptions -Wnon-virtual-dtor -g3 -c -fPIC ../../src/Log.cpp -o 'odhtdb@exe/src/Log.cpp.o'", + "file": "../../src/Log.cpp" + }, + { + "directory": "/home/dec05eba/git/odhtdb/sibs-build/debug", + "command": "ccache c++ '-I/home/dec05eba/.sibs/lib' '-I/home/dec05eba/.sibs/lib/fmt/4.1.0/.' '-I/home/dec05eba/.sibs/lib/ntpclient/0.2.0/include' '-I/home/dec05eba/.sibs/lib/sibs-serializer/0.2.0/.' '-I/home/dec05eba/.sibs/lib/boost-filesystem/1.66.0/include' '-I/home/dec05eba/.sibs/lib/boost-system/1.66.0/include' '-I/home/dec05eba/.sibs/lib/boost-uuid/1.66.0/include' -std=c++14 -pedantic -MMD -MP '-Iodhtdb@exe' -I/usr/include/p11-kit-1 '-I..' -Wall -Wextra -Werror=return-type -fdiagnostics-show-option '-fdiagnostics-color=always' '-pipe' '-D_FILE_OFFSET_BITS=64' '-Winvalid-pch' -fstack-protector '-O0' -fexceptions -Wnon-virtual-dtor -g3 -c -fPIC ../../src/DataView.cpp -o 'odhtdb@exe/src/DataView.cpp.o'", + "file": "../../src/DataView.cpp" + }, + { + "directory": "/home/dec05eba/git/odhtdb/sibs-build/debug", + "command": "ccache c++ '-I/home/dec05eba/.sibs/lib' '-I/home/dec05eba/.sibs/lib/fmt/4.1.0/.' '-I/home/dec05eba/.sibs/lib/ntpclient/0.2.0/include' '-I/home/dec05eba/.sibs/lib/sibs-serializer/0.2.0/.' '-I/home/dec05eba/.sibs/lib/boost-filesystem/1.66.0/include' '-I/home/dec05eba/.sibs/lib/boost-system/1.66.0/include' '-I/home/dec05eba/.sibs/lib/boost-uuid/1.66.0/include' -std=c++14 -pedantic -MMD -MP '-Iodhtdb@exe' -I/usr/include/p11-kit-1 '-I..' -Wall -Wextra -Werror=return-type -fdiagnostics-show-option '-fdiagnostics-color=always' '-pipe' '-D_FILE_OFFSET_BITS=64' '-Winvalid-pch' -fstack-protector '-O0' -fexceptions -Wnon-virtual-dtor -g3 -c -fPIC ../../src/User.cpp -o 'odhtdb@exe/src/User.cpp.o'", + "file": "../../src/User.cpp" + }, + { + "directory": "/home/dec05eba/git/odhtdb/sibs-build/debug", + "command": "ccache c++ '-I/home/dec05eba/.sibs/lib' '-I/home/dec05eba/.sibs/lib/fmt/4.1.0/.' '-I/home/dec05eba/.sibs/lib/ntpclient/0.2.0/include' '-I/home/dec05eba/.sibs/lib/sibs-serializer/0.2.0/.' '-I/home/dec05eba/.sibs/lib/boost-filesystem/1.66.0/include' '-I/home/dec05eba/.sibs/lib/boost-system/1.66.0/include' '-I/home/dec05eba/.sibs/lib/boost-uuid/1.66.0/include' -std=c++14 -pedantic -MMD -MP '-Iodhtdb@exe' -I/usr/include/p11-kit-1 '-I..' -Wall -Wextra -Werror=return-type -fdiagnostics-show-option '-fdiagnostics-color=always' '-pipe' '-D_FILE_OFFSET_BITS=64' '-Winvalid-pch' -fstack-protector '-O0' -fexceptions -Wnon-virtual-dtor -g3 -c -fPIC ../../src/DatabaseStorage.cpp -o 'odhtdb@exe/src/DatabaseStorage.cpp.o'", + "file": "../../src/DatabaseStorage.cpp" + }, + { + "directory": "/home/dec05eba/git/odhtdb/sibs-build/debug", + "command": "ccache c++ '-I/home/dec05eba/.sibs/lib' '-I/home/dec05eba/.sibs/lib/fmt/4.1.0/.' '-I/home/dec05eba/.sibs/lib/ntpclient/0.2.0/include' '-I/home/dec05eba/.sibs/lib/sibs-serializer/0.2.0/.' '-I/home/dec05eba/.sibs/lib/boost-filesystem/1.66.0/include' '-I/home/dec05eba/.sibs/lib/boost-system/1.66.0/include' '-I/home/dec05eba/.sibs/lib/boost-uuid/1.66.0/include' -std=c++14 -pedantic -MMD -MP '-Iodhtdb@exe' -I/usr/include/p11-kit-1 '-I..' -Wall -Wextra -Werror=return-type -fdiagnostics-show-option '-fdiagnostics-color=always' '-pipe' '-D_FILE_OFFSET_BITS=64' '-Winvalid-pch' -fstack-protector '-O0' -fexceptions -Wnon-virtual-dtor -g3 -c -fPIC ../../src/DhtKey.cpp -o 'odhtdb@exe/src/DhtKey.cpp.o'", + "file": "../../src/DhtKey.cpp" + }, + { + "directory": "/home/dec05eba/git/odhtdb/sibs-build/debug", + "command": "ccache c++ 'odhtdb@exe/src/Database.cpp.o' 'odhtdb@exe/src/Encryption.cpp.o' 'odhtdb@exe/src/Group.cpp.o' 'odhtdb@exe/src/Signature.cpp.o' 'odhtdb@exe/src/Permission.cpp.o' 'odhtdb@exe/src/Hash.cpp.o' 'odhtdb@exe/src/Log.cpp.o' 'odhtdb@exe/src/DataView.cpp.o' 'odhtdb@exe/src/User.cpp.o' 'odhtdb@exe/src/DatabaseStorage.cpp.o' 'odhtdb@exe/src/DhtKey.cpp.o' -shared -o libodhtdb.so '-Wl,--no-undefined,--as-needed' -ldl -pthread -lopendht -lsodium '/home/dec05eba/.sibs/lib/fmt/4.1.0/sibs-build/debug/libfmt.a' '/home/dec05eba/.sibs/lib/ntpclient/0.2.0/sibs-build/debug/libntpclient.a' '/home/dec05eba/.sibs/lib/boost-filesystem/1.66.0/sibs-build/debug/libboost-filesystem.a' '/home/dec05eba/.sibs/lib/boost-system/1.66.0/sibs-build/debug/libboost-system.a' ", + "file": "odhtdb@exe/src/Database.cpp.o" + } +] diff --git a/include/odhtdb/Database.hpp b/include/odhtdb/Database.hpp index 5114d28..926437a 100644 --- a/include/odhtdb/Database.hpp +++ b/include/odhtdb/Database.hpp @@ -15,6 +15,7 @@ #include #include #include +#include namespace odhtdb { @@ -52,57 +53,64 @@ namespace odhtdb ADD_USER }; - struct DatabaseCreateRequest + struct DatabaseCreateNodeRequest { - DISABLE_COPY(DatabaseCreateRequest) + DISABLE_COPY(DatabaseCreateNodeRequest) + const Hash *nodeHash; u64 timestamp; // In microseconds - Group *creatorGroup; + const User *creatorUser; std::string name; - DatabaseCreateRequest(u64 _timestamp, Group *_creatorGroup, std::string &&_name) : + DatabaseCreateNodeRequest(const Hash *_nodeHash, u64 _timestamp, const User *_creatorUser, std::string &&_name) : + nodeHash(_nodeHash), timestamp(_timestamp), - creatorGroup(_creatorGroup), + creatorUser(_creatorUser), name(std::move(_name)) { } - - DatabaseCreateRequest(DatabaseCreateRequest &&other) - { - timestamp = other.timestamp; - creatorGroup = other.creatorGroup; - name = std::move(other.name); - - other.timestamp = 0; - other.creatorGroup = nullptr; - } }; - struct DatabaseAddRequest + struct DatabaseAddNodeRequest { - DISABLE_COPY(DatabaseAddRequest) + DISABLE_COPY(DatabaseAddNodeRequest) + const Hash *nodeHash; + const Hash *requestHash; u64 timestamp; // In microseconds const User *creatorUser; - Decryption decryptedData; + const DataView decryptedData; - DatabaseAddRequest(u64 _timestamp, const User *_creatorUser, Decryption &&_decryptedData) : + DatabaseAddNodeRequest(const Hash *_nodeHash, const Hash *_requestHash, u64 _timestamp, const User *_creatorUser, const DataView &_decryptedData) : + nodeHash(_nodeHash), + requestHash(_requestHash), timestamp(_timestamp), creatorUser(_creatorUser), - decryptedData(std::move(_decryptedData)) + decryptedData(_decryptedData) { } - - DatabaseAddRequest(DatabaseAddRequest &&other) + }; + + struct DatabaseAddUserRequest + { + DISABLE_COPY(DatabaseAddUserRequest) + + const Hash *nodeHash; + const Hash *requestHash; + u64 timestamp; // In microseconds + const User *creatorUser; + const User *userToAdd; + const Group *groupToAddUserTo; + + DatabaseAddUserRequest(const Hash *_nodeHash, const Hash *_requestHash, u64 _timestamp, const User *_creatorUser, const User *_userToAdd, const Group *_groupToAddUserTo) : + timestamp(_timestamp), + creatorUser(_creatorUser), + userToAdd(_userToAdd), + groupToAddUserTo(_groupToAddUserTo) { - timestamp = other.timestamp; - creatorUser = other.creatorUser; - decryptedData = std::move(other.decryptedData); - - other.timestamp = 0; - other.creatorUser = nullptr; + } }; @@ -135,13 +143,19 @@ namespace odhtdb // Throws PermissionDeniedException if user @userToPerformActionWith is not allowed to add user @userToAdd to group @groupToAddUserTo void addUserToGroup(const DatabaseNode &nodeInfo, LocalUser *userToPerformActionWith, const std::string &userToAddName, const Signature::PublicKey &userToAddPublicKey, Group *groupToAddUserTo); void commit(); + + void setOnCreateNodeCallback(std::function callbackFunc); + void setOnAddNodeCallback(std::function callbackFunc); + void setOnAddUserCallback(std::function callbackFunc); + + DatabaseStorage& getStorage(); private: // Throws CommitCreateException on failure void commitStagedCreateObject(const std::unique_ptr &stagedObject); // Throws CommitAddException on failure void commitStagedAddObject(const std::unique_ptr &stagedObject); ntp::NtpTimestamp getSyncedTimestampUtc() const; - DatabaseCreateRequest deserializeCreateRequest(const std::shared_ptr &value, const Hash &hash, const std::shared_ptr encryptionKey); + void deserializeCreateRequest(const std::shared_ptr &value, const Hash &hash, const std::shared_ptr encryptionKey); void deserializeAddRequest(const std::shared_ptr &value, const Hash &requestDataHash, const std::shared_ptr encryptionKey); bool listenCreateData(std::shared_ptr value, const Hash &hash, const std::shared_ptr encryptionKey); bool listenAddData(std::shared_ptr value, const Hash &requestDataHash, const std::shared_ptr encryptionKey); @@ -150,5 +164,8 @@ namespace odhtdb std::vector> stagedCreateObjects; std::vector> stagedAddObjects; DatabaseStorage databaseStorage; + std::function onCreateNodeCallbackFunc; + std::function onAddNodeCallbackFunc; + std::function onAddUserCallbackFunc; }; } diff --git a/src/Database.cpp b/src/Database.cpp index 5d082d1..41b0d73 100644 --- a/src/Database.cpp +++ b/src/Database.cpp @@ -83,7 +83,10 @@ namespace odhtdb return hash; } - Database::Database(const char *bootstrapNodeAddr, u16 port, boost::filesystem::path storageDir) + Database::Database(const char *bootstrapNodeAddr, u16 port, boost::filesystem::path storageDir) : + onCreateNodeCallbackFunc(nullptr), + onAddNodeCallbackFunc(nullptr), + onAddUserCallbackFunc(nullptr) { // TODO: Cache this in storage. It takes pretty long time to generate new identity auto identity = dht::crypto::generateIdentity(); @@ -151,12 +154,12 @@ namespace odhtdb // (only if there are plenty of other seeders for the cached files. This could also cause race issue // where all nodes with a cached file delete it at same time) - Log::debug("Seeding key: %s\n", hash->toString().c_str()); + Log::debug("Seeding key: %s", hash->toString().c_str()); DhtKey dhtKey(*hash); node.listen(dhtKey.getNewDataListenerKey(), [this, hash, encryptionKey](const shared_ptr &value) { - Log::debug("Seed: New data listener received data...\n"); + Log::debug("Seed: New data listener received data..."); const Hash requestHash(value->data.data(), value->data.size()); if(requestHash == *hash) return true; @@ -182,7 +185,7 @@ namespace odhtdb // This is to prevent too many peers from responding to a request to get old data. node.listen(dhtKey.getRequestOldDataKey(), [this, hash](const shared_ptr &value) { - Log::debug("Request: Got request to send old data\n"); + Log::debug("Request: Got request to send old data"); try { sibs::SafeDeserializer deserializer(value->data.data(), value->data.size()); @@ -193,7 +196,7 @@ namespace odhtdb auto requestedData = databaseStorage.getStorage(*hash); if(!requestedData) { - Log::warn("No data found for hash %s, unable to serve peer\n", hash->toString().c_str()); + Log::warn("No data found for hash %s, unable to serve peer", hash->toString().c_str()); return true; } @@ -201,11 +204,11 @@ namespace odhtdb if(dataStartTimestamp == 0) { - Log::debug("Request: Sent create packet to requesting peer\n"); + Log::debug("Request: Sent create packet to requesting peer"); node.put(requestResponseInfoHash, Value((u8*)requestedData->data.data, requestedData->data.size), [](bool ok) { if(!ok) - Log::warn("Failed to put response for old data for 'create' data\n"); + Log::warn("Failed to put response for old data for 'create' data"); }); } @@ -214,13 +217,13 @@ namespace odhtdb node.put(requestResponseInfoHash, Value((u8*)requestedObject->data.data, requestedObject->data.size), [](bool ok) { if(!ok) - Log::warn("Failed to put response for old data for 'add' data\n"); + Log::warn("Failed to put response for old data for 'add' data"); }); } } catch (sibs::DeserializeException &e) { - Log::warn("Failed to deserialize 'get old data' request: %s\n", e.what()); + Log::warn("Failed to deserialize 'get old data' request: %s", e.what()); } return true; }); @@ -231,7 +234,7 @@ namespace odhtdb node.put(dhtKey.getRequestOldDataKey(), Value(serializer.getBuffer().data(), serializer.getBuffer().size()), [](bool ok) { if(!ok) - Log::warn("Failed to put request to get old data\n"); + Log::warn("Failed to put request to get old data"); }); //node.listen(CREATE_DATA_HASH, bind(&Database::listenCreateData, this, _1)); @@ -270,6 +273,12 @@ namespace odhtdb DataView requestData = combine(serializer, encryptedBody); shared_ptr hashRequestKey = make_shared(requestData.data, requestData.size); databaseStorage.createStorage(*hashRequestKey, adminGroup, timestampMicroseconds, (const u8*)requestData.data, requestData.size); + + string nodeNameCopy(nodeName); + DatabaseCreateNodeRequest createNodeRequest(hashRequestKey.get(), timestampMicroseconds, nodeAdminUser, move(nodeNameCopy)); + if(onCreateNodeCallbackFunc) + onCreateNodeCallbackFunc(createNodeRequest); + stagedCreateObjects.emplace_back(make_unique(requestData, hashRequestKey)); assert(encryptedBody.getKey().size == KEY_BYTE_SIZE); @@ -300,6 +309,11 @@ namespace odhtdb DataView stagedAddObject = combine(userToPerformActionWith->getPublicKey(), signedRequestData); Hash requestDataHash(stagedAddObject.data, stagedAddObject.size); databaseStorage.appendStorage(*nodeInfo.getRequestHash(), requestDataHash, userToPerformActionWith, timestampMicroseconds, (u8*)stagedAddObject.data, stagedAddObject.size); + + DatabaseAddNodeRequest addNodeRequest(&*nodeInfo.getRequestHash(), &requestDataHash, timestampMicroseconds, userToPerformActionWith, dataToAdd); + if(onAddNodeCallbackFunc) + onAddNodeCallbackFunc(addNodeRequest); + stagedAddObjects.emplace_back(make_unique(stagedAddObject, nodeInfo.getRequestHash())); } @@ -352,6 +366,11 @@ namespace odhtdb databaseStorage.appendStorage(*nodeInfo.getRequestHash(), requestDataHash, userToPerformActionWith, timestampMicroseconds, (u8*)stagedAddObject.data, stagedAddObject.size); auto userToAdd = RemoteUser::create(userToAddPublicKey, userToAddName, groupToAddUserTo); databaseStorage.addUser(userToAdd, *nodeInfo.getRequestHash()); + + DatabaseAddUserRequest addUserRequest(&*nodeInfo.getRequestHash(), &requestDataHash, timestampMicroseconds, userToPerformActionWith, userToAdd, groupToAddUserTo); + if(onAddUserCallbackFunc) + onAddUserCallbackFunc(addUserRequest); + stagedAddObjects.emplace_back(make_unique(stagedAddObject, nodeInfo.getRequestHash())); } @@ -361,13 +380,13 @@ namespace odhtdb try { - Log::debug("Num objects to create: %zu\n", stagedCreateObjects.size()); + Log::debug("Num objects to create: %zu", stagedCreateObjects.size()); for(const auto &stagedObject : stagedCreateObjects) { commitStagedCreateObject(stagedObject); } - Log::debug("Num objects to add: %zu\n", stagedAddObjects.size()); + Log::debug("Num objects to add: %zu", stagedAddObjects.size()); for(const auto &stagedObject : stagedAddObjects) { commitStagedAddObject(stagedObject); @@ -376,7 +395,7 @@ namespace odhtdb catch (exception &e) { // TODO: Add rollback - Log::error("Failed to commit, reason: %s\n", e.what()); + Log::error("Failed to commit, reason: %s", e.what()); } for(const auto &stagedObject : stagedCreateObjects) @@ -402,7 +421,7 @@ namespace odhtdb { // TODO: Handle failure to put data if(!ok) - Log::warn("Failed to put: %s, what to do?\n", "commitStagedCreateObject"); + Log::warn("Failed to put: %s, what to do?", "commitStagedCreateObject"); }/* TODO: How to make this work?, time_point(), false*/); } @@ -414,7 +433,7 @@ namespace odhtdb { // TODO: Handle failure to put data if(!ok) - Log::warn("Failed to put: %s, what to do?\n", "commitStagedAddObject"); + Log::warn("Failed to put: %s, what to do?", "commitStagedAddObject"); }/* TODO: How to make this work?, time_point(), false*/); } @@ -427,7 +446,7 @@ namespace odhtdb return timestamp; } - DatabaseCreateRequest Database::deserializeCreateRequest(const std::shared_ptr &value, const Hash &hash, const shared_ptr encryptionKey) + void Database::deserializeCreateRequest(const std::shared_ptr &value, const Hash &hash, const shared_ptr encryptionKey) { sibs::SafeDeserializer deserializer(value->data.data(), value->data.size()); u16 packetStructureVersion = deserializer.extract(); @@ -467,17 +486,21 @@ namespace odhtdb DataView dataToDecrypt((void*)deserializer.getBuffer(), deserializer.getSize()); Decryption decryptedBody(dataToDecrypt, DataView(nonce, NONCE_BYTE_SIZE), DataView(*encryptionKey, KEY_BYTE_SIZE)); sibs::SafeDeserializer bodyDeserializer((const u8*)decryptedBody.getDecryptedText().data, decryptedBody.getDecryptedText().size); + u8 creatorNameLength = bodyDeserializer.extract(); - string creatorName; + string creatorName; // TODO: Add this user name to storage added above creatorName.resize(creatorNameLength); bodyDeserializer.extract((u8*)&creatorName[0], creatorNameLength); u8 nameLength = bodyDeserializer.extract(); string name; name.resize(nameLength); - bodyDeserializer.extract((u8*)&name[0], nameLength); // TODO: Add this user name to storage added above + bodyDeserializer.extract((u8*)&name[0], nameLength); - return { creationDate, adminGroup, move(name) }; + Log::debug("Got create object, name: %s", name.c_str()); + DatabaseCreateNodeRequest createNodeRequest(&hash, creationDate, creatorUser, move(name)); + if(onCreateNodeCallbackFunc) + onCreateNodeCallbackFunc(createNodeRequest); } bool isUserAllowedToAddData(const User *user) @@ -558,11 +581,13 @@ namespace odhtdb throw PermissionDeniedException(errMsg); } - Log::debug("Got add object, timestamp: %zu, data: %.*s\n", creationDate, decryptedBody.getDecryptedText().size, decryptedBody.getDecryptedText().data); + Log::debug("Got add object, timestamp: %zu, data: %.*s", creationDate, decryptedBody.getDecryptedText().size, decryptedBody.getDecryptedText().data); + const DatabaseAddNodeRequest addNodeRequest(node, &requestDataHash, creationDate, creatorUser, decryptedBody.getDecryptedText()); + if(onAddNodeCallbackFunc) + onAddNodeCallbackFunc(addNodeRequest); } else if(operation == DatabaseOperation::ADD_USER) - { - + { u8 nameLength = deserializerUnsigned.extract(); string name; name.resize(nameLength); @@ -596,7 +621,10 @@ namespace odhtdb throw PermissionDeniedException(errMsg); } - Log::debug("Got add user object, timestamp: %zu, user added: %.*s\n", creationDate, nameLength, name.c_str()); + Log::debug("Got add user object, timestamp: %zu, user added: %.*s", creationDate, nameLength, name.c_str()); + DatabaseAddUserRequest addUserRequest(node, &requestDataHash, creationDate, creatorUser, user, group); + if(onAddUserCallbackFunc) + onAddUserCallbackFunc(addUserRequest); } else { @@ -613,28 +641,27 @@ namespace odhtdb bool Database::listenCreateData(std::shared_ptr value, const Hash &hash, const shared_ptr encryptionKey) { - Log::debug("Got create data\n"); + Log::debug("Got create data"); try { if(databaseStorage.getStorage(hash)) throw DatabaseStorageAlreadyExists("Create request hash is equal to hash already in storage (duplicate data?)"); - DatabaseCreateRequest createObject = deserializeCreateRequest(value, hash, encryptionKey); - Log::debug("Got create object, name: %s\n", createObject.name.c_str()); + deserializeCreateRequest(value, hash, encryptionKey); } catch (exception &e) { - Log::warn("Failed to deserialize 'create' request: %s\n", e.what()); + Log::warn("Failed to deserialize 'create' request: %s", e.what()); } return true; } bool Database::listenAddData(std::shared_ptr value, const Hash &requestDataHash, const shared_ptr encryptionKey) { - Log::debug("Got add data\n"); + Log::debug("Got add data"); try { deserializeAddRequest(value, requestDataHash, encryptionKey); - //Log::debug("Got add object, timestamp: %zu\n", addObject.timestamp); + //Log::debug("Got add object, timestamp: %zu", addObject.timestamp); } catch (RequestQuarantineException &e) { @@ -642,8 +669,28 @@ namespace odhtdb } catch (exception &e) { - Log::warn("Failed to deserialize 'add' request: %s\n", e.what()); + Log::warn("Failed to deserialize 'add' request: %s", e.what()); } return true; } + + void Database::setOnCreateNodeCallback(std::function callbackFunc) + { + onCreateNodeCallbackFunc = callbackFunc; + } + + void Database::setOnAddNodeCallback(std::function callbackFunc) + { + onAddNodeCallbackFunc = callbackFunc; + } + + void Database::setOnAddUserCallback(std::function callbackFunc) + { + onAddUserCallbackFunc = callbackFunc; + } + + DatabaseStorage& Database::getStorage() + { + return databaseStorage; + } } diff --git a/src/Log.cpp b/src/Log.cpp index 28a2784..9c06a62 100644 --- a/src/Log.cpp +++ b/src/Log.cpp @@ -15,6 +15,7 @@ namespace odhtdb va_start(args, fmt); fputs("Debug: ", stdout); vfprintf(stdout, fmt, args); + fputs("\n", stdout); va_end(args); } @@ -25,6 +26,7 @@ namespace odhtdb va_start(args, fmt); fputs("Warning: ", stdout); vfprintf(stdout, fmt, args); + fputs("\n", stdout); va_end(args); } @@ -35,6 +37,7 @@ namespace odhtdb va_start(args, fmt); fputs("Error: ", stderr); vfprintf(stderr, fmt, args); + fputs("\n", stderr); va_end(args); } } diff --git a/tests/main.cpp b/tests/main.cpp index 30703ea..bd7d75c 100644 --- a/tests/main.cpp +++ b/tests/main.cpp @@ -17,16 +17,16 @@ void testHash() { Hash hash("odhtdb", 6); assertEquals("a7b30ec8ab92de60e551b26bb8f78d315697f84dd7f5549a143477e095ec934f", hash.toString()); - Log::debug("hash of 'odhtdb' is: a7b30ec8ab92de60e551b26bb8f78d315697f84dd7f5549a143477e095ec934f\n"); + Log::debug("hash of 'odhtdb' is: a7b30ec8ab92de60e551b26bb8f78d315697f84dd7f5549a143477e095ec934f"); } void testSignData(LocalUser *localUser) { std::string publicKeyStr = localUser->getPublicKey().toString(); - Log::debug("Local user public key: %s\n", publicKeyStr.c_str()); + Log::debug("Local user public key: %s", publicKeyStr.c_str()); std::string privateKeyStr = localUser->getPrivateKey().toString(); - Log::debug("Local user private key: %s\n", privateKeyStr.c_str()); + Log::debug("Local user private key: %s", privateKeyStr.c_str()); string expectedUnsignedData = "hello, world!"; string signedData = localUser->getPrivateKey().sign(DataView((void*)expectedUnsignedData.data(), expectedUnsignedData.size())); @@ -83,7 +83,7 @@ void testEncryption() int main() { - Log::debug("Starting tests...\n"); + Log::debug("Starting tests..."); LocalUser *localUser = LocalUser::create(Signature::KeyPair(), "dec05eba", nullptr); testSignData(localUser); testEncryption(); @@ -91,6 +91,22 @@ int main() // TODO: Setup local bootstrap node for tests Database database("bootstrap.ring.cx", 4222, "storage"); + + database.setOnCreateNodeCallback([](const DatabaseCreateNodeRequest &request) + { + Log::debug("Create node callback"); + }); + + database.setOnAddNodeCallback([](const DatabaseAddNodeRequest &request) + { + Log::debug("Add node callback"); + }); + + database.setOnAddUserCallback([](const DatabaseAddUserRequest &request) + { + Log::debug("Add user callback"); + }); + auto databaseCreateResponse = database.create("adminUserName", "latenight"); DatabaseNode databaseNode(databaseCreateResponse->getNodeEncryptionKey(), databaseCreateResponse->getRequestHash()); auto adminUser = (LocalUser*)databaseCreateResponse->getNodeAdminUser(); -- cgit v1.2.3