-
-
Notifications
You must be signed in to change notification settings - Fork 504
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
backend: add connection_initiator and corresponding tests
- Loading branch information
1 parent
614a0c6
commit 2ac2e5c
Showing
9 changed files
with
239 additions
and
82 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
#include <gmock/gmock.h> | ||
|
||
#include "connection_result.h" | ||
|
||
namespace dronecore { | ||
namespace testing { | ||
|
||
typedef std::function<void(uint64_t uuid)> event_callback_t; | ||
|
||
class MockDroneCore | ||
{ | ||
public: | ||
MOCK_CONST_METHOD1(add_udp_connection, ConnectionResult(int local_port_number)); | ||
MOCK_CONST_METHOD1(register_on_discover, void(event_callback_t)); | ||
MOCK_CONST_METHOD1(register_on_timeout, void(event_callback_t)); | ||
}; | ||
|
||
} // testing | ||
} // dronecore |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
#include <future> | ||
|
||
#include "connection_result.h" | ||
#include "log.h" | ||
|
||
namespace dronecore { | ||
namespace backend { | ||
|
||
template <typename DroneCore> | ||
class ConnectionInitiator | ||
{ | ||
public: | ||
ConnectionInitiator() {} | ||
~ConnectionInitiator() {} | ||
|
||
bool start(DroneCore &dc, const int port) | ||
{ | ||
init_mutex(); | ||
init_timeout_logging(dc); | ||
|
||
_discovery_future = wrapped_register_on_discover(dc); | ||
|
||
if (!add_udp_connection(dc, port)) { | ||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
|
||
void wait() | ||
{ | ||
_discovery_future.wait(); | ||
} | ||
|
||
private: | ||
void init_mutex() | ||
{ | ||
_discovery_promise = std::make_shared<std::promise<uint64_t>>(); | ||
} | ||
|
||
void init_timeout_logging(DroneCore &dc) const | ||
{ | ||
dc.register_on_timeout([](uint64_t uuid) { | ||
LogInfo() << "Device timed out [UUID: " << uuid << "]"; | ||
}); | ||
} | ||
|
||
bool add_udp_connection(DroneCore &dc, const int port) | ||
{ | ||
dronecore::ConnectionResult connection_result = dc.add_udp_connection(port); | ||
|
||
if (connection_result != ConnectionResult::SUCCESS) { | ||
LogErr() << "Connection failed: " << connection_result_str(connection_result); | ||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
|
||
std::future<uint64_t> wrapped_register_on_discover(DroneCore &dc) | ||
{ | ||
auto future = _discovery_promise->get_future(); | ||
|
||
LogInfo() << "Waiting to discover device..."; | ||
|
||
dc.register_on_discover([this](uint64_t uuid) { | ||
std::call_once(_discovery_flag, [this, uuid]() { | ||
LogInfo() << "Device discovered [UUID: " << uuid << "]"; | ||
_discovery_promise->set_value(uuid); | ||
}); | ||
}); | ||
|
||
return future; | ||
} | ||
|
||
std::once_flag _discovery_flag; | ||
std::shared_ptr<std::promise<uint64_t>> _discovery_promise; | ||
std::future<uint64_t> _discovery_future; | ||
}; | ||
|
||
} // backend | ||
} // dronecore |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
cmake_minimum_required(VERSION 3.1) | ||
|
||
add_executable(unit_tests_backend | ||
connection_initiator_test.cpp | ||
) | ||
|
||
set_target_properties(unit_tests_backend PROPERTIES COMPILE_FLAGS ${warnings}) | ||
|
||
target_include_directories(unit_tests_backend | ||
PRIVATE | ||
${CMAKE_CURRENT_SOURCE_DIR}/../src | ||
) | ||
|
||
target_link_libraries(unit_tests_backend | ||
backend | ||
gtest | ||
gmock | ||
gmock_main | ||
) | ||
|
||
add_test(unit_tests unit_tests_backend) |
Oops, something went wrong.