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 
25 #include <blackboard/remote.h>
26 #include <interfaces/GameStateInterface.h>
27 #include <logging/logger.h>
28 
29 #include <cstdlib>
30 #include <cstring>
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  const char * bb_host,
51  unsigned short int bb_port,
52  const char * iface_id)
53 : name_("RBBRefBoxRep")
54 {
55  logger_ = logger;
56  rbb_ = NULL;
57  gamestate_if_ = NULL;
58 
59  message_shown_ = false;
60 
61  bb_host_ = strdup(bb_host);
62  bb_port_ = bb_port;
63  iface_id_ = strdup(iface_id);
64 
65  try {
66  reconnect();
67  } catch (Exception &e) {
68  logger_->log_warn(name_,
69  "Could not connect to remote blackboard, "
70  "will keep trying");
71  }
72 }
73 
74 /** Destructor. */
76 {
77  free(bb_host_);
78  free(iface_id_);
79  if (rbb_) {
80  rbb_->close(gamestate_if_);
81  delete rbb_;
82  }
83 }
84 
85 /** Reconnect to refbox. */
86 void
87 RemoteBlackBoardRefBoxProcessor::reconnect()
88 {
89  if (rbb_) {
90  rbb_->close(gamestate_if_);
91  delete rbb_;
92  }
93  rbb_ = NULL;
94 
95  // logger_->log_info(name_, "Trying to connect to blackboard at %s:%u",
96  // bb_host_, bb_port_);
97  try {
98  rbb_ = new RemoteBlackBoard(bb_host_, bb_port_);
99  gamestate_if_ = rbb_->open_for_reading<GameStateInterface>(iface_id_);
100  } catch (Exception &e) {
101  delete rbb_;
102  rbb_ = NULL;
103  throw;
104  }
105 }
106 
107 void
109 {
110  if (rbb_ && rbb_->is_alive() && gamestate_if_->is_valid()) {
111  try {
112  gamestate_if_->read();
113  _rsh->set_gamestate(gamestate_if_->game_state(),
114  (worldinfo_gamestate_team_t)gamestate_if_->state_team());
115  _rsh->set_score(gamestate_if_->score_cyan(), gamestate_if_->score_magenta());
118  _rsh->set_half((worldinfo_gamestate_half_t)gamestate_if_->half(),
119  gamestate_if_->is_kickoff());
120 
121  } catch (Exception &e) {
122  logger_->log_warn(name_, "Processing BB data failed, exception follows");
123  logger_->log_warn(name_, e);
124  }
125  }
126 }
127 
128 bool
130 {
131  if (!(rbb_ && rbb_->is_alive() && gamestate_if_->is_valid())) {
132  try {
133  reconnect();
134  message_shown_ = false;
135  } catch (Exception &e) {
136  if (!message_shown_) {
137  logger_->log_warn(name_, "Reconnect failed, exception follows");
138  logger_->log_warn(name_, e);
139  message_shown_ = true;
140  }
141  return false;
142  }
143  }
144  return true;
145 }
fawkes::GameStateInterface::half
if_gamestate_half_t half() const
Get half value.
Definition: GameStateInterface.cpp:312
RemoteBlackBoardRefBoxProcessor::~RemoteBlackBoardRefBoxProcessor
~RemoteBlackBoardRefBoxProcessor()
Destructor.
Definition: remotebb.cpp:75
fawkes::GameStateInterface::score_cyan
uint32_t score_cyan() const
Get score_cyan value.
Definition: GameStateInterface.cpp:405
RefBoxStateHandler::set_team_goal
virtual void set_team_goal(fawkes::worldinfo_gamestate_team_t our_team, fawkes::worldinfo_gamestate_goalcolor_t goal_color)=0
RemoteBlackBoardRefBoxProcessor::RemoteBlackBoardRefBoxProcessor
RemoteBlackBoardRefBoxProcessor(fawkes::Logger *logger, const char *bb_host, unsigned short int bb_port, const char *iface_id)
Constructor.
Definition: remotebb.cpp:49
fawkes::Interface::read
void read()
Read from BlackBoard into local copy.
Definition: interface.cpp:477
fawkes::GameStateInterface::our_goal_color
if_gamestate_goalcolor_t our_goal_color() const
Get our_goal_color value.
Definition: GameStateInterface.cpp:281
fawkes::GameStateInterface
Definition: GameStateInterface.h:39
fawkes::RemoteBlackBoard
Definition: remote.h:53
fawkes::GameStateInterface::is_kickoff
bool is_kickoff() const
Get kickoff value.
Definition: GameStateInterface.cpp:343
fawkes::GameStateInterface::game_state
uint32_t game_state() const
Get game_state value.
Definition: GameStateInterface.cpp:188
fawkes::GameStateInterface::state_team
if_gamestate_team_t state_team() const
Get state_team value.
Definition: GameStateInterface.cpp:219
RemoteBlackBoardRefBoxProcessor::check_connection
bool check_connection()
Definition: remotebb.cpp:129
RefBoxStateHandler::set_score
virtual void set_score(unsigned int score_cyan, unsigned int score_magenta)=0
fawkes::BlackBoard::close
virtual void close(Interface *interface)=0
fawkes::Logger
Definition: logger.h:41
fawkes::GameStateInterface::score_magenta
uint32_t score_magenta() const
Get score_magenta value.
Definition: GameStateInterface.cpp:436
fawkes
fawkes::worldinfo_gamestate_goalcolor_t
worldinfo_gamestate_goalcolor_t
Goal color.
Definition: enums.h:67
fawkes::Logger::log_warn
virtual void log_warn(const char *component, const char *format,...)=0
fawkes::worldinfo_gamestate_team_t
worldinfo_gamestate_team_t
Team.
Definition: enums.h:59
fawkes::BlackBoard::is_alive
virtual bool is_alive() const =0
RefBoxStateHandler::set_gamestate
virtual void set_gamestate(int game_state, fawkes::worldinfo_gamestate_team_t state_team)=0
fawkes::GameStateInterface::our_team
if_gamestate_team_t our_team() const
Get our_team value.
Definition: GameStateInterface.cpp:250
fawkes::worldinfo_gamestate_half_t
worldinfo_gamestate_half_t
Game time half.
Definition: enums.h:73
fawkes::BlackBoard::open_for_reading
virtual Interface * open_for_reading(const char *interface_type, const char *identifier, const char *owner=NULL)=0
RefBoxStateHandler::set_half
virtual void set_half(fawkes::worldinfo_gamestate_half_t half, bool kickoff=false)=0
fawkes::Interface::is_valid
bool is_valid() const
Check validity of interface.
Definition: interface.cpp:467
RefBoxProcessor::_rsh
RefBoxStateHandler * _rsh
Refbox state handler, set via set_handler()
Definition: processor.h:40
RemoteBlackBoardRefBoxProcessor::refbox_process
void refbox_process()
Definition: remotebb.cpp:108
fawkes::Exception
Definition: exception.h:41