diff --git a/net/cpp/include/ftl/net/universe.hpp b/net/cpp/include/ftl/net/universe.hpp index b29dc10ac5f2c39964ba43b54ba8cfa3bbe9e0a4..728421c47b244656f004c9e398298b0cef727c39 100644 --- a/net/cpp/include/ftl/net/universe.hpp +++ b/net/cpp/include/ftl/net/universe.hpp @@ -56,7 +56,7 @@ class Universe { * * @param addr URI giving protocol, interface and port */ - bool connect(const std::string &addr); + Peer *connect(const std::string &addr); int numberOfPeers() const { return peers_.size(); } diff --git a/net/cpp/src/peer.cpp b/net/cpp/src/peer.cpp index 25a1721cb1af4efbb1341e2c43aeb28e61910f30..2032b5acfc07a0dd52da55e238d3953ce59ef29d 100644 --- a/net/cpp/src/peer.cpp +++ b/net/cpp/src/peer.cpp @@ -178,7 +178,8 @@ Peer::Peer(const char *pUri, Dispatcher *d) : uri_(pUri) { scheme_ = uri.getProtocol(); if (uri.getProtocol() == URI::SCHEME_TCP) { sock_ = tcpConnect(uri); - status_ = kConnecting; + if (sock_ != INVALID_SOCKET) status_ = kConnecting; + else status_ = kReconnecting; } else if (uri.getProtocol() == URI::SCHEME_WS) { LOG(INFO) << "Websocket connect " << uri.getPath(); sock_ = tcpConnect(uri); diff --git a/net/cpp/src/universe.cpp b/net/cpp/src/universe.cpp index f6d7e2acba34e6fde82b07a6db7064555512c303..421f5eebe90d6cabd6758cd99c0ceae9d99332fd 100644 --- a/net/cpp/src/universe.cpp +++ b/net/cpp/src/universe.cpp @@ -66,9 +66,9 @@ bool Universe::listen(const string &addr) { return l->isListening(); } -bool Universe::connect(const string &addr) { +Peer *Universe::connect(const string &addr) { auto p = new Peer(addr.c_str(), &disp_); - if (!p) return false; + if (!p) return nullptr; if (p->status() != Peer::kInvalid) { unique_lock<mutex> lk(net_mutex_); @@ -81,7 +81,7 @@ bool Universe::connect(const string &addr) { peer_ids_[p.id()] = &p; }); - return p->status() == Peer::kConnecting; + return p; } int Universe::_setDescriptors() { diff --git a/net/cpp/test/net_integration.cpp b/net/cpp/test/net_integration.cpp index 34f01215a63d2ac371d51854b576a68f875ad454..f6e4de548701237d7d2d031bd177aa148aba1e1b 100644 --- a/net/cpp/test/net_integration.cpp +++ b/net/cpp/test/net_integration.cpp @@ -17,28 +17,31 @@ TEST_CASE("Universe::connect()", "[net]") { Universe b; a.listen("tcp://localhost:7077"); - sleep_for(milliseconds(100)); + //sleep_for(milliseconds(100)); SECTION("valid tcp connection using ipv4") { - REQUIRE( b.connect("tcp://127.0.0.1:7077") ); + auto p = b.connect("tcp://127.0.0.1:7077"); + REQUIRE( p ); - sleep_for(milliseconds(200)); + while (!p->isConnected()) sleep_for(milliseconds(20)); REQUIRE( a.numberOfPeers() == 1 ); REQUIRE( b.numberOfPeers() == 1 ); } SECTION("valid tcp connection using hostname") { - REQUIRE( b.connect("tcp://localhost:7077") ); + auto p = b.connect("tcp://localhost:7077"); + REQUIRE( p ); - sleep_for(milliseconds(200)); + while (!p->isConnected()) sleep_for(milliseconds(20)); REQUIRE( a.numberOfPeers() == 1 ); REQUIRE( b.numberOfPeers() == 1 ); } SECTION("invalid protocol") { - REQUIRE( !b.connect("http://127.0.0.1:7077") ); + auto p = b.connect("http://127.0.0.1:7077"); + REQUIRE( !p->isValid() ); sleep_for(milliseconds(100));