25 #include <netcomm/socket/datagram_multicast.h> 26 #include <logging/logger.h> 33 #include <libxml++/libxml++.h> 45 using namespace xmlpp;
50 static const std::string REFBOX_EVENT =
"RefboxEvent";
51 static const std::string REFBOX_GAMEINFO =
"GameInfo";
52 static const std::string REFBOX_EVENT_REFEREE =
"Referee";
53 static const std::string REFBOX_EVENT_TEAMSETUP =
"TeamSetup";
55 static const std::string REFBOX_CANCEL =
"Cancel";
57 static const std::string REFBOX_GAMESTART =
"GameStart";
58 static const std::string REFBOX_GAMESTOP =
"GameStop";
60 static const std::string REFBOX_STAGE_CHANGED =
"StageChanged";
61 static const std::string REFBOX_STAGETYPE_PREGAME =
"preGame";
62 static const std::string REFBOX_STAGETYPE_FIRSTHALF =
"firstHalf";
63 static const std::string REFBOX_STAGETYPE_HALFTIME =
"halfTime";
64 static const std::string REFBOX_STAGETYPE_SECONDHALF =
"secondHalf";
65 static const std::string REFBOX_STAGETYPE_SHOOTOUT =
"shootOut";
66 static const std::string REFBOX_STAGETYPE_ENDGAME =
"endGame";
68 static const std::string REFBOX_GOAL_AWARDED =
"GoalAwarded";
69 static const std::string REFBOX_GOAL_REMOVED =
"GoalRemoved";
71 static const std::string REFBOX_CARD_AWARDED =
"CardAwarded";
72 static const std::string REFBOX_CARD_REMOVED =
"CardRemoved";
74 static const std::string REFBOX_SUBSTITUTION =
"Substitution";
75 static const std::string REFBOX_PLAYER_OUT =
"PlayerOut";
76 static const std::string REFBOX_PLAYER_IN =
"PlayerIn";
78 static const std::string REFBOX_DROPPEDBALL =
"DroppedBall";
79 static const std::string REFBOX_KICKOFF =
"KickOff";
80 static const std::string REFBOX_FREEKICK =
"FreeKick";
81 static const std::string REFBOX_GOALKICK =
"GoalKick";
82 static const std::string REFBOX_THROWIN =
"ThrowIn";
83 static const std::string REFBOX_CORNER =
"Corner";
84 static const std::string REFBOX_PENALTY =
"Penalty";
86 static const std::string REFBOX_TEAMCOLOR_CYAN =
"Cyan";
87 static const std::string REFBOX_TEAMCOLOR_MAGENTA =
"Magenta";
89 static const std::string REFBOX_GOALCOLOR_YELLOW =
"yellow";
90 static const std::string REFBOX_GOALCOLOR_BLUE =
"blue";
92 static const std::string REFBOX_CARDCOLOR_YELLOW =
"yellow";
93 static const std::string REFBOX_CARDCOLOR_RED =
"red";
109 const char *refbox_host,
110 unsigned short int refbox_port)
111 : __name(
"Msl2010RefBoxProc")
116 __score_cyan = __score_magenta = 0;
117 __connection_died =
false;
119 __refbox_host = strdup(refbox_host);
120 __refbox_port = refbox_port;
139 Msl2010RefBoxProcessor::reconnect()
145 __logger->
log_info(__name,
"Trying to connect to refbox at %s:%u",
146 __refbox_host, __refbox_port);
148 __logger->
log_info(__name,
"Creating MulticastDatagramSocket");
163 __connection_died =
false;
173 __logger->
log_info(__name,
"Init done");
179 Msl2010RefBoxProcessor::process_string(
char *buf,
size_t len)
181 __logger->
log_info(__name,
"Received\n *****\n %s \n *****", buf);
183 std::istringstream iss( std::string(buf), std::istringstream::in);
185 dom =
new DomParser();
187 dom->set_substitute_entities();
188 dom->parse_stream(iss);
189 root = dom->get_document()->get_root_node();
193 const Element * el =
dynamic_cast<const Element *
>(root);
198 __logger->
log_info(__name,
"root-element name is '%s'", el->get_name().data() );
200 const Node::NodeList nl = el->get_children();
202 if( nl.size() == 0 ) {
203 __logger->
log_info(__name,
"root has NO children!");
208 for (Node::NodeList::const_iterator it = nl.begin(); it != nl.end(); ++it) {
209 const Node* node = *it;
210 __logger->
log_info(__name,
"1st level child name is '%s'", node->get_name().data() );
222 const Node::NodeList cnl = node->get_children();
224 if( cnl.size() == 0 ) {
225 __logger->
log_info(__name,
"child has NO children!");
230 for (Node::NodeList::const_iterator cit = cnl.begin(); cit != cnl.end(); ++cit) {
231 const Node* cnode = *cit;
232 const Element* cel =
dynamic_cast<const Element *
>(cnode);
233 std::string cnodename(cnode->get_name().data());
235 __logger->
log_info(__name,
"2nd level child name is '%s'", cnode->get_name().data() );
237 const Attribute* cattr;
238 std::string cteamcolor;
241 std::string cstagetype;
243 if( cnodename == REFBOX_KICKOFF || cnodename == REFBOX_FREEKICK ||
244 cnodename == REFBOX_GOALKICK || cnodename == REFBOX_THROWIN ||
245 cnodename == REFBOX_CORNER || cnodename == REFBOX_PENALTY ||
246 cnodename == REFBOX_GOAL_AWARDED || cnodename == REFBOX_GOAL_REMOVED ||
247 cnodename == REFBOX_CARD_AWARDED || cnodename == REFBOX_CARD_REMOVED ||
248 cnodename == REFBOX_PLAYER_OUT || cnodename == REFBOX_PLAYER_IN ||
249 cnodename == REFBOX_SUBSTITUTION )
251 cattr = cel->get_attribute(
"team");
252 cteamcolor = std::string( cattr->get_value().data() );
255 if( cnodename == REFBOX_CANCEL ) {
257 __logger->
log_info(__name,
"RefBox cancelled last command");
259 else if( cnodename == REFBOX_GAMESTOP ) {
262 else if( cnodename == REFBOX_GAMESTART ) {
265 else if( cnodename == REFBOX_DROPPEDBALL ) {
268 else if( cnodename == REFBOX_GOAL_AWARDED ) {
270 if( cteamcolor == REFBOX_TEAMCOLOR_CYAN ) {
273 else if ( cteamcolor == REFBOX_TEAMCOLOR_MAGENTA ) {
278 else if( cnodename == REFBOX_KICKOFF ) {
279 if( cteamcolor == REFBOX_TEAMCOLOR_CYAN ) {
282 else if ( cteamcolor == REFBOX_TEAMCOLOR_MAGENTA ) {
286 else if( cnodename == REFBOX_PENALTY ) {
287 if( cteamcolor == REFBOX_TEAMCOLOR_CYAN ) {
290 else if ( cteamcolor == REFBOX_TEAMCOLOR_MAGENTA ) {
294 else if( cnodename == REFBOX_CORNER ) {
295 if( cteamcolor == REFBOX_TEAMCOLOR_CYAN ) {
298 else if ( cteamcolor == REFBOX_TEAMCOLOR_MAGENTA ) {
302 else if( cnodename == REFBOX_THROWIN ) {
303 if( cteamcolor == REFBOX_TEAMCOLOR_CYAN ) {
306 else if ( cteamcolor == REFBOX_TEAMCOLOR_MAGENTA ) {
310 else if( cnodename == REFBOX_FREEKICK ) {
311 if( cteamcolor == REFBOX_TEAMCOLOR_CYAN ) {
314 else if ( cteamcolor == REFBOX_TEAMCOLOR_MAGENTA ) {
318 else if( cnodename == REFBOX_GOALKICK ) {
319 if( cteamcolor == REFBOX_TEAMCOLOR_CYAN ) {
322 else if ( cteamcolor == REFBOX_TEAMCOLOR_MAGENTA ) {
326 else if( cnodename == REFBOX_STAGE_CHANGED ) {
327 cattr = cel->get_attribute(
"newStage");
328 cstagetype = std::string( cattr->get_value().data() );
329 if( cstagetype == REFBOX_STAGETYPE_PREGAME ) {
331 }
else if( cstagetype == REFBOX_STAGETYPE_FIRSTHALF ) {
333 }
else if( cstagetype == REFBOX_STAGETYPE_HALFTIME ) {
335 }
else if( cstagetype == REFBOX_STAGETYPE_SECONDHALF ) {
337 }
else if( cstagetype == REFBOX_STAGETYPE_SHOOTOUT ) {
339 }
else if( cstagetype == REFBOX_STAGETYPE_ENDGAME ) {
352 __logger->
log_info(__name,
"root is NOT a valid element");
360 short pollrv = __s->
poll(0, Socket::POLL_IN);
363 if (pollrv == Socket::POLL_ERR) {
364 __logger->
log_warn(__name,
"Polling socket failed");
365 }
else if (pollrv & Socket::POLL_IN) {
367 size_t bytes_read = __s->
read(tmpbuf,
sizeof(tmpbuf),
false);
368 __logger->
log_debug(__name,
"Read %zu bytes", bytes_read);
369 if ( bytes_read == 0 ) {
371 __connection_died =
true;
373 tmpbuf[bytes_read] =
'\0';
374 process_string(tmpbuf, bytes_read);
377 pollrv = __s->
poll(0, Socket::POLL_IN);
378 }
while (pollrv & Socket::POLL_IN);
384 if (__connection_died) {
387 return ! __connection_died;
virtual void close()
Close socket.
virtual void log_info(const char *component, const char *format,...)=0
Log informational message.
Referee drops ball, both teams can wrestle for the ball.
Fawkes library namespace.
virtual void set_gamestate(int game_state, fawkes::worldinfo_gamestate_team_t state_team)=0
Set current game state.
void set_loop(bool loop)
Set loopback of sent packets.
RefBoxStateHandler * _rsh
Refbox state handler, set via set_handler()
virtual void bind()
Bind socket.
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 set_score(unsigned int score_cyan, unsigned int score_magenta)=0
Set score.
Multicast datagram socket.
virtual size_t read(void *buf, size_t count, bool read_all=true)
Read from socket.
void refbox_process()
Process incoming refbox communication.
virtual void log_warn(const char *component, const char *format,...)=0
Log warning message.
~Msl2010RefBoxProcessor()
Destructor.
virtual void log_debug(const char *component, const char *format,...)=0
Log debug message.
virtual short poll(int timeout=-1, short what=POLL_IN|POLL_HUP|POLL_PRI|POLL_RDHUP)
Wait for some event on socket.
bool check_connection()
Check if the connection is alive and reconnect.
Msl2010RefBoxProcessor(fawkes::Logger *logger, const char *refbox_host, unsigned short int refbox_port)
Constructor.