Skip to content

Commit 46fdcae

Browse files
committed
Simplify to reduce diff & just use mmLock instead of Value::Listener
1 parent c193bec commit 46fdcae

2 files changed

Lines changed: 120 additions & 118 deletions

File tree

Source/Plugins/NetworkEvents/NetworkEvents.cpp

Lines changed: 119 additions & 110 deletions
Original file line numberDiff line numberDiff line change
@@ -44,36 +44,24 @@ NetworkEvents::NetworkEvents()
4444
, threshold (200.0)
4545
, bufferZone (5.0f)
4646
, state (false)
47-
, urlport (0)
48-
, firstTime (true)
4947
, restart (false)
5048
, changeResponder (false)
5149
{
5250
setProcessorType (PROCESSOR_TYPE_SOURCE);
5351

54-
portString = getPortString();
55-
portString.addListener(this);
52+
firstTime = true;
53+
urlport = 0;
5654

5755
if (!setNewListeningPort(5556))
5856
{
5957
// resort to choosing a port automatically
6058
setNewListeningPort(0);
6159
}
6260

63-
sendSampleCount = false; // disable updating the continuous buffer sample counts,
64-
// since this processor only sends events
65-
6661
startThread();
67-
}
68-
6962

70-
NetworkEvents::~NetworkEvents()
71-
{
72-
if (!stopThread(1000))
73-
{
74-
jassertfalse; // shouldn't block for more than 100 ms, something's wrong
75-
std::cerr << "Network thread timeout. Forcing thread termination, system could be left in an unstable state" << std::endl;
76-
}
63+
sendSampleCount = false; // disable updating the continuous buffer sample counts,
64+
// since this processor only sends events
7765
}
7866

7967

@@ -92,6 +80,16 @@ bool NetworkEvents::setNewListeningPort(uint16 port)
9280
}
9381

9482

83+
NetworkEvents::~NetworkEvents()
84+
{
85+
if (!stopThread(1000))
86+
{
87+
jassertfalse; // shouldn't block for more than 100 ms, something's wrong
88+
std::cerr << "Network thread timeout. Forcing thread termination, system could be left in an unstable state" << std::endl;
89+
}
90+
}
91+
92+
9593
String NetworkEvents::getPortString() const
9694
{
9795
#ifdef ZEROMQ
@@ -257,135 +255,154 @@ String NetworkEvents::handleSpecialMessages(const String& s)
257255
std::vector<String> input = msg.splitString(' ');
258256
if (input[0] == "StartRecord")
259257
{
260-
getUIComponent()->getLogWindow()->addLineToLog("Remote triggered start recording");
261-
262-
if (input.size() > 1)
263-
{
264-
getUIComponent()->getLogWindow()->addLineToLog("Remote setting session name to "+input[1]);
265-
// session name was also given.
266-
getProcessorGraph()->getRecordNode()->setDirectoryName(input[1]);
267-
}
268-
const MessageManagerLock mmLock;
269-
getControlPanel()->recordButton->setToggleState(true,true);
270-
return String("OK");
258+
getUIComponent()->getLogWindow()->addLineToLog("Remote triggered start recording");
259+
260+
if (input.size() > 1)
261+
{
262+
getUIComponent()->getLogWindow()->addLineToLog("Remote setting session name to "+input[1]);
263+
// session name was also given.
264+
getProcessorGraph()->getRecordNode()->setDirectoryName(input[1]);
265+
}
266+
const MessageManagerLock mmLock;
267+
getControlPanel()->recordButton->setToggleState(true,true);
268+
return String("OK");
271269
// getControlPanel()->placeMessageInQueue("StartRecord");
272270
} if (input[0] == "SetSessionName")
273271
{
274-
getProcessorGraph()->getRecordNode()->setDirectoryName(input[1]);
272+
getProcessorGraph()->getRecordNode()->setDirectoryName(input[1]);
275273
} else if (input[0] == "StopRecord")
276274
{
277-
const MessageManagerLock mmLock;
278-
//getControlPanel()->placeMessageInQueue("StopRecord");
279-
getControlPanel()->recordButton->setToggleState(false,true);
280-
return String("OK");
275+
const MessageManagerLock mmLock;
276+
//getControlPanel()->placeMessageInQueue("StopRecord");
277+
getControlPanel()->recordButton->setToggleState(false,true);
278+
return String("OK");
281279
} else if (input[0] == "ProcessorCommunication")
282280
{
283-
ProcessorGraph *g = getProcessorGraph();
284-
Array<GenericProcessor*> p = g->getListOfProcessors();
285-
for (int k=0;k<p.size();k++)
286-
{
287-
if (p[k]->getName().toLowerCase() == input[1].toLowerCase())
288-
{
289-
String Query="";
290-
for (int i=2;i<input.size();i++)
291-
{
292-
if (i == input.size()-1)
293-
Query+=input[i];
294-
else
295-
Query+=input[i]+" ";
296-
}
281+
ProcessorGraph *g = getProcessorGraph();
282+
Array<GenericProcessor*> p = g->getListOfProcessors();
283+
for (int k=0;k<p.size();k++)
284+
{
285+
if (p[k]->getName().toLowerCase() == input[1].toLowerCase())
286+
{
287+
String Query="";
288+
for (int i=2;i<input.size();i++)
289+
{
290+
if (i == input.size()-1)
291+
Query+=input[i];
292+
else
293+
Query+=input[i]+" ";
294+
}
297295
298-
return p[k]->interProcessorCommunication(Query);
299-
}
300-
}
296+
return p[k]->interProcessorCommunication(Query);
297+
}
298+
}
301299
302-
return String("OK");
300+
return String("OK");
303301
}
304302
305303
*/
306304

307305
/** Command is first substring */
308306
String cmd = s.initialSectionNotContaining(" ");
309-
String params = s.substring(cmd.length()).trim();
310307

311308
const MessageManagerLock mmLock;
312-
if (cmd.equalsIgnoreCase("StartAcquisition"))
309+
if (cmd.compareIgnoreCase ("StartAcquisition") == 0)
313310
{
314-
CoreServices::setAcquisitionStatus(true);
315-
return "StartedAcquisition";
311+
if (! CoreServices::getAcquisitionStatus())
312+
{
313+
CoreServices::setAcquisitionStatus (true);
314+
}
315+
return String ("StartedAcquisition");
316316
}
317-
else if (cmd.equalsIgnoreCase("StopAcquisition"))
317+
else if (cmd.compareIgnoreCase ("StopAcquisition") == 0)
318318
{
319-
CoreServices::setAcquisitionStatus(false);
320-
return "StoppedAcquisition";
319+
if (CoreServices::getAcquisitionStatus())
320+
{
321+
CoreServices::setAcquisitionStatus (false);
322+
}
323+
return String ("StoppedAcquisition");
321324
}
322-
else if (cmd.equalsIgnoreCase("StartRecord"))
325+
else if (String ("StartRecord").compareIgnoreCase (cmd) == 0)
323326
{
324-
if (!CoreServices::getRecordingStatus())
327+
if (! CoreServices::getRecordingStatus())
325328
{
326329
/** First set optional parameters (name/value pairs)*/
327-
StringPairArray dict = parseNetworkMessage(params);
328-
329-
StringArray keys = dict.getAllKeys();
330-
for (int i = 0; i < keys.size(); ++i)
330+
if (s.contains ("="))
331331
{
332-
String key = keys[i];
333-
String value = dict[key];
332+
String params = s.substring (cmd.length()).trim();
333+
StringPairArray dict = parseNetworkMessage (params);
334334

335-
if (key.equalsIgnoreCase("CreateNewDir"))
335+
StringArray keys = dict.getAllKeys();
336+
for (int i = 0; i < keys.size(); ++i)
336337
{
337-
if (value.equalsIgnoreCase("1"))
338+
String key = keys[i];
339+
String value = dict[key];
340+
341+
if (key.compareIgnoreCase ("CreateNewDir") == 0)
338342
{
339-
CoreServices::createNewRecordingDir();
343+
if (value.compareIgnoreCase ("1") == 0)
344+
{
345+
CoreServices::createNewRecordingDir();
346+
}
347+
}
348+
else if (key.compareIgnoreCase ("RecDir") == 0)
349+
{
350+
CoreServices::setRecordingDirectory (value);
351+
}
352+
else if (key.compareIgnoreCase ("PrependText") == 0)
353+
{
354+
CoreServices::setPrependTextToRecordingDir (value);
355+
}
356+
else if (key.compareIgnoreCase ("AppendText") == 0)
357+
{
358+
CoreServices::setAppendTextToRecordingDir (value);
340359
}
341-
}
342-
else if (key.equalsIgnoreCase("RecDir"))
343-
{
344-
CoreServices::setRecordingDirectory(value);
345-
}
346-
else if (key.equalsIgnoreCase("PrependText"))
347-
{
348-
CoreServices::setPrependTextToRecordingDir(value);
349-
}
350-
else if (key.equalsIgnoreCase("AppendText"))
351-
{
352-
CoreServices::setAppendTextToRecordingDir(value);
353360
}
354361
}
355362

356363
/** Start recording */
357-
CoreServices::setRecordingStatus(true);
358-
return "StartedRecording";
364+
CoreServices::setRecordingStatus (true);
365+
return String ("StartedRecording");
359366
}
360367
}
361-
else if (cmd.equalsIgnoreCase("StopRecord"))
368+
else if (String ("StopRecord").compareIgnoreCase (cmd) == 0)
362369
{
363-
CoreServices::setRecordingStatus(false);
364-
return "StoppedRecording";
370+
if (CoreServices::getRecordingStatus())
371+
{
372+
CoreServices::setRecordingStatus (false);
373+
return String ("StoppedRecording");
374+
}
365375
}
366-
else if (cmd.equalsIgnoreCase("IsAcquiring"))
376+
else if (cmd.compareIgnoreCase ("IsAcquiring") == 0)
367377
{
368-
return CoreServices::getAcquisitionStatus() ? String("1") : String("0");
378+
String status = CoreServices::getAcquisitionStatus() ? String ("1") : String ("0");
379+
return status;
369380
}
370-
else if (cmd.equalsIgnoreCase("IsRecording"))
381+
else if (cmd.compareIgnoreCase ("IsRecording") == 0)
371382
{
372-
return CoreServices::getRecordingStatus() ? String("1") : String("0");
383+
String status = CoreServices::getRecordingStatus() ? String ("1") : String ("0");
384+
return status;
373385
}
374-
else if (cmd.equalsIgnoreCase("GetRecordingPath"))
386+
else if (cmd.compareIgnoreCase ("GetRecordingPath") == 0)
375387
{
376388
File file = CoreServices::RecordNode::getRecordingPath();
377-
return file.getFullPathName();
389+
String msg (file.getFullPathName());
390+
return msg;
378391
}
379-
else if (cmd.equalsIgnoreCase("GetRecordingNumber"))
392+
else if (cmd.compareIgnoreCase ("GetRecordingNumber") == 0)
380393
{
381-
return String(CoreServices::RecordNode::getRecordingNumber() + 1);
394+
String status;
395+
status += (CoreServices::RecordNode::getRecordingNumber() + 1);
396+
return status;
382397
}
383-
else if (cmd.equalsIgnoreCase("GetExperimentNumber"))
398+
else if (cmd.compareIgnoreCase ("GetExperimentNumber") == 0)
384399
{
385-
return String(CoreServices::RecordNode::getExperimentNumber());
400+
String status;
401+
status += CoreServices::RecordNode::getExperimentNumber();
402+
return status;
386403
}
387404

388-
return "NotHandled";
405+
return String ("NotHandled");
389406
}
390407

391408

@@ -564,23 +581,15 @@ StringPairArray NetworkEvents::parseNetworkMessage(StringRef msg)
564581
}
565582

566583

567-
void NetworkEvents::valueChanged(Value& value)
568-
{
569-
if (value.refersToSameSourceAs(portString))
570-
{
571-
auto ed = static_cast<NetworkEventsEditor*>(getEditor());
572-
if (ed)
573-
{
574-
ed->setPortText(value.toString());
575-
}
576-
}
577-
}
578-
579-
580584
void NetworkEvents::updatePort(uint16 port)
581585
{
582586
urlport = port;
583-
portString = getPortString();
587+
auto ed = static_cast<NetworkEventsEditor*>(getEditor());
588+
if (ed)
589+
{
590+
const MessageManagerLock mmLock;
591+
ed->setPortText(getPortString());
592+
}
584593
}
585594

586595

Source/Plugins/NetworkEvents/NetworkEvents.h

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@
4949
*/
5050
class NetworkEvents : public GenericProcessor
5151
, public Thread
52-
, public Value::Listener
5352
{
5453
public:
5554
NetworkEvents();
@@ -99,9 +98,6 @@ class NetworkEvents : public GenericProcessor
9998

10099
void restartConnection();
101100

102-
// to update the port string from the thread
103-
void valueChanged(Value& value) override;
104-
105101
private:
106102
// combines a string and a timestamp
107103
class StringTS
@@ -181,8 +177,7 @@ class NetworkEvents : public GenericProcessor
181177
//* Split network message into name/value pairs (name1=val1 name2=val2 etc) */
182178
StringPairArray parseNetworkMessage(StringRef msg);
183179

184-
// updates urlport and the portString Value (controlling the port input on the editor)
185-
// 0 indicates disconnected. should only be called from the thread!
180+
// updates urlport and the port input on the editor (0 indicates not connected)
186181
void updatePort(uint16 port);
187182

188183
// get an endpoint url for the given port (using 0 to represent *)
@@ -204,8 +199,6 @@ class NetworkEvents : public GenericProcessor
204199
bool changeResponder;
205200

206201
uint16 urlport; // 0 indicates not connected
207-
// thread can modify this to update editor's port text without acquiring MessageManagerLock
208-
Value portString;
209202

210203
float threshold;
211204
float bufferZone;

0 commit comments

Comments
 (0)