Fawkes API  Fawkes Development Version
remotebb.cpp
1 
2 /***************************************************************************
3  * remotebb.cpp - Fawkes remote blackboard processor
4  *
5  * Created: Wed Apr 09 10:38:16 2008
6  * Copyright 2010 Tim Niemueller [www.niemueller.de]
7  *
8  ****************************************************************************/
9 
10 /* This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU Library General Public License for more details.
19  *
20  * Read the full text in the LICENSE.GPL file in the doc directory.
21  */
22 
23 #include "remotebb.h"
24 #include <logging/logger.h>
25 
26 #include <blackboard/remote.h>
27 #include <interfaces/GameStateInterface.h>
28 
29 #include <cstring>
30 #include <cstdlib>
31 
32 using namespace fawkes;
33 
34 /** @class RemoteBlackBoardRefBoxProcessor "processor/remotebb.h"
35  * Remote BlackBoard refbox repeater.
36  * This class will establish the connection to a remote blackboard and copy
37  * the refbox information from there to the local state handler.
38  * It can be used as a fallback for unicast communcation to a central
39  * repeater host.
40  * @author Tim Niemueller
41  */
42 
43 /** Constructor.
44  * @param logger logger for output
45  * @param bb_host remote blackboard host
46  * @param bb_port remote blackboard port
47  * @param iface_id ID of the GameStateInterface on the remote blackboard
48  */
50  Logger *logger,
51  const char *bb_host,
52  unsigned short int bb_port,
53  const char *iface_id)
54  : __name("RBBRefBoxRep")
55 {
56  __logger = logger;
57  __rbb = NULL;
58  __gamestate_if = NULL;
59 
60  __message_shown = false;
61 
62  __bb_host = strdup(bb_host);
63  __bb_port = bb_port;
64  __iface_id = strdup(iface_id);
65 
66  try {
67  reconnect();
68  } catch (Exception &e) {
69  __logger->log_warn(__name, "Could not connect to remote blackboard, "
70  "will keep trying");
71  }
72 }
73 
74 
75 /** Destructor. */
77 {
78  free(__bb_host);
79  free(__iface_id);
80  if (__rbb) {
81  __rbb->close(__gamestate_if);
82  delete __rbb;
83  }
84 }
85 
86 
87 /** Reconnect to refbox. */
88 void
89 RemoteBlackBoardRefBoxProcessor::reconnect()
90 {
91  if ( __rbb ) {
92  __rbb->close(__gamestate_if);
93  delete __rbb;
94  }
95  __rbb = NULL;
96 
97  // __logger->log_info(__name, "Trying to connect to blackboard at %s:%u",
98  // __bb_host, __bb_port);
99  try {
100  __rbb = new RemoteBlackBoard(__bb_host, __bb_port);
101  __gamestate_if = __rbb->open_for_reading<GameStateInterface>(__iface_id);
102  } catch (Exception &e) {
103  delete __rbb;
104  __rbb = NULL;
105  throw;
106  }
107 }
108 
109 void
111 {
112  if (__rbb && __rbb->is_alive() && __gamestate_if->is_valid()) {
113  try {
114  __gamestate_if->read();
115  _rsh->set_gamestate(__gamestate_if->game_state(),
116  (worldinfo_gamestate_team_t)__gamestate_if->state_team());
117  _rsh->set_score(__gamestate_if->score_cyan(),
118  __gamestate_if->score_magenta());
121  _rsh->set_half((worldinfo_gamestate_half_t)__gamestate_if->half(),
122  __gamestate_if->is_kickoff());
123 
124  } catch (Exception &e) {
125  __logger->log_warn(__name, "Processing BB data failed, exception follows");
126  __logger->log_warn(__name, e);
127  }
128  }
129 }
130 
131 bool
133 {
134  if (! (__rbb && __rbb->is_alive() && __gamestate_if->is_valid())) {
135  try {
136  reconnect();
137  __message_shown = false;
138  } catch (Exception &e) {
139  if (! __message_shown) {
140  __logger->log_warn(__name, "Reconnect failed, exception follows");
141  __logger->log_warn(__name, e);
142  __message_shown = true;
143  }
144  return false;
145  }
146  }
147  return true;
148 }
bool is_kickoff() const
Get kickoff value.
if_gamestate_team_t our_team() const
Get our_team value.
Fawkes library namespace.
virtual void set_gamestate(int game_state, fawkes::worldinfo_gamestate_team_t state_team)=0
Set current game state.
RefBoxStateHandler * _rsh
Refbox state handler, set via set_handler()
Definition: processor.h:40
bool is_valid() const
Check validity of interface.
Definition: interface.cpp:466
virtual void set_half(fawkes::worldinfo_gamestate_half_t half, bool kickoff=false)=0
Set current half of the game time.
worldinfo_gamestate_half_t
Game time half.
Definition: enums.h:70
void refbox_process()
Process incoming refbox communication.
Definition: remotebb.cpp:110
uint32_t score_cyan() const
Get score_cyan value.
Base class for exceptions in Fawkes.
Definition: exception.h:36
virtual void set_score(unsigned int score_cyan, unsigned int score_magenta)=0
Set score.
void read()
Read from BlackBoard into local copy.
Definition: interface.cpp:477
virtual bool is_alive() const =0
Check if the BlackBoard is still alive.
RemoteBlackBoardRefBoxProcessor(fawkes::Logger *logger, const char *bb_host, unsigned short int bb_port, const char *iface_id)
Constructor.
Definition: remotebb.cpp:49
bool check_connection()
Check if the connection is alive and reconnect.
Definition: remotebb.cpp:132
if_gamestate_goalcolor_t our_goal_color() const
Get our_goal_color value.
virtual void log_warn(const char *component, const char *format,...)=0
Log warning message.
if_gamestate_half_t half() const
Get half value.
Remote BlackBoard.
Definition: remote.h:48
if_gamestate_team_t state_team() const
Get state_team value.
worldinfo_gamestate_team_t
Team.
Definition: enums.h:54
uint32_t game_state() const
Get game_state value.
virtual Interface * open_for_reading(const char *interface_type, const char *identifier, const char *owner=NULL)=0
Open interface for reading.
GameStateInterface Fawkes BlackBoard Interface.
worldinfo_gamestate_goalcolor_t
Goal color.
Definition: enums.h:63
virtual void set_team_goal(fawkes::worldinfo_gamestate_team_t our_team, fawkes::worldinfo_gamestate_goalcolor_t goal_color)=0
Set team and goal info.
uint32_t score_magenta() const
Get score_magenta value.
~RemoteBlackBoardRefBoxProcessor()
Destructor.
Definition: remotebb.cpp:76
virtual void close(Interface *interface)=0
Close interface.
Interface for logging.
Definition: logger.h:34