~ubuntu-branches/ubuntu/saucy/zeromq3/saucy

« back to all changes in this revision

Viewing changes to tests/test_reqrep_device.cpp

  • Committer: Package Import Robot
  • Author(s): Alessandro Ghedini
  • Date: 2012-06-04 21:21:09 UTC
  • Revision ID: package-import@ubuntu.com-20120604212109-b7b3m0rn21o8oo2q
Tags: upstream-3.1.0~beta+dfsg
ImportĀ upstreamĀ versionĀ 3.1.0~beta+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
    Copyright (c) 2010-2011 250bpm s.r.o.
 
3
    Copyright (c) 2011 VMware, Inc.
 
4
    Copyright (c) 2010-2011 Other contributors as noted in the AUTHORS file
 
5
 
 
6
    This file is part of 0MQ.
 
7
 
 
8
    0MQ is free software; you can redistribute it and/or modify it under
 
9
    the terms of the GNU Lesser General Public License as published by
 
10
    the Free Software Foundation; either version 3 of the License, or
 
11
    (at your option) any later version.
 
12
 
 
13
    0MQ is distributed in the hope that it will be useful,
 
14
    but WITHOUT ANY WARRANTY; without even the implied warranty of
 
15
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
16
    GNU Lesser General Public License for more details.
 
17
 
 
18
    You should have received a copy of the GNU Lesser General Public License
 
19
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
20
*/
 
21
 
 
22
#include <assert.h>
 
23
#include <string.h>
 
24
#include <stdio.h>
 
25
 
 
26
#include "../include/zmq.h"
 
27
 
 
28
int main (int argc, char *argv [])
 
29
{
 
30
    fprintf (stderr, "test_reqrep_device running...\n");
 
31
 
 
32
    void *ctx = zmq_init (1);
 
33
    assert (ctx);
 
34
 
 
35
    //  Create a req/rep device.
 
36
    void *xreq = zmq_socket (ctx, ZMQ_XREQ);
 
37
    assert (xreq);
 
38
    int rc = zmq_bind (xreq, "tcp://127.0.0.1:5560");
 
39
    assert (rc == 0);
 
40
    void *xrep = zmq_socket (ctx, ZMQ_XREP);
 
41
    assert (xrep);
 
42
    rc = zmq_bind (xrep, "tcp://127.0.0.1:5561");
 
43
    assert (rc == 0);
 
44
 
 
45
    //  Create a worker.
 
46
    void *rep = zmq_socket (ctx, ZMQ_REP);
 
47
    assert (rep);
 
48
    rc = zmq_connect (rep, "tcp://127.0.0.1:5560");
 
49
    assert (rc == 0);
 
50
 
 
51
    //  Create a client.
 
52
    void *req = zmq_socket (ctx, ZMQ_REQ);
 
53
    assert (req);
 
54
    rc = zmq_connect (req, "tcp://127.0.0.1:5561");
 
55
    assert (rc == 0);
 
56
 
 
57
    //  Send a request.
 
58
    rc = zmq_send (req, "ABC", 3, ZMQ_SNDMORE);
 
59
    assert (rc == 3);
 
60
    rc = zmq_send (req, "DEF", 3, 0);
 
61
    assert (rc == 3);
 
62
 
 
63
    //  Pass the request through the device.
 
64
    for (int i = 0; i != 4; i++) {
 
65
        zmq_msg_t msg;
 
66
        rc = zmq_msg_init (&msg);
 
67
        assert (rc == 0);
 
68
        rc = zmq_recvmsg (xrep, &msg, 0);
 
69
        assert (rc >= 0);
 
70
        int rcvmore;
 
71
        size_t sz = sizeof (rcvmore);
 
72
        rc = zmq_getsockopt (xrep, ZMQ_RCVMORE, &rcvmore, &sz);
 
73
        assert (rc == 0);
 
74
        rc = zmq_sendmsg (xreq, &msg, rcvmore ? ZMQ_SNDMORE : 0);
 
75
        assert (rc >= 0);
 
76
    }
 
77
 
 
78
    //  Receive the request.
 
79
    char buff [3];
 
80
    rc = zmq_recv (rep, buff, 3, 0);
 
81
    assert (rc == 3);
 
82
    assert (memcmp (buff, "ABC", 3) == 0);
 
83
    int rcvmore;
 
84
    size_t sz = sizeof (rcvmore);
 
85
    rc = zmq_getsockopt (rep, ZMQ_RCVMORE, &rcvmore, &sz);
 
86
    assert (rc == 0);
 
87
    assert (rcvmore);
 
88
    rc = zmq_recv (rep, buff, 3, 0);
 
89
    assert (rc == 3);
 
90
    assert (memcmp (buff, "DEF", 3) == 0);
 
91
    rc = zmq_getsockopt (rep, ZMQ_RCVMORE, &rcvmore, &sz);
 
92
    assert (rc == 0);
 
93
    assert (!rcvmore);
 
94
 
 
95
    //  Send the reply.
 
96
    rc = zmq_send (rep, "GHI", 3, ZMQ_SNDMORE);
 
97
    assert (rc == 3);
 
98
    rc = zmq_send (rep, "JKL", 3, 0);
 
99
    assert (rc == 3);
 
100
 
 
101
    //  Pass the reply through the device.
 
102
    for (int i = 0; i != 4; i++) {
 
103
        zmq_msg_t msg;
 
104
        rc = zmq_msg_init (&msg);
 
105
        assert (rc == 0);
 
106
        rc = zmq_recvmsg (xreq, &msg, 0);
 
107
        assert (rc >= 0);
 
108
        int rcvmore;
 
109
        rc = zmq_getsockopt (xreq, ZMQ_RCVMORE, &rcvmore, &sz);
 
110
        assert (rc == 0);
 
111
        rc = zmq_sendmsg (xrep, &msg, rcvmore ? ZMQ_SNDMORE : 0);
 
112
        assert (rc >= 0);
 
113
    }
 
114
 
 
115
    //  Receive the reply.
 
116
    rc = zmq_recv (req, buff, 3, 0);
 
117
    assert (rc == 3);
 
118
    assert (memcmp (buff, "GHI", 3) == 0);
 
119
    rc = zmq_getsockopt (req, ZMQ_RCVMORE, &rcvmore, &sz);
 
120
    assert (rc == 0);
 
121
    assert (rcvmore);
 
122
    rc = zmq_recv (req, buff, 3, 0);
 
123
    assert (rc == 3);
 
124
    assert (memcmp (buff, "JKL", 3) == 0);
 
125
    rc = zmq_getsockopt (req, ZMQ_RCVMORE, &rcvmore, &sz);
 
126
    assert (rc == 0);
 
127
    assert (!rcvmore);
 
128
 
 
129
    //  Clean up.
 
130
    rc = zmq_close (req);
 
131
    assert (rc == 0);
 
132
    rc = zmq_close (rep);
 
133
    assert (rc == 0);
 
134
    rc = zmq_close (xrep);
 
135
    assert (rc == 0);
 
136
    rc = zmq_close (xreq);
 
137
    assert (rc == 0);
 
138
    rc = zmq_term (ctx);
 
139
    assert (rc == 0);
 
140
 
 
141
    return 0 ;
 
142
}