Commit 638ddeb4 authored by Thomas Rodgers's avatar Thomas Rodgers

resolve #1347 Support limited metadata for STREAM sockets

parent 39338e2f
...@@ -176,7 +176,7 @@ int zmq::stream_t::xsetsockopt (int option_, const void *optval_, ...@@ -176,7 +176,7 @@ int zmq::stream_t::xsetsockopt (int option_, const void *optval_,
return 0; return 0;
} }
break; break;
case ZMQ_STREAM_NOTIFY: case ZMQ_STREAM_NOTIFY:
if (is_int && (value == 0 || value == 1)) { if (is_int && (value == 0 || value == 1)) {
options.raw_notify = (value != 0); options.raw_notify = (value != 0);
...@@ -221,6 +221,12 @@ int zmq::stream_t::xrecv (msg_t *msg_) ...@@ -221,6 +221,12 @@ int zmq::stream_t::xrecv (msg_t *msg_)
blob_t identity = pipe->get_identity (); blob_t identity = pipe->get_identity ();
rc = msg_->init_size (identity.size ()); rc = msg_->init_size (identity.size ());
errno_assert (rc == 0); errno_assert (rc == 0);
// forward metadata (if any)
metadata_t *metadata = prefetched_msg.metadata();
if (metadata)
msg_->set_metadata(metadata);
memcpy (msg_->data (), identity.data (), identity.size ()); memcpy (msg_->data (), identity.data (), identity.size ());
msg_->set_flags (msg_t::more); msg_->set_flags (msg_t::more);
...@@ -277,7 +283,7 @@ void zmq::stream_t::identify_peer (pipe_t *pipe_) ...@@ -277,7 +283,7 @@ void zmq::stream_t::identify_peer (pipe_t *pipe_)
connect_rid.length ()); connect_rid.length ());
connect_rid.clear (); connect_rid.clear ();
outpipes_t::iterator it = outpipes.find (identity); outpipes_t::iterator it = outpipes.find (identity);
if (it != outpipes.end ()) if (it != outpipes.end ())
zmq_assert(false); zmq_assert(false);
} }
else { else {
......
...@@ -192,14 +192,23 @@ void zmq::stream_engine_t::plug (io_thread_t *io_thread_, ...@@ -192,14 +192,23 @@ void zmq::stream_engine_t::plug (io_thread_t *io_thread_,
handshaking = false; handshaking = false;
next_msg = &stream_engine_t::pull_msg_from_session; next_msg = &stream_engine_t::pull_msg_from_session;
process_msg = &stream_engine_t::push_msg_to_session; process_msg = &stream_engine_t::push_raw_msg_to_session;
if (!peer_address.empty()) {
// Compile metadata.
typedef metadata_t::dict_t properties_t;
properties_t properties;
properties.insert(std::make_pair("Peer-Address", peer_address));
zmq_assert (metadata == NULL);
metadata = new (std::nothrow) metadata_t (properties);
}
if (options.raw_notify) { if (options.raw_notify) {
// For raw sockets, send an initial 0-length message to the // For raw sockets, send an initial 0-length message to the
// application so that it knows a peer has connected. // application so that it knows a peer has connected.
msg_t connector; msg_t connector;
connector.init(); connector.init();
push_msg_to_session (&connector); push_raw_msg_to_session (&connector);
connector.close(); connector.close();
session->flush (); session->flush ();
} }
...@@ -835,6 +844,12 @@ int zmq::stream_engine_t::push_msg_to_session (msg_t *msg_) ...@@ -835,6 +844,12 @@ int zmq::stream_engine_t::push_msg_to_session (msg_t *msg_)
return session->push_msg (msg_); return session->push_msg (msg_);
} }
int zmq::stream_engine_t::push_raw_msg_to_session (msg_t *msg_) {
if (metadata)
msg_->set_metadata(metadata);
return push_msg_to_session(msg_);
}
int zmq::stream_engine_t::write_credential (msg_t *msg_) int zmq::stream_engine_t::write_credential (msg_t *msg_)
{ {
zmq_assert (mechanism != NULL); zmq_assert (mechanism != NULL);
......
...@@ -59,7 +59,7 @@ namespace zmq ...@@ -59,7 +59,7 @@ namespace zmq
timeout_error timeout_error
}; };
stream_engine_t (fd_t fd_, const options_t &options_, stream_engine_t (fd_t fd_, const options_t &options_,
const std::string &endpoint); const std::string &endpoint);
~stream_engine_t (); ~stream_engine_t ();
...@@ -99,6 +99,8 @@ namespace zmq ...@@ -99,6 +99,8 @@ namespace zmq
int pull_msg_from_session (msg_t *msg_); int pull_msg_from_session (msg_t *msg_);
int push_msg_to_session (msg_t *msg); int push_msg_to_session (msg_t *msg);
int push_raw_msg_to_session (msg_t *msg);
int write_credential (msg_t *msg_); int write_credential (msg_t *msg_);
int pull_and_encode (msg_t *msg_); int pull_and_encode (msg_t *msg_);
int decode_and_push (msg_t *msg_); int decode_and_push (msg_t *msg_);
......
...@@ -80,11 +80,14 @@ test_stream_to_dealer (void) ...@@ -80,11 +80,14 @@ test_stream_to_dealer (void)
assert (rc > 0); assert (rc > 0);
assert (zmq_msg_more (&identity)); assert (zmq_msg_more (&identity));
// Verify the existence of Peer-Address metadata
assert (streq (zmq_msg_gets (&identity, "Peer-Address"), "127.0.0.1"));
// Second frame is zero // Second frame is zero
byte buffer [255]; byte buffer [255];
rc = zmq_recv (stream, buffer, 255, 0); rc = zmq_recv (stream, buffer, 255, 0);
assert (rc == 0); assert (rc == 0);
// Real data follows // Real data follows
// First frame is identity // First frame is identity
rc = zmq_msg_recv (&identity, stream, 0); rc = zmq_msg_recv (&identity, stream, 0);
...@@ -92,6 +95,9 @@ test_stream_to_dealer (void) ...@@ -92,6 +95,9 @@ test_stream_to_dealer (void)
assert (zmq_msg_more (&identity)); assert (zmq_msg_more (&identity));
// Second frame is greeting signature // Second frame is greeting signature
// Verify the existence of Peer-Address metadata
assert (streq (zmq_msg_gets (&identity, "Peer-Address"), "127.0.0.1"));
rc = zmq_recv (stream, buffer, 255, 0); rc = zmq_recv (stream, buffer, 255, 0);
assert (rc == 10); assert (rc == 10);
assert (memcmp (buffer, greeting.signature, 10) == 0); assert (memcmp (buffer, greeting.signature, 10) == 0);
...@@ -182,7 +188,7 @@ test_stream_to_stream (void) ...@@ -182,7 +188,7 @@ test_stream_to_stream (void)
// Set-up our context and sockets // Set-up our context and sockets
void *ctx = zmq_ctx_new (); void *ctx = zmq_ctx_new ();
assert (ctx); assert (ctx);
void *server = zmq_socket (ctx, ZMQ_STREAM); void *server = zmq_socket (ctx, ZMQ_STREAM);
assert (server); assert (server);
int enabled = 1; int enabled = 1;
...@@ -200,7 +206,7 @@ test_stream_to_stream (void) ...@@ -200,7 +206,7 @@ test_stream_to_stream (void)
uint8_t id [256]; uint8_t id [256];
size_t id_size = 256; size_t id_size = 256;
uint8_t buffer [256]; uint8_t buffer [256];
// Connecting sends a zero message // Connecting sends a zero message
// Server: First frame is identity, second frame is zero // Server: First frame is identity, second frame is zero
id_size = zmq_recv (server, id, 256, 0); id_size = zmq_recv (server, id, 256, 0);
...@@ -223,19 +229,19 @@ test_stream_to_stream (void) ...@@ -223,19 +229,19 @@ test_stream_to_stream (void)
// Second frame is HTTP GET request // Second frame is HTTP GET request
rc = zmq_send (client, "GET /\n\n", 7, 0); rc = zmq_send (client, "GET /\n\n", 7, 0);
assert (rc == 7); assert (rc == 7);
// Get HTTP request; ID frame and then request // Get HTTP request; ID frame and then request
id_size = zmq_recv (server, id, 256, 0); id_size = zmq_recv (server, id, 256, 0);
assert (id_size > 0); assert (id_size > 0);
rc = zmq_recv (server, buffer, 256, 0); rc = zmq_recv (server, buffer, 256, 0);
assert (rc != -1); assert (rc != -1);
assert (memcmp (buffer, "GET /\n\n", 7) == 0); assert (memcmp (buffer, "GET /\n\n", 7) == 0);
// Send reply back to client // Send reply back to client
char http_response [] = char http_response [] =
"HTTP/1.0 200 OK\r\n" "HTTP/1.0 200 OK\r\n"
"Content-Type: text/plain\r\n" "Content-Type: text/plain\r\n"
"\r\n" "\r\n"
"Hello, World!"; "Hello, World!";
rc = zmq_send (server, id, id_size, ZMQ_SNDMORE); rc = zmq_send (server, id, id_size, ZMQ_SNDMORE);
assert (rc != -1); assert (rc != -1);
......
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