1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
|
/*
*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*
*/
#include "qpid/log/Statement.h"
#include <iostream>
#include "ClientChannel.h"
#include "qpid/sys/Monitor.h"
#include "ClientMessage.h"
#include "qpid/QpidError.h"
#include "Connection.h"
#include "ConnectionHandler.h"
#include "FutureResponse.h"
#include "MessageListener.h"
#include <boost/format.hpp>
#include <boost/bind.hpp>
// FIXME aconway 2007-01-26: Evaluate all throws, ensure consistent
// handling of errors that should close the connection or the channel.
// Make sure the user thread receives a connection in each case.
//
using namespace std;
using namespace boost;
using namespace qpid::client;
using namespace qpid::framing;
using namespace qpid::sys;
namespace qpid{
namespace client{
const std::string empty;
class ScopedSync
{
Session& session;
public:
ScopedSync(Session& s, bool enabled = true) : session(s) { session.setSynchronous(enabled); }
~ScopedSync() { session.setSynchronous(false); }
};
}}
Channel::Channel(bool _transactional, u_int16_t _prefetch) :
prefetch(_prefetch), transactional(_transactional), running(false)
{
}
Channel::~Channel()
{
join();
}
void Channel::open(ConnectionImpl::shared_ptr c, SessionCore::shared_ptr s)
{
if (isOpen())
THROW_QPID_ERROR(INTERNAL_ERROR, "Attempt to re-open channel");
connection = c;
sessionCore = s;
session = auto_ptr<Session>(new Session(c, s));
}
bool Channel::isOpen() const {
Mutex::ScopedLock l(lock);
return connection;
}
void Channel::setQos() {
session->basicQos(0, getPrefetch(), false);
if(isTransactional()) {
//I think this is wrong! should only send TxSelect once...
session->txSelect();
}
}
void Channel::setPrefetch(uint16_t _prefetch){
prefetch = _prefetch;
setQos();
}
void Channel::declareExchange(Exchange& exchange, bool synch){
FieldTable args;
ScopedSync s(*session, synch);
session->exchangeDeclare(0, exchange.getName(), exchange.getType(), empty, false, false, false, args);
}
void Channel::deleteExchange(Exchange& exchange, bool synch){
ScopedSync s(*session, synch);
session->exchangeDelete(0, exchange.getName(), false);
}
void Channel::declareQueue(Queue& queue, bool synch){
FieldTable args;
ScopedSync s(*session, synch);
Response r = session->queueDeclare(0, queue.getName(), empty, false/*passive*/, queue.isDurable(),
queue.isExclusive(), queue.isAutoDelete(), !synch, args);
if(synch) {
if(queue.getName().length() == 0)
queue.setName(r.as<QueueDeclareOkBody>().getQueue());
}
}
void Channel::deleteQueue(Queue& queue, bool ifunused, bool ifempty, bool synch){
ScopedSync s(*session, synch);
session->queueDelete(0, queue.getName(), ifunused, ifempty, !synch);
}
void Channel::bind(const Exchange& exchange, const Queue& queue, const std::string& key, const FieldTable& args, bool synch){
string e = exchange.getName();
string q = queue.getName();
ScopedSync s(*session, synch);
session->queueBind(0, q, e, key, args);
}
void Channel::commit(){
session->txCommit();
}
void Channel::rollback(){
session->txRollback();
}
void Channel::consume(
Queue& queue, const std::string& tag, MessageListener* listener,
AckMode ackMode, bool noLocal, bool synch, const FieldTable* fields) {
if (tag.empty()) {
throw Exception("A tag must be specified for a consumer.");
}
{
Mutex::ScopedLock l(lock);
ConsumerMap::iterator i = consumers.find(tag);
if (i != consumers.end())
throw Exception(boost::format("Consumer already exists with tag: '%1%'") % tag);
Consumer& c = consumers[tag];
c.listener = listener;
c.ackMode = ackMode;
c.lastDeliveryTag = 0;
}
ScopedSync s(*session, synch);
session->basicConsume(0, queue.getName(), tag, noLocal,
ackMode == NO_ACK, false, !synch,
fields ? *fields : FieldTable());
}
void Channel::cancel(const std::string& tag, bool synch) {
Consumer c;
{
Mutex::ScopedLock l(lock);
ConsumerMap::iterator i = consumers.find(tag);
if (i == consumers.end())
return;
c = i->second;
consumers.erase(i);
}
ScopedSync s(*session, synch);
session->basicCancel(tag, !synch);
}
bool Channel::get(Message& msg, const Queue& queue, AckMode ackMode) {
Response response = session->basicGet(0, queue.getName(), ackMode == NO_ACK);
sessionCore->flush();//TODO: need to expose the ability to request completion info through session
if (response.isA<BasicGetEmptyBody>()) {
return false;
} else {
ReceivedContent::shared_ptr content = gets.pop();
content->populate(msg);
return true;
}
}
void Channel::publish(const Message& msg, const Exchange& exchange,
const std::string& routingKey,
bool mandatory, bool immediate) {
const string e = exchange.getName();
string key = routingKey;
session->basicPublish(0, e, key, mandatory, immediate, msg);
}
void Channel::close()
{
session->close();
{
Mutex::ScopedLock l(lock);
if (connection);
{
sessionCore.reset();
connection.reset();
}
}
stop();
}
void Channel::start(){
running = true;
dispatcher = Thread(*this);
}
void Channel::stop() {
//session->stop();
gets.close();
join();
}
void Channel::join() {
Mutex::ScopedLock l(stopLock);
if(running && dispatcher.id()) {
dispatcher.join();
running = false;
}
}
void Channel::run() {
try {
while (true) {
ReceivedContent::shared_ptr content = session->get();
//need to dispatch this to the relevant listener:
if (content->isA<BasicDeliverBody>()) {
ConsumerMap::iterator i = consumers.find(content->as<BasicDeliverBody>()->getConsumerTag());
if (i != consumers.end()) {
Message msg;
content->populate(msg);
i->second.listener->received(msg);
} else {
QPID_LOG(warning, "Dropping message for unrecognised consumer: " << content->getMethod());
}
} else if (content->isA<BasicGetOkBody>()) {
gets.push(content);
} else {
QPID_LOG(warning, "Dropping unsupported message type: " << content->getMethod());
}
}
} catch (const QueueClosed&) {}
}
|