23 #include <tools/refboxrep/msl2010.h>
24 #include <netcomm/socket/stream.h>
25 #include <netcomm/socket/datagram_multicast.h>
34 #include <libxml++/libxml++.h>
37 using namespace xmlpp;
45 static const std::string REFBOX_EVENT =
"RefboxEvent";
46 static const std::string REFBOX_GAMEINFO =
"GameInfo";
47 static const std::string REFBOX_EVENT_REFEREE =
"Referee";
48 static const std::string REFBOX_EVENT_TEAMSETUP =
"TeamSetup";
50 static const std::string REFBOX_CANCEL =
"Cancel";
52 static const std::string REFBOX_GAMESTART =
"GameStart";
53 static const std::string REFBOX_GAMESTOP =
"GameStop";
55 static const std::string REFBOX_STAGE_CHANGED =
"StageChanged";
56 static const std::string REFBOX_STAGETYPE_PREGAME =
"preGame";
57 static const std::string REFBOX_STAGETYPE_FIRSTHALF =
"firstHalf";
58 static const std::string REFBOX_STAGETYPE_HALFTIME =
"halfTime";
59 static const std::string REFBOX_STAGETYPE_SECONDHALF =
"secondHalf";
60 static const std::string REFBOX_STAGETYPE_SHOOTOUT =
"shootOut";
61 static const std::string REFBOX_STAGETYPE_ENDGAME =
"endGame";
63 static const std::string REFBOX_GOAL_AWARDED =
"GoalAwarded";
64 static const std::string REFBOX_GOAL_REMOVED =
"GoalRemoved";
66 static const std::string REFBOX_CARD_AWARDED =
"CardAwarded";
67 static const std::string REFBOX_CARD_REMOVED =
"CardRemoved";
69 static const std::string REFBOX_SUBSTITUTION =
"Substitution";
70 static const std::string REFBOX_PLAYER_OUT =
"PlayerOut";
71 static const std::string REFBOX_PLAYER_IN =
"PlayerIn";
73 static const std::string REFBOX_DROPPEDBALL =
"DroppedBall";
74 static const std::string REFBOX_KICKOFF =
"KickOff";
75 static const std::string REFBOX_FREEKICK =
"FreeKick";
76 static const std::string REFBOX_GOALKICK =
"GoalKick";
77 static const std::string REFBOX_THROWIN =
"ThrowIn";
78 static const std::string REFBOX_CORNER =
"Corner";
79 static const std::string REFBOX_PENALTY =
"Penalty";
81 static const std::string REFBOX_TEAMCOLOR_CYAN =
"Cyan";
82 static const std::string REFBOX_TEAMCOLOR_MAGENTA =
"Magenta";
84 static const std::string REFBOX_GOALCOLOR_YELLOW =
"yellow";
85 static const std::string REFBOX_GOALCOLOR_BLUE =
"blue";
87 static const std::string REFBOX_CARDCOLOR_YELLOW =
"yellow";
88 static const std::string REFBOX_CARDCOLOR_RED =
"red";
105 const char *refbox_host,
106 unsigned short int refbox_port,
107 const bool use_multicast )
112 __score_cyan = __score_magenta = 0;
114 __refbox_host = strdup(refbox_host);
115 __refbox_port = refbox_port;
117 __use_multicast = use_multicast;
134 Msl2010RefBoxRepeater::reconnect()
140 printf(
"Trying to connect to refbox at %s:%u\n", __refbox_host, __refbox_port);
144 if( __use_multicast ) {
146 printf(
"Creating MulticastDatagramSocket\n");
154 printf(
"check for data availability ...\n");
156 printf(
"... nothing to receive\n");
158 printf(
"... data is available!\n");
165 __s->
connect(__refbox_host, __refbox_port);
179 printf(
"%s",e.
what());
186 printf(
"Connected.\n");
192 Msl2010RefBoxRepeater::process_string(
char *buf,
size_t len)
194 printf(
"Received\n *****\n %s \n *****\n", buf);
196 std::istringstream iss( std::string(buf), std::istringstream::in);
198 dom =
new DomParser();
200 dom->set_substitute_entities();
201 dom->parse_stream(iss);
202 root = dom->get_document()->get_root_node();
206 const Element * el =
dynamic_cast<const Element *
>(root);
211 printf(
"root-element name is '%s'\n", el->get_name().data() );
213 const Node::NodeList nl = el->get_children();
215 if( nl.size() == 0 ) {
216 printf(
"root has NO children!\n");
221 for (Node::NodeList::const_iterator it = nl.begin(); it != nl.end(); ++it) {
222 const Node* node = *it;
223 printf(
"1st level child name is '%s'\n", node->get_name().data() );
235 const Node::NodeList cnl = node->get_children();
237 if( cnl.size() == 0 ) {
238 printf(
"child has NO children!\n");
243 for (Node::NodeList::const_iterator cit = cnl.begin(); cit != cnl.end(); ++cit) {
244 const Node* cnode = *cit;
245 const Element* cel =
dynamic_cast<const Element *
>(cnode);
246 std::string cnodename(cnode->get_name().data());
248 printf(
"2nd level child name is '%s'\n", cnode->get_name().data() );
250 const Attribute* cattr;
251 std::string cteamcolor;
254 std::string cstagetype;
256 if( cnodename == REFBOX_KICKOFF || cnodename == REFBOX_FREEKICK ||
257 cnodename == REFBOX_GOALKICK || cnodename == REFBOX_THROWIN ||
258 cnodename == REFBOX_CORNER || cnodename == REFBOX_PENALTY ||
259 cnodename == REFBOX_GOAL_AWARDED || cnodename == REFBOX_GOAL_REMOVED ||
260 cnodename == REFBOX_CARD_AWARDED || cnodename == REFBOX_CARD_REMOVED ||
261 cnodename == REFBOX_PLAYER_OUT || cnodename == REFBOX_PLAYER_IN ||
262 cnodename == REFBOX_SUBSTITUTION )
264 cattr = cel->get_attribute(
"team");
265 cteamcolor = std::string( cattr->get_value().data() );
268 if( cnodename == REFBOX_CANCEL ) {
270 printf(
"RefBox cancelled last command\n");
272 else if( cnodename == REFBOX_GAMESTOP ) {
273 printf(
"sending command: REFBOX_GAMESTOP\n");
276 else if( cnodename == REFBOX_GAMESTART ) {
277 printf(
"sending command: REFBOX_GAMESTART\n");
280 else if( cnodename == REFBOX_DROPPEDBALL ) {
281 printf(
"sending command: REFBOX_DROPPEDBALL\n");
284 else if( cnodename == REFBOX_GOAL_AWARDED ) {
286 if( cteamcolor == REFBOX_TEAMCOLOR_CYAN ) {
287 printf(
"sending command: REFBOX_TEAMCOLOR_CYAN\n");
288 __rss.
set_score(++__score_cyan, __score_magenta);
290 else if ( cteamcolor == REFBOX_TEAMCOLOR_MAGENTA ) {
291 printf(
"sending command: REFBOX_TEAMCOLOR_MAGENTA\n");
292 __rss.
set_score(__score_cyan, ++__score_magenta);
294 printf(
"sending command: GS_FROZEN\n");
297 else if( cnodename == REFBOX_KICKOFF ) {
298 if( cteamcolor == REFBOX_TEAMCOLOR_CYAN ) {
299 printf(
"sending command: GS_KICK_OFF, TEAM_CYAN\n");
302 else if ( cteamcolor == REFBOX_TEAMCOLOR_MAGENTA ) {
303 printf(
"sending command: GS_KICK_OFF, TEAM_MAGENTA\n");
307 else if( cnodename == REFBOX_PENALTY ) {
308 if( cteamcolor == REFBOX_TEAMCOLOR_CYAN ) {
311 else if ( cteamcolor == REFBOX_TEAMCOLOR_MAGENTA ) {
315 else if( cnodename == REFBOX_CORNER ) {
316 if( cteamcolor == REFBOX_TEAMCOLOR_CYAN ) {
319 else if ( cteamcolor == REFBOX_TEAMCOLOR_MAGENTA ) {
323 else if( cnodename == REFBOX_THROWIN ) {
324 if( cteamcolor == REFBOX_TEAMCOLOR_CYAN ) {
327 else if ( cteamcolor == REFBOX_TEAMCOLOR_MAGENTA ) {
331 else if( cnodename == REFBOX_FREEKICK ) {
332 if( cteamcolor == REFBOX_TEAMCOLOR_CYAN ) {
335 else if ( cteamcolor == REFBOX_TEAMCOLOR_MAGENTA ) {
339 else if( cnodename == REFBOX_GOALKICK ) {
340 if( cteamcolor == REFBOX_TEAMCOLOR_CYAN ) {
343 else if ( cteamcolor == REFBOX_TEAMCOLOR_MAGENTA ) {
347 else if( cnodename == REFBOX_STAGE_CHANGED ) {
348 cattr = cel->get_attribute(
"newStage");
349 cstagetype = std::string( cattr->get_value().data() );
350 if( cstagetype == REFBOX_STAGETYPE_PREGAME ) {
352 }
else if( cstagetype == REFBOX_STAGETYPE_FIRSTHALF ) {
354 }
else if( cstagetype == REFBOX_STAGETYPE_HALFTIME ) {
356 }
else if( cstagetype == REFBOX_STAGETYPE_SECONDHALF ) {
358 }
else if( cstagetype == REFBOX_STAGETYPE_SHOOTOUT ) {
360 }
else if( cstagetype == REFBOX_STAGETYPE_ENDGAME ) {
373 printf(
"root is NOT a valid element\n");
389 size_t bytes_read = __s->
read(tmpbuf,
sizeof(tmpbuf),
false);
391 if ( bytes_read == 0 ) {
393 printf(
"Connection died, reconnecting\n");
396 printf(
"Received %zu bytes, processing ...\n", bytes_read);
397 tmpbuf[bytes_read] =
'\0';
398 process_string(tmpbuf, bytes_read);
virtual void connect(const char *hostname, const unsigned short int port)
Connect socket.
virtual void close()
Close socket.
~Msl2010RefBoxRepeater()
Destructor.
RefBox repeater state sender.
Fawkes library namespace.
virtual void set_gamestate(int game_state, fawkes::worldinfo_gamestate_team_t state_team)
Set current game state.
TCP stream socket over IP.
virtual bool available()
Check if data is available.
virtual const char * what() const
Get primary string.
Base class for exceptions in Fawkes.
Msl2010RefBoxRepeater(RefBoxStateSender &rss, const char *refbox_host, unsigned short int refbox_port, const bool use_multicast=true)
Constructor.
Multicast datagram socket.
virtual size_t read(void *buf, size_t count, bool read_all=true)
Read from socket.
virtual void send()
Send worldinfo.
virtual void set_half(fawkes::worldinfo_gamestate_half_t half)
Set current half of the game time.
virtual void set_score(unsigned int score_cyan, unsigned int score_magenta)
Set score.