From 8df7dc71d29524cd9c193d3670f56ad11ef2c4b5 Mon Sep 17 00:00:00 2001 From: Shital Shah Date: Wed, 28 Jun 2017 04:00:12 -0700 Subject: [PATCH] remove hardcoded 127.0.0.1 as this doesn't work well with docker --- AirLib/include/rpc/RpcLibClient.hpp | 7 ++++++- AirLib/src/rpc/RpcLibClient.cpp | 11 +++++++++++ AirLib/src/rpc/RpcLibServer.cpp | 9 ++++++++- DroneShell/src/main.cpp | 19 +++++++++++++------ .../AirSim/Source/MultiRotorConnector.cpp | 3 +++ .../AirSim/Source/SimMode/SimModeBase.cpp | 5 ++++- 6 files changed, 45 insertions(+), 9 deletions(-) diff --git a/AirLib/include/rpc/RpcLibClient.hpp b/AirLib/include/rpc/RpcLibClient.hpp index e8511cd657..00e8762f96 100644 --- a/AirLib/include/rpc/RpcLibClient.hpp +++ b/AirLib/include/rpc/RpcLibClient.hpp @@ -15,7 +15,12 @@ namespace msr { namespace airlib { class RpcLibClient { public: - RpcLibClient(const string& ip_address = "127.0.0.1", uint16_t port = 41451); + enum class ConnectionState { + Initial, Connected, Disconnected, Reset, Unknown + }; +public: + RpcLibClient(const string& ip_address = "localhost", uint16_t port = 41451); + ConnectionState getConnectionState(); bool ping(); bool armDisarm(bool arm); void setOffboardMode(bool is_set); diff --git a/AirLib/src/rpc/RpcLibClient.cpp b/AirLib/src/rpc/RpcLibClient.cpp index d77f46b4a8..ffd8ec3ee0 100644 --- a/AirLib/src/rpc/RpcLibClient.cpp +++ b/AirLib/src/rpc/RpcLibClient.cpp @@ -54,6 +54,17 @@ bool RpcLibClient::ping() { return pimpl_->client.call("ping").as(); } +RpcLibClient::ConnectionState RpcLibClient::getConnectionState() +{ + switch (pimpl_->client.get_connection_state()) { + case rpc::client::connection_state::connected: return ConnectionState::Connected; + case rpc::client::connection_state::disconnected: return ConnectionState::Disconnected; + case rpc::client::connection_state::initial: return ConnectionState::Initial; + case rpc::client::connection_state::reset: return ConnectionState::Reset; + default: + return ConnectionState::Unknown; + } +} bool RpcLibClient::armDisarm(bool arm) { return pimpl_->client.call("armDisarm", arm).as(); diff --git a/AirLib/src/rpc/RpcLibServer.cpp b/AirLib/src/rpc/RpcLibServer.cpp index 3bf59beb80..3f60a03338 100644 --- a/AirLib/src/rpc/RpcLibServer.cpp +++ b/AirLib/src/rpc/RpcLibServer.cpp @@ -29,6 +29,10 @@ struct RpcLibServer::impl { : server(server_address, port) {} + impl(uint16_t port) + : server(port) + {} + ~impl() { } @@ -40,7 +44,10 @@ typedef msr::airlib_rpclib::RpcLibAdapators RpcLibAdapators; RpcLibServer::RpcLibServer(DroneControllerCancelable* drone, string server_address, uint16_t port) : drone_(drone) { - pimpl_.reset(new impl(server_address, port)); + if (server_address == "") + pimpl_.reset(new impl(port)); + else + pimpl_.reset(new impl(server_address, port)); pimpl_->server.bind("ping", [&]() -> bool { return true; }); pimpl_->server.bind("armDisarm", [&](bool arm) -> bool { return drone_->armDisarm(arm); }); pimpl_->server.bind("setOffboardMode", [&](bool is_set) -> void { drone_->setOffboardMode(is_set); }); diff --git a/DroneShell/src/main.cpp b/DroneShell/src/main.cpp index fda55ab827..9bca34f083 100644 --- a/DroneShell/src/main.cpp +++ b/DroneShell/src/main.cpp @@ -27,7 +27,7 @@ using namespace common_utils; struct CommandContext { public: - CommandContext(const std::string& server_address = "127.0.0.1") + CommandContext(const std::string& server_address = "localhost") : client(server_address) { } @@ -1312,25 +1312,32 @@ int main(int argc, const char *argv[]) { shell.showMessage(R"( Welcome to DroneShell 1.0. Type ? for help. - Microsoft Research (c) 2016. + Microsoft Research (c) 2017. )"); // make sure we can talk to the DroneServer //std::cout << "Contacting DroneServer..." << std::flush; //command_context.client.ping(); //std::cout << "DroneServer is responding." << std::endl; + + std::cout << "Waiting for connection--" << std::flush; + const TTimeDelta pause_time = 1; + while (command_context.client.getConnectionState() != RpcLibClient::ConnectionState::Connected) + { + std::cout << "X" << std::flush; + command_context.sleep_for(pause_time); + } + std::cout << "Connected!" << std::endl; std::cout << "Waiting for drone to report a valid GPS location..." << std::flush; - const TTimeDelta pause_time = 1; - auto gps = command_context.client.getGpsLocation(); - while (gps.latitude == 0 && gps.longitude == 0 && gps.altitude == 0) + int count = 0; + while (gps.latitude == 0 && gps.longitude == 0 && gps.altitude == 0 && count++ < 10) { std::cout << "." << std::flush; command_context.sleep_for(pause_time); gps = command_context.client.getGpsLocation(); } - std::cout << std::endl; std::cout << "Global position: lat=" << gps.latitude << ", lon=" << gps.longitude << ", alt=" << gps.altitude << std::endl; diff --git a/Unreal/Plugins/AirSim/Source/MultiRotorConnector.cpp b/Unreal/Plugins/AirSim/Source/MultiRotorConnector.cpp index ce851ec764..50cfa08da9 100644 --- a/Unreal/Plugins/AirSim/Source/MultiRotorConnector.cpp +++ b/Unreal/Plugins/AirSim/Source/MultiRotorConnector.cpp @@ -207,7 +207,10 @@ void MultiRotorConnector::startApiServer() vehicle_.getController())); rpclib_server_.reset(new msr::airlib::RpcLibServer(controller_cancelable_.get(), api_server_address_)); rpclib_server_->start(); + UAirBlueprintLib::LogMessageString("API server started at ", api_server_address_, LogDebugLevel::Informational); } + else + UAirBlueprintLib::LogMessageString("API server is disabled in settings", "", LogDebugLevel::Informational); } void MultiRotorConnector::stopApiServer() diff --git a/Unreal/Plugins/AirSim/Source/SimMode/SimModeBase.cpp b/Unreal/Plugins/AirSim/Source/SimMode/SimModeBase.cpp index c210ffd92e..51bc7d73f7 100644 --- a/Unreal/Plugins/AirSim/Source/SimMode/SimModeBase.cpp +++ b/Unreal/Plugins/AirSim/Source/SimMode/SimModeBase.cpp @@ -33,7 +33,10 @@ void ASimModeBase::BeginPlay() Settings& settings = Settings::singleton(); enable_rpc = settings.getBool("RpcEnabled", true); - api_server_address = settings.getString("LocalHostIp", "127.0.0.1"); + //by default we spawn server at local endpoint. Do not use 127.0.0.1 as default below + //because for docker container default is 0.0.0.0 and people get really confused why things + //don't work + api_server_address = settings.getString("LocalHostIp", ""); is_record_ui_visible = settings.getBool("RecordUIVisible", true); std::string view_mode_string = settings.getString("ViewMode", "FlyWithMe");