aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--include/sibs/BootstrapConnection.hpp6
-rw-r--r--project.conf2
-rw-r--r--src/BootstrapConnection.cpp19
-rw-r--r--tests/main.cpp29
4 files changed, 45 insertions, 11 deletions
diff --git a/include/sibs/BootstrapConnection.hpp b/include/sibs/BootstrapConnection.hpp
index 8156bd7..790b7ee 100644
--- a/include/sibs/BootstrapConnection.hpp
+++ b/include/sibs/BootstrapConnection.hpp
@@ -5,6 +5,7 @@
#include "PubsubKey.hpp"
#include <mutex>
#include <atomic>
+#include <future>
namespace sibs
{
@@ -41,7 +42,7 @@ namespace sibs
DISABLE_COPY(BootstrapConnection)
public:
// Throws BootstrapConnectionException on error
- BootstrapConnection(const Ipv4 &bootstrapAddress);
+ static std::future<std::unique_ptr<BootstrapConnection>> create(const Ipv4 &bootstrapAddress);
~BootstrapConnection();
// If we are already listening on the key @pubsubKey then the callback function is overwritten
@@ -55,6 +56,9 @@ namespace sibs
std::vector<std::shared_ptr<DirectConnectionPeer>> getPeers();
private:
+ // Throws BootstrapConnectionException on error
+ BootstrapConnection(const Ipv4 &bootstrapAddress);
+
ListenHandle listen(const PubsubKey &pubsubKey, BoostrapConnectionListenCallbackFunc callbackFunc, bool registerCallbackFunc);
void receiveDataFromServer(std::shared_ptr<DirectConnectionPeer> peer, MessageType messageType, const void *data, const usize size);
void receiveDataFromPeer(std::shared_ptr<DirectConnectionPeer> peer, MessageType messageType, const void *data, const usize size);
diff --git a/project.conf b/project.conf
index 5863279..47851f6 100644
--- a/project.conf
+++ b/project.conf
@@ -9,4 +9,4 @@ expose_include_dirs = ["include"]
[dependencies]
udt = "4.11"
-sibs-serializer = "2.0.0"
+sibs-serializer = ">=2.0.0"
diff --git a/src/BootstrapConnection.cpp b/src/BootstrapConnection.cpp
index f74129b..c70fbc9 100644
--- a/src/BootstrapConnection.cpp
+++ b/src/BootstrapConnection.cpp
@@ -54,6 +54,25 @@ namespace sibs
}
}
+ std::future<std::unique_ptr<BootstrapConnection>> BootstrapConnection::create(const Ipv4 &bootstrapAddress)
+ {
+ std::promise<std::unique_ptr<BootstrapConnection>> connectionPromise;
+ std::future<std::unique_ptr<BootstrapConnection>> connectionFuture = connectionPromise.get_future();
+ std::thread([bootstrapAddress](std::promise<std::unique_ptr<BootstrapConnection>> connectionPromise)
+ {
+ try
+ {
+ BootstrapConnection *connection = new BootstrapConnection(bootstrapAddress);
+ connectionPromise.set_value(std::unique_ptr<BootstrapConnection>(connection));
+ }
+ catch(...)
+ {
+ connectionPromise.set_exception(std::current_exception());
+ }
+ }, std::move(connectionPromise)).detach();
+ return connectionFuture;
+ }
+
BootstrapConnection::~BootstrapConnection()
{
alive = false;
diff --git a/tests/main.cpp b/tests/main.cpp
index e262e02..7bb63e2 100644
--- a/tests/main.cpp
+++ b/tests/main.cpp
@@ -14,10 +14,21 @@ int main()
const sibs::PubsubKey key2("zbcdefghcjklmn3pqrs5uvx2z0123F56789", 35);
sibs::BootstrapNode boostrapNode(sibs::Ipv4(nullptr, PORT));
- sibs::BootstrapConnection connection1(sibs::Ipv4("127.0.0.1", PORT));
+ bool connectionFailure = false;
+ try
+ {
+ sibs::BootstrapConnection::create(sibs::Ipv4("127.0.0.1", 2222)).get();
+ }
+ catch(sibs::BootstrapConnectionException &e)
+ {
+ connectionFailure = true;
+ }
+ REQUIRE(connectionFailure);
+
+ std::unique_ptr<sibs::BootstrapConnection> connection1 = sibs::BootstrapConnection::create(sibs::Ipv4("127.0.0.1", PORT)).get();
bool gotData1 = false;
bool gotAsdf1 = false;
- connection1.listen(key, [&gotData1, &gotAsdf1](const sibs::DirectConnectionPeer *peer, const void *data, const sibs::usize size)
+ connection1->listen(key, [&gotData1, &gotAsdf1](const sibs::DirectConnectionPeer *peer, const void *data, const sibs::usize size)
{
if(size == 5 && strncmp((const char*)data, "hello", 5) == 0)
gotData1 = true;
@@ -26,10 +37,10 @@ int main()
return true;
});
- sibs::BootstrapConnection connection2(sibs::Ipv4("127.0.0.1", PORT));
+ std::unique_ptr<sibs::BootstrapConnection> connection2 = sibs::BootstrapConnection::create(sibs::Ipv4("127.0.0.1", PORT)).get();
bool gotData2 = false;
bool gotAsdf2 = false;
- connection2.listen(key, [&gotData2, &gotAsdf2](const sibs::DirectConnectionPeer *peer, const void *data, const sibs::usize size)
+ connection2->listen(key, [&gotData2, &gotAsdf2](const sibs::DirectConnectionPeer *peer, const void *data, const sibs::usize size)
{
if(size == 5 && strncmp((const char*)data, "hello", 5) == 0)
gotData2 = true;
@@ -39,7 +50,7 @@ int main()
});
bool gotListen = false;
- connection1.listen(key2, [&gotListen](const sibs::DirectConnectionPeer *peer, const void *data, const sibs::usize size)
+ connection1->listen(key2, [&gotListen](const sibs::DirectConnectionPeer *peer, const void *data, const sibs::usize size)
{
if(size == 14 && strncmp((const char*)data, "secondListener", 14) == 0)
gotListen = true;
@@ -47,16 +58,16 @@ int main()
});
bool gotListen2 = false;
- connection2.listen(key2, [&gotListen2](const sibs::DirectConnectionPeer *peer, const void *data, const sibs::usize size)
+ connection2->listen(key2, [&gotListen2](const sibs::DirectConnectionPeer *peer, const void *data, const sibs::usize size)
{
if(size == 14 && strncmp((const char*)data, "secondListener", 14) == 0)
gotListen2 = true;
return true;
});
- connection1.put(key, "hello", 5);
- connection2.put(key, "asdf", 4);
- connection1.put(key2, "secondListener", 14);
+ connection1->put(key, "hello", 5);
+ connection2->put(key, "asdf", 4);
+ connection1->put(key2, "secondListener", 14);
std::this_thread::sleep_for(std::chrono::seconds(6));
REQUIRE(gotData1);