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_YPIPE_CONFLATE_HPP_INCLUDED__
31#define __ZMQ_YPIPE_CONFLATE_HPP_INCLUDED__
32
33#include "platform.hpp"
34#include "dbuffer.hpp"
35#include "ypipe_base.hpp"
36
37namespace zmq
38{
39// Adapter for dbuffer, to plug it in instead of a queue for the sake
40// of implementing the conflate socket option, which, if set, makes
41// the receiving side to discard all incoming messages but the last one.
42//
43// reader_awake flag is needed here to mimic ypipe delicate behaviour
44// around the reader being asleep (see 'c' pointer being NULL in ypipe.hpp)
45
46template <typename T> class ypipe_conflate_t : public ypipe_base_t<T>
47{
48 public:
49 // Initialises the pipe.
50 inline ypipe_conflate_t () : reader_awake (false) {}
51
52 // Following function (write) deliberately copies uninitialised data
53 // when used with zmq_msg. Initialising the VSM body for
54 // non-VSM messages won't be good for performance.
55
56#ifdef ZMQ_HAVE_OPENVMS
57#pragma message save
58#pragma message disable(UNINIT)
59#endif
60 inline void write (const T &value_, bool incomplete_)
61 {
62 (void) incomplete_;
63
64 dbuffer.write (value_);
65 }
66
67#ifdef ZMQ_HAVE_OPENVMS
68#pragma message restore
69#endif
70
71 // There are no incomplete items for conflate ypipe
72 inline bool unwrite (T *) { return false; }
73
74 // Flush is no-op for conflate ypipe. Reader asleep behaviour
75 // is as of the usual ypipe.
76 // Returns false if the reader thread is sleeping. In that case,
77 // caller is obliged to wake the reader up before using the pipe again.
78 inline bool flush () { return reader_awake; }
79
80 // Check whether item is available for reading.
81 inline bool check_read ()
82 {
83 bool res = dbuffer.check_read ();
84 if (!res)
85 reader_awake = false;
86
87 return res;
88 }
89
90 // Reads an item from the pipe. Returns false if there is no value.
91 // available.
92 inline bool read (T *value_)
93 {
94 if (!check_read ())
95 return false;
96
97 return dbuffer.read (value_);
98 }
99
100 // Applies the function fn to the first elemenent in the pipe
101 // and returns the value returned by the fn.
102 // The pipe mustn't be empty or the function crashes.
103 inline bool probe (bool (*fn_) (const T &)) { return dbuffer.probe (fn_); }
104
105 protected:
106 dbuffer_t<T> dbuffer;
107 bool reader_awake;
108
109 ZMQ_NON_COPYABLE_NOR_MOVABLE (ypipe_conflate_t)
110};
111}
112
113#endif
114