The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
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.

281 lines
9.5KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2015 - ROLI Ltd.
  5. Permission is granted to use this software under the terms of either:
  6. a) the GPL v2 (or any later version)
  7. b) the Affero GPL v3
  8. Details of these licenses can be found at: www.gnu.org/licenses
  9. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  11. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  12. ------------------------------------------------------------------------------
  13. To release a closed-source product which uses JUCE, commercial licenses are
  14. available: visit www.juce.com for more information.
  15. ==============================================================================
  16. */
  17. #include "../JuceDemoHeader.h"
  18. #if JUCE_WINDOWS || JUCE_MAC || JUCE_LINUX
  19. //==============================================================================
  20. // This is a token that's used at both ends of our parent-child processes, to
  21. // act as a unique token in the command line arguments.
  22. static const char* demoCommandLineUID = "demoUID";
  23. // A few quick utility functions to convert between raw data and ValueTrees
  24. static ValueTree memoryBlockToValueTree (const MemoryBlock& mb)
  25. {
  26. return ValueTree::readFromData (mb.getData(), mb.getSize());
  27. }
  28. static MemoryBlock valueTreeToMemoryBlock (const ValueTree& v)
  29. {
  30. MemoryOutputStream mo;
  31. v.writeToStream (mo);
  32. return mo.getMemoryBlock();
  33. }
  34. static String valueTreeToString (const ValueTree& v)
  35. {
  36. const ScopedPointer<XmlElement> xml (v.createXml());
  37. return xml != nullptr ? xml->createDocument ("", true, false) : String();
  38. }
  39. //==============================================================================
  40. class ChildProcessDemo : public Component,
  41. private Button::Listener,
  42. private MessageListener
  43. {
  44. public:
  45. ChildProcessDemo()
  46. {
  47. setOpaque (true);
  48. addAndMakeVisible (launchButton);
  49. launchButton.setButtonText ("Launch Child Process");
  50. launchButton.addListener (this);
  51. addAndMakeVisible (pingButton);
  52. pingButton.setButtonText ("Send Ping");
  53. pingButton.addListener (this);
  54. addAndMakeVisible (killButton);
  55. killButton.setButtonText ("Kill Child Process");
  56. killButton.addListener (this);
  57. addAndMakeVisible (testResultsBox);
  58. testResultsBox.setMultiLine (true);
  59. testResultsBox.setFont (Font (Font::getDefaultMonospacedFontName(), 12.0f, Font::plain));
  60. logMessage (String ("This demo uses the ChildProcessMaster and ChildProcessSlave classes to launch and communicate "
  61. "with a child process, sending messages in the form of serialised ValueTree objects.") + newLine);
  62. }
  63. ~ChildProcessDemo()
  64. {
  65. masterProcess = nullptr;
  66. }
  67. void paint (Graphics& g) override
  68. {
  69. fillStandardDemoBackground (g);
  70. }
  71. void resized() override
  72. {
  73. Rectangle<int> area (getLocalBounds());
  74. Rectangle<int> top (area.removeFromTop (40));
  75. launchButton.setBounds (top.removeFromLeft (180).reduced (8));
  76. pingButton.setBounds (top.removeFromLeft (180).reduced (8));
  77. killButton.setBounds (top.removeFromLeft (180).reduced (8));
  78. testResultsBox.setBounds (area.reduced (8));
  79. }
  80. // Appends a message to the textbox that's shown in the demo as the console
  81. void logMessage (const String& message)
  82. {
  83. postMessage (new LogMessage (message));
  84. }
  85. // invoked by the 'launch' button.
  86. void launchChildProcess()
  87. {
  88. if (masterProcess == nullptr)
  89. {
  90. masterProcess = new DemoMasterProcess (*this);
  91. if (masterProcess->launchSlaveProcess (File::getSpecialLocation (File::currentExecutableFile), demoCommandLineUID))
  92. logMessage ("Child process started");
  93. }
  94. }
  95. // invoked by the 'ping' button.
  96. void pingChildProcess()
  97. {
  98. if (masterProcess != nullptr)
  99. masterProcess->sendPingMessageToSlave();
  100. else
  101. logMessage ("Child process is not running!");
  102. }
  103. // invoked by the 'kill' button.
  104. void killChildProcess()
  105. {
  106. if (masterProcess != nullptr)
  107. {
  108. masterProcess = nullptr;
  109. logMessage ("Child process killed");
  110. }
  111. }
  112. //==============================================================================
  113. // This class is used by the main process, acting as the master and receiving messages
  114. // from the slave process.
  115. class DemoMasterProcess : public ChildProcessMaster,
  116. private DeletedAtShutdown
  117. {
  118. public:
  119. DemoMasterProcess (ChildProcessDemo& d) : demo (d), count (0) {}
  120. // This gets called when a message arrives from the slave process..
  121. void handleMessageFromSlave (const MemoryBlock& mb) override
  122. {
  123. ValueTree incomingMessage (memoryBlockToValueTree (mb));
  124. demo.logMessage ("Received: " + valueTreeToString (incomingMessage));
  125. }
  126. // This gets called if the slave process dies.
  127. void handleConnectionLost() override
  128. {
  129. demo.logMessage ("Connection lost to child process!");
  130. demo.killChildProcess();
  131. }
  132. void sendPingMessageToSlave()
  133. {
  134. ValueTree message ("MESSAGE");
  135. message.setProperty ("count", count++, nullptr);
  136. demo.logMessage ("Sending: " + valueTreeToString (message));
  137. sendMessageToSlave (valueTreeToMemoryBlock (message));
  138. }
  139. ChildProcessDemo& demo;
  140. int count;
  141. };
  142. //==============================================================================
  143. ScopedPointer<DemoMasterProcess> masterProcess;
  144. private:
  145. TextButton launchButton, pingButton, killButton;
  146. TextEditor testResultsBox;
  147. void buttonClicked (Button* button) override
  148. {
  149. if (button == &launchButton) launchChildProcess();
  150. if (button == &pingButton) pingChildProcess();
  151. if (button == &killButton) killChildProcess();
  152. }
  153. struct LogMessage : public Message
  154. {
  155. LogMessage (const String& m) : message (m) {}
  156. String message;
  157. };
  158. void handleMessage (const Message& message) override
  159. {
  160. testResultsBox.moveCaretToEnd();
  161. testResultsBox.insertTextAtCaret (static_cast<const LogMessage&> (message).message + newLine);
  162. testResultsBox.moveCaretToEnd();
  163. }
  164. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ChildProcessDemo)
  165. };
  166. //==============================================================================
  167. /* This class gets instantiated in the child process, and receives messages from
  168. the master process.
  169. */
  170. class DemoSlaveProcess : public ChildProcessSlave,
  171. private DeletedAtShutdown
  172. {
  173. public:
  174. DemoSlaveProcess() {}
  175. void handleMessageFromMaster (const MemoryBlock& mb) override
  176. {
  177. ValueTree incomingMessage (memoryBlockToValueTree (mb));
  178. /* In the demo we're only expecting one type of message, which will contain a 'count' parameter -
  179. we'll just increment that number and send back a new message containing the new number.
  180. Obviously in a real app you'll probably want to look at the type of the message, and do
  181. some more interesting behaviour.
  182. */
  183. ValueTree reply ("REPLY");
  184. reply.setProperty ("countPlusOne", static_cast<int> (incomingMessage["count"]) + 1, nullptr);
  185. sendMessageToMaster (valueTreeToMemoryBlock (reply));
  186. }
  187. void handleConnectionMade() override
  188. {
  189. // This method is called when the connection is established, and in response, we'll just
  190. // send off a message to say hello.
  191. ValueTree reply ("HelloWorld");
  192. sendMessageToMaster (valueTreeToMemoryBlock (reply));
  193. }
  194. /* If no pings are received from the master process for a number of seconds, then this will get invoked.
  195. Typically you'll want to use this as a signal to kill the process as quickly as possible, as you
  196. don't want to leave it hanging around as a zombie..
  197. */
  198. void handleConnectionLost() override
  199. {
  200. JUCEApplication::quit();
  201. }
  202. };
  203. //==============================================================================
  204. /* The JuceDemoApplication::initialise method calls this function to allow the
  205. child process to launch when the command line parameters indicate that we're
  206. being asked to run as a child process..
  207. */
  208. bool invokeChildProcessDemo (const String& commandLine)
  209. {
  210. ScopedPointer<DemoSlaveProcess> slave (new DemoSlaveProcess());
  211. if (slave->initialiseFromCommandLine (commandLine, demoCommandLineUID))
  212. {
  213. slave.release(); // allow the slave object to stay alive - it'll handle its own deletion.
  214. return true;
  215. }
  216. return false;
  217. }
  218. // This static object will register this demo type in a global list of demos..
  219. static JuceDemoType<ChildProcessDemo> childProcessDemo ("40 Child Process Comms");
  220. #else
  221. // (Dummy stub for platforms that don't support this demo)
  222. bool invokeChildProcessDemo (const String&) { return false; }
  223. #endif