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_PGM_RECEIVER_HPP_INCLUDED__
31#define __ZMQ_PGM_RECEIVER_HPP_INCLUDED__
32
33#if defined ZMQ_HAVE_OPENPGM
34
35#include <map>
36#include <algorithm>
37
38#include "io_object.hpp"
39#include "i_engine.hpp"
40#include "options.hpp"
41#include "v1_decoder.hpp"
42#include "pgm_socket.hpp"
43
44namespace zmq
45{
46class io_thread_t;
47class session_base_t;
48
49class pgm_receiver_t : public io_object_t, public i_engine
50{
51 public:
52 pgm_receiver_t (zmq::io_thread_t *parent_, const options_t &options_);
53 ~pgm_receiver_t ();
54
55 int init (bool udp_encapsulation_, const char *network_);
56
57 // i_engine interface implementation.
58 void plug (zmq::io_thread_t *io_thread_, zmq::session_base_t *session_);
59 void terminate ();
60 bool restart_input ();
61 void restart_output ();
62 void zap_msg_available () {}
63 const endpoint_uri_pair_t &get_endpoint () const;
64
65 // i_poll_events interface implementation.
66 void in_event ();
67 void timer_event (int token);
68
69 private:
70 // Unplug the engine from the session.
71 void unplug ();
72
73 // Decode received data (inpos, insize) and forward decoded
74 // messages to the session.
75 int process_input (v1_decoder_t *decoder);
76
77 // PGM is not able to move subscriptions upstream. Thus, drop all
78 // the pending subscriptions.
79 void drop_subscriptions ();
80
81 // RX timeout timer ID.
82 enum
83 {
84 rx_timer_id = 0xa1
85 };
86
87 const endpoint_uri_pair_t _empty_endpoint;
88
89 // RX timer is running.
90 bool has_rx_timer;
91
92 // If joined is true we are already getting messages from the peer.
93 // It it's false, we are getting data but still we haven't seen
94 // beginning of a message.
95 struct peer_info_t
96 {
97 bool joined;
98 v1_decoder_t *decoder;
99 };
100
101 struct tsi_comp
102 {
103 bool operator() (const pgm_tsi_t &ltsi, const pgm_tsi_t &rtsi) const
104 {
105 uint32_t ll[2], rl[2];
106 memcpy (ll, &ltsi, sizeof (ll));
107 memcpy (rl, &rtsi, sizeof (rl));
108 return (ll[0] < rl[0]) || (ll[0] == rl[0] && ll[1] < rl[1]);
109 }
110 };
111
112 typedef std::map<pgm_tsi_t, peer_info_t, tsi_comp> peers_t;
113 peers_t peers;
114
115 // PGM socket.
116 pgm_socket_t pgm_socket;
117
118 // Socket options.
119 options_t options;
120
121 // Associated session.
122 zmq::session_base_t *session;
123
124 const pgm_tsi_t *active_tsi;
125
126 // Number of bytes not consumed by the decoder due to pipe overflow.
127 size_t insize;
128
129 // Pointer to data still waiting to be processed by the decoder.
130 const unsigned char *inpos;
131
132 // Poll handle associated with PGM socket.
133 handle_t socket_handle;
134
135 // Poll handle associated with engine PGM waiting pipe.
136 handle_t pipe_handle;
137
138 ZMQ_NON_COPYABLE_NOR_MOVABLE (pgm_receiver_t)
139};
140}
141
142#endif
143
144#endif
145