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