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
32using 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. */
86void
87RemoteBlackBoardRefBoxProcessor::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
107void
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
128bool
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}
RefBoxStateHandler * _rsh
Refbox state handler, set via set_handler()
Definition: processor.h:40
virtual void set_gamestate(int game_state, fawkes::worldinfo_gamestate_team_t state_team)=0
Set current game state.
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.
virtual void set_score(unsigned int score_cyan, unsigned int score_magenta)=0
Set score.
virtual void set_half(fawkes::worldinfo_gamestate_half_t half, bool kickoff=false)=0
Set current half of the game time.
bool check_connection()
Check if the connection is alive and reconnect.
Definition: remotebb.cpp:129
~RemoteBlackBoardRefBoxProcessor()
Destructor.
Definition: remotebb.cpp:75
RemoteBlackBoardRefBoxProcessor(fawkes::Logger *logger, const char *bb_host, unsigned short int bb_port, const char *iface_id)
Constructor.
Definition: remotebb.cpp:49
void refbox_process()
Process incoming refbox communication.
Definition: remotebb.cpp:108
virtual bool is_alive() const noexcept=0
Check if the BlackBoard is still alive.
virtual Interface * open_for_reading(const char *interface_type, const char *identifier, const char *owner=NULL)=0
Open interface for reading.
virtual void close(Interface *interface)=0
Close interface.
Base class for exceptions in Fawkes.
Definition: exception.h:36
GameStateInterface Fawkes BlackBoard Interface.
if_gamestate_team_t our_team() const
Get our_team value.
uint32_t game_state() const
Get game_state value.
uint32_t score_cyan() const
Get score_cyan value.
if_gamestate_team_t state_team() const
Get state_team value.
bool is_kickoff() const
Get kickoff value.
if_gamestate_half_t half() const
Get half value.
uint32_t score_magenta() const
Get score_magenta value.
if_gamestate_goalcolor_t our_goal_color() const
Get our_goal_color value.
bool is_valid() const
Check validity of interface.
Definition: interface.cpp:469
void read()
Read from BlackBoard into local copy.
Definition: interface.cpp:479
Interface for logging.
Definition: logger.h:42
virtual void log_warn(const char *component, const char *format,...)=0
Log warning message.
Remote BlackBoard.
Definition: remote.h:50
Fawkes library namespace.
worldinfo_gamestate_goalcolor_t
Goal color.
Definition: enums.h:61
worldinfo_gamestate_half_t
Game time half.
Definition: enums.h:67
worldinfo_gamestate_team_t
Team.
Definition: enums.h:53