gusimplewhiteboard
RemoteWhiteboard.cc
Go to the documentation of this file.
1/*
2 * RemoteWhiteboard.cc
3 *
4 * Created by Carl Lusty on 21/12/11.
5 * Copyright (c) 2011 Carl Lusty.
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 *
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 *
15 * 2. Redistributions in binary form must reproduce the above
16 * copyright notice, this list of conditions and the following
17 * disclaimer in the documentation and/or other materials
18 * provided with the distribution.
19 *
20 * 3. All advertising materials mentioning features or use of this
21 * software must display the following acknowledgement:
22 *
23 * This product includes software developed by Rene Hexel.
24 *
25 * 4. Neither the name of the author nor the names of contributors
26 * may be used to endorse or promote products derived from this
27 * software without specific prior written permission.
28 *
29 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
30 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
31 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
32 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
33 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
34 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
35 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
36 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
37 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
38 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
39 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
40 *
41 * -----------------------------------------------------------------------
42 * This program is free software; you can redistribute it and/or
43 * modify it under the above terms or under the terms of the GNU
44 * General Public License as published by the Free Software Foundation;
45 * either version 2 of the License, or (at your option) any later version.
46 *
47 * This program is distributed in the hope that it will be useful,
48 * but WITHOUT ANY WARRANTY; without even the implied warranty of
49 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
50 * GNU General Public License for more details.
51 *
52 * You should have received a copy of the GNU General Public License
53 * along with this program; if not, see http://www.gnu.org/licenses/
54 * or write to the Free Software Foundation, Inc., 51 Franklin Street,
55 * Fifth Floor, Boston, MA 02110-1301, USA.
56 *
57 */
58#define FROM_WHITEBOARD_CC_
59
60#include <cstring>
61#include <cstdio>
62#include <gu_util.h>
63#include "RemoteWhiteboard.h"
64#include "guudpbridge/guudpManager.h"
65#include <signal.h> // for kill()
66//#include <stdlib.h> // for daemon()
67//#include <sys/types.h>
68//#include <sys/stat.h>
69#include <pthread.h>
70
71using namespace guWhiteboard;
72using namespace std;
73
74static inline bool udp_pid_fail(const pid_t pid)
75{
76 return pid == 0 ||
77 (pid != getpid() &&
78 kill(pid, WHITEBOARD_SIGNAL) != 0);
79}
80
81static void *monitor_bridge(void *local_whiteboard)
82{
83 bool init_run = true;
84 guWhiteboard::Whiteboard *whiteboard = (guWhiteboard::Whiteboard *)local_whiteboard;
85 while(true)
86 {
87 pid_t pid = whiteboard->getMessage("UDP_BRIDGE_PID").getIntValue();
88 if (udp_pid_fail(pid))
89 {
90 fprintf(stderr, "Attempt UDP Bridge start . . . \n");
91 setup_udp_with_id(-1);
92 }
93 if(init_run)
94 {
95 init_run = false;
96// gsw_vacate(whiteboard->_wbd->sem, GSW_SEM_UDP);
97 }
98 protected_usleep(1000000);
99 }
100}
101
102RemoteWhiteboard::RemoteWhiteboard(const char *wbName, RWBMachine n, Whiteboard *local_whiteboard): Whiteboard(wbName, true, n+1), _machine(RWBMachine(n+1)), local_wb_needs_free(local_wb != NULL)
103{
104 if(local_whiteboard == NULL)
105 {
106 local_whiteboard = new Whiteboard();
107 }
108 local_wb = local_whiteboard;
109
110// gsw_procure(local_wb->_wbd->sem, GSW_SEM_UDP);
111
112 pthread_t child;
113 pthread_create(&child, NULL, monitor_bridge, local_wb);
114
115 pid_t pid = -1;
116 int i, timeout = 20;
117 for (i = 0; i < timeout; i++)
118 {
119 pid = local_wb->getMessage("UDP_BRIDGE_PID").getIntValue();
120 if (udp_pid_fail(pid))
121 usleep(50000);
122 else break;
123 }
124 if (i >= timeout)
125 fprintf(stderr, " *** Warning: UDP bridge failed to start, pid = %d\n", pid);
126// gsw_procure(local_wb->_wbd->sem, GSW_SEM_UDP);
127// gsw_vacate(local_wb->_wbd->sem, GSW_SEM_UDP);
128}
129
131{
132 if (local_wb_needs_free && local_wb) delete local_wb;
133}
134
135void RemoteWhiteboard::addReplicationType(const std::string &type)
136{
137 Whiteboard::addMessage(string(ADD_BROADCAST_TYPE_MSG_TYPE), WBMsg(type));
138}
139
141{
143 std::vector<std::string> known_types;
144 int i = 0;
145 while (i < GSW_TOTAL_MESSAGE_TYPES)
146 {
147 if(strlen(_wbd->wb->typenames[i].hash.string) > 0)
148 {
149 known_types.push_back(std::string(_wbd->wb->typenames[i].hash.string));
150 if(known_types.size() > _wbd->wb->num_types)
151 break;
152 }
153 i++;
154 }
156
157 return known_types;
158}
159
160void RemoteWhiteboard::subscribeToMessage(const std::string &type, WBFunctorBase *func, WBResult &result)
161{
162 addReplicationType(type);
163 Whiteboard::subscribeToMessage(type, func, result);
164 protected_usleep(30000);
165}
Base class for WBFunctor.
Definition: WBFunctor.h:41
Old WB class for storing shared data.
Definition: WBMsg.h:86
int getIntValue() const
getIntValue.
Definition: WBMsg.h:365
void addReplicationType(const std::string &type)
virtual ~RemoteWhiteboard()
Destructor.
virtual void subscribeToMessage(const std::string &type, WBFunctorBase *func, WBResult &result)
Subscribe To Message Subscribes to a message type on a whiteboard or whiteboards.
std::vector< std::string > getKnownTypesForMachine()
Get Known Types For Machine Gets all the currently known types for a machine.
compatibility API for accessing the whiteboard
Definition: Whiteboard.h:116
WBMsg getMessage(gsw_hash_info *hashinfo, WBResult *result=NULLPTR)
Get Message Gets a message from a simple whiteboard.
Definition: Whiteboard.cc:194
gu_simple_whiteboard_descriptor * _wbd
underlying whiteboard
Definition: Whiteboard.h:136
virtual void subscribeToMessage(gsw_hash_info *hashinfo, WBFunctorBase *func, WBResult &result)
Subscribe To Message Subscribes to a message type on a whiteboard or whiteboards.
Definition: Whiteboard.cc:227
Whiteboard(const char *sharedMemoryObjectName=gsw_global_whiteboard_name, bool checkVersion=true, int number=0)
API Constructor This sets up the API and the callback soap server.
Definition: Whiteboard.cc:91
void addMessage(gsw_hash_info *hashinfo, const WBMsg &msg, bool nonatomic=false, bool notifySubscribers=true)
Add Message Adds a message to the whiteboard that the API is connected to.
Definition: Whiteboard.cc:130
enum guWhiteboard::Whiteboard::wb_method_result WBResult
Return Type Enum.
int gsw_procure(gsw_sema_t sem, enum gsw_semaphores s)
grab a whiteboard semaphore
int gsw_vacate(gsw_sema_t sem, enum gsw_semaphores s)
release a whiteboard semaphore
#define WHITEBOARD_SIGNAL
#define GSW_TOTAL_MESSAGE_TYPES
message types (max)
@ GSW_SEM_MSGTYPE
semaphore for message type registration
/file APM_Interface.h
enum guWhiteboard::remote_wb_id RWBMachine
Remote WB enum.
gu_simple_message typenames[512]
message types for numbers
uint16_t num_types
total number of current, registered types
gsw_sema_t sem
semaphore to use
gu_simple_whiteboard * wb
the actual whiteboard in shared mem
char string[128]
string type
struct gsw_simple_message::@3 hash
whiteboard hash type