diff options
| author | Dennis Brentjes <d.brentjes@gmail.com> | 2016-09-28 10:55:58 +0200 |
|---|---|---|
| committer | Dennis Brentjes <d.brentjes@gmail.com> | 2016-09-28 10:55:58 +0200 |
| commit | 85d25eebd38bb278ad598a291a007938854945a4 (patch) | |
| tree | b6f26a8f5a301c9a18d7ab6de5805de7d660cade /node | |
| parent | fa0b4963e977c59586b41e146ea13f44bda714ab (diff) | |
| download | cmix-85d25eebd38bb278ad598a291a007938854945a4.tar.gz cmix-85d25eebd38bb278ad598a291a007938854945a4.tar.bz2 cmix-85d25eebd38bb278ad598a291a007938854945a4.zip | |
Made changes so we can have a 1 Node cmix network.
Diffstat (limited to 'node')
| -rw-r--r-- | node/CMakeLists.txt | 1 | ||||
| -rw-r--r-- | node/main.cpp | 51 | ||||
| -rw-r--r-- | node/nextnode.cpp | 27 | ||||
| -rw-r--r-- | node/nextnode.hpp | 4 | ||||
| -rw-r--r-- | node/node.cpp | 52 | ||||
| -rw-r--r-- | node/node.hpp | 17 |
6 files changed, 141 insertions, 11 deletions
diff --git a/node/CMakeLists.txt b/node/CMakeLists.txt index a4e1f09..c3c8e46 100644 --- a/node/CMakeLists.txt +++ b/node/CMakeLists.txt @@ -17,4 +17,5 @@ target_link_libraries(node PRIVATE log PRIVATE cmix PRIVATE cmix-network + PRIVATE cmix-protobuf ) diff --git a/node/main.cpp b/node/main.cpp index 970e150..1327ac5 100644 --- a/node/main.cpp +++ b/node/main.cpp @@ -1,4 +1,53 @@ -int main() { +#include "node.hpp" +#include "uriparser.hpp" +#include "logging.hpp" +#include <boost/program_options.hpp> + +#include <iostream> + +int main(int argc, char* argv[]) { + namespace po = boost::program_options; + + init_logging(boost::log::trivial::severity_level::trace); + + po::options_description desc("Allowed options"); + desc.add_options() + ("help,h", "produce help message.") + ("port,p", po::value<uint16_t>()->default_value(9200), "Set listening port.") + ("enable_v4", po::value<bool>()->default_value(true), "Enable/disable ipv4 accept support.") + ("interface4,4", po::value<std::string>()->default_value("0.0.0.0"), "Set the ipv4 address to listen on.") + ("enable_v6", po::value<bool>()->default_value(true), "Enable/disable ipv6 accept support.") + ("interface6,6", po::value<std::string>()->default_value("::"), "Set the ipv6 address to listen on.") + ("next_node,n", po::value<std::string>()->required(), "The address of the next node in the network") + ("first,f", "This is the first node and will be the communication point for the clients.") + ; + + po::variables_map vm; + po::store(po::parse_command_line(argc, argv, desc), vm); + po::notify(vm); + + if (vm.count("help")) { + std::cout << desc << "\n"; + return 0; + } + + bool en4 = vm["enable_v4"].as<bool>(); + std::string if4 = vm["interface4"].as<std::string>(); + bool en6 = vm["enable_v6"].as<bool>(); + std::string if6 = vm["interface6"].as<std::string>(); + uint16_t port = vm["port"].as<uint16_t>(); + + ListenSettings lsettings{en4, if4, en6, if6, port}; + + bool is_first = bool(vm.count("first")); + std::string next_node = vm["next_node"].as<std::string>(); + + Uri uri = parse_uri(next_node); + + NodeNetworkSettings nsettings{is_first, uri.host, uri.port}; + + Node node(lsettings, nsettings); + node.run(); } diff --git a/node/nextnode.cpp b/node/nextnode.cpp index 3045462..d38200d 100644 --- a/node/nextnode.cpp +++ b/node/nextnode.cpp @@ -1,7 +1,34 @@ #include "nextnode.hpp" +#include "connect.hpp" + +#include "logging.hpp" + using namespace boost::asio::ip; NextNode::NextNode(tcp::socket&& socket) : Client(std::move(socket)) {} + +void NextNode::send(std::string message) +{ + auto handler = [](boost::system::error_code const& ec, std::size_t bytes_transferred) { + BOOST_LOG_TRIVIAL(trace) << "sent message"; + if(ec) { + BOOST_LOG_TRIVIAL(fatal) << ec; + throw std::runtime_error("unable to send message"); + } + }; + + socket.async_send(boost::asio::buffer(message), 0, handler); +} + +void NextNode::connect(std::string next_host, std::string next_port, std::function<void ()> on_connect) +{ + async_connect(socket, next_host, next_port, on_connect); +} + +void NextNode::close() +{ + socket.close(); +} diff --git a/node/nextnode.hpp b/node/nextnode.hpp index c1ce6a1..42206bb 100644 --- a/node/nextnode.hpp +++ b/node/nextnode.hpp @@ -8,5 +8,9 @@ class NextNode : public Client { public: NextNode(boost::asio::ip::tcp::socket&& socket); + + void send(std::string message); + void connect(std::string next_host, std::string next_port, std::function<void()> on_connect); + void close(); }; diff --git a/node/node.cpp b/node/node.cpp index 4b4bc2b..b33f8a5 100644 --- a/node/node.cpp +++ b/node/node.cpp @@ -1,17 +1,31 @@ #include "node.hpp" -#include "connect.hpp" + +#include "logging.hpp" + +using namespace boost::asio::ip; Node::Node(ListenSettings const& listen_settings, NodeNetworkSettings network_settings) : io_service() , server(io_service, listen_settings, [this](boost::asio::ip::tcp::socket&& socket){accept_handler(std::move(socket));}) , clients() -, next_node(connect(network_settings.next_host, network_settings.next_port, io_service)) +, network_settings(network_settings) +, next_node(tcp::socket(io_service)) , api(get_curve25519_implementation()) , keypair(api.create_key_pair()) +, network_pub_key() { - if(network_settings.is_first) { - start_initialisation(); - } + auto on_connect = [this, network_settings](){ + BOOST_LOG_TRIVIAL(trace) << "is first: " << std::boolalpha << network_settings.is_first; + if(network_settings.is_first) { + start_initialisation(); + } + }; + + next_node.connect(network_settings.next_host, network_settings.next_port, on_connect); +} + +Node::~Node() { + api.free_key_pair(keypair); } void Node::accept_handler(boost::asio::ip::tcp::socket&& socket) @@ -24,6 +38,32 @@ void Node::accept_handler(boost::asio::ip::tcp::socket&& socket) clients.erase(it); } ); +} + +void Node::start_initialisation() { + initialization init; + init.set_public_share(keypair.pub, keypair.pub_len); + + BOOST_LOG_TRIVIAL(trace) << "length of keypair.pub: " << keypair.pub_len; + + std::string message; + init.SerializeToString(&message); + BOOST_LOG_TRIVIAL(trace) << init.DebugString(); + next_node.send(message); + + auto f = [this](std::vector<uint8_t> bytes) { + network_pub_key = bytes; + + if(network_settings.is_first) { + start_precomputation(); + } + }; + + BOOST_LOG_TRIVIAL(trace) << "number of clients: " << clients.size(); + for(auto&& client : clients) { + client.receive(f); + } +} - clients.back().receive(); +void Node::start_precomputation() { } diff --git a/node/node.hpp b/node/node.hpp index 9e321df..5668dec 100644 --- a/node/node.hpp +++ b/node/node.hpp @@ -7,6 +7,8 @@ #include "api.h" #include "curve25519.h" +#include "cmix.pb.h" + #include <boost/asio/io_service.hpp> #include <list> @@ -21,20 +23,27 @@ class Node { boost::asio::io_service io_service; Server server; - std::list<NodeClient> clients; + std::list<Client> clients; + + NodeNetworkSettings network_settings; NextNode next_node; Api api; KeyPair keypair; + std::vector<uint8_t> network_pub_key; void accept_handler(boost::asio::ip::tcp::socket&& socket); - void start_initialisation() { - - } + void start_precomputation(); + void start_initialisation(); public: Node(ListenSettings const& listen_settings, NodeNetworkSettings network_settings); + ~Node(); + + void run() { + io_service.run(); + } }; |
