Jamoma API  0.6.0.a19
WebSocketAnswer.cpp
Go to the documentation of this file.
1 /** @file
2  *
3  * @ingroup modularWebSocket
4  *
5  * @brief WebSocket Answer
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 #include "WebSocketAnswer.h"
17 
18 #ifdef TT_PLATFORM_WIN
19  #include "Time2.hpp"
20 #endif
21 
22 WebSocketAnswer::WebSocketAnswer()
23 {
24  struct timeval tv;
25 
26 #ifdef TT_PLATFORM_WIN
27  Time2 time2;
28  time2.gettimeofday(&tv, NULL);
29 #else
30  struct timezone tz;
31  gettimeofday(&tv, &tz);
32 #endif
33 
34  mThread = new TTThread(NULL, NULL);
35  mAnswer = kTTValNONE;
36  mState = NO_ANSWER;
37  mLaunchTimeInMs = (tv.tv_sec * 1000000L + tv.tv_usec)/1000;
38  mTimeOutInMs = NO_TIMEOUT;
39 }
40 
41 WebSocketAnswer::~WebSocketAnswer()
42 {
43  if (mThread)
44  mThread->wait();
45 
46  delete mThread;
47 }
48 
49 void WebSocketAnswer::setAnswer(const TTValue& value, TTErr error)
50 {
51  mAnswer = value;
52 
53  if (!error)
54  mState = ANSWER_RECEIVED;
55  else
56  mState = ANSWER_ERROR;
57 }
58 
59 void WebSocketAnswer::getAnswer(TTValue& value)
60 {
61  value = mAnswer;
62 }
63 
64 void WebSocketAnswer::setTimeOut(int timeout) {
65  mTimeOutInMs = timeout;
66 }
67 
68 void WebSocketAnswer::wait()
69 {
70  if (mState == NO_ANSWER)
71  mThread->sleep(1);
72 }
73 
74 int WebSocketAnswer::getState()
75 {
76  if ((mState == NO_ANSWER) && (mTimeOutInMs != NO_TIMEOUT)) {
77  struct timeval tv;
78 
79 #ifdef TT_PLATFORM_WIN
80  Time2 time2;
81  time2.gettimeofday(&tv, NULL);
82 #else
83  struct timezone tz;
84  gettimeofday(&tv, &tz);
85 #endif
86 
87  long long dt = 0;
88 
89  dt = (tv.tv_sec * 1000000L + tv.tv_usec)/1000 - mLaunchTimeInMs;
90 
91  if (dt > mTimeOutInMs) {
92  mState = TIMEOUT_EXCEEDED;
93  }
94  }
95  return mState;
96 }
97 
WebSocket Answer.
TTErr
Jamoma Error Codes Enumeration of error codes that might be returned by any of the TTBlue functions a...
Definition: TTBase.h:342
[doxygenAppendixC_copyExample]
Definition: TTValue.h:34