26#include <libxml++/libxml++.h>
27#include <logging/logger.h>
28#include <netcomm/socket/datagram_multicast.h>
49static const std::string REFBOX_EVENT =
"RefboxEvent";
50static const std::string REFBOX_GAMEINFO =
"GameInfo";
51static const std::string REFBOX_EVENT_REFEREE =
"Referee";
52static const std::string REFBOX_EVENT_TEAMSETUP =
"TeamSetup";
54static const std::string REFBOX_CANCEL =
"Cancel";
56static const std::string REFBOX_GAMESTART =
"GameStart";
57static const std::string REFBOX_GAMESTOP =
"GameStop";
59static const std::string REFBOX_STAGE_CHANGED =
"StageChanged";
60static const std::string REFBOX_STAGETYPE_PREGAME =
"preGame";
61static const std::string REFBOX_STAGETYPE_FIRSTHALF =
"firstHalf";
62static const std::string REFBOX_STAGETYPE_HALFTIME =
"halfTime";
63static const std::string REFBOX_STAGETYPE_SECONDHALF =
"secondHalf";
64static const std::string REFBOX_STAGETYPE_SHOOTOUT =
"shootOut";
65static const std::string REFBOX_STAGETYPE_ENDGAME =
"endGame";
67static const std::string REFBOX_GOAL_AWARDED =
"GoalAwarded";
68static const std::string REFBOX_GOAL_REMOVED =
"GoalRemoved";
70static const std::string REFBOX_CARD_AWARDED =
"CardAwarded";
71static const std::string REFBOX_CARD_REMOVED =
"CardRemoved";
73static const std::string REFBOX_SUBSTITUTION =
"Substitution";
74static const std::string REFBOX_PLAYER_OUT =
"PlayerOut";
75static const std::string REFBOX_PLAYER_IN =
"PlayerIn";
77static const std::string REFBOX_DROPPEDBALL =
"DroppedBall";
78static const std::string REFBOX_KICKOFF =
"KickOff";
79static const std::string REFBOX_FREEKICK =
"FreeKick";
80static const std::string REFBOX_GOALKICK =
"GoalKick";
81static const std::string REFBOX_THROWIN =
"ThrowIn";
82static const std::string REFBOX_CORNER =
"Corner";
83static const std::string REFBOX_PENALTY =
"Penalty";
85static const std::string REFBOX_TEAMCOLOR_CYAN =
"Cyan";
86static const std::string REFBOX_TEAMCOLOR_MAGENTA =
"Magenta";
88static const std::string REFBOX_GOALCOLOR_YELLOW =
"yellow";
89static const std::string REFBOX_GOALCOLOR_BLUE =
"blue";
91static const std::string REFBOX_CARDCOLOR_YELLOW =
"yellow";
92static const std::string REFBOX_CARDCOLOR_RED =
"red";
107 const char * refbox_host,
108 unsigned short int refbox_port)
109: name_(
"Msl2010RefBoxProc")
114 score_cyan_ = score_magenta_ = 0;
115 connection_died_ =
false;
117 refbox_host_ = strdup(refbox_host);
118 refbox_port_ = refbox_port;
135Msl2010RefBoxProcessor::reconnect()
141 logger_->
log_info(name_,
"Trying to connect to refbox at %s:%u", refbox_host_, refbox_port_);
143 logger_->
log_info(name_,
"Creating MulticastDatagramSocket");
158 connection_died_ =
false;
168 logger_->
log_info(name_,
"Init done");
173Msl2010RefBoxProcessor::process_string(
char *buf,
size_t len)
175 logger_->
log_info(name_,
"Received\n *****\n %s \n *****", buf);
177 std::istringstream iss(std::string(buf), std::istringstream::in);
179 dom =
new DomParser();
181 dom->set_substitute_entities();
182 dom->parse_stream(iss);
183 root = dom->get_document()->get_root_node();
187 const Element *el =
dynamic_cast<const Element *
>(root);
192 logger_->
log_info(name_,
"root-element name is '%s'", el->get_name().data());
194 const Node::NodeList nl = el->get_children();
196 if (nl.size() == 0) {
197 logger_->
log_info(name_,
"root has NO children!");
201 for (Node::NodeList::const_iterator it = nl.begin(); it != nl.end(); ++it) {
202 const Node *node = *it;
203 logger_->
log_info(name_,
"1st level child name is '%s'", node->get_name().data());
215 const Node::NodeList cnl = node->get_children();
217 if (cnl.size() == 0) {
218 logger_->
log_info(name_,
"child has NO children!");
222 for (Node::NodeList::const_iterator cit = cnl.begin(); cit != cnl.end(); ++cit) {
223 const Node * cnode = *cit;
224 const Element *cel =
dynamic_cast<const Element *
>(cnode);
225 std::string cnodename(cnode->get_name().data());
227 logger_->
log_info(name_,
"2nd level child name is '%s'", cnode->get_name().data());
229 const Attribute *cattr;
230 std::string cteamcolor;
233 std::string cstagetype;
235 if (cnodename == REFBOX_KICKOFF || cnodename == REFBOX_FREEKICK
236 || cnodename == REFBOX_GOALKICK || cnodename == REFBOX_THROWIN
237 || cnodename == REFBOX_CORNER || cnodename == REFBOX_PENALTY
238 || cnodename == REFBOX_GOAL_AWARDED || cnodename == REFBOX_GOAL_REMOVED
239 || cnodename == REFBOX_CARD_AWARDED || cnodename == REFBOX_CARD_REMOVED
240 || cnodename == REFBOX_PLAYER_OUT || cnodename == REFBOX_PLAYER_IN
241 || cnodename == REFBOX_SUBSTITUTION) {
242 cattr = cel->get_attribute(
"team");
243 cteamcolor = std::string(cattr->get_value().data());
246 if (cnodename == REFBOX_CANCEL) {
248 logger_->
log_info(name_,
"RefBox cancelled last command");
249 }
else if (cnodename == REFBOX_GAMESTOP) {
251 }
else if (cnodename == REFBOX_GAMESTART) {
253 }
else if (cnodename == REFBOX_DROPPEDBALL) {
255 }
else if (cnodename == REFBOX_GOAL_AWARDED) {
257 if (cteamcolor == REFBOX_TEAMCOLOR_CYAN) {
259 }
else if (cteamcolor == REFBOX_TEAMCOLOR_MAGENTA) {
263 }
else if (cnodename == REFBOX_KICKOFF) {
264 if (cteamcolor == REFBOX_TEAMCOLOR_CYAN) {
266 }
else if (cteamcolor == REFBOX_TEAMCOLOR_MAGENTA) {
269 }
else if (cnodename == REFBOX_PENALTY) {
270 if (cteamcolor == REFBOX_TEAMCOLOR_CYAN) {
272 }
else if (cteamcolor == REFBOX_TEAMCOLOR_MAGENTA) {
275 }
else if (cnodename == REFBOX_CORNER) {
276 if (cteamcolor == REFBOX_TEAMCOLOR_CYAN) {
278 }
else if (cteamcolor == REFBOX_TEAMCOLOR_MAGENTA) {
281 }
else if (cnodename == REFBOX_THROWIN) {
282 if (cteamcolor == REFBOX_TEAMCOLOR_CYAN) {
284 }
else if (cteamcolor == REFBOX_TEAMCOLOR_MAGENTA) {
287 }
else if (cnodename == REFBOX_FREEKICK) {
288 if (cteamcolor == REFBOX_TEAMCOLOR_CYAN) {
290 }
else if (cteamcolor == REFBOX_TEAMCOLOR_MAGENTA) {
293 }
else if (cnodename == REFBOX_GOALKICK) {
294 if (cteamcolor == REFBOX_TEAMCOLOR_CYAN) {
296 }
else if (cteamcolor == REFBOX_TEAMCOLOR_MAGENTA) {
299 }
else if (cnodename == REFBOX_STAGE_CHANGED) {
300 cattr = cel->get_attribute(
"newStage");
301 cstagetype = std::string(cattr->get_value().data());
302 if (cstagetype == REFBOX_STAGETYPE_PREGAME) {
304 }
else if (cstagetype == REFBOX_STAGETYPE_FIRSTHALF) {
306 }
else if (cstagetype == REFBOX_STAGETYPE_HALFTIME) {
308 }
else if (cstagetype == REFBOX_STAGETYPE_SECONDHALF) {
310 }
else if (cstagetype == REFBOX_STAGETYPE_SHOOTOUT) {
312 }
else if (cstagetype == REFBOX_STAGETYPE_ENDGAME) {
323 logger_->
log_info(name_,
"root is NOT a valid element");
330 short pollrv = s_->
poll(0, Socket::POLL_IN);
332 if (pollrv == Socket::POLL_ERR) {
333 logger_->
log_warn(name_,
"Polling socket failed");
334 }
else if (pollrv & Socket::POLL_IN) {
336 size_t bytes_read = s_->
read(tmpbuf,
sizeof(tmpbuf),
false);
337 logger_->
log_debug(name_,
"Read %zu bytes", bytes_read);
338 if (bytes_read == 0) {
340 connection_died_ =
true;
342 tmpbuf[bytes_read] =
'\0';
343 process_string(tmpbuf, bytes_read);
346 pollrv = s_->
poll(0, Socket::POLL_IN);
347 }
while (pollrv & Socket::POLL_IN);
353 if (connection_died_) {
356 return !connection_died_;
void refbox_process()
Process incoming refbox communication.
~Msl2010RefBoxProcessor()
Destructor.
Msl2010RefBoxProcessor(fawkes::Logger *logger, const char *refbox_host, unsigned short int refbox_port)
Constructor.
bool check_connection()
Check if the connection is alive and reconnect.
RefBoxStateHandler * _rsh
Refbox state handler, set via set_handler()
virtual void set_gamestate(int game_state, fawkes::worldinfo_gamestate_team_t state_team)=0
Set current game state.
virtual void set_score(unsigned int score_cyan, unsigned int score_magenta)=0
Set score.
virtual void set_half(fawkes::worldinfo_gamestate_half_t half, bool kickoff=false)=0
Set current half of the game time.
Base class for exceptions in Fawkes.
virtual void log_debug(const char *component, const char *format,...)=0
Log debug message.
virtual void log_warn(const char *component, const char *format,...)=0
Log warning message.
virtual void log_info(const char *component, const char *format,...)=0
Log informational message.
Multicast datagram socket.
void set_loop(bool loop)
Set loopback of sent packets.
virtual void bind()
Bind socket.
virtual size_t read(void *buf, size_t count, bool read_all=true)
Read from socket.
virtual short poll(int timeout=-1, short what=POLL_IN|POLL_HUP|POLL_PRI|POLL_RDHUP)
Wait for some event on socket.
virtual void close()
Close socket.
Fawkes library namespace.