diff options
author | dec05eba <dec05eba@protonmail.com> | 2018-03-17 14:18:14 +0100 |
---|---|---|
committer | dec05eba <dec05eba@protonmail.com> | 2020-08-18 23:25:46 +0200 |
commit | 53a1850ce2a253c574113684ac38c8ed31b1d0ae (patch) | |
tree | 00d73cd00217e2c64b08377f146862d644cef55c /src | |
parent | 89d9a70f8bc284970138d0fb824b5fcbf13d0b3c (diff) |
Add callback functions for operations
Diffstat (limited to 'src')
-rw-r--r-- | src/Database.cpp | 107 | ||||
-rw-r--r-- | src/Log.cpp | 3 |
2 files changed, 80 insertions, 30 deletions
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> &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> &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<Hash> hashRequestKey = make_shared<Hash>(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<StagedObject>(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<StagedObject>(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<StagedObject>(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<dht::Value> &value, const Hash &hash, const shared_ptr<char*> encryptionKey) + void Database::deserializeCreateRequest(const std::shared_ptr<dht::Value> &value, const Hash &hash, const shared_ptr<char*> encryptionKey) { sibs::SafeDeserializer deserializer(value->data.data(), value->data.size()); u16 packetStructureVersion = deserializer.extract<u16>(); @@ -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<u8>(); - 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<u8>(); 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<u8>(); 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<dht::Value> value, const Hash &hash, const shared_ptr<char*> 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<dht::Value> value, const Hash &requestDataHash, const shared_ptr<char*> 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<void(const DatabaseCreateNodeRequest&)> callbackFunc) + { + onCreateNodeCallbackFunc = callbackFunc; + } + + void Database::setOnAddNodeCallback(std::function<void(const DatabaseAddNodeRequest&)> callbackFunc) + { + onAddNodeCallbackFunc = callbackFunc; + } + + void Database::setOnAddUserCallback(std::function<void(const DatabaseAddUserRequest&)> 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); } } |