Jamoma API  0.6.0.a19
TTOscReceive.cpp
1 /*
2  * Jamoma OSC Receiver
3  * Copyright © 2011, Théo de la Hogue
4  *
5  * License: This code is licensed under the terms of the "New BSD License"
6  * http://creativecommons.org/licenses/BSD/
7  */
8 
9 #include "TTOscReceive.h"
10 
11 #define thisTTClass TTOscReceive
12 #define thisTTClassName "osc.receive"
13 #define thisTTClassTags "network, receive"
14 
15 
17 mPort(0),
18 mSocket(NULL),
19 mWaitThread(NULL)
20 {
21  if (arguments.size() == 1)
22  mCallback = arguments[0];
23 
25 
26  // callbacks from mSocket
27  addMessageWithArguments(oscSocketReceive);
28  addMessageProperty(oscSocketReceive, hidden, YES);
29 
30  mWaitThread = new TTThread(NULL, NULL);
31 }
32 
33 TTOscReceive::~TTOscReceive()
34 {
35  delete mSocket;
36 
37  // théo : is it necessary ? we used to do this in Minuit::Stop and OSC::Stop to avoid strange crash ...
38  mWaitThread->sleep(1);
39 
40  if (mWaitThread)
41  mWaitThread->wait();
42 
43  delete mWaitThread;
44 }
45 
46 TTErr TTOscReceive::bind()
47 {
48  if (mPort) {
49 
50  delete mSocket;
51 
52  mSocket = new TTOscSocket((TTObjectBasePtr)this, mPort);
53 
54  do {
55  mWaitThread->sleep(1);
56  }
57  while (mSocket->getSocketListenerStatus() == kOscSocketConnectionTrying);
58 
59  if (mSocket->getSocketListenerStatus() == kOscSocketConnectionSucceeded)
60  return kTTErrNone;
61  }
62 
63  return kTTErrGeneric;
64 }
65 
67 {
68  TTUInt16 newPort = newValue;
69 
70  if (mPort != newPort) {
71 
72  mPort = newPort;
73  return bind();
74  }
75 
76  return kTTErrGeneric;
77 }
78 
79 TTErr TTOscReceive::oscSocketReceive(const TTValue& message, TTValue& unusedOutput)
80 {
81  if (mCallback.valid())
82  this->mCallback.send("notify", message, unusedOutput);
83  else
84  this->sendNotification(TTSymbol("receivedMessage"), message);
85 
86  return kTTErrNone;
87 }
std::uint16_t TTUInt16
16 bit unsigned integer
Definition: TTBase.h:176
TTUInt16 mPort
port number
Definition: TTOscReceive.h:22
TTErr send(const TTSymbol aName)
Send a message to this object with no arguments.
Definition: TTObject.cpp:135
TTObject mCallback
an optionnal callback to get message and baton data back
Definition: TTOscReceive.h:24
TTErr sendNotification(const TTSymbol name, const TTValue &arguments)
Send a notification.
Base class for all first-class Jamoma objects.
Definition: TTObjectBase.h:109
TTErr setPort(const TTValue &value)
Setter.
16-bit unsigned integer, range is 0 through 65,535.
Definition: TTBase.h:276
TTOscSocket is ...
Definition: TTOscSocket.h:39
The TTSymbol class is used to represent a string and efficiently pass and compare that string...
Definition: TTSymbol.h:26
TTErr oscSocketReceive(const TTValue &message, TTValue &unusedOutput)
Callbacks from socket receive.
this flag means the OSC socket is trying to connect
Definition: TTOscSocket.h:32
#define addMessageWithArguments(name)
A convenience macro to be used by subclasses for registering messages.
Definition: TTMessage.h:27
TTThreadPtr mWaitThread
a thread used to wait a connection status
Definition: TTOscReceive.h:25
this flag means the OSC socket connection succeeded
Definition: TTOscSocket.h:33
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
#define addAttributeWithSetter(name, type)
A convenience macro to be used by subclasses for registering attributes with a custom setter...
Definition: TTAttribute.h:47
No Error.
Definition: TTBase.h:343
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