gusimplewhiteboard
iOSWhiteboard.cc
Go to the documentation of this file.
1/*
2 * iOSWhiteboard.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#include <cstring>
59#include <cstdio>
60#include <gu_util.h>
61#include <sstream>
62#include "iOSWhiteboard.h"
63
64using namespace guWhiteboard;
65using namespace std;
66
67
68iOSWhiteboard::iOSWhiteboard()
69{
70 int udp_id = getplayernumber();
71
72 if(udp_id > (NUM_OF_BROADCASTERS))
73 {
74 fprintf(stderr, "Bad UDP_ID passed in, exiting...\n\n");
75 exit(EXIT_FAILURE);
76 }
77#ifdef FAKE_BROADCAST
78 udp_id = FAKE_BROADCAST;
79#endif
80
81 set_udp_id(udp_id-1);
82
83
84 timeval tim;
85 gettimeofday(&tim, NULL);
86
87
88#ifdef DEBUG
89 fprintf(stderr, "\n\nMessages to a packet: %d\nPackets to send all messages: %d\nHashes to a packet: %d\nPackets to send all hashes: %d\nInjections to a packet: %d\n\nCYCLES_PER_SECOND: %d\nPACKETS_PER_TS_INTERVAL %d\n", MESSAGES_PER_PACKET, TOTAL_MESSAGE_PACKETS, HASHES_PER_PACKET, TOTAL_HASH_PACKETS, INJECTIONS_PER_PACKET, CYCLES_PER_SEC, PACKETS_PER_TS_INTERVAL);
90
91 fprintf(stderr, "\nMessages sizes:\n\tgsw_single_message:\t%d\n\tgsw_hash_message:\t%d\n\n", (int)sizeof(gsw_single_message), (int)sizeof(gsw_hash_message));
92#endif
93
94
95
96
97 BridgeBroadcaster *broadcaster = new BridgeBroadcaster(NULL, NULL, &dynamic_messages_to_inject, &injection_mutex, tim);
98
99 // WBMsg msg = WBMsg("StringValue");
100 // addInjectionMessage(std::string("TypeToInject"), &msg, 3);
101}
102
103
105{
106
107}
108
109void iOSWhiteboard::convWBMsgToSimpleMsg(WBMsg *value, gsw_simple_message *m)
110{
111 m->wbmsg.type = value->getType();
112 switch (m->wbmsg.type)
113 {
114 case WBMsg::TypeEmpty:
115 m->wbmsg.len = 0;
116 break;
117
118 case WBMsg::TypeBool:
119 m->wbmsg.len = sizeof(int);
120 m->sint = value->getBoolValue();
121 break;
122
123 case WBMsg::TypeInt:
124 m->wbmsg.len = sizeof(int);
125 m->sint = value->getIntValue();
126 break;
127
128 case WBMsg::TypeFloat:
129 m->wbmsg.len = sizeof(float);
130 m->sfloat = value->getFloatValue();
131 break;
132
134 gu_strlcpy(m->wbmsg.data, value->getStringValue().c_str(), sizeof(m->wbmsg.data));
135 m->wbmsg.len = strlen(m->wbmsg.data) + 1;
136 break;
137
138 case WBMsg::TypeArray:
139 {
140 int k = 0;
141 for (std::vector<int>::const_iterator i = value->getArrayValue().begin(); i < value->getArrayValue().end(); i++)
142 m->ivec[k++] = *i;
143 m->wbmsg.len = k;
144 break;
145 }
147 {
148 int len = value->getSizeInBytes();
149 if (len > sizeof(m->wbmsg.data)) len = sizeof(m->wbmsg.data);
150 m->wbmsg.len = len;
151 if (len) memcpy(m->wbmsg.data, value->getBinaryValue(), len);
152 break;
153 }
154 }
155}
156
157static WBMsg getWBMsg(gu_simple_message *m)
158{
159 switch (m->wbmsg.type)
160 {
161 case WBMsg::TypeBool:
162 return WBMsg((bool)m->sint);
163
164 case WBMsg::TypeInt:
165 return WBMsg(m->sint);
166
167 case WBMsg::TypeFloat:
168 return WBMsg(m->sfloat);
169
171 return WBMsg(m->wbmsg.data);
172
174 return WBMsg(m->wbmsg.data, m->wbmsg.len);
175
176 case WBMsg::TypeArray:
177 {
178 vector<int> *v = new vector<int>();
179 for (int i = 0; i < m->wbmsg.len; i++)
180 v->push_back(m->ivec[i]);
181 return WBMsg(*v, true);
182 }
183 default:
184 return WBMsg();
185 }
186 /* NOTREACHED */
187}
188
189void iOSWhiteboard::injectRemoteMessage(const std::string &type, const WBMsg &value, RWBMachine machine)
190{
191 gsw_injection_message msg;
192 msg.machineId = machine;
193
194 strcpy(msg.type.hash.string, (char *)type.c_str());
195
196 convWBMsgToSimpleMsg((WBMsg *)&value, &msg.m);
197
198
199 fprintf(stderr, "Machine: %d\tType %s\tContent %s\n", (msg.machineId+1), msg.type.hash.string, (char *)msg.m.wbmsg.data);
200
201 pthread_mutex_lock(&injection_mutex);
202 dynamic_messages_to_inject.push_back(msg);
203 pthread_mutex_unlock(&injection_mutex);
204}
205
206void iOSWhiteboard::addReplicationType(const std::string &type, RWBMachine machine)
207{
208 //NOT YET SUPPORTED
209}
210
212{
213 //NOT YET SUPPORTED
214}
215
216std::vector<std::string> iOSWhiteboard::getKnownTypesForMachine(RWBMachine machine)
217{
218 //NOT YET SUPPORTED
219}
Old WB class for storing shared data.
Definition: WBMsg.h:86
const std::string & getStringValue() const
getStringValue.
Definition: WBMsg.h:379
int getIntValue() const
getIntValue.
Definition: WBMsg.h:365
const std::vector< int > & getArrayValue() const
getArrayValue.
Definition: WBMsg.h:393
@ TypeEmpty
Definition: WBMsg.h:100
@ TypeString
Definition: WBMsg.h:97
@ TypeBinary
Definition: WBMsg.h:99
@ TypeFloat
Definition: WBMsg.h:96
@ TypeBool
Definition: WBMsg.h:94
@ TypeArray
Definition: WBMsg.h:98
@ TypeInt
Definition: WBMsg.h:95
float getFloatValue() const
getFloatValue.
Definition: WBMsg.h:372
WBType getType() const
getType.
Definition: WBMsg.h:199
const void * getBinaryValue() const
getBinaryValue.
Definition: WBMsg.h:386
int getSizeInBytes() const
getSizeInBytes.
Definition: WBMsg.h:400
bool getBoolValue() const
getBoolValue.
Definition: WBMsg.h:358
WBMsg getMessage(std::string type, RWBMachine machine, WBResult *result=NULL)
Get Message Gets a message from a remote simple whiteboard.
void addReplicationType(const std::string &type, RWBMachine machine)
Add Message Adds a message to the whiteboard that the API is connected to.
void injectRemoteMessage(const std::string &type, const WBMsg &msg, RWBMachine machine)
Inject Message Adds a message to a remote whiteboard that the API is connected to.
std::vector< std::string > getKnownTypesForMachine(RWBMachine machine)
Get Known Types For Machine Gets all the currently known types for a machine.
enum guWhiteboard::iOSWhiteboard::wb_method_result WBResult
Return Type Enum.
enum guWhiteboard::iOSWhiteboard::remote_wb_id RWBMachine
Remote WB enum.
/file APM_Interface.h
union type that is used to store data in shared memory
float sfloat
signed float
int ivec[128/sizeof(int)]
int array
struct gsw_simple_message::@4 wbmsg
compatibility WBMsg type
int sint
signed integer