Skip to content

Commit 877677e

Browse files
committed
Revert to last good port if a connection fails
1 parent 1c13ce2 commit 877677e

3 files changed

Lines changed: 70 additions & 14 deletions

File tree

Source/Plugins/NetworkEvents/NetworkEvents.cpp

Lines changed: 50 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -163,9 +163,13 @@ NetworkEvents::NetworkEvents()
163163
, threshold (200.0)
164164
, bufferZone (5.0f)
165165
, state (false)
166+
, connectionErr (var(false))
167+
, lastGoodPort (0)
166168
{
167169
setProcessorType (PROCESSOR_TYPE_SOURCE);
168170

171+
connectionErr.addListener(this);
172+
169173
createZmqContext();
170174

171175
firstTime = true;
@@ -193,7 +197,15 @@ void NetworkEvents::setNewListeningPort (int port)
193197
#endif
194198

195199
urlport = port;
200+
connectionErr = false;
196201
opensocket();
202+
203+
// update editor
204+
auto ed = static_cast<NetworkEventsEditor*>(getEditor());
205+
if (ed)
206+
{
207+
ed->setPortString(String(port));
208+
}
197209
}
198210

199211

@@ -204,6 +216,19 @@ NetworkEvents::~NetworkEvents()
204216
}
205217

206218

219+
void NetworkEvents::valueChanged(Value& value)
220+
{
221+
if (value.refersToSameSourceAs(connectionErr))
222+
{
223+
if ((bool)value.getValue() && lastGoodPort > 0 && lastGoodPort != urlport)
224+
{
225+
// try to connect to the last good port
226+
setNewListeningPort(lastGoodPort);
227+
}
228+
}
229+
}
230+
231+
207232
bool NetworkEvents::closesocket()
208233
{
209234
std::cout << "Disabling network node" << std::endl;
@@ -555,24 +580,28 @@ void NetworkEvents::opensocket()
555580
void NetworkEvents::run()
556581
{
557582
#ifdef ZEROMQ
558-
SocketPtr responder(zmq_socket(zmqcontext, ZMQ_REP), &closeZmqSocket);
583+
Responder responder(zmqcontext);
559584
String url= String ("tcp://*:") + String (urlport);
560-
int rc = zmq_bind (responder.get(), url.toRawUTF8());
585+
int rc = zmq_bind (responder, url.toRawUTF8());
561586

562587
if (rc != 0)
563588
{
564589
// failed to open or bind socket?
565-
std::cout << "Failed to open socket: " << zmq_strerror (zmq_errno()) << std::endl;
590+
String msg = String("Network Events failed to open socket: ") + zmq_strerror(zmq_errno());
591+
std::cout << msg << std::endl;
592+
CoreServices::sendStatusMessage(msg);
593+
connectionErr = true;
566594
return;
567595
}
568596

569597
threadRunning = true;
598+
lastGoodPort = urlport;
570599
HeapBlock<unsigned char> buffer(MAX_MESSAGE_LENGTH);
571600
int result = -1;
572601

573602
while (threadRunning)
574603
{
575-
result = zmq_recv (responder.get(), buffer, MAX_MESSAGE_LENGTH - 1, 0); // blocking
604+
result = zmq_recv (responder, buffer, MAX_MESSAGE_LENGTH - 1, 0); // blocking
576605

577606
juce::int64 timestamp_software = timer.getHighResolutionTicks();
578607

@@ -590,19 +619,18 @@ void NetworkEvents::run()
590619
// handle special messages
591620
String response = handleSpecialMessages (Msg);
592621

593-
zmq_send (responder.get(), response.getCharPointer(), response.length(), 0);
622+
zmq_send (responder, response.getCharPointer(), response.length(), 0);
594623
}
595624
else
596625
{
597626
String zeroMessageError = "Recieved Zero Message?!?!?";
598627
//std::cout << "Received Zero Message!" << std::endl;
599628

600-
zmq_send (responder.get(), zeroMessageError.getCharPointer(), zeroMessageError.length(), 0);
629+
zmq_send (responder, zeroMessageError.getCharPointer(), zeroMessageError.length(), 0);
601630
}
602631
}
603632

604633
threadRunning = false;
605-
606634
return;
607635
#endif
608636
}
@@ -651,9 +679,7 @@ void NetworkEvents::loadCustomParametersFromXml()
651679
{
652680
if (mainNode->hasTagName ("NETWORKEVENTS"))
653681
{
654-
auto ed = static_cast<NetworkEventsEditor*>(getEditor());
655-
if (!ed) { return; }
656-
ed->setPortString(mainNode->getStringAttribute("port"));
682+
setNewListeningPort(mainNode->getIntAttribute("port"));
657683
}
658684
}
659685
}
@@ -671,14 +697,27 @@ void NetworkEvents::createZmqContext()
671697
}
672698

673699

674-
void NetworkEvents::closeZmqSocket(void* socket)
700+
NetworkEvents::Responder::Responder(void* context)
701+
#ifdef ZEROMQ
702+
: socket(zmq_socket(context, ZMQ_REP))
703+
#endif
704+
{}
705+
706+
707+
NetworkEvents::Responder::~Responder()
675708
{
676709
#ifdef ZEROMQ
677710
zmq_close(socket);
678711
#endif
679712
}
680713

681714

715+
NetworkEvents::Responder::operator void*()
716+
{
717+
return socket;
718+
}
719+
720+
682721
StringPairArray NetworkEvents::parseNetworkMessage (String msg)
683722
{
684723
StringArray splitted;

Source/Plugins/NetworkEvents/NetworkEvents.h

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ class StringTS
7070
*/
7171
class NetworkEvents : public GenericProcessor
7272
, public Thread
73+
, public Value::Listener
7374
{
7475
public:
7576
NetworkEvents();
@@ -116,6 +117,10 @@ class NetworkEvents : public GenericProcessor
116117
void postTimestamppedStringToMidiBuffer (StringTS s);
117118
void setNewListeningPort (int port);
118119

120+
// to monitor responderStatus and connect to last good port if necessary
121+
void valueChanged(Value& value) override;
122+
123+
119124
int urlport;
120125
String socketStatus;
121126
std::atomic<bool> threadRunning;
@@ -124,8 +129,20 @@ class NetworkEvents : public GenericProcessor
124129
private:
125130
void createZmqContext();
126131

127-
static void closeZmqSocket(void* socket);
128-
typedef std::unique_ptr<void, decltype(&closeZmqSocket)> SocketPtr;
132+
// RAII wrapper for ZMQ socket
133+
class Responder
134+
{
135+
public:
136+
Responder(void* context);
137+
~Responder();
138+
operator void*();
139+
private:
140+
void* socket;
141+
};
142+
143+
// allow reconnecting to last good port if connection fails
144+
Value connectionErr;
145+
int lastGoodPort;
129146

130147
//* Split network message into name/value pairs (name1=val1 name2=val2 etc) */
131148
StringPairArray parseNetworkMessage (String msg);

Source/Plugins/NetworkEvents/NetworkEventsEditor.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ void NetworkEventsEditor::setLabelColor(juce::Colour color)
117117

118118
void NetworkEventsEditor::setPortString(const String& port)
119119
{
120-
labelPort->setText(port, sendNotification);
120+
labelPort->setText(port, dontSendNotification);
121121
}
122122

123123
void NetworkEventsEditor::labelTextChanged(juce::Label *label)

0 commit comments

Comments
 (0)