Jamoma API  0.6.0.a19
WebSocketAnswerManager.cpp
Go to the documentation of this file.
1 /** @file
2  *
3  * @ingroup modularMinuit
4  *
5  * @brief A Protocol interface
6  *
7  * @details
8  *
9  * @authors Laurent Garnier, Théo de la Hogue
10  *
11  * @copyright © 2011, Laurent Garnier, Théo de la Hogue @n
12  * This code is licensed under the terms of the "New BSD License" @n
13  * http://creativecommons.org/licenses/BSD/
14  */
15 
16 
17 #include "WebSocketAnswerManager.h"
18 
19 WebSocketAnswerManager::WebSocketAnswerManager(WebSocketPtr aWebSocketProtocol)
20 {
21  mProtocol = aWebSocketProtocol;
22 
23  mDiscoverAnswers = new TTHash();
24  mDiscoverAllAnswers = new TTHash();
25  mGetAnswers = new TTHash();
26 }
27 
28 WebSocketAnswerManager::~WebSocketAnswerManager()
29 {
30  delete mDiscoverAnswers;
31  delete mDiscoverAllAnswers;
32  delete mGetAnswers;
33 }
34 
35 void WebSocketAnswerManager::AddDiscoverAnswer(TTSymbol from, TTAddress address, int timeOutInMs)
36 {
37  TTString key = from.string();
38  key += address.string();
39 
40  WebSocketAnswerPtr anAnswer = new WebSocketAnswer();
41  anAnswer->setTimeOut(timeOutInMs);
42 
43  mDiscoverAnswers->append(TTSymbol(key), (TTPtr)anAnswer);
44 }
45 
46 TTErr WebSocketAnswerManager::ReceiveDiscoverAnswer(TTSymbol from, TTAddress address, const TTValue& value, TTErr error)
47 {
48  TTValue v;
49  TTString key;
50  WebSocketAnswerPtr anAnswer;
51  TTErr err;
52 
53  key = from.string();
54  key += address.string();
55 
56  err = mDiscoverAnswers->lookup(TTSymbol(key), v);
57 
58  if (!err) {
59  anAnswer = WebSocketAnswerPtr((TTPtr)v[0]);
60 
61  if (anAnswer->getState() != TIMEOUT_EXCEEDED) {
62  anAnswer->setAnswer(value, error);
63  return kTTErrNone;
64  }
65  }
66  else
67  cout << "WebSocketAnswerManager::ReceiveDiscoverAnswer can't find a request at " << key << endl;
68 
69  return kTTErrGeneric;
70 }
71 
72 int WebSocketAnswerManager::CheckDiscoverAnswer(TTSymbol from, TTAddress address, TTValue& value)
73 {
74  int state;
75  TTValue v;
76  TTString key;
77  WebSocketAnswerPtr anAnswer;
78  TTErr err;
79 
80  key = from.string();
81  key += address.string();
82 
83  // Looking for a WebSocketDiscoverAnswer object at the given address
84  err = mDiscoverAnswers->lookup(TTSymbol(key), v);
85 
86  if (!err) {
87  anAnswer = WebSocketAnswerPtr((TTPtr)v[0]);
88 
89  // wait
90  anAnswer->wait();
91 
92  state = anAnswer->getState();
93 
94  // if an answer is received
95  if(state != NO_ANSWER)
96  {
97  // get the answer
98  anAnswer->getAnswer(value);
99  mDiscoverAnswers->remove(TTSymbol(key));
100  delete anAnswer;
101  }
102 
103  return state;
104  }
105  else
106  return REQUEST_NOT_SENT;
107 }
108 
109 TTErr WebSocketAnswerManager::ParseDiscoverAnswer(const TTValue& answer, TTSymbol& returnedType, TTValue& returnedChildren, TTValue& returnedAttributes)
110 {
111  TTSymbol toParse, parsed;
112  TTBoolean endFlagFound;
113 
114  // pars the type
115  returnedType = answer[0];
116 
117  // parse something like : nodes={ all nodes below } attributes={ all current node's attributes }
118  for (TTUInt32 i = 1; i < answer.size(); i++) {
119 
120  toParse = answer[i];
121 
122  // parse nodes
123  if (toParse == TTSymbol(WEBSOCKET_START_NODES)) {
124 
125  endFlagFound = NO;
126  do {
127  i++;
128  parsed = kTTSymEmpty;
129  if (answer[i].type() == kTypeSymbol)
130  parsed = answer[i];
131 
132  // don't store the end flag
133  if (parsed != TTSymbol(WEBSOCKET_END_NODES)) {
134 
135  returnedChildren.append(parsed);
136 
137 #ifdef TT_PROTOCOL_DEBUG
138  cout << "Parsed node " << parsed.c_str() << endl;
139 #endif
140  }
141  else
142  endFlagFound = YES;
143  }
144  while (!endFlagFound);
145 
146  }
147  else if (toParse == TTSymbol(WEBSOCKET_START_ATTRIBUTES)) {
148 
149  endFlagFound = NO;
150  do {
151  i++;
152  parsed = kTTSymEmpty;
153  if (answer[i].type() == kTypeSymbol)
154  parsed = answer[i];
155 
156  // don't store the end flag
157  if (parsed != TTSymbol(WEBSOCKET_END_ATTRIBUTES)) {
158 
159  returnedAttributes.append(parsed);
160 
161 #ifdef TT_PROTOCOL_DEBUG
162  cout << "Parsed attribute " << parsed.c_str() << endl;
163 #endif
164  }
165  else
166  endFlagFound = YES;
167  }
168  while (!endFlagFound);
169 
170  }
171  else
172  i++;
173  } // end for to parse answer
174 
175  return kTTErrNone;
176 }
177 
178 void WebSocketAnswerManager::AddDiscoverAllAnswer(TTSymbol from, TTAddress address, int timeOutInMs)
179 {
180  TTString key = from.string();
181  key += address.string();
182 
183  WebSocketAnswerPtr anAnswer = new WebSocketAnswer();
184  anAnswer->setTimeOut(timeOutInMs);
185 
186  mDiscoverAllAnswers->append(TTSymbol(key), (TTPtr)anAnswer);
187 }
188 
189 TTErr WebSocketAnswerManager::ReceiveDiscoverAllAnswer(TTSymbol from, TTAddress address, const TTValue& value, TTErr error)
190 {
191  // TODO : implement it
192 
193 
194  return kTTErrGeneric;
195 }
196 
197 int WebSocketAnswerManager::CheckDiscoverAllAnswer(TTSymbol from, TTAddress address, TTValue& value)
198 {
199  int state;
200  TTValue v;
201  TTString key;
202  WebSocketAnswerPtr anAnswer;
203  TTErr err;
204 
205  key = from.string();
206  key += address.string();
207 
208  // Looking for a MinuitDiscoverAllAnswer object at the given address
209  err = mDiscoverAllAnswers->lookup(TTSymbol(key), v);
210 
211  if (!err) {
212  anAnswer = WebSocketAnswerPtr((TTPtr)v[0]);
213 
214  // wait
215  anAnswer->wait();
216 
217  state = anAnswer->getState();
218 
219  // if an answer is received
220  if(state != NO_ANSWER)
221  {
222  // get the answer
223  anAnswer->getAnswer(value);
224  mDiscoverAllAnswers->remove(TTSymbol(key));
225  delete anAnswer;
226  }
227 
228  return state;
229  }
230  else
231  return REQUEST_NOT_SENT;
232 }
233 
234 TTErr WebSocketAnswerManager::ParseDiscoverAllAnswer(const TTValue& answer, TTNodePtr node)
235 {
236 
237 
238  return kTTErrNone;
239 }
240 
241 void WebSocketAnswerManager::AddGetAnswer(TTSymbol from, TTAddress address, int timeOutInMs)
242 {
243  TTString key = from.string();
244  key += address.string();
245 
246  WebSocketAnswerPtr anAnswer = new WebSocketAnswer();
247  anAnswer->setTimeOut(timeOutInMs);
248 
249  mGetAnswers->append(TTSymbol(key), (TTPtr)anAnswer);
250 }
251 
252 TTErr WebSocketAnswerManager::ReceiveGetAnswer(TTSymbol from, TTAddress address, const TTValue& value, TTErr error)
253 {
254  TTValue v;
255  TTString key;
256  WebSocketAnswerPtr anAnswer;
257  TTErr err;
258 
259  key = from.string();
260  key += address.string();
261 
262  err = mGetAnswers->lookup(TTSymbol(key), v);
263 
264  if (!err) {
265  anAnswer = WebSocketAnswerPtr((TTPtr)v[0]);
266 
267  if (anAnswer->getState() != TIMEOUT_EXCEEDED) {
268  anAnswer->setAnswer(value, error);
269  return kTTErrNone;
270  }
271  }
272  else
273  cout << "WebSocketAnswerManager::ReceiveGetAnswer can't find a request at " << key << endl;
274 
275  return kTTErrGeneric;
276 }
277 
278 int WebSocketAnswerManager::CheckGetAnswer(TTSymbol from, TTAddress address, TTValue& value)
279 {
280  int state;
281  TTValue v;
282  TTString key;
283  WebSocketAnswerPtr anAnswer;
284  TTErr err;
285 
286  key = from.string();
287  key += address.string();
288 
289  // Looking for a WebSocketDiscoverAnswer object at the given address
290  err = mGetAnswers->lookup(TTSymbol(key), v);
291 
292  if (!err) {
293  anAnswer = WebSocketAnswerPtr((TTPtr)v[0]);
294 
295  // wait
296  anAnswer->wait();
297 
298  state = anAnswer->getState();
299 
300  // if an answer is received
301  if(state != NO_ANSWER)
302  {
303  // get the answer
304  anAnswer->getAnswer(value);
305  mGetAnswers->remove(TTSymbol(key));
306  delete anAnswer;
307  }
308 
309  return state;
310  }
311  else
312  return REQUEST_NOT_SENT;
313 }
bool TTBoolean
Boolean flag, same as Boolean on the Mac.
Definition: TTBase.h:167
We build a directory of TTNodes, and you can request a pointer for any TTNode, or add an observer to ...
Definition: TTNode.h:59
The TTAddress class is used to represent a string and efficiently pass and compare that string...
Definition: TTAddress.h:29
size_type size() const noexcept
Return the number of elements.
Maintain a collection of TTValue objects indexed by TTSymbol pointers.
Definition: TTHash.h:36
Symbol type.
Definition: TTBase.h:282
void append(const T &anElementValueToAppend)
Insert a single TTElement at the end.
Definition: TTValue.h:243
void * TTPtr
A generic pointer.
Definition: TTBase.h:248
Create a web socket plugin.
Definition: WebSocket.h:33
The TTSymbol class is used to represent a string and efficiently pass and compare that string...
Definition: TTSymbol.h:26
const char * c_str() const
Return a pointer to the internal string as a C-string.
Definition: TTSymbol.h:77
Something went wrong, but what exactly is not known. Typically used for context-specific problems...
Definition: TTBase.h:344
TTErr
Jamoma Error Codes Enumeration of error codes that might be returned by any of the TTBlue functions a...
Definition: TTBase.h:342
std::uint32_t TTUInt32
32 bit unsigned integer
Definition: TTBase.h:178
No Error.
Definition: TTBase.h:343
The TTString class is used to represent a string.
Definition: TTString.h:34
A Protocol interface.
[doxygenAppendixC_copyExample]
Definition: TTValue.h:34