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_XSUB_HPP_INCLUDED__
31 #define __ZMQ_XSUB_HPP_INCLUDED__
32 
33 #include "socket_base.hpp"
34 #include "session_base.hpp"
35 #include "dist.hpp"
36 #include "fq.hpp"
37 #ifdef ZMQ_USE_RADIX_TREE
38 #include "radix_tree.hpp"
39 #else
40 #include "trie.hpp"
41 #endif
42 
43 namespace zmq
44 {
45 class ctx_t;
46 class pipe_t;
47 class io_thread_t;
48 
49 class xsub_t : public socket_base_t
50 {
51   public:
52     xsub_t (zmq::ctx_t *parent_, uint32_t tid_, int sid_);
53     ~xsub_t () ZMQ_OVERRIDE;
54 
55   protected:
56     //  Overrides of functions from socket_base_t.
57     void xattach_pipe (zmq::pipe_t *pipe_,
58                        bool subscribe_to_all_,
59                        bool locally_initiated_) ZMQ_FINAL;
60     int xsetsockopt (int option_,
61                      const void *optval_,
62                      size_t optvallen_) ZMQ_OVERRIDE;
63     int xsend (zmq::msg_t *msg_) ZMQ_OVERRIDE;
64     bool xhas_out () ZMQ_OVERRIDE;
65     int xrecv (zmq::msg_t *msg_) ZMQ_FINAL;
66     bool xhas_in () ZMQ_FINAL;
67     void xread_activated (zmq::pipe_t *pipe_) ZMQ_FINAL;
68     void xwrite_activated (zmq::pipe_t *pipe_) ZMQ_FINAL;
69     void xhiccuped (pipe_t *pipe_) ZMQ_FINAL;
70     void xpipe_terminated (zmq::pipe_t *pipe_) ZMQ_FINAL;
71 
72   private:
73     //  Check whether the message matches at least one subscription.
74     bool match (zmq::msg_t *msg_);
75 
76     //  Function to be applied to the trie to send all the subsciptions
77     //  upstream.
78     static void
79     send_subscription (unsigned char *data_, size_t size_, void *arg_);
80 
81     //  Fair queueing object for inbound pipes.
82     fq_t _fq;
83 
84     //  Object for distributing the subscriptions upstream.
85     dist_t _dist;
86 
87     //  The repository of subscriptions.
88 #ifdef ZMQ_USE_RADIX_TREE
89     radix_tree_t _subscriptions;
90 #else
91     trie_t _subscriptions;
92 #endif
93 
94     //  If true, 'message' contains a matching message to return on the
95     //  next recv call.
96     bool _has_message;
97     msg_t _message;
98 
99     //  If true, part of a multipart message was already sent, but
100     //  there are following parts still waiting.
101     bool _more_send;
102 
103     //  If true, part of a multipart message was already received, but
104     //  there are following parts still waiting.
105     bool _more_recv;
106 
107     //  If true, subscribe and cancel messages are processed for the rest
108     //  of multipart message.
109     bool _process_subscribe;
110 
111     //  This option is enabled with ZMQ_ONLY_FIRST_SUBSCRIBE.
112     //  If true, messages following subscribe/unsubscribe in a multipart
113     //  message are treated as user data regardless of the first byte.
114     bool _only_first_subscribe;
115 
116     ZMQ_NON_COPYABLE_NOR_MOVABLE (xsub_t)
117 };
118 }
119 
120 #endif
121