-
-
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.
dronecore_server: make a library out of the grpc server
* Required for some platforms (e.g. iOS) * Rename dronecore_server into libdronecore_backend and backend_bin * Use smart pointers and references instead of pointers
- Loading branch information
1 parent
813224a
commit f27110e
Showing
13 changed files
with
237 additions
and
168 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
cmake_minimum_required(VERSION 2.8.12) | ||
cmake_minimum_required(VERSION 3.1) | ||
|
||
project(dronecore-server) | ||
|
||
|
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,110 @@ | ||
#include "backend.h" | ||
|
||
#include <grpc/grpc.h> | ||
#include <grpc++/server.h> | ||
#include <grpc++/server_builder.h> | ||
#include <grpc++/server_context.h> | ||
#include <grpc++/security/server_credentials.h> | ||
|
||
#include "action/actionrpc_impl.h" | ||
#include "core/corerpc_impl.h" | ||
#include "log.h" | ||
#include "mission/missionrpc_impl.h" | ||
#include "telemetry/telemetryrpc_impl.h" | ||
|
||
namespace dronecore { | ||
namespace backend { | ||
|
||
bool DroneCoreBackend::run(const int mavlink_listen_port) | ||
{ | ||
if (!connect_to_vehicle(mavlink_listen_port)) { | ||
return false; | ||
} | ||
|
||
grpc::ServerBuilder builder; | ||
setup_port(builder); | ||
|
||
CoreServiceImpl core(dc); | ||
builder.RegisterService(&core); | ||
|
||
Action action(&dc.device()); | ||
ActionServiceImpl actionService(action); | ||
builder.RegisterService(&actionService); | ||
|
||
Mission mission(&dc.device()); | ||
MissionServiceImpl missionService(mission); | ||
builder.RegisterService(&missionService); | ||
|
||
Telemetry telemetry(&dc.device()); | ||
TelemetryServiceImpl telemetryService(telemetry); | ||
builder.RegisterService(&telemetryService); | ||
|
||
server = builder.BuildAndStart(); | ||
LogInfo() << "Server started"; | ||
server->Wait(); | ||
|
||
return true; | ||
} | ||
|
||
bool DroneCoreBackend::connect_to_vehicle(const int port) | ||
{ | ||
if (!add_udp_connection(port)) { | ||
return false; | ||
} | ||
|
||
log_uuid_on_timeout(); | ||
wait_for_discovery(); | ||
|
||
return true; | ||
} | ||
|
||
bool DroneCoreBackend::add_udp_connection(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; | ||
} | ||
|
||
void DroneCoreBackend::log_uuid_on_timeout() | ||
{ | ||
dc.register_on_timeout([](uint64_t uuid) { | ||
LogInfo() << "Device timed out [UUID: " << uuid << "]"; | ||
}); | ||
} | ||
|
||
void DroneCoreBackend::wait_for_discovery() | ||
{ | ||
LogInfo() << "Waiting to discover device..."; | ||
auto discoveryFuture = wrapped_register_on_discover(); | ||
discoveryFuture.wait(); | ||
LogInfo() << "Device discovered [UUID: " << discoveryFuture.get() << "]"; | ||
} | ||
|
||
std::future<uint64_t> DroneCoreBackend::wrapped_register_on_discover() | ||
{ | ||
auto promise = std::make_shared<std::promise<uint64_t>>(); | ||
auto future = promise->get_future(); | ||
|
||
dc.register_on_discover([this, promise](uint64_t uuid) { | ||
std::call_once(discovery_flag, [promise, uuid]() { | ||
promise->set_value(uuid); | ||
}); | ||
}); | ||
|
||
return future; | ||
} | ||
|
||
void DroneCoreBackend::setup_port(grpc::ServerBuilder &builder) | ||
{ | ||
std::string server_address("0.0.0.0:50051"); | ||
builder.AddListeningPort(server_address, grpc::InsecureServerCredentials()); | ||
LogInfo() << "Server set to listen on " << server_address; | ||
} | ||
|
||
} // namespace backend | ||
} //namespace 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,33 @@ | ||
#include <future> | ||
#include <grpc++/server.h> | ||
#include <memory> | ||
|
||
#include "dronecore.h" | ||
|
||
namespace dronecore { | ||
namespace backend { | ||
|
||
class DroneCoreBackend | ||
{ | ||
public: | ||
DroneCoreBackend() {} | ||
~DroneCoreBackend() {} | ||
|
||
bool run(const int mavlink_listen_port = 14540); | ||
|
||
private: | ||
bool connect_to_vehicle(int port); | ||
bool add_udp_connection(int port); | ||
void log_uuid_on_timeout(); | ||
void wait_for_discovery(); | ||
std::future<uint64_t> wrapped_register_on_discover(); | ||
|
||
bool run_server(); | ||
void setup_port(grpc::ServerBuilder &builder); | ||
|
||
dronecore::DroneCore dc; | ||
std::unique_ptr<grpc::Server> server; | ||
}; | ||
|
||
} // namespace backend | ||
} // namespace 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,30 @@ | ||
set(PROTOC_BINARY ${CMAKE_BINARY_DIR}/../default/third_party/protobuf/bin/protoc) | ||
set(GRPC_CPP_PLUGIN_BINARY ${CMAKE_BINARY_DIR}/../default/third_party/grpc/bin/grpc_cpp_plugin) | ||
|
||
if(NOT EXISTS ${PROTOC_BINARY} OR NOT EXISTS ${GRPC_CPP_PLUGIN_BINARY}) | ||
message(FATAL_ERROR "Could not find 'protoc' or 'grpc_cpp_plugin' in the 'default' build folder. Please build for your host first (`make BUILD_DRONECORESERVER=YES default`).") | ||
endif() | ||
|
||
function(compile_proto_pb COMPONENT_NAME PB_COMPILED_SOURCE) | ||
add_custom_command(OUTPUT ${COMPONENT_NAME}/${COMPONENT_NAME}.pb.cc | ||
COMMAND ${PROTOC_BINARY} | ||
-I ${PROTO_DIR} | ||
--cpp_out=. | ||
${PROTO_DIR}/${COMPONENT_NAME}/${COMPONENT_NAME}.proto | ||
) | ||
|
||
set(PB_COMPILED_SOURCE ${COMPONENT_NAME}/${COMPONENT_NAME}.pb.cc PARENT_SCOPE) | ||
endfunction() | ||
|
||
function(compile_proto_grpc COMPONENT_NAME GRPC_COMPILED_SOURCES) | ||
add_custom_command(OUTPUT ${COMPONENT_NAME}/${COMPONENT_NAME}.grpc.pb.cc | ||
COMMAND ${PROTOC_BINARY} | ||
-I ${PROTO_DIR} | ||
--grpc_out=. | ||
--plugin=protoc-gen-grpc=${GRPC_CPP_PLUGIN_BINARY} | ||
--cpp_out=. | ||
${PROTO_DIR}/${COMPONENT_NAME}/${COMPONENT_NAME}.proto | ||
) | ||
|
||
set(GRPC_COMPILED_SOURCE ${COMPONENT_NAME}/${COMPONENT_NAME}.grpc.pb.cc PARENT_SCOPE) | ||
endfunction() |
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
Oops, something went wrong.