From f1657f1ffd5103272b64b06219a280a348f3f23d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=A1s=20Ortega=20Froysa?= Date: Mon, 18 Sep 2017 10:30:18 +0200 Subject: [PATCH] Starting new with C interface. --- CMakeLists.txt | 3 +- include/neocomm.h | 70 +++++++++++++++++++++++++++++++++++++++++++++++ src/node.cpp | 36 ++++++++++++++++++++++++ 3 files changed, 108 insertions(+), 1 deletion(-) create mode 100644 include/neocomm.h create mode 100644 src/node.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index bdf350c..baec35f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -38,7 +38,8 @@ include_directories( "include/" SYSTEM OPENDHT_INCLUDE_DIRS) -set(SRCS) +set(SRCS + src/node.cpp) set(CMAKE_CXX_FLAGS "-std=c++11 -Wall -Wextra -Wpedantic -Werror -Wfatal-errors -pedantic-errors -fno-elide-constructors") set(CMAKE_CXX_FLAGS_DEBUG "-g -O0") diff --git a/include/neocomm.h b/include/neocomm.h new file mode 100644 index 0000000..0c129e8 --- /dev/null +++ b/include/neocomm.h @@ -0,0 +1,70 @@ +/* + * Copyright (C) 2017 Ortega Froysa, Nicolás + * Author: Ortega Froysa, Nicolás + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +#pragma once + +extern "C" { + +/** + * @brief Initialize a local DHT node. + * + * @param port The port for the node to listen on. + */ +void NeoComm_init(const unsigned short port); + +/** + * @brief Deinitialize the local node. + */ +void NeoComm_deinit(); + +/** + * @brief A foreign node to connect to. + * + * @param address DNS/IP of the node. + * @param port Port of the foreign node. + */ +void NeoComm_connect(const char *address, const unsigned short port); + +/** + * @brief Import a list of nodes from a node file and connect to them. + * + * @param node_file Path to the node list file. + * + * @return 1 upon success, 0 upon failure. Use NeoComm_get_last_error for a + * text version of the error. + */ +int NeoComm_import_nodes(const char *node_file); + +/** + * @brief Export current list of nodes into a file. + * + * @param node_file path to node list file. + * + * @return 1 upon success, 0 upon failure. Use NeoComm_get_last_error for a + * text version of the error. + */ +int NeoComm_export_nodes(const char *node_file); + +/** + * @brief Get the last error that occurred in text. + * + * @return A string with the last error occurred. + */ +const char *NeoComm_get_last_error(); + +} diff --git a/src/node.cpp b/src/node.cpp new file mode 100644 index 0000000..b515c44 --- /dev/null +++ b/src/node.cpp @@ -0,0 +1,36 @@ +/* + * Copyright (C) 2017 Ortega Froysa, Nicolás + * Author: Ortega Froysa, Nicolás + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +#include "neocomm.h" + +#include +#include + +static dht::DhtRunner node; + +void NeoComm_init(const unsigned short port) { + node.run(port, dht::crypto::generateIdentity(), true); +} + +void NeoComm_deinit() { + node.join(); +} + +void NeoComm_connect(const char *address, const unsigned short port) { + node.bootstrap(address, std::to_string(port)); +}