![]() |
libzmq master
The Intelligent Transport Layer
|
00001 /* 00002 Copyright (c) 2010-2011 250bpm s.r.o. 00003 Copyright (c) 2011 VMware, Inc. 00004 Copyright (c) 2010-2011 Other contributors as noted in the AUTHORS file 00005 00006 This file is part of 0MQ. 00007 00008 0MQ is free software; you can redistribute it and/or modify it under 00009 the terms of the GNU Lesser General Public License as published by 00010 the Free Software Foundation; either version 3 of the License, or 00011 (at your option) any later version. 00012 00013 0MQ is distributed in the hope that it will be useful, 00014 but WITHOUT ANY WARRANTY; without even the implied warranty of 00015 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00016 GNU Lesser General Public License for more details. 00017 00018 You should have received a copy of the GNU Lesser General Public License 00019 along with this program. If not, see <http://www.gnu.org/licenses/>. 00020 */ 00021 00022 #include "../include/zmq.h" 00023 #include <assert.h> 00024 #include <stdio.h> 00025 00026 int main (int argc, char *argv []) 00027 { 00028 fprintf (stderr, "test_invalid_rep running...\n"); 00029 00030 // Create REQ/XREP wiring. 00031 void *ctx = zmq_init (1); 00032 assert (ctx); 00033 void *xrep_socket = zmq_socket (ctx, ZMQ_XREP); 00034 assert (xrep_socket); 00035 void *req_socket = zmq_socket (ctx, ZMQ_REQ); 00036 assert (req_socket); 00037 int linger = 0; 00038 int rc = zmq_setsockopt (xrep_socket, ZMQ_LINGER, &linger, sizeof (int)); 00039 assert (rc == 0); 00040 rc = zmq_setsockopt (req_socket, ZMQ_LINGER, &linger, sizeof (int)); 00041 assert (rc == 0); 00042 rc = zmq_bind (xrep_socket, "inproc://hi"); 00043 assert (rc == 0); 00044 rc = zmq_connect (req_socket, "inproc://hi"); 00045 assert (rc == 0); 00046 00047 // Initial request. 00048 rc = zmq_send (req_socket, "r", 1, 0); 00049 assert (rc == 1); 00050 00051 // Receive the request. 00052 char addr [32]; 00053 int addr_size; 00054 char bottom [1]; 00055 char body [1]; 00056 addr_size = zmq_recv (xrep_socket, addr, sizeof (addr), 0); 00057 assert (addr_size >= 0); 00058 rc = zmq_recv (xrep_socket, bottom, sizeof (bottom), 0); 00059 assert (rc == 0); 00060 rc = zmq_recv (xrep_socket, body, sizeof (body), 0); 00061 assert (rc == 1); 00062 00063 // Send invalid reply. 00064 rc = zmq_send (xrep_socket, addr, addr_size, 0); 00065 assert (rc == addr_size); 00066 00067 // Send valid reply. 00068 rc = zmq_send (xrep_socket, addr, addr_size, ZMQ_SNDMORE); 00069 assert (rc == addr_size); 00070 rc = zmq_send (xrep_socket, bottom, 0, ZMQ_SNDMORE); 00071 assert (rc == 0); 00072 rc = zmq_send (xrep_socket, "b", 1, 0); 00073 assert (rc == 1); 00074 00075 // Check whether we've got the valid reply. 00076 rc = zmq_recv (req_socket, body, sizeof (body), 0); 00077 assert (rc == 1); 00078 assert (body [0] == 'b'); 00079 00080 // Tear down the wiring. 00081 rc = zmq_close (xrep_socket); 00082 assert (rc == 0); 00083 rc = zmq_close (req_socket); 00084 assert (rc == 0); 00085 rc = zmq_term (ctx); 00086 assert (rc == 0); 00087 00088 return 0; 00089 } 00090