libzmq  master
ZeroMQ C++ Core Engine (LIBZMQ)
dealer.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 "precompiled.hpp"
31 #include "macros.hpp"
32 #include "dealer.hpp"
33 #include "err.hpp"
34 #include "msg.hpp"
35 
36 zmq::dealer_t::dealer_t (class ctx_t *parent_, uint32_t tid_, int sid_) :
37  socket_base_t (parent_, tid_, sid_),
38  probe_router (false)
39 {
41 }
42 
44 {
45 }
46 
47 void zmq::dealer_t::xattach_pipe (pipe_t *pipe_, bool subscribe_to_all_)
48 {
49  LIBZMQ_UNUSED (subscribe_to_all_);
50 
51  zmq_assert (pipe_);
52 
53  if (probe_router) {
54  msg_t probe_msg_;
55  int rc = probe_msg_.init ();
56  errno_assert (rc == 0);
57 
58  rc = pipe_->write (&probe_msg_);
59  // zmq_assert (rc) is not applicable here, since it is not a bug.
60  pipe_->flush ();
61 
62  rc = probe_msg_.close ();
63  errno_assert (rc == 0);
64  }
65 
66  fq.attach (pipe_);
67  lb.attach (pipe_);
68 }
69 
70 int zmq::dealer_t::xsetsockopt (int option_, const void *optval_,
71  size_t optvallen_)
72 {
73  bool is_int = (optvallen_ == sizeof (int));
74  int value = 0;
75  if (is_int) memcpy(&value, optval_, sizeof (int));
76 
77  switch (option_) {
78  case ZMQ_PROBE_ROUTER:
79  if (is_int && value >= 0) {
80  probe_router = (value != 0);
81  return 0;
82  }
83  break;
84 
85  default:
86  break;
87  }
88 
89  errno = EINVAL;
90  return -1;
91 }
92 
94 {
95  return sendpipe (msg_, NULL);
96 }
97 
99 {
100  return recvpipe (msg_, NULL);
101 }
102 
104 {
105  return fq.has_in ();
106 }
107 
109 {
110  return lb.has_out ();
111 }
112 
114 {
115  return fq.get_credential ();
116 }
117 
118 
120 {
121  fq.activated (pipe_);
122 }
123 
125 {
126  lb.activated (pipe_);
127 }
128 
130 {
131  fq.pipe_terminated (pipe_);
132  lb.pipe_terminated (pipe_);
133 }
134 
136 {
137  return lb.sendpipe (msg_, pipe_);
138 }
139 
141 {
142  return fq.recvpipe (msg_, pipe_);
143 }
void xread_activated(zmq::pipe_t *pipe_)
Definition: dealer.cpp:119
int close()
Definition: msg.cpp:217
blob_t get_credential() const
Definition: fq.cpp:158
bool write(msg_t *msg_)
Definition: pipe.cpp:221
#define ZMQ_DEALER
Definition: zmq.h:251
int sendpipe(zmq::msg_t *msg_, zmq::pipe_t **pipe_)
Definition: dealer.cpp:135
#define zmq_assert(x)
Definition: err.hpp:119
int xsetsockopt(int option_, const void *optval_, size_t optvallen_)
Definition: dealer.cpp:70
void pipe_terminated(pipe_t *pipe_)
Definition: lb.cpp:55
int recvpipe(msg_t *msg_, pipe_t **pipe_)
Definition: fq.cpp:88
int recvpipe(zmq::msg_t *msg_, zmq::pipe_t **pipe_)
Definition: dealer.cpp:140
void xattach_pipe(zmq::pipe_t *pipe_, bool subscribe_to_all_)
Definition: dealer.cpp:47
void pipe_terminated(pipe_t *pipe_)
Definition: fq.cpp:56
bool probe_router
Definition: dealer.hpp:81
bool xhas_in()
Definition: dealer.cpp:103
bool has_out()
Definition: lb.cpp:150
#define ZMQ_PROBE_ROUTER
Definition: zmq.h:304
#define LIBZMQ_UNUSED(object)
Definition: macros.hpp:6
int init()
Definition: msg.cpp:82
int sendpipe(msg_t *msg_, pipe_t **pipe_)
Definition: lb.cpp:87
void attach(pipe_t *pipe_)
Definition: fq.cpp:49
bool has_in()
Definition: fq.cpp:134
blob_t get_credential() const
Definition: dealer.cpp:113
std::basic_string< unsigned char > blob_t
Definition: blob.hpp:134
void flush()
Definition: pipe.cpp:248
void xpipe_terminated(zmq::pipe_t *pipe_)
Definition: dealer.cpp:129
void attach(pipe_t *pipe_)
Definition: lb.cpp:49
bool xhas_out()
Definition: dealer.cpp:108
int xrecv(zmq::msg_t *msg_)
Definition: dealer.cpp:98
void activated(pipe_t *pipe_)
Definition: fq.cpp:76
#define errno_assert(x)
Definition: err.hpp:129
void xwrite_activated(zmq::pipe_t *pipe_)
Definition: dealer.cpp:124
dealer_t(zmq::ctx_t *parent_, uint32_t tid_, int sid)
Definition: dealer.cpp:36
int xsend(zmq::msg_t *msg_)
Definition: dealer.cpp:93
options_t options
Definition: own.hpp:109
void activated(pipe_t *pipe_)
Definition: lb.cpp:75