Jamoma API  0.6.0.a19
TTWebReceive.cpp
1 /*
2  * @file TTWebReceive.cpp
3  * @group network library
4  * @brief Jamoma WebSocket Receiver
5  * @see TTWebSocket, TTWebSend
6  *
7  * @copyright © 2013, Laurent Garnier
8  * License: This code is licensed under the terms of the "New BSD License"
9  * http://creativecommons.org/licenses/BSD/
10  */
11 
12 #include "TTWebReceive.h"
13 
14 #define thisTTClass TTWebReceive
15 #define thisTTClassName "web.receive"
16 #define thisTTClassTags "network, receive"
17 
18 
20 mPort(0),
21 mHtmlPath(kTTSymEmpty),
22 mSocket(NULL),
23 mCallback(NULL)
24 {
25  if (arguments.size() == 1)
26  mCallback = arguments[0];
27 
30 
31  // callback from mSocket
32  addMessageWithArguments(WebSocketReceive);
33  addMessageProperty(WebSocketReceive, hidden, YES);
34 }
35 
36 TTWebReceive::~TTWebReceive()
37 {
38  delete mSocket;
39 }
40 
41 TTErr TTWebReceive::bind()
42 {
43  TTErr err = kTTErrNone;
44 
45  if (mPort) {
46  delete mSocket;
47  mSocket = new TTWebSocket((TTObjectBasePtr)this, mPort, mHtmlPath);
48 
49  err = mSocket->bind();
50  }
51  return err;
52 }
53 
55 {
56  TTUInt16 newPort = newValue;
57  TTErr err = kTTErrNone;
58 
59  if (mPort != newPort) {
60  mPort = newPort;
61  err = bind();
62  }
63  return err;
64 }
65 
67 {
68  TTSymbol newHtmlPath = newValue;
69  TTErr err = kTTErrNone;
70 
71  if (mHtmlPath != newHtmlPath) {
72  mHtmlPath = newHtmlPath;
73  err = bind();
74  }
75  return err;
76 }
77 
78 TTErr TTWebReceive::WebSocketReceive(const TTValue& message, TTValue& unusedOutput)
79 {
80  if (mCallback.valid())
81  this->mCallback.send("notify", message, unusedOutput);
82  else
83  this->sendNotification(TTSymbol("receivedMessage"), message);
84 
85  return kTTErrNone;
86 }
std::uint16_t TTUInt16
16 bit unsigned integer
Definition: TTBase.h:176
TTObject mCallback
an optionnal callback to get message and baton data back
Definition: TTWebReceive.h:29
TTErr send(const TTSymbol aName)
Send a message to this object with no arguments.
Definition: TTObject.cpp:135
Create a websocket server.
Definition: TTWebSocket.h:47
TTErr setHtmlPath(const TTValue &value)
Setter.
TTErr sendNotification(const TTSymbol name, const TTValue &arguments)
Send a notification.
Base class for all first-class Jamoma objects.
Definition: TTObjectBase.h:109
16-bit unsigned integer, range is 0 through 65,535.
Definition: TTBase.h:276
The TTSymbol class is used to represent a string and efficiently pass and compare that string...
Definition: TTSymbol.h:26
TTUInt16 mPort
port number
Definition: TTWebReceive.h:26
#define addMessageWithArguments(name)
A convenience macro to be used by subclasses for registering messages.
Definition: TTMessage.h:27
TTErr bind()
Bind websocket server.
TTErr setPort(const TTValue &value)
Setter.
TTSymbol mHtmlPath
path to index.html file
Definition: TTWebReceive.h:27
TTErr
Jamoma Error Codes Enumeration of error codes that might be returned by any of the TTBlue functions a...
Definition: TTBase.h:342
#define addAttributeWithSetter(name, type)
A convenience macro to be used by subclasses for registering attributes with a custom setter...
Definition: TTAttribute.h:47
TTErr WebSocketReceive(const TTValue &message, TTValue &unusedOutput)
Callback from socket receive.
No Error.
Definition: TTBase.h:343
String type.
Definition: TTBase.h:285
TT_OBJECT_CONSTRUCTOR
Constructor macro.
TTBoolean valid() const
Determine if the object contained by this TTObject is truly ready for use.
Definition: TTObject.cpp:179
[doxygenAppendixC_copyExample]
Definition: TTValue.h:34
#define addMessageProperty(messageName, propertyName, initialValue)
A convenience macro to be used for registering properties of messages.
Definition: TTMessage.h:37