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_XPUB_HPP_INCLUDED__
31#define __ZMQ_XPUB_HPP_INCLUDED__
32
33#include <deque>
34
35#include "socket_base.hpp"
36#include "session_base.hpp"
37#include "mtrie.hpp"
38#include "dist.hpp"
39
40namespace zmq
41{
42class ctx_t;
43class msg_t;
44class pipe_t;
45class io_thread_t;
46
47class xpub_t : public socket_base_t
48{
49 public:
50 xpub_t (zmq::ctx_t *parent_, uint32_t tid_, int sid_);
51 ~xpub_t ();
52
53 // Implementations of virtual functions from socket_base_t.
54 void xattach_pipe (zmq::pipe_t *pipe_,
55 bool subscribe_to_all_ = false,
56 bool locally_initiated_ = false);
57 int xsend (zmq::msg_t *msg_);
58 bool xhas_out ();
59 int xrecv (zmq::msg_t *msg_);
60 bool xhas_in ();
61 void xread_activated (zmq::pipe_t *pipe_);
62 void xwrite_activated (zmq::pipe_t *pipe_);
63 int xsetsockopt (int option_, const void *optval_, size_t optvallen_);
64 void xpipe_terminated (zmq::pipe_t *pipe_);
65
66 private:
67 // Function to be applied to the trie to send all the subscriptions
68 // upstream.
69 static void send_unsubscription (zmq::mtrie_t::prefix_t data_,
70 size_t size_,
71 xpub_t *self_);
72
73 // Function to be applied to each matching pipes.
74 static void mark_as_matching (zmq::pipe_t *pipe_, xpub_t *self_);
75
76 // List of all subscriptions mapped to corresponding pipes.
77 mtrie_t _subscriptions;
78
79 // List of manual subscriptions mapped to corresponding pipes.
80 mtrie_t _manual_subscriptions;
81
82 // Distributor of messages holding the list of outbound pipes.
83 dist_t _dist;
84
85 // If true, send all subscription messages upstream, not just
86 // unique ones
87 bool _verbose_subs;
88
89 // If true, send all unsubscription messages upstream, not just
90 // unique ones
91 bool _verbose_unsubs;
92
93 // True if we are in the middle of sending a multi-part message.
94 bool _more_send;
95
96 // True if we are in the middle of receiving a multi-part message.
97 bool _more_recv;
98
99 // If true, subscribe and cancel messages are processed for the rest
100 // of multipart message.
101 bool _process_subscribe;
102
103 // This option is enabled with ZMQ_ONLY_FIRST_SUBSCRIBE.
104 // If true, messages following subscribe/unsubscribe in a multipart
105 // message are treated as user data regardless of the first byte.
106 bool _only_first_subscribe;
107
108 // Drop messages if HWM reached, otherwise return with EAGAIN
109 bool _lossy;
110
111 // Subscriptions will not bed added automatically, only after calling set option with ZMQ_SUBSCRIBE or ZMQ_UNSUBSCRIBE
112 bool _manual;
113
114 // Send message to the last pipe, only used if xpub is on manual and after calling set option with ZMQ_SUBSCRIBE
115 bool _send_last_pipe;
116
117 // Function to be applied to match the last pipe.
118 static void mark_last_pipe_as_matching (zmq::pipe_t *pipe_, xpub_t *self_);
119
120 // Last pipe that sent subscription message, only used if xpub is on manual
121 pipe_t *_last_pipe;
122
123 // Pipes that sent subscriptions messages that have not yet been processed, only used if xpub is on manual
124 std::deque<pipe_t *> _pending_pipes;
125
126 // Welcome message to send to pipe when attached
127 msg_t _welcome_msg;
128
129 // List of pending (un)subscriptions, ie. those that were already
130 // applied to the trie, but not yet received by the user.
131 std::deque<blob_t> _pending_data;
132 std::deque<metadata_t *> _pending_metadata;
133 std::deque<unsigned char> _pending_flags;
134
135 ZMQ_NON_COPYABLE_NOR_MOVABLE (xpub_t)
136};
137}
138
139#endif
140