libzmq  master
ZeroMQ C++ Core Engine (LIBZMQ)
fq.cpp
Go to the documentation of this file.
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 #include "precompiled.hpp"
31 #include "fq.hpp"
32 #include "pipe.hpp"
33 #include "err.hpp"
34 #include "msg.hpp"
35 
37  active (0),
38  last_in (NULL),
39  current (0),
40  more (false)
41 {
42 }
43 
45 {
46  zmq_assert (pipes.empty ());
47 }
48 
50 {
51  pipes.push_back (pipe_);
52  pipes.swap (active, pipes.size () - 1);
53  active++;
54 }
55 
57 {
58  const pipes_t::size_type index = pipes.index (pipe_);
59 
60  // Remove the pipe from the list; adjust number of active pipes
61  // accordingly.
62  if (index < active) {
63  active--;
64  pipes.swap (index, active);
65  if (current == active)
66  current = 0;
67  }
68  pipes.erase (pipe_);
69 
70  if (last_in == pipe_) {
72  last_in = NULL;
73  }
74 }
75 
77 {
78  // Move the pipe to the list of active pipes.
79  pipes.swap (pipes.index (pipe_), active);
80  active++;
81 }
82 
84 {
85  return recvpipe (msg_, NULL);
86 }
87 
88 int zmq::fq_t::recvpipe (msg_t *msg_, pipe_t **pipe_)
89 {
90  // Deallocate old content of the message.
91  int rc = msg_->close ();
92  errno_assert (rc == 0);
93 
94  // Round-robin over the pipes to get the next message.
95  while (active > 0) {
96 
97  // Try to fetch new message. If we've already read part of the message
98  // subsequent part should be immediately available.
99  bool fetched = pipes [current]->read (msg_);
100 
101  // Note that when message is not fetched, current pipe is deactivated
102  // and replaced by another active pipe. Thus we don't have to increase
103  // the 'current' pointer.
104  if (fetched) {
105  if (pipe_)
106  *pipe_ = pipes [current];
107  more = msg_->flags () & msg_t::more? true: false;
108  if (!more) {
109  last_in = pipes [current];
110  current = (current + 1) % active;
111  }
112  return 0;
113  }
114 
115  // Check the atomicity of the message.
116  // If we've already received the first part of the message
117  // we should get the remaining parts without blocking.
118  zmq_assert (!more);
119 
120  active--;
122  if (current == active)
123  current = 0;
124  }
125 
126  // No message is available. Initialise the output parameter
127  // to be a 0-byte message.
128  rc = msg_->init ();
129  errno_assert (rc == 0);
130  errno = EAGAIN;
131  return -1;
132 }
133 
135 {
136  // There are subsequent parts of the partly-read message available.
137  if (more)
138  return true;
139 
140  // Note that messing with current doesn't break the fairness of fair
141  // queueing algorithm. If there are no messages available current will
142  // get back to its original value. Otherwise it'll point to the first
143  // pipe holding messages, skipping only pipes with no messages available.
144  while (active > 0) {
145  if (pipes [current]->check_read ())
146  return true;
147 
148  // Deactivate the pipe.
149  active--;
151  if (current == active)
152  current = 0;
153  }
154 
155  return false;
156 }
157 
159 {
160  return last_in?
162 }
163 
~fq_t()
Definition: fq.cpp:44
int close()
Definition: msg.cpp:217
size_type size()
Definition: array.hpp:103
blob_t get_credential() const
Definition: fq.cpp:158
std::vector< pipe_t * >::size_type size_type
Definition: array.hpp:93
#define zmq_assert(x)
Definition: err.hpp:119
void swap(size_type index1_, size_type index2_)
Definition: array.hpp:136
bool more
Definition: fq.hpp:81
int recvpipe(msg_t *msg_, pipe_t **pipe_)
Definition: fq.cpp:88
void push_back(T *item_)
Definition: array.hpp:118
blob_t get_credential() const
Definition: pipe.cpp:138
void pipe_terminated(pipe_t *pipe_)
Definition: fq.cpp:56
pipes_t::size_type current
Definition: fq.hpp:77
void erase(T *item_)
Definition: array.hpp:125
fq_t()
Definition: fq.cpp:36
int init()
Definition: msg.cpp:82
void attach(pipe_t *pipe_)
Definition: fq.cpp:49
bool has_in()
Definition: fq.cpp:134
std::basic_string< unsigned char > blob_t
Definition: blob.hpp:134
size_type index(T *item_)
Definition: array.hpp:150
pipes_t::size_type active
Definition: fq.hpp:69
int recv(msg_t *msg_)
Definition: fq.cpp:83
void activated(pipe_t *pipe_)
Definition: fq.cpp:76
#define errno_assert(x)
Definition: err.hpp:129
pipes_t pipes
Definition: fq.hpp:65
bool empty()
Definition: array.hpp:108
pipe_t * last_in
Definition: fq.hpp:74
unsigned char flags
Definition: msg.hpp:181
blob_t saved_credential
Definition: fq.hpp:84