![]() |
libzmq master
The Intelligent Transport Layer
|
00001 /* 00002 Copyright (c) 2009-2011 250bpm s.r.o. 00003 Copyright (c) 2007-2009 iMatix Corporation 00004 Copyright (c) 2007-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 "../include/zmq_utils.h" 00024 #include <stdio.h> 00025 #include <stdlib.h> 00026 00027 int main (int argc, char *argv []) 00028 { 00029 const char *bind_to; 00030 int roundtrip_count; 00031 size_t message_size; 00032 void *ctx; 00033 void *s; 00034 int rc; 00035 int i; 00036 zmq_msg_t msg; 00037 00038 if (argc != 4) { 00039 printf ("usage: local_lat <bind-to> <message-size> " 00040 "<roundtrip-count>\n"); 00041 return 1; 00042 } 00043 bind_to = argv [1]; 00044 message_size = atoi (argv [2]); 00045 roundtrip_count = atoi (argv [3]); 00046 00047 ctx = zmq_init (1); 00048 if (!ctx) { 00049 printf ("error in zmq_init: %s\n", zmq_strerror (errno)); 00050 return -1; 00051 } 00052 00053 s = zmq_socket (ctx, ZMQ_REP); 00054 if (!s) { 00055 printf ("error in zmq_socket: %s\n", zmq_strerror (errno)); 00056 return -1; 00057 } 00058 00059 rc = zmq_bind (s, bind_to); 00060 if (rc != 0) { 00061 printf ("error in zmq_bind: %s\n", zmq_strerror (errno)); 00062 return -1; 00063 } 00064 00065 rc = zmq_msg_init (&msg); 00066 if (rc != 0) { 00067 printf ("error in zmq_msg_init: %s\n", zmq_strerror (errno)); 00068 return -1; 00069 } 00070 00071 for (i = 0; i != roundtrip_count; i++) { 00072 rc = zmq_recvmsg (s, &msg, 0); 00073 if (rc < 0) { 00074 printf ("error in zmq_recvmsg: %s\n", zmq_strerror (errno)); 00075 return -1; 00076 } 00077 if (zmq_msg_size (&msg) != message_size) { 00078 printf ("message of incorrect size received\n"); 00079 return -1; 00080 } 00081 rc = zmq_sendmsg (s, &msg, 0); 00082 if (rc < 0) { 00083 printf ("error in zmq_sendmsg: %s\n", zmq_strerror (errno)); 00084 return -1; 00085 } 00086 } 00087 00088 rc = zmq_msg_close (&msg); 00089 if (rc != 0) { 00090 printf ("error in zmq_msg_close: %s\n", zmq_strerror (errno)); 00091 return -1; 00092 } 00093 00094 zmq_sleep (1); 00095 00096 rc = zmq_close (s); 00097 if (rc != 0) { 00098 printf ("error in zmq_close: %s\n", zmq_strerror (errno)); 00099 return -1; 00100 } 00101 00102 rc = zmq_term (ctx); 00103 if (rc != 0) { 00104 printf ("error in zmq_term: %s\n", zmq_strerror (errno)); 00105 return -1; 00106 } 00107 00108 return 0; 00109 }