You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

183 lines
5.4KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCETICE project - Copyright 2009 by Lucio Asnaghi.
  4. JUCETICE is based around the JUCE library - "Jules' Utility Class Extensions"
  5. Copyright 2007 by Julian Storer.
  6. ------------------------------------------------------------------------------
  7. JUCE and JUCETICE can be redistributed and/or modified under the terms of
  8. the GNU General Public License, as published by the Free Software Foundation;
  9. either version 2 of the License, or (at your option) any later version.
  10. JUCE and JUCETICE are distributed in the hope that they will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with JUCE and JUCETICE; if not, visit www.gnu.org/licenses or write to
  16. Free Software Foundation, Inc., 59 Temple Place, Suite 330,
  17. Boston, MA 02111-1307 USA
  18. ==============================================================================
  19. This file is based around Niall Moody's OSC/UDP library, but was modified to
  20. be more independent and modularized, using the best practices from JUCE.
  21. ==============================================================================
  22. */
  23. BEGIN_JUCE_NAMESPACE
  24. //==============================================================================
  25. OpenSoundController::OpenSoundController()
  26. : Thread ("OSCThread"),
  27. port (6789)
  28. {
  29. sock = new UDPSocket ();
  30. sock->setPort (port);
  31. }
  32. OpenSoundController::~OpenSoundController()
  33. {
  34. stopListening ();
  35. deleteAndZero (sock);
  36. }
  37. //==============================================================================
  38. void OpenSoundController::setPort (int newPort)
  39. {
  40. port = newPort;
  41. sock->setPort (port);
  42. }
  43. void OpenSoundController::setRootAddress (const String& address)
  44. {
  45. root = "/" + address + "/";
  46. }
  47. //==============================================================================
  48. void OpenSoundController::startListening()
  49. {
  50. startThread (5);
  51. }
  52. void OpenSoundController::stopListening()
  53. {
  54. if (isThreadRunning ())
  55. stopThread (2000);
  56. }
  57. //==============================================================================
  58. void OpenSoundController::addListener (OpenSoundControllerListener* const listener)
  59. {
  60. listeners.add (listener);
  61. }
  62. void OpenSoundController::removeListener (OpenSoundControllerListener* const listener)
  63. {
  64. int index = listeners.indexOf (listener);
  65. if (index >= 0)
  66. listeners.remove (index);
  67. }
  68. //==============================================================================
  69. void OpenSoundController::run()
  70. {
  71. int i;
  72. OpenSoundBundle *recBundle;
  73. OpenSoundMessage *recMessage;
  74. int recursiveSize;
  75. char *recursiveData;
  76. long receivedSize = -1;
  77. char *receivedData = 0;
  78. sock->bindSocket();
  79. while (! threadShouldExit ())
  80. {
  81. // Listen for OSC packets.
  82. receivedData = sock->getData (receivedSize);
  83. if ((receivedSize > -1) && (receivedData))
  84. {
  85. // If we've received an OSC message.
  86. if (OpenSoundMessage::isMessage (receivedData, receivedSize))
  87. {
  88. recMessage = new OpenSoundMessage (receivedData, receivedSize);
  89. handleOSCMessage (recMessage);
  90. delete recMessage;
  91. }
  92. // If we've received an OSC bundle.
  93. // Note: we don't do anything about timestamps...
  94. else if (OpenSoundBundle::isBundle (receivedData, receivedSize))
  95. {
  96. recBundle = new OpenSoundBundle (receivedData, receivedSize);
  97. for (i = 0; i < recBundle->getNumMessages(); ++i)
  98. {
  99. recursiveSize = recBundle->getMessage(i)->getSize();
  100. recursiveData = recBundle->getMessage(i)->getData();
  101. recMessage = new OpenSoundMessage (recursiveData, recursiveSize);
  102. handleOSCMessage (recMessage);
  103. delete recMessage;
  104. }
  105. delete recBundle;
  106. }
  107. #if JUCE_DEBUG
  108. else
  109. {
  110. printf ("WARNING: Received non-OSC packet: \n");
  111. for (i = 0; i < receivedSize; ++i)
  112. printf ("%c", receivedData[i]);
  113. printf ("\n");
  114. }
  115. #endif
  116. }
  117. }
  118. }
  119. //==============================================================================
  120. void OpenSoundController::handleOSCMessage (OpenSoundMessage *message)
  121. {
  122. for (int i = 0; i < listeners.size (); i++)
  123. {
  124. OpenSoundControllerListener* listener =
  125. (OpenSoundControllerListener*) listeners.getUnchecked (i);
  126. if (listener->handleOSCMessage (this, message))
  127. break;
  128. }
  129. }
  130. //==============================================================================
  131. bool OpenSoundController::isCorrectAddress (const String& address)
  132. {
  133. return address.indexOf (root) > -1;
  134. }
  135. String OpenSoundController::getPathIndexed (const String& address, const int index)
  136. {
  137. int old = 1, next = 1, count = 0;
  138. while ((next = address.indexOf (old, "/")) >= 0)
  139. {
  140. if (++count == index)
  141. return address.substring (old, next);
  142. old = next + 1;
  143. }
  144. if (++count == index)
  145. return address.substring (old);
  146. return String();
  147. }
  148. END_JUCE_NAMESPACE