From f9e0b0fc2070ab0726aad8016dce5c6ab0f5c02f Mon Sep 17 00:00:00 2001
From: jsiegle <joshs@alleninstitute.org>
Date: Wed, 14 Jan 2015 17:56:22 -0800
Subject: [PATCH] Fix problems with message saving

---
 Source/Processors/Channel/Channel.cpp         |  4 ++--
 .../GenericProcessor/GenericProcessor.cpp     |  2 +-
 .../GenericProcessor/GenericProcessor.h       |  2 +-
 .../LfpDisplayNode/LfpDisplayNode.cpp         |  9 +++++--
 .../MessageCenter/MessageCenter.cpp           |  5 ++++
 .../NetworkEvents/NetworkEvents.cpp           | 24 +++++++++++++++----
 .../Processors/NetworkEvents/NetworkEvents.h  |  5 ++--
 .../PSTH/PeriStimulusTimeHistogramNode.cpp    |  2 +-
 .../Processors/RecordNode/HDF5Recording.cpp   |  4 ++--
 .../RecordNode/OriginalRecording.cpp          |  6 ++---
 Source/Processors/RecordNode/RecordNode.cpp   |  2 +-
 11 files changed, 45 insertions(+), 20 deletions(-)

diff --git a/Source/Processors/Channel/Channel.cpp b/Source/Processors/Channel/Channel.cpp
index d7f7ec265..843cc340c 100644
--- a/Source/Processors/Channel/Channel.cpp
+++ b/Source/Processors/Channel/Channel.cpp
@@ -121,8 +121,6 @@ void Channel::setName(String name_)
     name = name_;
 }
 
-
-
 void Channel::createDefaultName()
 {
     switch (type)
@@ -148,6 +146,8 @@ void Channel::createDefaultName()
         case TETRODE:
             name = String("TT");
             break;
+        case MESSAGE_CHANNEL:
+            name = String("MSG");
     }
 
     name += index;
diff --git a/Source/Processors/GenericProcessor/GenericProcessor.cpp b/Source/Processors/GenericProcessor/GenericProcessor.cpp
index 8b08e4e64..33f2234b8 100755
--- a/Source/Processors/GenericProcessor/GenericProcessor.cpp
+++ b/Source/Processors/GenericProcessor/GenericProcessor.cpp
@@ -734,7 +734,7 @@ void GenericProcessor::addEvent(MidiBuffer& eventBuffer,
     data[2] = eventId; // event ID (1 = on, 0 = off, usually)
     data[3] = eventChannel; // event channel
     data[4] = 1; // saving flag
-    data[5] = (uint8) channels[0]->sourceNodeId;  // source node ID (for nSamples)
+    data[5] = (uint8) eventChannels[eventChannel]->sourceNodeId;  // source node ID (for nSamples)
     memcpy(data + 6, eventData, numBytes);
 
     //std::cout << "Node id: " << data[1] << std::endl;
diff --git a/Source/Processors/GenericProcessor/GenericProcessor.h b/Source/Processors/GenericProcessor/GenericProcessor.h
index ce62e607d..22bd22228 100755
--- a/Source/Processors/GenericProcessor/GenericProcessor.h
+++ b/Source/Processors/GenericProcessor/GenericProcessor.h
@@ -25,7 +25,7 @@
 #define __GENERICPROCESSOR_H_1F469DAF__
 
 enum ChannelType {HEADSTAGE_CHANNEL = 0, AUX_CHANNEL = 1, ADC_CHANNEL = 2, EVENT_CHANNEL = 3,
-                  SINGLE_ELECTRODE = 4, STEREOTRODE = 5, TETRODE = 6
+                  SINGLE_ELECTRODE = 4, STEREOTRODE = 5, TETRODE = 6, MESSAGE_CHANNEL = 7
                  };
 
 #include "../../../JuceLibraryCode/JuceHeader.h"
diff --git a/Source/Processors/LfpDisplayNode/LfpDisplayNode.cpp b/Source/Processors/LfpDisplayNode/LfpDisplayNode.cpp
index 747fa6e05..5092af286 100755
--- a/Source/Processors/LfpDisplayNode/LfpDisplayNode.cpp
+++ b/Source/Processors/LfpDisplayNode/LfpDisplayNode.cpp
@@ -65,12 +65,12 @@ void LfpDisplayNode::updateSettings()
 
     for (int i = 0; i < eventChannels.size(); i++)
     {
-        if (!eventSourceNodes.contains(eventChannels[i]->sourceNodeId))
+        if (!eventSourceNodes.contains(eventChannels[i]->sourceNodeId) && eventChannels[i]->type == EVENT_CHANNEL)
         {
             eventSourceNodes.add(eventChannels[i]->sourceNodeId);
 
         }
-    }
+    }\
 
     numEventChannels = eventSourceNodes.size();
 
@@ -241,9 +241,14 @@ void LfpDisplayNode::initializeEventChannels()
         int chan = channelForEventSource[eventSourceNodes[i]];
         int index = displayBufferIndex[15]; //displayBufferIndex[chan];
 
+        //std::cout << "Event source node " << i << ", channel " << chan << std::endl;
+
         int samplesLeft = displayBuffer->getNumSamples() - index;
+
         int nSamples = numSamples.at(eventSourceNodes[i]);
 
+
+
         if (nSamples < samplesLeft)
         {
 
diff --git a/Source/Processors/MessageCenter/MessageCenter.cpp b/Source/Processors/MessageCenter/MessageCenter.cpp
index 30c7615b6..e0bba5c8c 100644
--- a/Source/Processors/MessageCenter/MessageCenter.cpp
+++ b/Source/Processors/MessageCenter/MessageCenter.cpp
@@ -35,6 +35,10 @@ MessageCenter::MessageCenter() :
                          44100.0, // sampleRate
                          128);    // blockSize
 
+    Channel* ch = new Channel(this, 0, EVENT_CHANNEL);
+    ch->sourceNodeId = nodeId;
+    eventChannels.add(ch);
+
 }
 
 MessageCenter::~MessageCenter()
@@ -68,6 +72,7 @@ void MessageCenter::setParameter(int parameterIndex, float newValue)
 void MessageCenter::setSourceNodeId(int id)
 {
     sourceNodeId = id;
+    eventChannels[0]->sourceNodeId = id;
 }
 
 int MessageCenter::getSourceNodeId()
diff --git a/Source/Processors/NetworkEvents/NetworkEvents.cpp b/Source/Processors/NetworkEvents/NetworkEvents.cpp
index 0687532b7..835d1b2e1 100644
--- a/Source/Processors/NetworkEvents/NetworkEvents.cpp
+++ b/Source/Processors/NetworkEvents/NetworkEvents.cpp
@@ -159,7 +159,7 @@ NetworkEvents::NetworkEvents(void *zmq_context)
 void NetworkEvents::setNewListeningPort(int port)
 {
 	// first, close existing thread.
-	disable();
+	closesocket();
 	// allow some time for thread to quit
 #ifdef WIN32
 	Sleep(300);
@@ -175,11 +175,14 @@ void NetworkEvents::setNewListeningPort(int port)
 NetworkEvents::~NetworkEvents()
 {
 	shutdown = true;
-	disable();
+	closesocket();
 }
 
-bool NetworkEvents::disable()
+bool NetworkEvents::closesocket()
 {
+
+std::cout << "Disabling network node" << std::endl;
+
 #ifdef ZEROMQ 
 	if (threadRunning)
 	{
@@ -193,6 +196,16 @@ bool NetworkEvents::disable()
 	return true;
 }
 
+int NetworkEvents::getNumEventChannels()
+{
+	return 1;
+}
+
+void NetworkEvents::updateSettings()
+{
+	eventChannels[0]->type = MESSAGE_CHANNEL; // so it's ignored by LFP Viewer
+}
+
 AudioProcessorEditor* NetworkEvents::createEditor(
 ){
     editor = new NetworkEventsEditor(this, true);
@@ -265,6 +278,7 @@ void NetworkEvents::postTimestamppedStringToMidiBuffer(StringTS s, MidiBuffer& e
 	uint8* msg_with_ts = new uint8[s.len+8]; // for the two timestamps
 	memcpy(msg_with_ts, s.str, s.len);	
 	memcpy(msg_with_ts+s.len, &s.timestamp, 8);
+	
 	addEvent(events, 
 			 (uint8) NETWORK,
 			 0,
@@ -440,7 +454,7 @@ void NetworkEvents::run() {
 			networkMessagesQueue.push(Msg);
 		    lock.exit();
 
-		    std::cout << "Received message!" << std::endl;
+		    //std::cout << "Received message!" << std::endl;
 			// handle special messages
 			String response = handleSpecialMessages(Msg);
 	
@@ -448,7 +462,7 @@ void NetworkEvents::run() {
 		} else 
 		{
 			String zeroMessageError = "Recieved Zero Message?!?!?";
-			std::cout << "Received Zero Message!" << std::endl;
+			//std::cout << "Received Zero Message!" << std::endl;
 
 			zmq_send (responder, zeroMessageError.getCharPointer(), zeroMessageError.length(), 0);
 		}
diff --git a/Source/Processors/NetworkEvents/NetworkEvents.h b/Source/Processors/NetworkEvents/NetworkEvents.h
index 92f780fc1..64d235f23 100644
--- a/Source/Processors/NetworkEvents/NetworkEvents.h
+++ b/Source/Processors/NetworkEvents/NetworkEvents.h
@@ -90,10 +90,11 @@ public:
 
 	void simulateStartRecord();
 	void simulateStopRecord();
-	bool disable();
+	bool closesocket();
 	void run();
 	void opensocket();
 
+	void updateSettings();
 
 	bool isReady();
 	float getDefaultSampleRate();
@@ -101,7 +102,7 @@ public:
 	float getDefaultBitVolts();
 	void enabledState(bool t);
 
-
+	int getNumEventChannels();
 
 	void postTimestamppedStringToMidiBuffer(StringTS s, MidiBuffer& events);
 	void setNewListeningPort(int port);
diff --git a/Source/Processors/PSTH/PeriStimulusTimeHistogramNode.cpp b/Source/Processors/PSTH/PeriStimulusTimeHistogramNode.cpp
index b4659e074..3aa78eb1f 100644
--- a/Source/Processors/PSTH/PeriStimulusTimeHistogramNode.cpp
+++ b/Source/Processors/PSTH/PeriStimulusTimeHistogramNode.cpp
@@ -39,7 +39,7 @@ PeriStimulusTimeHistogramNode::PeriStimulusTimeHistogramNode()
 	trialCircularBuffer  = nullptr;
 	eventFile = nullptr;
 	isRecording = false;
-	saveEyeTracking = saveTTLs = saveNetworkEvents = true;
+	saveEyeTracking = saveTTLs = saveNetworkEvents = false;
 	saveNetworkEventsWhenNotRecording = false;
 	spikeSavingMode = 2;
 	syncCounter = 0;
diff --git a/Source/Processors/RecordNode/HDF5Recording.cpp b/Source/Processors/RecordNode/HDF5Recording.cpp
index 4a4938553..253662aed 100644
--- a/Source/Processors/RecordNode/HDF5Recording.cpp
+++ b/Source/Processors/RecordNode/HDF5Recording.cpp
@@ -172,8 +172,8 @@ void HDF5Recording::writeEvent(int eventType, MidiMessage& event, int samplePosi
     const uint8* dataptr = event.getRawData();
     if (eventType == GenericProcessor::TTL)
         mainFile->writeEvent(0,*(dataptr+2),*(dataptr+1),(void*)(dataptr+3),timestamp+samplePosition);
-    else if (eventType == GenericProcessor::MESSAGE)
-        mainFile->writeEvent(1,*(dataptr+2),*(dataptr+1),(void*)(dataptr+4),timestamp+samplePosition);
+    else if (eventType == GenericProcessor::MESSAGE || eventType == GenericProcessor::NETWORK)
+        mainFile->writeEvent(1,*(dataptr+2),*(dataptr+1),(void*)(dataptr+6),timestamp+samplePosition);
 }
 
 void HDF5Recording::addSpikeElectrode(int index, SpikeRecordInfo* elec)
diff --git a/Source/Processors/RecordNode/OriginalRecording.cpp b/Source/Processors/RecordNode/OriginalRecording.cpp
index d0d3eb0a9..f6c95fd54 100644
--- a/Source/Processors/RecordNode/OriginalRecording.cpp
+++ b/Source/Processors/RecordNode/OriginalRecording.cpp
@@ -368,7 +368,7 @@ void OriginalRecording::writeEvent(int eventType, MidiMessage& event, int sample
 {
     if (eventType == GenericProcessor::TTL)
         writeTTLEvent(event,samplePosition);
-    else if (eventType == GenericProcessor::MESSAGE)
+    else if (eventType == GenericProcessor::MESSAGE || eventType == GenericProcessor::NETWORK)
         writeMessage(event,samplePosition);
 }
 
@@ -381,8 +381,8 @@ void OriginalRecording::writeMessage(MidiMessage& event, int samplePosition)
 
     int64 eventTimestamp = (*timestamps)[sourceNodeId] + samplePos;
 
-    int msgLength = event.getRawDataSize() - 5;
-    const char* dataptr = (const char*)event.getRawData() + 4;
+    int msgLength = event.getRawDataSize() - 6;
+    const char* dataptr = (const char*)event.getRawData() + 6;
 
     String timestampText(eventTimestamp);
 
diff --git a/Source/Processors/RecordNode/RecordNode.cpp b/Source/Processors/RecordNode/RecordNode.cpp
index 48b283eee..f51f361e3 100755
--- a/Source/Processors/RecordNode/RecordNode.cpp
+++ b/Source/Processors/RecordNode/RecordNode.cpp
@@ -412,7 +412,7 @@ void RecordNode::handleEvent(int eventType, MidiMessage& event, int samplePositi
 {
     if (isRecording && allFilesOpened)
     {
-        if ((eventType == TTL) || (eventType == MESSAGE))
+        if ((eventType == TTL) || (eventType == MESSAGE) || (eventType == NETWORK))
         {
             if (event.getRawData()+4 > 0) // saving flag > 0 (i.e., event has not already been processed)
             {
-- 
GitLab