File: ChildProcessDemo.h

package info (click to toggle)
juce 7.0.5%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 63,880 kB
  • sloc: cpp: 458,845; ansic: 24,200; java: 2,877; xml: 265; python: 216; sh: 135; makefile: 76
file content (377 lines) | stat: -rw-r--r-- 13,488 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
/*
  ==============================================================================

   This file is part of the JUCE examples.
   Copyright (c) 2022 - Raw Material Software Limited

   The code included in this file is provided under the terms of the ISC license
   http://www.isc.org/downloads/software-support-policy/isc-license. Permission
   To use, copy, modify, and/or distribute this software for any purpose with or
   without fee is hereby granted provided that the above copyright notice and
   this permission notice appear in all copies.

   THE SOFTWARE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES,
   WHETHER EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR
   PURPOSE, ARE DISCLAIMED.

  ==============================================================================
*/

/*******************************************************************************
 The block below describes the properties of this PIP. A PIP is a short snippet
 of code that can be read by the Projucer and used to generate a JUCE project.

 BEGIN_JUCE_PIP_METADATA

 name:             ChildProcessDemo
 version:          1.0.0
 vendor:           JUCE
 website:          http://juce.com
 description:      Launches applications as child processes.

 dependencies:     juce_core, juce_data_structures, juce_events, juce_graphics,
                   juce_gui_basics
 exporters:        xcode_mac, vs2022, linux_make

 moduleFlags:      JUCE_STRICT_REFCOUNTEDPOINTER=1

 type:             Console
 mainClass:        ChildProcessDemo

 useLocalCopy:     1

 END_JUCE_PIP_METADATA

*******************************************************************************/

#pragma once

#include "../Assets/DemoUtilities.h"

//==============================================================================
// This is a token that's used at both ends of our parent-child processes, to
// act as a unique token in the command line arguments.
static const char* demoCommandLineUID = "demoUID";

// A few quick utility functions to convert between raw data and ValueTrees
static ValueTree memoryBlockToValueTree (const MemoryBlock& mb)
{
    return ValueTree::readFromData (mb.getData(), mb.getSize());
}

static MemoryBlock valueTreeToMemoryBlock (const ValueTree& v)
{
    MemoryOutputStream mo;
    v.writeToStream (mo);

    return mo.getMemoryBlock();
}

static String valueTreeToString (const ValueTree& v)
{
    if (auto xml = v.createXml())
        return xml->toString (XmlElement::TextFormat().singleLine().withoutHeader());

    return {};
}

//==============================================================================
class ChildProcessDemo   : public Component,
                           private MessageListener
{
public:
    ChildProcessDemo()
    {
        setOpaque (true);

        addAndMakeVisible (launchButton);
        launchButton.onClick = [this] { launchChildProcess(); };

        addAndMakeVisible (pingButton);
        pingButton.onClick = [this] { pingChildProcess(); };

        addAndMakeVisible (killButton);
        killButton.onClick = [this] { killChildProcess(); };

        addAndMakeVisible (testResultsBox);
        testResultsBox.setMultiLine (true);
        testResultsBox.setFont ({ Font::getDefaultMonospacedFontName(), 12.0f, Font::plain });

        logMessage (String ("This demo uses the ChildProcessCoordinator and ChildProcessWorker classes to launch and communicate "
                            "with a child process, sending messages in the form of serialised ValueTree objects.") + newLine
                  + String ("In this demo, the child process will automatically quit if it fails to receive a ping message at least every ")
                  + String (timeoutSeconds)
                  + String (" seconds. To keep the process alive, press the \"")
                  + pingButton.getButtonText()
                  + String ("\" button periodically.") + newLine);

        setSize (500, 500);
    }

    ~ChildProcessDemo() override
    {
        coordinatorProcess.reset();
    }

    void paint (Graphics& g) override
    {
        g.fillAll (getUIColourIfAvailable (LookAndFeel_V4::ColourScheme::UIColour::windowBackground));
    }

    void resized() override
    {
        auto area = getLocalBounds();

        auto top = area.removeFromTop (40);
        launchButton.setBounds (top.removeFromLeft (180).reduced (8));
        pingButton  .setBounds (top.removeFromLeft (180).reduced (8));
        killButton  .setBounds (top.removeFromLeft (180).reduced (8));

        testResultsBox.setBounds (area.reduced (8));
    }

    // Appends a message to the textbox that's shown in the demo as the console
    void logMessage (const String& message)
    {
        postMessage (new LogMessage (message));
    }

    // invoked by the 'launch' button.
    void launchChildProcess()
    {
        if (coordinatorProcess.get() == nullptr)
        {
            coordinatorProcess = std::make_unique<DemoCoordinatorProcess> (*this);

            if (coordinatorProcess->launchWorkerProcess (File::getSpecialLocation (File::currentExecutableFile),
                                                         demoCommandLineUID,
                                                         timeoutMillis))
            {
                logMessage ("Child process started");
            }
        }
    }

    // invoked by the 'ping' button.
    void pingChildProcess()
    {
        if (coordinatorProcess.get() != nullptr)
            coordinatorProcess->sendPingMessageToWorker();
        else
            logMessage ("Child process is not running!");
    }

    // invoked by the 'kill' button.
    void killChildProcess()
    {
        if (coordinatorProcess.get() != nullptr)
        {
            coordinatorProcess.reset();
            logMessage ("Child process killed");
        }
    }

    //==============================================================================
    // This class is used by the main process, acting as the coordinator and receiving messages
    // from the worker process.
    class DemoCoordinatorProcess  : public ChildProcessCoordinator,
                                    private DeletedAtShutdown,
                                    private AsyncUpdater
    {
    public:
        DemoCoordinatorProcess (ChildProcessDemo& d) : demo (d) {}

        ~DemoCoordinatorProcess() override { cancelPendingUpdate(); }

        // This gets called when a message arrives from the worker process..
        void handleMessageFromWorker (const MemoryBlock& mb) override
        {
            auto incomingMessage = memoryBlockToValueTree (mb);

            demo.logMessage ("Received: " + valueTreeToString (incomingMessage));
        }

        // This gets called if the worker process dies.
        void handleConnectionLost() override
        {
            demo.logMessage ("Connection lost to child process!");
            triggerAsyncUpdate();
        }

        void handleAsyncUpdate() override
        {
            demo.killChildProcess();
        }

        void sendPingMessageToWorker()
        {
            ValueTree message ("MESSAGE");
            message.setProperty ("count", count++, nullptr);

            demo.logMessage ("Sending: " + valueTreeToString (message));

            sendMessageToWorker (valueTreeToMemoryBlock (message));
        }

        ChildProcessDemo& demo;
        int count = 0;
    };

    //==============================================================================
    std::unique_ptr<DemoCoordinatorProcess> coordinatorProcess;

    static constexpr auto timeoutSeconds = 10;
    static constexpr auto timeoutMillis = timeoutSeconds * 1000;

private:

    TextButton launchButton  { "Launch Child Process" };
    TextButton pingButton    { "Send Ping" };
    TextButton killButton    { "Kill Child Process" };

    TextEditor testResultsBox;

    struct LogMessage  : public Message
    {
        LogMessage (const String& m) : message (m) {}

        String message;
    };

    void handleMessage (const Message& message) override
    {
        testResultsBox.moveCaretToEnd();
        testResultsBox.insertTextAtCaret (static_cast<const LogMessage&> (message).message + newLine);
        testResultsBox.moveCaretToEnd();
    }

    void lookAndFeelChanged() override
    {
        testResultsBox.applyFontToAllText (testResultsBox.getFont());
    }

    JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ChildProcessDemo)
};

//==============================================================================
/*  This class gets instantiated in the child process, and receives messages from
    the coordinator process.
*/
class DemoWorkerProcess  : public ChildProcessWorker,
                           private DeletedAtShutdown
{
public:
    DemoWorkerProcess() = default;

    void handleMessageFromCoordinator (const MemoryBlock& mb) override
    {
        ValueTree incomingMessage (memoryBlockToValueTree (mb));

        /*  In this demo we're only expecting one type of message, which will contain a 'count' parameter -
            we'll just increment that number and send back a new message containing the new number.

            Obviously in a real app you'll probably want to look at the type of the message, and do
            some more interesting behaviour.
        */

        ValueTree reply ("REPLY");
        reply.setProperty ("countPlusOne", static_cast<int> (incomingMessage["count"]) + 1, nullptr);

        sendMessageToCoordinator (valueTreeToMemoryBlock (reply));
    }

    void handleConnectionMade() override
    {
        // This method is called when the connection is established, and in response, we'll just
        // send off a message to say hello.
        ValueTree reply ("HelloWorld");
        sendMessageToCoordinator (valueTreeToMemoryBlock (reply));
    }

    /* If no pings are received from the coordinator process for a number of seconds, then this will get invoked.
       Typically, you'll want to use this as a signal to kill the process as quickly as possible, as you
       don't want to leave it hanging around as a zombie.
    */
    void handleConnectionLost() override
    {
        JUCEApplication::quit();
    }
};

//==============================================================================
/*  The JUCEApplication::initialise method calls this function to allow the
    child process to launch when the command line parameters indicate that we're
    being asked to run as a child process.
*/
inline bool invokeChildProcessDemo (const String& commandLine)
{
    auto worker = std::make_unique<DemoWorkerProcess>();

    if (worker->initialiseFromCommandLine (commandLine, demoCommandLineUID, ChildProcessDemo::timeoutMillis))
    {
        worker.release(); // allow the worker object to stay alive - it'll handle its own deletion.
        return true;
    }

    return false;
}

#ifndef JUCE_DEMO_RUNNER
 //==============================================================================
 // As we need to modify the JUCEApplication::initialise method to launch the child process
 // based on the command line parameters, we can't just use the normal auto-generated Main.cpp.
 // Instead, we don't do anything in Main.cpp and create a JUCEApplication subclass here with
 // the necessary modifications.
 class Application    : public JUCEApplication
 {
 public:
     //==============================================================================
     Application() {}

     const String getApplicationName() override              { return "ChildProcessDemo"; }
     const String getApplicationVersion() override           { return "1.0.0"; }

     void initialise (const String& commandLine) override
     {
         // launches the child process if the command line parameters contain the demo UID
         if (invokeChildProcessDemo (commandLine))
             return;

         mainWindow = std::make_unique<MainWindow> ("ChildProcessDemo", std::make_unique<ChildProcessDemo>());
     }

     void shutdown() override                                { mainWindow = nullptr; }

 private:
     class MainWindow    : public DocumentWindow
     {
     public:
         MainWindow (const String& name, std::unique_ptr<Component> c)
            : DocumentWindow (name,
                              Desktop::getInstance().getDefaultLookAndFeel()
                                                    .findColour (ResizableWindow::backgroundColourId),
                              DocumentWindow::allButtons)
         {
             setUsingNativeTitleBar (true);
             setContentOwned (c.release(), true);

             centreWithSize (getWidth(), getHeight());

             setVisible (true);
         }

         void closeButtonPressed() override
         {
             JUCEApplication::getInstance()->systemRequestedQuit();
         }

     private:
         JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (MainWindow)
     };

     std::unique_ptr<MainWindow> mainWindow;
 };

 //==============================================================================
 START_JUCE_APPLICATION (Application)
#endif