23 #include <tools/refboxrep/msl2008.h>
24 #include <netcomm/socket/datagram_multicast.h>
33 #include <libxml++/libxml++.h>
36 using namespace xmlpp;
41 static const std::string REFBOX_EVENT =
"RefboxEvent";
42 static const std::string REFBOX_GAMEINFO =
"GameInfo";
43 static const std::string REFBOX_EVENT_REFEREE =
"Referee";
44 static const std::string REFBOX_EVENT_TEAMSETUP =
"TeamSetup";
46 static const std::string REFBOX_CANCEL =
"Cancel";
48 static const std::string REFBOX_GAMESTART =
"GameStart";
49 static const std::string REFBOX_GAMESTOP =
"GameStop";
51 static const std::string REFBOX_STAGE_CHANGED =
"StageChanged";
52 static const std::string REFBOX_STAGETYPE_PREGAME =
"preGame";
53 static const std::string REFBOX_STAGETYPE_FIRSTHALF =
"firstHalf";
54 static const std::string REFBOX_STAGETYPE_HALFTIME =
"halfTime";
55 static const std::string REFBOX_STAGETYPE_SECONDHALF =
"secondHalf";
56 static const std::string REFBOX_STAGETYPE_SHOOTOUT =
"shootOut";
57 static const std::string REFBOX_STAGETYPE_ENDGAME =
"endGame";
59 static const std::string REFBOX_GOAL_AWARDED =
"GoalAwarded";
60 static const std::string REFBOX_GOAL_REMOVED =
"GoalRemoved";
62 static const std::string REFBOX_CARD_AWARDED =
"CardAwarded";
63 static const std::string REFBOX_CARD_REMOVED =
"CardRemoved";
65 static const std::string REFBOX_SUBSTITUTION =
"Substitution";
66 static const std::string REFBOX_PLAYER_OUT =
"PlayerOut";
67 static const std::string REFBOX_PLAYER_IN =
"PlayerIn";
69 static const std::string REFBOX_DROPPEDBALL =
"DroppedBall";
70 static const std::string REFBOX_KICKOFF =
"KickOff";
71 static const std::string REFBOX_FREEKICK =
"FreeKick";
72 static const std::string REFBOX_GOALKICK =
"GoalKick";
73 static const std::string REFBOX_THROWIN =
"ThrowIn";
74 static const std::string REFBOX_CORNER =
"Corner";
75 static const std::string REFBOX_PENALTY =
"Penalty";
77 static const std::string REFBOX_TEAMCOLOR_CYAN =
"Cyan";
78 static const std::string REFBOX_TEAMCOLOR_MAGENTA =
"Magenta";
80 static const std::string REFBOX_GOALCOLOR_YELLOW =
"yellow";
81 static const std::string REFBOX_GOALCOLOR_BLUE =
"blue";
83 static const std::string REFBOX_CARDCOLOR_YELLOW =
"yellow";
84 static const std::string REFBOX_CARDCOLOR_RED =
"red";
100 const char *refbox_host,
101 unsigned short int refbox_port)
106 __score_cyan = __score_magenta = 0;
108 __refbox_host = strdup(refbox_host);
109 __refbox_port = refbox_port;
126 Msl2008RefBoxRepeater::reconnect()
132 printf(
"Trying to connect to refbox at %s:%u\n", __refbox_host, __refbox_port);
135 printf(
"Creating MulticastDatagramSocket\n");
163 Msl2008RefBoxRepeater::process_string(
char *buf,
size_t len)
165 printf(
"Received\n *****\n %s \n *****\n", buf);
167 std::istringstream iss( std::string(buf), std::istringstream::in);
169 dom =
new DomParser();
171 dom->set_substitute_entities();
172 dom->parse_stream(iss);
173 root = dom->get_document()->get_root_node();
177 const Element * el =
dynamic_cast<const Element *
>(root);
182 printf(
"root-element name is '%s'\n", el->get_name().data() );
184 const Node::NodeList nl = el->get_children();
186 if( nl.size() == 0 ) {
187 printf(
"root has NO children!\n");
192 for (Node::NodeList::const_iterator it = nl.begin(); it != nl.end(); ++it) {
193 const Node* node = *it;
194 printf(
"1st level child name is '%s'\n", node->get_name().data() );
206 const Node::NodeList cnl = node->get_children();
208 if( cnl.size() == 0 ) {
209 printf(
"child has NO children!\n");
214 for (Node::NodeList::const_iterator cit = cnl.begin(); cit != cnl.end(); ++cit) {
215 const Node* cnode = *cit;
216 const Element* cel =
dynamic_cast<const Element *
>(cnode);
217 std::string cnodename(cnode->get_name().data());
219 printf(
"2nd level child name is '%s'\n", cnode->get_name().data() );
221 const Attribute* cattr;
222 std::string cteamcolor;
225 std::string cstagetype;
227 if( cnodename == REFBOX_KICKOFF || cnodename == REFBOX_FREEKICK ||
228 cnodename == REFBOX_GOALKICK || cnodename == REFBOX_THROWIN ||
229 cnodename == REFBOX_CORNER || cnodename == REFBOX_PENALTY ||
230 cnodename == REFBOX_GOAL_AWARDED || cnodename == REFBOX_GOAL_REMOVED ||
231 cnodename == REFBOX_CARD_AWARDED || cnodename == REFBOX_CARD_REMOVED ||
232 cnodename == REFBOX_PLAYER_OUT || cnodename == REFBOX_PLAYER_IN ||
233 cnodename == REFBOX_SUBSTITUTION )
235 cattr = cel->get_attribute(
"team");
236 cteamcolor = std::string( cattr->get_value().data() );
239 if( cnodename == REFBOX_CANCEL ) {
241 printf(
"RefBox cancelled last command\n");
243 else if( cnodename == REFBOX_GAMESTOP ) {
246 else if( cnodename == REFBOX_GAMESTART ) {
249 else if( cnodename == REFBOX_DROPPEDBALL ) {
252 else if( cnodename == REFBOX_GOAL_AWARDED ) {
254 if( cteamcolor == REFBOX_TEAMCOLOR_CYAN ) {
255 __rss.
set_score(++__score_cyan, __score_magenta);
257 else if ( cteamcolor == REFBOX_TEAMCOLOR_MAGENTA ) {
258 __rss.
set_score(__score_cyan, ++__score_magenta);
262 else if( cnodename == REFBOX_KICKOFF ) {
263 if( cteamcolor == REFBOX_TEAMCOLOR_CYAN ) {
266 else if ( cteamcolor == REFBOX_TEAMCOLOR_MAGENTA ) {
270 else if( cnodename == REFBOX_PENALTY ) {
271 if( cteamcolor == REFBOX_TEAMCOLOR_CYAN ) {
274 else if ( cteamcolor == REFBOX_TEAMCOLOR_MAGENTA ) {
278 else if( cnodename == REFBOX_CORNER ) {
279 if( cteamcolor == REFBOX_TEAMCOLOR_CYAN ) {
282 else if ( cteamcolor == REFBOX_TEAMCOLOR_MAGENTA ) {
286 else if( cnodename == REFBOX_THROWIN ) {
287 if( cteamcolor == REFBOX_TEAMCOLOR_CYAN ) {
290 else if ( cteamcolor == REFBOX_TEAMCOLOR_MAGENTA ) {
294 else if( cnodename == REFBOX_FREEKICK ) {
295 if( cteamcolor == REFBOX_TEAMCOLOR_CYAN ) {
298 else if ( cteamcolor == REFBOX_TEAMCOLOR_MAGENTA ) {
302 else if( cnodename == REFBOX_GOALKICK ) {
303 if( cteamcolor == REFBOX_TEAMCOLOR_CYAN ) {
306 else if ( cteamcolor == REFBOX_TEAMCOLOR_MAGENTA ) {
310 else if( cnodename == REFBOX_STAGE_CHANGED ) {
311 cattr = cel->get_attribute(
"newStage");
312 cstagetype = std::string( cattr->get_value().data() );
313 if( cstagetype == REFBOX_STAGETYPE_PREGAME ) {
315 }
else if( cstagetype == REFBOX_STAGETYPE_FIRSTHALF ) {
317 }
else if( cstagetype == REFBOX_STAGETYPE_HALFTIME ) {
319 }
else if( cstagetype == REFBOX_STAGETYPE_SECONDHALF ) {
321 }
else if( cstagetype == REFBOX_STAGETYPE_SHOOTOUT ) {
323 }
else if( cstagetype == REFBOX_STAGETYPE_ENDGAME ) {
336 printf(
"root is NOT a valid element\n");
351 size_t bytes_read = __s->
read(tmpbuf,
sizeof(tmpbuf),
false);
352 if ( bytes_read == 0 ) {
354 printf(
"Connection died, reconnecting\n");
357 tmpbuf[bytes_read] =
'\0';
358 process_string(tmpbuf, bytes_read);
Msl2008RefBoxRepeater(RefBoxStateSender &rss, const char *refbox_host, unsigned short int refbox_port)
Constructor.
virtual void close()
Close socket.
RefBox repeater state sender.
Fawkes library namespace.
void set_loop(bool loop)
Set loopback of sent packets.
virtual void bind()
Bind socket.
virtual void set_gamestate(int game_state, fawkes::worldinfo_gamestate_team_t state_team)
Set current game state.
Base class for exceptions in Fawkes.
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.
~Msl2008RefBoxRepeater()
Destructor.
virtual void set_score(unsigned int score_cyan, unsigned int score_magenta)
Set score.