Fawkes API  Fawkes Development Version
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
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 }