Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Making command line parser easier to use #3

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 53 additions & 8 deletions simulator_interface/SoccerAgentServer_Main.cpp
Original file line number Diff line number Diff line change
@@ -1,17 +1,56 @@
#include "SoccerAgentService.h"

#include "representations/ITAndroidsConstants.h"

#include "external/easylogging++.h"
#include "core/utils/command_line_parser/AgentCommandLineParser.h"
#include <boost/program_options.hpp>

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<std::string>()->default_value(ITAndroidsConstants::DEFAULT_IP),
"set server ip")
("server-port", po::value<int>()->default_value(ITAndroidsConstants::DEFAULT_SERVER_PORT),
"set agent port")
("monitor-port", po::value<int>()->default_value(ITAndroidsConstants::DEFAULT_MONITOR_PORT),
"set monitor port")
("team-name", po::value<std::string>()->default_value(ITAndroidsConstants::TEAM_DEFAULT_NAME),
"set team name")
("agent-type", po::value<std::string>()->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<int>(&value))
LOG(INFO) << param << " " << *v;
else if (auto v = boost::any_cast<std::string>(&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<void> &closeRequested = service.getClosePromise();

ServerBuilder builder;
Expand All @@ -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<std::string>(),
vm["server-port"].as<int>(),
vm["monitor-port"].as<int>(),
vm["team-name"].as<std::string>(),
vm["agent-type"].as<std::string>());
}
24 changes: 20 additions & 4 deletions simulator_interface/SoccerAgentService.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,33 @@
#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;
}

Status SoccerAgentService::SetupEnvironment(ServerContext *context, const SetupEnvRequest *request,
SetupEnvResponse *response) {
agent = std::make_unique<StealBallLearningAgent>(serverIp, serverPort, monitorPort, nbAgents, 0,
std::string("ITAndroids"));
if (agentType == "kick") {
agent = std::make_unique<KickLearningAgent>(
serverIp, serverPort, monitorPort, nbAgents, 0, std::string("ITAndroids"));
}
else { // "steal" == default
agent = std::make_unique<StealBallLearningAgent>(
serverIp, serverPort, monitorPort, nbAgents, 0, std::string("ITAndroids"));
}

response->CopyFrom(agent->setup());
return Status::OK;
}
Expand Down
3 changes: 2 additions & 1 deletion simulator_interface/SoccerAgentService.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

Expand Down