libzmq  master
ZeroMQ C++ Core Engine (LIBZMQ)
local_lat.cpp
Go to the documentation of this file.
1 /*
2  Copyright (c) 2007-2016 Contributors as noted in the AUTHORS file
3 
4  This file is part of libzmq, the ZeroMQ core engine in C++.
5 
6  libzmq is free software; you can redistribute it and/or modify it under
7  the terms of the GNU Lesser General Public License (LGPL) as published
8  by the Free Software Foundation; either version 3 of the License, or
9  (at your option) any later version.
10 
11  As a special exception, the Contributors give you permission to link
12  this library with independent modules to produce an executable,
13  regardless of the license terms of these independent modules, and to
14  copy and distribute the resulting executable under terms of your choice,
15  provided that you also meet, for each linked independent module, the
16  terms and conditions of the license of that module. An independent
17  module is a module which is not derived from or based on this library.
18  If you modify this library, you must extend this exception to your
19  version of the library.
20 
21  libzmq is distributed in the hope that it will be useful, but WITHOUT
22  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
23  FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
24  License for more details.
25 
26  You should have received a copy of the GNU Lesser General Public License
27  along with this program. If not, see <http://www.gnu.org/licenses/>.
28 */
29 
30 #include "../include/zmq.h"
31 #include <stdio.h>
32 #include <stdlib.h>
33 
34 int main (int argc, char *argv [])
35 {
36  const char *bind_to;
37  int roundtrip_count;
38  size_t message_size;
39  void *ctx;
40  void *s;
41  int rc;
42  int i;
43  zmq_msg_t msg;
44 
45  if (argc != 4) {
46  printf ("usage: local_lat <bind-to> <message-size> "
47  "<roundtrip-count>\n");
48  return 1;
49  }
50  bind_to = argv [1];
51  message_size = atoi (argv [2]);
52  roundtrip_count = atoi (argv [3]);
53 
54  ctx = zmq_init (1);
55  if (!ctx) {
56  printf ("error in zmq_init: %s\n", zmq_strerror (errno));
57  return -1;
58  }
59 
60  s = zmq_socket (ctx, ZMQ_REP);
61  if (!s) {
62  printf ("error in zmq_socket: %s\n", zmq_strerror (errno));
63  return -1;
64  }
65 
66  rc = zmq_bind (s, bind_to);
67  if (rc != 0) {
68  printf ("error in zmq_bind: %s\n", zmq_strerror (errno));
69  return -1;
70  }
71 
72  rc = zmq_msg_init (&msg);
73  if (rc != 0) {
74  printf ("error in zmq_msg_init: %s\n", zmq_strerror (errno));
75  return -1;
76  }
77 
78  for (i = 0; i != roundtrip_count; i++) {
79  rc = zmq_recvmsg (s, &msg, 0);
80  if (rc < 0) {
81  printf ("error in zmq_recvmsg: %s\n", zmq_strerror (errno));
82  return -1;
83  }
84  if (zmq_msg_size (&msg) != message_size) {
85  printf ("message of incorrect size received\n");
86  return -1;
87  }
88  rc = zmq_sendmsg (s, &msg, 0);
89  if (rc < 0) {
90  printf ("error in zmq_sendmsg: %s\n", zmq_strerror (errno));
91  return -1;
92  }
93  }
94 
95  rc = zmq_msg_close (&msg);
96  if (rc != 0) {
97  printf ("error in zmq_msg_close: %s\n", zmq_strerror (errno));
98  return -1;
99  }
100 
101  zmq_sleep (1);
102 
103  rc = zmq_close (s);
104  if (rc != 0) {
105  printf ("error in zmq_close: %s\n", zmq_strerror (errno));
106  return -1;
107  }
108 
109  rc = zmq_ctx_term (ctx);
110  if (rc != 0) {
111  printf ("error in zmq_ctx_term: %s\n", zmq_strerror (errno));
112  return -1;
113  }
114 
115  return 0;
116 }
static size_t message_size
Definition: inproc_lat.cpp:45
#define ZMQ_REP
Definition: zmq.h:250
ZMQ_EXPORT void * zmq_init(int io_threads)
Definition: zmq.cpp:220
ZMQ_EXPORT int zmq_recvmsg(void *s, zmq_msg_t *msg, int flags)
Definition: zmq.cpp:501
ZMQ_EXPORT void * zmq_socket(void *, int type)
Definition: zmq.cpp:244
int main(int argc, char *argv[])
Definition: local_lat.cpp:34
ZMQ_EXPORT int zmq_close(void *s)
Definition: zmq.cpp:255
ZMQ_EXPORT int zmq_sendmsg(void *s, zmq_msg_t *msg, int flags)
Definition: zmq.cpp:382
ZMQ_EXPORT const char * zmq_strerror(int errnum)
Definition: zmq.cpp:102
Definition: zmq.h:221
ZMQ_EXPORT void zmq_sleep(int seconds_)
Definition: zmq_utils.cpp:53
ZMQ_EXPORT int zmq_bind(void *s, const char *addr)
Definition: zmq.cpp:321
ZMQ_EXPORT int zmq_ctx_term(void *context)
Definition: zmq.cpp:162
ZMQ_EXPORT int zmq_msg_close(zmq_msg_t *msg)
Definition: zmq.cpp:651
ZMQ_EXPORT size_t zmq_msg_size(zmq_msg_t *msg)
Definition: zmq.cpp:671
ZMQ_EXPORT int zmq_msg_init(zmq_msg_t *msg)
Definition: zmq.cpp:613
static int roundtrip_count
Definition: inproc_lat.cpp:46