-
-
Notifications
You must be signed in to change notification settings - Fork 503
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
e57829d
commit 5e17e66
Showing
12 changed files
with
224 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
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,104 @@ | ||
#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::backend { | ||
|
||
int DroneCoreBackend::run(const int mavlink_listen_port) | ||
{ | ||
if (int returnCode = connectToVehicle(mavlink_listen_port) != 0) { | ||
return returnCode; | ||
} | ||
|
||
grpc::ServerBuilder builder; | ||
setupPort(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 0; | ||
} | ||
|
||
int DroneCoreBackend::connectToVehicle(const int port) | ||
{ | ||
if (!addUdpConnection(port)) { | ||
return 1; | ||
} | ||
|
||
logUUIDOnTimeout(); | ||
waitForDiscovery(); | ||
} | ||
|
||
bool DroneCoreBackend::addUdpConnection(const int port) | ||
{ | ||
dronecore::DroneCore::ConnectionResult connection_result = dc.add_udp_connection(port); | ||
|
||
if (connection_result != DroneCore::ConnectionResult::SUCCESS) { | ||
LogErr() << "Connection failed: " << DroneCore::connection_result_str(connection_result); | ||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
|
||
void DroneCoreBackend::logUUIDOnTimeout() | ||
{ | ||
dc.register_on_timeout([](uint64_t uuid) { | ||
LogInfo() << "Device timed out [UUID: " << uuid << "]"; | ||
}); | ||
} | ||
|
||
void DroneCoreBackend::waitForDiscovery() | ||
{ | ||
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([promise](uint64_t uuid) { | ||
promise->set_value(uuid); | ||
}); | ||
|
||
return future; | ||
} | ||
|
||
void DroneCoreBackend::setupPort(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 dronecore::backend |
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,31 @@ | ||
#include <future> | ||
#include <grpc++/server.h> | ||
#include <memory> | ||
|
||
#include "dronecore.h" | ||
|
||
namespace dronecore::backend { | ||
|
||
class DroneCoreBackend | ||
{ | ||
public: | ||
DroneCoreBackend() {} | ||
~DroneCoreBackend() {} | ||
|
||
int run(const int mavlink_listen_port = 14540); | ||
|
||
private: | ||
int connectToVehicle(int port); | ||
bool addUdpConnection(int port); | ||
void logUUIDOnTimeout(); | ||
void waitForDiscovery(); | ||
std::future<uint64_t> wrapped_register_on_discover(); | ||
|
||
int runServer(); | ||
void setupPort(grpc::ServerBuilder &builder); | ||
|
||
dronecore::DroneCore dc; | ||
std::unique_ptr<grpc::Server> server; | ||
}; | ||
|
||
} // namespace dronecore::backend |
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
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,72 +1,7 @@ | ||
#include <iostream> | ||
#include <fstream> | ||
#include <thread> | ||
#include <chrono> | ||
#include <cstdint> | ||
#include <future> | ||
|
||
#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 "dronecore.h" | ||
#include "log.h" | ||
#include "action/actionrpc_impl.h" | ||
#include "core/core.grpc.pb.h" | ||
#include "core/corerpc_impl.h" | ||
#include "mission/missionrpc_impl.h" | ||
#include "telemetry/telemetryrpc_impl.h" | ||
|
||
using grpc::Server; | ||
using grpc::ServerBuilder; | ||
using grpc::ServerContext; | ||
using grpc::ServerReader; | ||
using grpc::ServerReaderWriter; | ||
using grpc::Status; | ||
|
||
using namespace dronecore; | ||
using namespace std::placeholders; | ||
|
||
static DroneCore dc; | ||
|
||
template<typename T> ::grpc::Service *createInstances(DroneCore *dc_obj) { return new T(dc_obj); } | ||
|
||
int RunServer() | ||
{ | ||
DroneCore::ConnectionResult connection_result = dc.add_udp_connection(14540); | ||
|
||
if (connection_result != DroneCore::ConnectionResult::SUCCESS) { | ||
LogErr() << "Connection failed: " << DroneCore::connection_result_str(connection_result); | ||
return 1; | ||
} | ||
|
||
LogInfo() << "Waiting to discover device..."; | ||
dc.register_on_discover([](uint64_t uuid) { | ||
LogInfo() << "Device discovered [UUID: " << uuid << "]"; | ||
}); | ||
dc.register_on_timeout([](uint64_t uuid) { | ||
LogInfo() << "Device timed out [UUID: " << uuid << "]"; | ||
}); | ||
|
||
std::string server_address("0.0.0.0:50051"); | ||
|
||
ServerBuilder builder; | ||
builder.AddListeningPort(server_address, grpc::InsecureServerCredentials()); | ||
|
||
CoreServiceImpl service(&dc); | ||
builder.RegisterService(&service); | ||
builder.RegisterService(createInstances<ActionServiceImpl>(&dc)); | ||
builder.RegisterService(createInstances<TelemetryServiceImpl>(&dc)); | ||
builder.RegisterService(createInstances<MissionServiceImpl>(&dc)); | ||
|
||
std::unique_ptr<Server> server(builder.BuildAndStart()); | ||
LogInfo() << "Server listening on " << server_address; | ||
server->Wait(); | ||
} | ||
#include "backend.h" | ||
|
||
int main(int argc, char **argv) | ||
{ | ||
return RunServer(); | ||
dronecore::backend::DroneCoreBackend backend; | ||
backend.run(); | ||
} |
Oops, something went wrong.