libzmq  master
ZeroMQ C++ Core Engine (LIBZMQ)
dist.hpp
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 #ifndef __ZMQ_DIST_HPP_INCLUDED__
31 #define __ZMQ_DIST_HPP_INCLUDED__
32 
33 #include <vector>
34 
35 #include "array.hpp"
36 #include "pipe.hpp"
37 
38 namespace zmq
39 {
40 
41  class pipe_t;
42  class msg_t;
43 
44  // Class manages a set of outbound pipes. It sends each messages to
45  // each of them.
46  class dist_t
47  {
48  public:
49 
50  dist_t ();
51  ~dist_t ();
52 
53  // Adds the pipe to the distributor object.
54  void attach (zmq::pipe_t *pipe_);
55 
56  // Activates pipe that have previously reached high watermark.
57  void activated (zmq::pipe_t *pipe_);
58 
59  // Mark the pipe as matching. Subsequent call to send_to_matching
60  // will send message also to this pipe.
61  void match (zmq::pipe_t *pipe_);
62 
63  // Marks all pipes that are not matched as matched and vice-versa.
64  void reverse_match();
65 
66  // Mark all pipes as non-matching.
67  void unmatch ();
68 
69  // Removes the pipe from the distributor object.
70  void pipe_terminated (zmq::pipe_t *pipe_);
71 
72  // Send the message to the matching outbound pipes.
73  int send_to_matching (zmq::msg_t *msg_);
74 
75  // Send the message to all the outbound pipes.
76  int send_to_all (zmq::msg_t *msg_);
77 
78  bool has_out ();
79 
80  // check HWM of all pipes matching
81  bool check_hwm ();
82 
83  private:
84 
85  // Write the message to the pipe. Make the pipe inactive if writing
86  // fails. In such a case false is returned.
87  bool write (zmq::pipe_t *pipe_, zmq::msg_t *msg_);
88 
89  // Put the message to all active pipes.
90  void distribute (zmq::msg_t *msg_);
91 
92  // List of outbound pipes.
94  pipes_t pipes;
95 
96  // Number of all the pipes to send the next message to.
98 
99  // Number of active pipes. All the active pipes are located at the
100  // beginning of the pipes array. These are the pipes the messages
101  // can be sent to at the moment.
103 
104  // Number of pipes eligible for sending messages to. This includes all
105  // the active pipes plus all the pipes that we can in theory send
106  // messages to (the HWM is not yet reached), but sending a message
107  // to them would result in partial message being delivered, ie. message
108  // with initial parts missing.
110 
111  // True if last we are in the middle of a multipart message.
112  bool more;
113 
114  dist_t (const dist_t&);
115  const dist_t &operator = (const dist_t&);
116  };
117 
118 }
119 
120 #endif
std::vector< zmq::pipe_t * >::size_type size_type
Definition: array.hpp:93
int send_to_all(zmq::msg_t *msg_)
Definition: dist.cpp:140
void activated(zmq::pipe_t *pipe_)
Definition: dist.cpp:124
const dist_t & operator=(const dist_t &)
void match(zmq::pipe_t *pipe_)
Definition: dist.cpp:68
pipes_t::size_type eligible
Definition: dist.hpp:109
pipes_t::size_type matching
Definition: dist.hpp:97
bool check_hwm()
Definition: dist.cpp:226
int send_to_matching(zmq::msg_t *msg_)
Definition: dist.cpp:146
void attach(zmq::pipe_t *pipe_)
Definition: dist.cpp:50
bool more
Definition: dist.hpp:112
bool has_out()
Definition: dist.cpp:205
~dist_t()
Definition: dist.cpp:45
pipes_t pipes
Definition: dist.hpp:94
bool write(zmq::pipe_t *pipe_, zmq::msg_t *msg_)
Definition: dist.cpp:210
pipes_t::size_type active
Definition: dist.hpp:102
void unmatch()
Definition: dist.cpp:99
dist_t()
Definition: dist.cpp:37
void pipe_terminated(zmq::pipe_t *pipe_)
Definition: dist.cpp:104
void reverse_match()
Definition: dist.cpp:83
Definition: address.hpp:35
void distribute(zmq::msg_t *msg_)
Definition: dist.cpp:163
array_t< zmq::pipe_t, 2 > pipes_t
Definition: dist.hpp:93