From 80a9c135b8bdca64246f147f22d98485e0f05ee5 Mon Sep 17 00:00:00 2001 From: Aleksi Lindeman Date: Sun, 28 Jan 2018 07:35:37 +0100 Subject: Add some files... --- src/Database.cpp | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ src/Group.cpp | 13 +++++++++++++ src/main.cpp | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 112 insertions(+) create mode 100644 src/Database.cpp create mode 100644 src/Group.cpp create mode 100644 src/main.cpp (limited to 'src') diff --git a/src/Database.cpp b/src/Database.cpp new file mode 100644 index 0000000..4fa8405 --- /dev/null +++ b/src/Database.cpp @@ -0,0 +1,48 @@ +#include "../include/Database.hpp" +#include +#include + +using namespace dht; +using namespace std; + +namespace odhtdb +{ + Database::Database(const char *bootstrapNodeAddr, u16 port) + { + node.run(port, dht::crypto::generateIdentity(), true); + fmt::MemoryWriter portStr; + portStr << port; + node.bootstrap(bootstrapNodeAddr, portStr.c_str()); + } + + Database::~Database() + { + node.join(); + } + + void Database::add(const Key &key, DataView data) + { + stagedObjects.emplace_back(StagedObject{ key, data }); + } + + void Database::commit() + { + // TODO: Combine staged objects into one object + for(StagedObject stagedObject : stagedObjects) + { + commitStagedObject(stagedObject); + } + stagedObjects.clear(); + } + + void Database::commitStagedObject(const StagedObject &stagedObject) + { + Value value((const u8*)stagedObject.data.data, stagedObject.data.size); + node.put(stagedObject.key.hashedKey, move(value), [](bool ok) + { + // TODO: Handle failure to put data + if(!ok) + fprintf(stderr, "Failed to put: %s, what to do?\n", "commitStagedObject"); + }, time_point(), false); + } +} \ No newline at end of file diff --git a/src/Group.cpp b/src/Group.cpp new file mode 100644 index 0000000..213a0bb --- /dev/null +++ b/src/Group.cpp @@ -0,0 +1,13 @@ +#include "../include/Group.hpp" +#include "../include/User.hpp" + +namespace odhtdb +{ + Group::~Group() + { + for(User *user : users) + { + delete user; + } + } +} \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp new file mode 100644 index 0000000..8e38e18 --- /dev/null +++ b/src/main.cpp @@ -0,0 +1,51 @@ +#include +#include +#include "../include/Database.hpp" + +using namespace odhtdb; + +int main() +{ + Database database("bootstrap.ring.cx", 4222); + Key channelChatKey("galax.channel.CAGERIJF232dKADS528392fawdkf3fas.chat"); + const char *data = "hello, world!"; + database.add(channelChatKey, DataView{ (void*)data, strlen(data) }); + database.commit(); + + /* + Database database("bootstrap.ring.cx", 4222); + Key channelChatKey("galax.channel.CAGERIJF232dKADS528392fawdkf3fas.chat"); + database.add(channelChatKey, { localUser, date, "hello, world!" }); + */ + +#if 0 + dht::DhtRunner node; + + // Launch a dht node on a new thread, using a + // generated RSA key pair, and listen on port 4222. + node.run(4222, dht::crypto::generateIdentity(), true); + + // Join the network through any running node, + // here using a known bootstrap node. + node.bootstrap("bootstrap.ring.cx", "4222"); + + // put some data on the dht + std::vector some_data(5, 10); + node.put("unique_key", some_data); + + // put some data on the dht, signed with our generated private key + node.putSigned("unique_key_42", some_data); + + // get data from the dht + node.get("other_unique_key", [](const std::vector>& values) { + // Callback called when values are found + for (const auto& value : values) + std::cout << "Found value: " << *value << std::endl; + return true; // return false to stop the search + }); + + // wait for dht threads to end + node.join(); + #endif + return 0; +} \ No newline at end of file -- cgit v1.2.3