Phoenix2D-Library  0.10
Controller.cpp
Go to the documentation of this file.
1 /*
2  * Phoenix2D (RoboCup Soccer Simulation 2D League)
3  * Copyright (c) 2013 Ivan Gonzalez
4  *
5  * This file is part of Phoenix2D.
6  *
7  * Phoenix2D is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation, either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * Phoenix2D is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with Phoenix2D. If not, see <http://www.gnu.org/licenses/>.
19  */
20 
21 #include "Controller.h"
22 #include <boost/regex.hpp>
23 #include <iostream>
24 #include <cstdlib>
25 #include "Connect.h"
26 #include "Self.h"
27 #include "Reader.h"
28 #include "Server.h"
29 #include "Parser.h"
30 #include "Commands.h"
31 #include "World.h"
32 namespace Phoenix
33 {
34 char Controller::AGENT_TYPE = 'p';
35 
36 Controller::Controller(const char *teamName, char agentType, const char *hostname) {
37  this->hostname = std::string(hostname);
38  Controller::AGENT_TYPE = agentType;
39  team_name = std::string(teamName);
40  connected = false;
41  c = 0;
42  reader = 0;
43  server = 0;
44  commands = 0;
45  world = 0;
46  self = 0;
47  parser = 0;
48 }
49 
51  if (world) delete world;
52  if (commands) delete commands;
53  if (reader) delete reader;
54  if (parser) delete parser;
55  if (self) delete self;
56  if (server) delete server;
57  if (c) delete c;
58 }
59 
61  std::string message = "(init " + team_name + " (version 15.1)";
62  int port = 6000;
63  switch (Controller::AGENT_TYPE) {
64  case 'g':
65  message = message + " (goalie))";
66  break;
67  case 't':
68  message = "(init (version 15.1))";
69  port = 6001;
70  break;
71  case 'c':
72  message = message + ")";
73  port = 6002;
74  break;
75  default:
76  message = message + ")";
77  break;
78  }
79  boost::regex error("\\(error\\s+([\\w\\_]+)\\)"); //i.e (error no_more_team_or_player_or_goalie)
80  boost::cmatch match;
81  c = new Connect(hostname.c_str(), port);
82  c->sendMessage(message);
83  message = c->receiveMessage();
84  if (boost::regex_match(message.c_str(), match, error)) {
85  std::cerr << "Controller::connect() -> " << match[1] << std::endl; //Error
86  return;
87  } else {
88  boost::regex player_response("\\(init\\s+(l|r)\\s+(\\d+)\\s+([\\w\\_]+)\\)"); //i.e (init l 1 before_kick_off)
89  boost::regex coach_response("\\(init\\s+(l|r)\\s+ok\\)");
90  std::string side;
91  int unum = 0;
92  switch (Controller::AGENT_TYPE) {
93  case 't':
94  side = "trainer";
95  break;
96  case 'c': //Especial case, we need to consider the regex for this case
97  unum = 0;
98  if (boost::regex_match(message.c_str(), match, coach_response)) {
99  side = match[1];
100  } else {
101  side = "undefined";
102  std::cerr << "Controller::connect() -> Does not match response for coach" << std::endl;
103  }
104  break;
105  default:
106  if (boost::regex_match(message.c_str(), match, player_response)) {
107  side = match[1];
108  unum = atoi((std::string() + match[2]).c_str()); //C++11: use std::stoi()
109  } else {
110  side = "undefined";
111  unum = 0;
112  std::cerr << "Controller::connect() -> Does not match response for player" << std::endl;
113  }
114  break;
115  }
116  message = c->receiveMessage(); //server_params
117  server = new Server(message);
118  message = c->receiveMessage(); //player_params
119  self = new Self(message, team_name, unum, side);
120  for (int i = 0; i < Self::PLAYER_TYPES; i++) {
121  message = c->receiveMessage(); //player_type
122  self->addPlayerType(message);
123  }
124  switch (Controller::AGENT_TYPE) {
125  case 'p':
126  c->sendMessage("(synch_see)");
127  break;
128  case 'g':
129  c->sendMessage("(synch_see)");
130  break;
131  case 'c':
132  c->sendMessage("(eye on)");
133  break;
134  default:
135  c->sendMessage("(eye on)");
136  break;
137  }
138  world = new World();
139  parser = new Parser(self, world);
140  reader = new Reader(c, parser);
141  reader->start();
142  connected = true;
143  }
144 }
145 
147  return connected;
148 }
149 
151 
152 }
153 
155  if (isConnected()) {
156  reader->stop();
157  }
158  connected = false;
159 }
160 
162  if (!commands) {
163  commands = new Commands(c);
164  }
165  return commands;
166 }
167 
169  return world;
170 }
171 
173  return self;
174 }
175 }
Parser * parser
Pointer to inner Parser Object.
Definition: Controller.h:111
void connect()
The main connection, should be called before anything else in Phoenix2D.
Definition: Controller.cpp:60
void disconnect()
Disconnect service.
Definition: Controller.cpp:154
std::string hostname
Definition: Controller.h:120
Server * server
Pointer to inner Server Object.
Definition: Controller.h:114
bool connected
Connection status.
Definition: Controller.h:118
Commands The Commans lorem Ipsum
Definition: Commands.h:42
static int PLAYER_TYPES
Definition: Self.h:75
void start()
Definition: Reader.cpp:60
World * getWorld()
World getter.
Definition: Controller.cpp:168
bool sendMessage(std::string msg)
Definition: Connect.cpp:86
World * world
Pointer to inner World Object.
Definition: Controller.h:116
Parser The Parser lorem Ipsum
Definition: Parser.h:44
Self The Self lorem Ipsum
Definition: Self.h:45
std::string receiveMessage()
Definition: Connect.cpp:95
Commands * commands
Pointer to inner Commans Object.
Definition: Controller.h:115
World The World lorem Ipsum
Definition: World.h:41
Controller(const char *teamName, char agentType, const char *hostname)
Default Constructor.
Definition: Controller.cpp:36
void stop()
Definition: Reader.cpp:69
Reader * reader
Pointer to inner Reader Object.
Definition: Controller.h:113
Commands * getCommands()
Commands getter.
Definition: Controller.cpp:161
Connect * c
Pointer to inner Connect Object.
Definition: Controller.h:112
Server The Server lorem Ipsum
Definition: Server.h:38
std::string team_name
Definition: Controller.h:119
void reconnect()
Reconnection Logic.
Definition: Controller.cpp:150
Reader The Reader lorem Ipsum
Definition: Reader.h:39
Self * getSelf()
Self getter.
Definition: Controller.cpp:172
static char AGENT_TYPE
p = Player, t = Trainer
Definition: Controller.h:78
~Controller()
Default Destructor.
Definition: Controller.cpp:50