Commit 41b2f83b authored by Richard Newton's avatar Richard Newton

Merge pull request #714 from hintjens/master

Cleaned up the code
parents 08c91c0f 406e6051
...@@ -76,8 +76,15 @@ int zmq::proxy ( ...@@ -76,8 +76,15 @@ int zmq::proxy (
{ control_, 0, ZMQ_POLLIN, 0 } { control_, 0, ZMQ_POLLIN, 0 }
}; };
int qt_poll_items = (control_ ? 3 : 2); int qt_poll_items = (control_ ? 3 : 2);
enum {suspend, resume, terminate} state = resume;
while (state != terminate) { // Proxy can be in these three states
enum {
active,
paused,
terminated
} state = active;
while (state != terminated) {
// Wait while there are either requests or replies to process. // Wait while there are either requests or replies to process.
rc = zmq_poll (&items [0], qt_poll_items, -1); rc = zmq_poll (&items [0], qt_poll_items, -1);
if (unlikely (rc < 0)) if (unlikely (rc < 0))
...@@ -107,24 +114,23 @@ int zmq::proxy ( ...@@ -107,24 +114,23 @@ int zmq::proxy (
if (unlikely (rc < 0)) if (unlikely (rc < 0))
return -1; return -1;
} }
if (msg.size () == 6 && memcmp (msg.data (), "PAUSE", 6) == 0)
// process control command state = paused;
int size = msg.size();
char* message = (char*) malloc(size + 1);
memcpy(message, msg.data(), size);
message[size] = '\0';
if (size == 8 && !memcmp(message, "SUSPEND", 8))
state = suspend;
else if (size == 7 && !memcmp(message, "RESUME", 7))
state = resume;
else if (size == 10 && !memcmp(message, "TERMINATE", 10))
state = terminate;
else else
fprintf(stderr, "Warning : \"%s\" bad command received by proxy\n", message); // prefered compared to "return -1" if (msg.size () == 7 && memcmp (msg.data (), "RESUME", 7) == 0)
free (message); state = active;
else
if (msg.size () == 10 && memcmp (msg.data (), "TERMINATE", 10) == 0)
state = terminated;
else {
// This is an API error, we should assert
puts ("E: invalid command sent to proxy");
assert (false);
}
} }
// Process a request // Process a request
if (state == resume && items [0].revents & ZMQ_POLLIN) { if (state == active
&& items [0].revents & ZMQ_POLLIN) {
while (true) { while (true) {
rc = frontend_->recv (&msg, 0); rc = frontend_->recv (&msg, 0);
if (unlikely (rc < 0)) if (unlikely (rc < 0))
...@@ -156,7 +162,8 @@ int zmq::proxy ( ...@@ -156,7 +162,8 @@ int zmq::proxy (
} }
} }
// Process a reply // Process a reply
if (state == resume && items [1].revents & ZMQ_POLLIN) { if (state == active
&& items [1].revents & ZMQ_POLLIN) {
while (true) { while (true) {
rc = backend_->recv (&msg, 0); rc = backend_->recv (&msg, 0);
if (unlikely (rc < 0)) if (unlikely (rc < 0))
...@@ -187,7 +194,6 @@ int zmq::proxy ( ...@@ -187,7 +194,6 @@ int zmq::proxy (
break; break;
} }
} }
} }
return 0; return 0;
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment