Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/code style #27

Merged
merged 2 commits into from
Sep 16, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
Boost Software License - Version 1.0 - August 17th, 2003

Permission is hereby granted, free of charge, to any person or organization
obtaining a copy of the software and accompanying documentation covered by
this license (the "Software") to use, reproduce, display, distribute,
execute, and transmit the Software, and to prepare derivative works of the
Software, and to permit third-parties to whom the Software is furnished to
do so, all subject to the following:

The copyright notices in the Software and this entire statement, including
the above license grant, this restriction and the following disclaimer,
must be included in all copies of the Software, in whole or in part, and
all derivative works of the Software, unless such copies or derivative
works are solely in the form of machine-executable object code generated by
a source language processor.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.

---------------------------------------------------------------------------
Note:
Individual files contain the following tag instead of the full license text.

SPDX-License-Identifier: BSL-1.0

This enables machine processing of license information based on the SPDX
License Identifiers that are here available: http://spdx.org/licenses/
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ Initialization:
// If an error occurs, this will throw.
// Following runtime errors will be reported
// through an std::error_code.
kademlia::session s{ initial_peer };
kademlia::Session s{ initial_peer };

// Run the library main loop in a dedicated thread.
auto main_loop_result = std::async( std::launch::async
Expand All @@ -46,7 +46,7 @@ Searching for value associated with "key1":
};

// Schedule an asynchronous load.
s.async_load( "key1", on_load );
s.asyncLoad( "key1", on_load );

// [...]
```
Expand All @@ -64,7 +64,7 @@ Saving a data into the table is similar:
}

// And perform the saving.
s.async_save( "key2", data, on_save );
s.asyncSave( "key2", data, on_save );

// [...]
```
Expand Down
65 changes: 27 additions & 38 deletions src/kademlia/SessionImpl.cpp → doc/Acknowledgements.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,27 @@
// Copyright (c) 2013-2014, David Keller
// All rights reserved.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">

<html>
<head>
<title>POCO Kademlia Library - Acknowledgements</title>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1">
<meta NAME="author" CONTENT="Applied Informatics">
<meta NAME="copyright" CONTENT="Applied Informatics">
<meta NAME="language" CONTENT="en">
<meta NAME="date" CONTENT="2005-02-21">
</head>

<body text="black" bgcolor="white">

<h1>Acknowledgements</h1>

<p>Portions of the POCO Kademlia Library utilize the
following copyrighted material, the use of which is hereby acknowledged.</p>

<h3>C++11 distributed hash table library</h3>

<pre>Copyright (c) 2013-2014, David Keller

All rights reserved.
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
Expand All @@ -22,40 +44,7 @@
// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
</pre>


#include "SessionImpl.h"
#include "kademlia/error.hpp"
#include "error_impl.hpp"
#include "Poco/Timespan.h"

namespace kademlia {
namespace detail {


SessionImpl::SessionImpl(endpoint const& ipv4, endpoint const& ipv6, int ms):
_ioService(Poco::Timespan(Poco::Timespan::TimeDiff(ms)*1000)),
_engine{ _ioService, ipv4, ipv6 }
{ }

SessionImpl::SessionImpl(endpoint const& initPeer, endpoint const& ipv4, endpoint const& ipv6, int ms):
_ioService(Poco::Timespan(Poco::Timespan::TimeDiff(ms)*1000)),
_engine{ _ioService, initPeer, ipv4, ipv6 }
{ }

std::error_code SessionImpl::run()
{
Poco::FastMutex::ScopedLock l(_mutex);
_ioService.run();
return make_error_code(RUN_ABORTED);
}


void SessionImpl::abort()
{
_ioService.stop();
_ioService.wakeUp();
}

} // namespace detail
} // namespace kademlia
</body>
</html>
9 changes: 5 additions & 4 deletions examples/bootstrap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@

#include <kademlia/endpoint.hpp>
#include <kademlia/error.hpp>
#include <kademlia/first_session.hpp>
#include "kademlia/Session.h"

namespace k = kademlia;
using Session = Kademlia::Session;
using Endpoint = kademlia::endpoint;

int main(int argc, char** argv )
{
Expand All @@ -25,7 +26,7 @@ int main(int argc, char** argv )
std::uint16_t const port = std::atoi( argv[1] );

// Create the session
k::first_session session{k::endpoint{"0.0.0.0", port}, k::endpoint{"::", port}};
Session session{Endpoint{"0.0.0.0", port}, Endpoint{"::", port}};

// Wait for exit request
std::cout << "Press any key to exit" << std::endl;
Expand All @@ -36,6 +37,6 @@ int main(int argc, char** argv )

// Wait for the main loop thread termination
auto failure = session.wait();
if ( failure != k::RUN_ABORTED )
if ( failure != kademlia::RUN_ABORTED )
std::cerr << failure.message() << std::endl;
}
23 changes: 12 additions & 11 deletions examples/cli.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@
#include <thread>

#include <kademlia/endpoint.hpp>
#include <kademlia/session.hpp>
#include "kademlia/Session.h"
#include <kademlia/error.hpp>

namespace k = kademlia;
using Session = Kademlia::Session;
using Endpoint = kademlia::endpoint;

namespace {

Expand All @@ -28,10 +29,10 @@ std::vector<std::string> split(std::string const& line)
return std::vector<std::string>{ iterator{ in }, iterator{} };
}

void load(k::session & session, std::string const& key)
void load(Session & session, std::string const& key)
{
std::vector<uint8_t> key_vec(key.begin(), key.end());
auto on_load = [key] (std::error_code const& error, k::session::data_type const& data)
auto on_load = [key] (std::error_code const& error, Session::DataType const& data)
{
if (error)
std::cerr << "Failed to load \"" << key << "\", error: " << error.message() << std::endl;
Expand All @@ -42,10 +43,10 @@ void load(k::session & session, std::string const& key)
}
};

session.async_load(key_vec, std::move(on_load));
session.asyncLoad(key_vec, std::move(on_load));
}

void save(k::session & session, std::string const& key, std::string const& val)
void save(Session & session, std::string const& key, std::string const& val)
{
std::vector<uint8_t> key_vec(key.begin(), key.end());
std::vector<uint8_t> val_vec(val.begin(), val.end());
Expand All @@ -57,7 +58,7 @@ void save(k::session & session, std::string const& key, std::string const& val)
std::cout << "Saved \"" << key << "\"" << std::endl;
};

session.async_save(key_vec, val_vec, std::move(on_save));
session.asyncSave(key_vec, val_vec, std::move(on_save));
}

void print_interactive_help()
Expand Down Expand Up @@ -90,9 +91,9 @@ int main(int argc, char** argv)
auto boot_port = boot_ep.substr(sep_idx+1);

// Create the session (runs in its own thread)
k::session session{ k::endpoint{ boot_addr, boot_port }
, k::endpoint{ "0.0.0.0", port }
, k::endpoint{ "::", port } };
Session session{ Endpoint{ boot_addr, boot_port }
, Endpoint{ "0.0.0.0", port }
, Endpoint{ "::", port } };

// Parse stdin until EOF (CTRL-D in Unix, CTRL-Z-Enter on Windows))
std::cout << "Enter \"help\" to see available actions" << std::endl;
Expand Down Expand Up @@ -131,7 +132,7 @@ int main(int argc, char** argv)

// Wait for the session termination
auto failure = session.wait();
if (failure != k::RUN_ABORTED)
if (failure != kademlia::RUN_ABORTED)
std::cerr << failure.message() << std::endl;
std::cout << "Goodbye!" << std::endl;
}
16 changes: 8 additions & 8 deletions examples/stress.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@
#include <iostream>

#include "kademlia/endpoint.hpp"
#include "kademlia/session.hpp"
#include "kademlia/first_session.hpp"
#include "kademlia/Session.h"
#include "kademlia/error.hpp"
#include "kademlia/detail/Util.h"
#include "Poco/Timestamp.h"
Expand All @@ -24,6 +23,7 @@ using Poco::Timespan;
using Poco::Logger;
using Poco::LogStream;
using Poco::Net::SocketAddress;
using Session = Kademlia::Session;


namespace {
Expand All @@ -38,7 +38,7 @@ void load(S& session, std::string const& key)
{
std::vector<uint8_t> key_vec(key.begin(), key.end());
Timestamp ts;
auto on_load = [key, ts] (std::error_code const& error, k::session::data_type const& data)
auto on_load = [key, ts] (std::error_code const& error, Session::DataType const& data)
{
if (error)
_logger.error() << "Failed to load \"" << key << "\", error: " << error.message() << std::endl;
Expand All @@ -51,7 +51,7 @@ void load(S& session, std::string const& key)
}
};

session.async_load(key_vec, std::move(on_load));
session.asyncLoad(key_vec, std::move(on_load));
_logger.debug() << "Async loading \"" << key << "\" ..." << std::endl;
}

Expand All @@ -73,7 +73,7 @@ void save(S& session, std::string const& key, std::string const& val)
}
};

session.async_save(key_vec, val_vec, std::move(on_save));
session.asyncSave(key_vec, val_vec, std::move(on_save));
_logger.debug() << "Async saving \"" << key << "\": \"" << val << "\"" << std::endl;
}

Expand Down Expand Up @@ -105,17 +105,17 @@ int main(int argc, char** argv)
int reps = 50;
pool.addCapacity(reps*2);

k::first_session firstSession{ k::endpoint{bootAddr4, bootPort4}, k::endpoint{bootAddr6, bootPort6} };
Session firstSession{ k::endpoint{bootAddr4, bootPort4}, k::endpoint{bootAddr6, bootPort6} };
_logger.information() << "bootstrap session listening on " << bootAddr4 << ':' << bootPort4 << ", " <<
'[' << bootAddr6 << "]:" << bootPort6 << std::endl;

uint16_t sessPort4 = kd::getAvailablePort(SocketAddress::IPv4, bootPort4 + 1);
uint16_t sessPort6 = kd::getAvailablePort(SocketAddress::IPv6, bootPort6 + 1);

std::vector<k::session*> sessions;
std::vector<Session*> sessions;
for (int i = 0; i < reps; ++i)
{
sessions.push_back(new k::session{ k::endpoint{ "127.0.0.1", bootPort4 }
sessions.push_back(new Session{ k::endpoint{ "127.0.0.1", bootPort4 }
, k::endpoint{ "127.0.0.1", sessPort4 }
, k::endpoint{ "::1", sessPort6} });
_logger.information() << "peer session connected to 127.0.0.1:" << bootPort4 <<
Expand Down
93 changes: 93 additions & 0 deletions include/kademlia/Session.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
//
// Session.h
//
// Library: Kademlia
// Package: DHT
// Module: Session
//
// Definition of the Session class.
//
// Copyright (c) 2021, Aleph ONE Software Engineering and Contributors.
//
// SPDX-License-Identifier: BSL-1.0
//

#ifndef KADEMLIA_SESSION_H
#define KADEMLIA_SESSION_H


#include <utility>
#include "Poco/ActiveMethod.h"
#include "Poco/Mutex.h"
#include "Poco/Net/DatagramSocket.h"
#include "Poco/Net/SocketProactor.h"
#include "kademlia/endpoint.hpp"


namespace Kademlia {

class EngineImpl;

class KADEMLIA_SYMBOL_VISIBILITY Session
{
public:
using DataType = std::vector<std::uint8_t>;
using KeyType = std::vector<std::uint8_t>;
using Endpoint = kademlia::endpoint;
using SaveHandlerType = std::function<void (const std::error_code&)>;
using LoadHandlerType = std::function<void (const std::error_code&, const DataType& data)>;

static const std::uint16_t DEFAULT_PORT;

Session(Endpoint const& ipv4 = {"0.0.0.0", DEFAULT_PORT},
Endpoint const& ipv6 = {"::", DEFAULT_PORT}, int ms = 300);

Session(Endpoint const& initPeer, Endpoint const& ipv4, Endpoint const& ipv6, int ms = 300);

~Session();

void asyncSave(KeyType const& key, DataType const& data, SaveHandlerType handler);

template<typename K, typename D>
void asyncSave(K const& key, D const& data, SaveHandlerType && handler)
{
asyncSave(KeyType(std::begin(key), std::end(key)),
DataType(std::begin(data), std::end(data)), std::move(handler));
}

void asyncLoad(KeyType const& key, LoadHandlerType handler );

template<typename K>
void asyncLoad(K const& key, LoadHandlerType && handler)
{
asyncLoad(KeyType(std::begin(key), std::end(key)), std::move(handler));
}

std::error_code run();

void abort();
std::error_code wait();

private:
using RunType = Poco::ActiveMethod<std::error_code, void, Session>;
using Result = Poco::ActiveResult<std::error_code>;
using ResultPtr = std::unique_ptr<Result>;

Result& result()
{
if (!_pResult)
_pResult.reset(new Result(this->_runMethod()));
return *_pResult;
}

RunType _runMethod;
Poco::Net::SocketProactor _ioService;
std::unique_ptr<EngineImpl> _pEngine;
ResultPtr _pResult;
Poco::FastMutex _mutex;
};

} // namespace kademlia

#endif

Loading