2012-10-29 08:03:36 +01:00
|
|
|
/*
|
2013-03-12 17:04:51 +01:00
|
|
|
Copyright (c) 2007-2013 Contributors as noted in the AUTHORS file
|
2012-10-29 08:03:36 +01:00
|
|
|
|
|
|
|
This file is part of 0MQ.
|
|
|
|
|
|
|
|
0MQ is free software; you can redistribute it and/or modify it under
|
|
|
|
the terms of the GNU Lesser General Public License as published by
|
|
|
|
the Free Software Foundation; either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
0MQ is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU Lesser General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Lesser General Public License
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2013-01-31 19:46:22 +01:00
|
|
|
#include "../include/zmq.h"
|
2013-01-31 15:00:17 +01:00
|
|
|
#include <string.h>
|
|
|
|
#include <stdbool.h>
|
2013-01-31 19:46:22 +01:00
|
|
|
#undef NDEBUG
|
|
|
|
#include <assert.h>
|
2012-10-29 08:03:36 +01:00
|
|
|
|
2013-01-31 15:00:17 +01:00
|
|
|
// ZMTP protocol greeting structure
|
2012-10-29 08:03:36 +01:00
|
|
|
|
2013-01-31 15:00:17 +01:00
|
|
|
typedef unsigned char byte;
|
|
|
|
typedef struct {
|
|
|
|
byte signature [10]; // 0xFF 8*0x00 0x7F
|
2013-04-28 10:40:52 +02:00
|
|
|
byte version [2]; // 0x03 0x00 for ZMTP/3.0
|
|
|
|
byte mechanism [20]; // "NULL"
|
|
|
|
byte as_server;
|
|
|
|
byte filler [31];
|
2013-01-31 15:00:17 +01:00
|
|
|
} zmtp_greeting_t;
|
2012-10-29 08:03:36 +01:00
|
|
|
|
2013-01-31 15:00:17 +01:00
|
|
|
#define ZMTP_DEALER 5 // Socket type constants
|
|
|
|
#define ZMTP_ROUTER 6
|
2012-10-29 08:03:36 +01:00
|
|
|
|
2013-01-31 15:00:17 +01:00
|
|
|
// This is a greeting matching what 0MQ will send us; note the
|
|
|
|
// 8-byte size is set to 1 for backwards compatibility
|
2012-10-29 08:03:36 +01:00
|
|
|
|
2013-01-31 15:00:17 +01:00
|
|
|
static zmtp_greeting_t greeting
|
2013-04-28 10:40:52 +02:00
|
|
|
= { { 0xFF, 0, 0, 0, 0, 0, 0, 0, 1, 0x7F }, {3, 0}, { 'N', 'U', 'L', 'L'} };
|
2012-10-29 08:03:36 +01:00
|
|
|
|
2013-01-31 15:00:17 +01:00
|
|
|
int main (void)
|
2012-11-08 14:31:26 +01:00
|
|
|
{
|
2013-01-31 15:00:17 +01:00
|
|
|
int rc;
|
2012-11-08 14:31:26 +01:00
|
|
|
|
2013-01-31 15:00:17 +01:00
|
|
|
// Set up our context and sockets
|
|
|
|
void *ctx = zmq_ctx_new ();
|
|
|
|
assert (ctx);
|
2012-11-08 14:31:26 +01:00
|
|
|
|
2013-01-31 15:00:17 +01:00
|
|
|
// We'll be using this socket in raw mode
|
|
|
|
void *router = zmq_socket (ctx, ZMQ_ROUTER);
|
|
|
|
assert (router);
|
2012-11-08 14:31:26 +01:00
|
|
|
|
2013-01-31 15:00:17 +01:00
|
|
|
int on = 1;
|
|
|
|
rc = zmq_setsockopt (router, ZMQ_ROUTER_RAW, &on, sizeof (on));
|
2012-11-08 14:31:26 +01:00
|
|
|
assert (rc == 0);
|
2013-01-31 15:00:17 +01:00
|
|
|
int zero = 0;
|
|
|
|
rc = zmq_setsockopt (router, ZMQ_LINGER, &zero, sizeof (zero));
|
2012-11-08 14:31:26 +01:00
|
|
|
assert (rc == 0);
|
2013-01-31 15:00:17 +01:00
|
|
|
rc = zmq_bind (router, "tcp://*:5555");
|
2012-11-08 14:31:26 +01:00
|
|
|
assert (rc == 0);
|
|
|
|
|
2013-01-31 15:00:17 +01:00
|
|
|
// We'll be using this socket as the other peer
|
|
|
|
void *dealer = zmq_socket (ctx, ZMQ_DEALER);
|
|
|
|
assert (dealer);
|
|
|
|
rc = zmq_setsockopt (dealer, ZMQ_LINGER, &zero, sizeof (zero));
|
2012-11-08 14:31:26 +01:00
|
|
|
assert (rc == 0);
|
2013-01-31 15:00:17 +01:00
|
|
|
rc = zmq_connect (dealer, "tcp://localhost:5555");
|
2012-11-08 14:31:26 +01:00
|
|
|
|
2013-01-31 15:00:17 +01:00
|
|
|
// Send a message on the dealer socket
|
|
|
|
rc = zmq_send (dealer, "Hello", 5, 0);
|
|
|
|
assert (rc == 5);
|
2012-11-08 14:31:26 +01:00
|
|
|
|
2013-01-31 15:00:17 +01:00
|
|
|
// First frame is identity
|
|
|
|
zmq_msg_t identity;
|
|
|
|
rc = zmq_msg_init (&identity);
|
2012-11-08 14:31:26 +01:00
|
|
|
assert (rc == 0);
|
2013-01-31 15:00:17 +01:00
|
|
|
rc = zmq_msg_recv (&identity, router, 0);
|
|
|
|
assert (rc > 0);
|
|
|
|
assert (zmq_msg_more (&identity));
|
2012-11-08 14:31:26 +01:00
|
|
|
|
2013-01-31 15:00:17 +01:00
|
|
|
// Second frame is greeting signature
|
|
|
|
byte buffer [255];
|
|
|
|
rc = zmq_recv (router, buffer, 255, 0);
|
|
|
|
assert (rc == 10);
|
|
|
|
assert (memcmp (buffer, greeting.signature, 10) == 0);
|
2012-11-08 14:31:26 +01:00
|
|
|
|
2013-01-31 15:00:17 +01:00
|
|
|
// Send our own protocol greeting
|
|
|
|
rc = zmq_msg_send (&identity, router, ZMQ_SNDMORE);
|
|
|
|
assert (rc > 0);
|
|
|
|
rc = zmq_send (router, &greeting, sizeof (greeting), 0);
|
|
|
|
assert (rc == sizeof (greeting));
|
2012-11-08 14:31:26 +01:00
|
|
|
|
2013-01-31 15:00:17 +01:00
|
|
|
// Now we expect the data from the DEALER socket
|
|
|
|
// First frame is, again, the identity of the connection
|
|
|
|
rc = zmq_msg_recv (&identity, router, 0);
|
|
|
|
assert (rc > 0);
|
|
|
|
assert (zmq_msg_more (&identity));
|
2012-11-08 14:31:26 +01:00
|
|
|
|
2013-04-28 10:40:52 +02:00
|
|
|
// Second frame contains the rest of greeting along with
|
|
|
|
// the Ready command
|
2013-01-31 15:00:17 +01:00
|
|
|
rc = zmq_recv (router, buffer, 255, 0);
|
2013-06-28 11:42:54 +02:00
|
|
|
assert (rc == 97);
|
2012-11-08 14:31:26 +01:00
|
|
|
|
2013-04-28 10:40:52 +02:00
|
|
|
// First two bytes are major and minor version numbers.
|
|
|
|
assert (buffer [0] == 3); // ZMTP/3.0
|
|
|
|
assert (buffer [1] == 0);
|
2012-11-08 14:31:26 +01:00
|
|
|
|
2013-04-28 10:40:52 +02:00
|
|
|
// Mechanism is "NULL"
|
|
|
|
assert (memcmp (buffer + 2, "NULL\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0", 22) == 0);
|
2013-06-28 11:42:54 +02:00
|
|
|
assert (memcmp (buffer + 54, "\0\51READY\0", 8) == 0);
|
|
|
|
assert (memcmp (buffer + 62, "\13Socket-Type\0\0\0\6DEALER", 22) == 0);
|
|
|
|
assert (memcmp (buffer + 84, "\10Identity\0\0\0\0", 13) == 0);
|
2013-04-28 10:40:52 +02:00
|
|
|
|
|
|
|
// Announce we are ready
|
2013-06-28 11:42:54 +02:00
|
|
|
memcpy (buffer, "\0\51READY\0", 8);
|
2013-07-01 10:04:54 +02:00
|
|
|
memcpy (buffer + 8, "\13Socket-Type\0\0\0\6ROUTER", 22);
|
2013-06-28 11:42:54 +02:00
|
|
|
memcpy (buffer + 30, "\10Identity\0\0\0\0", 13);
|
2013-04-28 10:40:52 +02:00
|
|
|
|
|
|
|
// Send Ready command
|
|
|
|
rc = zmq_msg_send (&identity, router, ZMQ_SNDMORE);
|
|
|
|
assert (rc > 0);
|
2013-06-28 11:42:54 +02:00
|
|
|
rc = zmq_send (router, buffer, 43, 0);
|
|
|
|
assert (rc == 43);
|
2013-04-28 10:40:52 +02:00
|
|
|
|
|
|
|
// Now we expect the data from the DEALER socket
|
|
|
|
// First frame is, again, the identity of the connection
|
|
|
|
rc = zmq_msg_recv (&identity, router, 0);
|
|
|
|
assert (rc > 0);
|
|
|
|
assert (zmq_msg_more (&identity));
|
|
|
|
|
|
|
|
// Third frame contains Hello message from DEALER
|
|
|
|
rc = zmq_recv (router, buffer, sizeof buffer, 0);
|
|
|
|
assert (rc == 7);
|
2012-11-08 14:31:26 +01:00
|
|
|
|
2013-01-31 15:00:17 +01:00
|
|
|
// Then we have a 5-byte message "Hello"
|
2013-04-28 10:40:52 +02:00
|
|
|
assert (buffer [0] == 0); // Flags = 0
|
|
|
|
assert (buffer [1] == 5); // Size = 5
|
|
|
|
assert (memcmp (buffer + 2, "Hello", 5) == 0);
|
2012-11-08 14:31:26 +01:00
|
|
|
|
2013-01-31 15:00:17 +01:00
|
|
|
// Send "World" back to DEALER
|
|
|
|
rc = zmq_msg_send (&identity, router, ZMQ_SNDMORE);
|
|
|
|
assert (rc > 0);
|
|
|
|
byte world [] = { 0, 5, 'W', 'o', 'r', 'l', 'd' };
|
|
|
|
rc = zmq_send (router, world, sizeof (world), 0);
|
|
|
|
assert (rc == sizeof (world));
|
2012-10-29 08:03:36 +01:00
|
|
|
|
2013-01-31 15:00:17 +01:00
|
|
|
// Expect response on DEALER socket
|
|
|
|
rc = zmq_recv (dealer, buffer, 255, 0);
|
|
|
|
assert (rc == 5);
|
|
|
|
assert (memcmp (buffer, "World", 5) == 0);
|
2012-10-30 12:18:13 +01:00
|
|
|
|
2013-01-31 15:00:17 +01:00
|
|
|
rc = zmq_close (dealer);
|
2012-10-30 12:18:13 +01:00
|
|
|
assert (rc == 0);
|
2012-10-29 08:03:36 +01:00
|
|
|
|
2013-01-31 15:00:17 +01:00
|
|
|
rc = zmq_close (router);
|
2012-10-29 08:03:36 +01:00
|
|
|
assert (rc == 0);
|
|
|
|
|
2013-01-31 15:00:17 +01:00
|
|
|
rc = zmq_ctx_term (ctx);
|
|
|
|
assert (rc == 0);
|
2012-10-29 08:03:36 +01:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|