From 6d195da5d740a60407b2bcc378533279d4417b85 Mon Sep 17 00:00:00 2001
From: Aaron Cuevas Lopez <aacuelo@teleco.upv.es>
Date: Fri, 13 Jan 2017 03:07:28 +0100
Subject: [PATCH] Update network events processor

---
 .../Plugins/NetworkEvents/NetworkEvents.cpp   | 54 +++++++------------
 Source/Plugins/NetworkEvents/NetworkEvents.h  | 12 ++---
 2 files changed, 25 insertions(+), 41 deletions(-)

diff --git a/Source/Plugins/NetworkEvents/NetworkEvents.cpp b/Source/Plugins/NetworkEvents/NetworkEvents.cpp
index 772132e21..ca09af504 100644
--- a/Source/Plugins/NetworkEvents/NetworkEvents.cpp
+++ b/Source/Plugins/NetworkEvents/NetworkEvents.cpp
@@ -224,15 +224,16 @@ bool NetworkEvents::closesocket()
 }
 
 
-int NetworkEvents::getNumEventChannels() const
+void NetworkEvents::createEventChannels()
 {
-    return 1;
-}
-
-
-void NetworkEvents::updateSettings()
-{
-    eventChannels[0]->type = MESSAGE_CHANNEL; // so it's ignored by LFP Viewer
+	EventChannel* chan = new EventChannel(EventChannel::TEXT, 1, MAX_MESSAGE_LENGTH, CoreServices::getGlobalSampleRate(), this);
+	chan->setName("Network messages");
+	chan->setDescription("Messages received through the network events module");
+	chan->setDescriptor("genericevent.network");
+	chan->addEventMetaData(new MetaDataDescriptor(MetaDataDescriptor::INT64, 1, "Software timestamp",
+		"OS high resolution timer count when the event was received", "timestamp.software"));
+	eventChannelArray.add(chan);
+	messageChannel = chan;
 }
 
 
@@ -272,7 +273,7 @@ void NetworkEvents::initSimulation()
 }
 
 
-void NetworkEvents::simulateDesignAndTrials (juce::MidiBuffer& events)
+void NetworkEvents::simulateDesignAndTrials ()
 {
     Time t;
     while (simulation.size() > 0)
@@ -284,7 +285,7 @@ void NetworkEvents::simulateDesignAndTrials (juce::MidiBuffer& events)
             // handle special messages
             handleSpecialMessages (S);
 
-            postTimestamppedStringToMidiBuffer (S,events);
+            postTimestamppedStringToMidiBuffer (S);
             //getUIComponent()->getLogWindow()->addLineToLog(S.getString());
             simulation.pop();
         }
@@ -296,27 +297,12 @@ void NetworkEvents::simulateDesignAndTrials (juce::MidiBuffer& events)
 
 }
 
-
-void NetworkEvents::handleEvent (int eventType, juce::MidiMessage& event, int samplePosition)
+void NetworkEvents::postTimestamppedStringToMidiBuffer (StringTS s)
 {
-}
-
-
-void NetworkEvents::postTimestamppedStringToMidiBuffer (StringTS s, MidiBuffer& events)
-{
-    uint8* msg_with_ts = new uint8[s.len+1];//+8]; // for the two timestamps
-    memcpy (msg_with_ts, s.str, s.len);
-    *(msg_with_ts + s.len) = '\0';
-
-    addEvent (events,
-              (uint8) MESSAGE,
-              0,
-              1,
-              0,
-              (uint8) s.len + 1,//+8,
-              msg_with_ts);
-
-    delete[] msg_with_ts;
+	MetaDataValueArray md;
+	md.add(new MetaDataValue(MetaDataDescriptor::INT64, 1, &s.timestamp));
+	TextEventPtr event = TextEvent::createTextEvent(messageChannel, CoreServices::getGlobalTimestamp(), String::fromUTF8(reinterpret_cast<const char*>(s.str), s.len), md);
+	addEvent(messageChannel, event, 0);
 }
 
 
@@ -537,17 +523,15 @@ String NetworkEvents::handleSpecialMessages (StringTS msg)
 }
 
 
-void NetworkEvents::process (AudioSampleBuffer& buffer, MidiBuffer& events)
+void NetworkEvents::process (AudioSampleBuffer& buffer)
 {
-    setTimestamp (events,CoreServices::getGlobalTimestamp());
-    checkForEvents (events);
-    //simulateDesignAndTrials(events);
+    setTimestampAndSamples(CoreServices::getGlobalTimestamp(),0);
 
     lock.enter();
     while (! networkMessagesQueue.empty())
     {
         StringTS msg = networkMessagesQueue.front();
-        postTimestamppedStringToMidiBuffer (msg, events);
+        postTimestamppedStringToMidiBuffer (msg);
         CoreServices::sendStatusMessage ( ("Network event received: " + msg.getString()).toRawUTF8());
         networkMessagesQueue.pop();
     }
diff --git a/Source/Plugins/NetworkEvents/NetworkEvents.h b/Source/Plugins/NetworkEvents/NetworkEvents.h
index dd6b89df2..a8388a044 100644
--- a/Source/Plugins/NetworkEvents/NetworkEvents.h
+++ b/Source/Plugins/NetworkEvents/NetworkEvents.h
@@ -78,11 +78,11 @@ public:
     // =========================================================================
     AudioProcessorEditor* createEditor() override;
 
-    void process (AudioSampleBuffer& buffer, MidiBuffer& midiMessages) override;
+    void process (AudioSampleBuffer& buffer) override;
 
     void setParameter (int parameterIndex, float newValue) override;
 
-    void updateSettings() override;
+    void createEventChannels() override;
 
     void setEnabledState (bool newState) override;
 
@@ -94,7 +94,6 @@ public:
     float getDefaultSampleRate() const override;
     float getDefaultBitVolts()   const override;
 
-    int getNumEventChannels() const override;
     // =========================================================================
 
     int getDefaultNumOutputs() const;
@@ -105,7 +104,7 @@ public:
     std::vector<String> splitString (String S, char sep);
 
     void initSimulation();
-    void simulateDesignAndTrials (juce::MidiBuffer& events);
+    void simulateDesignAndTrials ();
     void simulateSingleTrial();
     void simulateStartRecord();
     void simulateStopRecord();
@@ -113,7 +112,7 @@ public:
     void opensocket();
     bool closesocket();
 
-    void postTimestamppedStringToMidiBuffer (StringTS s, MidiBuffer& events);
+    void postTimestamppedStringToMidiBuffer (StringTS s);
     void setNewListeningPort (int port);
 
     int urlport;
@@ -122,7 +121,6 @@ public:
 
 
 private:
-    void handleEvent (int eventType, MidiMessage& event, int samplePos) override;
     void createZmqContext();
 
     //* Split network message into name/value pairs (name1=val1 name2=val2 etc) */
@@ -148,6 +146,8 @@ private:
     CriticalSection lock;
     int64 simulationStartTime;
 
+	const EventChannel* messageChannel{ nullptr };
+
     JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (NetworkEvents);
 };
 
-- 
GitLab