From 0cd83e529c4ab1aa8da9bfa42d3883aef3646d30 Mon Sep 17 00:00:00 2001 From: Alexandre Muzio Date: Fri, 21 Jun 2019 00:23:30 -0700 Subject: [PATCH] Making command line parser easier to use --- .../SoccerAgentServer_Main.cpp | 61 ++++++++++++++++--- simulator_interface/SoccerAgentService.cpp | 24 ++++++-- simulator_interface/SoccerAgentService.h | 3 +- 3 files changed, 75 insertions(+), 13 deletions(-) diff --git a/simulator_interface/SoccerAgentServer_Main.cpp b/simulator_interface/SoccerAgentServer_Main.cpp index f9ba9c8..ecf613a 100644 --- a/simulator_interface/SoccerAgentServer_Main.cpp +++ b/simulator_interface/SoccerAgentServer_Main.cpp @@ -1,17 +1,56 @@ #include "SoccerAgentService.h" +#include "representations/ITAndroidsConstants.h" + #include "external/easylogging++.h" -#include "core/utils/command_line_parser/AgentCommandLineParser.h" +#include -using core::utils::command_line_parser::AgentCommandLineParser; +namespace po = boost::program_options; using std::string; INITIALIZE_EASYLOGGINGPP -void RunServer(string serverIp, int serverPort, int monitorPort, string teamName) { +void parseArguments(int argc, char** argv, po::variables_map& variablesMap) { + po::options_description desc("Allowed options"); + desc.add_options() + ("help", "produce help message") + ("server-ip", po::value()->default_value(ITAndroidsConstants::DEFAULT_IP), + "set server ip") + ("server-port", po::value()->default_value(ITAndroidsConstants::DEFAULT_SERVER_PORT), + "set agent port") + ("monitor-port", po::value()->default_value(ITAndroidsConstants::DEFAULT_MONITOR_PORT), + "set monitor port") + ("team-name", po::value()->default_value(ITAndroidsConstants::TEAM_DEFAULT_NAME), + "set team name") + ("agent-type", po::value()->default_value("steal"), + "set team name") + ; + + po::store(po::parse_command_line(argc, argv, desc), variablesMap); + po::notify(variablesMap); + + if(variablesMap.count("help")){ + std::cout << desc << std::endl; + exit(1); + } + + LOG(INFO) << "COMMAND LINE PARAMETERS"; + for (const auto &it : variablesMap) { + std::string param = it.first.c_str(); + auto &value = it.second.value(); + if (auto v = boost::any_cast(&value)) + LOG(INFO) << param << " " << *v; + else if (auto v = boost::any_cast(&value)) + LOG(INFO) << param << " " << *v; + else + LOG(INFO) << "error"; + } +} + +void runServer(string serverIp, int serverPort, int monitorPort, string teamName, string agentType) { string serverAddress("0.0.0.0:" + std::to_string(5000)); - SoccerAgentService service(serverIp, serverPort, monitorPort, teamName); + SoccerAgentService service(serverIp, serverPort, monitorPort, teamName, agentType); std::promise &closeRequested = service.getClosePromise(); ServerBuilder builder; @@ -36,9 +75,15 @@ void RunServer(string serverIp, int serverPort, int monitorPort, string teamName int main(int argc, char** argv) { // Load configuration from file el::Configurations conf("config/log.conf"); - el::Loggers::reconfigureLogger("default", conf);; - AgentCommandLineParser parser; - parser.parse(argc, argv); + el::Loggers::reconfigureLogger("default", conf); + + po::variables_map vm; + parseArguments(argc, argv, vm); - RunServer(parser.getServerIP(), parser.getServerPort(), parser.getMonitorPort(), parser.getTeamName()); + runServer( + vm["server-ip"].as(), + vm["server-port"].as(), + vm["monitor-port"].as(), + vm["team-name"].as(), + vm["agent-type"].as()); } diff --git a/simulator_interface/SoccerAgentService.cpp b/simulator_interface/SoccerAgentService.cpp index e081c16..c7c9ea3 100644 --- a/simulator_interface/SoccerAgentService.cpp +++ b/simulator_interface/SoccerAgentService.cpp @@ -5,8 +5,17 @@ #include "external/easylogging++.h" -SoccerAgentService::SoccerAgentService(string serverIp, int serverPort, int monitorPort, string teamName) - : serverIp(serverIp), serverPort(serverPort), monitorPort(monitorPort), teamName(teamName) { +SoccerAgentService::SoccerAgentService( + string serverIp, + int serverPort, + int monitorPort, + string teamName, + string agentType) + : serverIp(serverIp), + serverPort(serverPort), + monitorPort(monitorPort), + teamName(teamName), + agentType(agentType) { LOG(INFO) << "Starting Server..."; LOG(INFO) << "S3D Server Port: " << serverPort; LOG(INFO) << "S3D Monitor Port: " << monitorPort; @@ -14,8 +23,15 @@ SoccerAgentService::SoccerAgentService(string serverIp, int serverPort, int moni Status SoccerAgentService::SetupEnvironment(ServerContext *context, const SetupEnvRequest *request, SetupEnvResponse *response) { - agent = std::make_unique(serverIp, serverPort, monitorPort, nbAgents, 0, - std::string("ITAndroids")); + if (agentType == "kick") { + agent = std::make_unique( + serverIp, serverPort, monitorPort, nbAgents, 0, std::string("ITAndroids")); + } + else { // "steal" == default + agent = std::make_unique( + serverIp, serverPort, monitorPort, nbAgents, 0, std::string("ITAndroids")); + } + response->CopyFrom(agent->setup()); return Status::OK; } diff --git a/simulator_interface/SoccerAgentService.h b/simulator_interface/SoccerAgentService.h index d4e5835..c895ac2 100644 --- a/simulator_interface/SoccerAgentService.h +++ b/simulator_interface/SoccerAgentService.h @@ -26,7 +26,7 @@ using api::CloseResponse; class SoccerAgentService final : public DDPGTrainer::Service { public: SoccerAgentService() { } - SoccerAgentService(string serverIp, int serverPort, int monitorPort, string teamName); + SoccerAgentService(string serverIp, int serverPort, int monitorPort, string teamName, string agentType); Status SetupEnvironment(ServerContext* context, const SetupEnvRequest* request, SetupEnvResponse* response) override; Status Simulate(ServerContext* context, const SimulationRequest* request, SimulationResponse* response) override; @@ -42,6 +42,7 @@ class SoccerAgentService final : public DDPGTrainer::Service { int serverPort = 3100; int monitorPort = 3200; string teamName = "ITAndroids"; + string agentType = "steal"; int nbAgents = 1;