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
|
/*
*
* 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 "ConnectionHandler.h"
#include "qpid/log/Statement.h"
#include "qpid/framing/amqp_framing.h"
#include "qpid/framing/AMQP_HighestVersion.h"
#include "qpid/framing/all_method_bodies.h"
#include "qpid/framing/ClientInvoker.h"
#include "qpid/framing/reply_exceptions.h"
using namespace qpid::client;
using namespace qpid::framing;
using namespace boost;
namespace {
const std::string OK("OK");
const std::string PLAIN("PLAIN");
const std::string en_US("en_US");
const std::string INVALID_STATE_START("start received in invalid state");
const std::string INVALID_STATE_TUNE("tune received in invalid state");
const std::string INVALID_STATE_OPEN_OK("open-ok received in invalid state");
const std::string INVALID_STATE_CLOSE_OK("close-ok received in invalid state");
}
ConnectionHandler::ConnectionHandler()
: StateManager(NOT_STARTED), outHandler(*this), proxy(outHandler), errorCode(200)
{
mechanism = PLAIN;
locale = en_US;
heartbeat = 0;
maxChannels = 32767;
maxFrameSize = 65535;
insist = true;
version = framing::highestProtocolVersion;
ESTABLISHED.insert(FAILED);
ESTABLISHED.insert(CLOSED);
ESTABLISHED.insert(OPEN);
}
void ConnectionHandler::incoming(AMQFrame& frame)
{
if (getState() == CLOSED) {
throw Exception("Received frame on closed connection");
}
AMQBody* body = frame.getBody();
try {
if (frame.getChannel() != 0 || !invoke(static_cast<ConnectionOperations&>(*this), *body)) {
switch(getState()) {
case OPEN:
in(frame);
break;
case CLOSING:
QPID_LOG(warning, "Ignoring frame while closing connection: " << frame);
break;
default:
throw Exception("Cannot receive frames on non-zero channel until connection is established.");
}
}
}catch(std::exception& e){
QPID_LOG(warning, "Closing connection due to " << e.what());
setState(CLOSING);
proxy.close(501, e.what());
if (onError) onError(501, e.what());
}
}
void ConnectionHandler::outgoing(AMQFrame& frame)
{
if (getState() == OPEN) {
out(frame);
} else {
throw Exception("Connection is not open.");
}
}
void ConnectionHandler::waitForOpen()
{
waitFor(ESTABLISHED);
if (getState() == FAILED || getState() == CLOSED) {
throw ConnectionException(errorCode, errorText);
}
}
void ConnectionHandler::close()
{
switch (getState()) {
case NEGOTIATING:
case OPENING:
fail("Connection closed before it was established");
break;
case OPEN:
setState(CLOSING);
proxy.close(200, OK);
waitFor(CLOSED);
break;
// Nothing to do for CLOSING, CLOSED, FAILED or NOT_STARTED
}
}
void ConnectionHandler::checkState(STATES s, const std::string& msg)
{
if (getState() != s) {
throw CommandInvalidException(msg);
}
}
void ConnectionHandler::fail(const std::string& message)
{
errorCode = 502;
errorText = message;
QPID_LOG(warning, message);
setState(FAILED);
}
void ConnectionHandler::start(const FieldTable& /*serverProps*/, const Array& /*mechanisms*/, const Array& /*locales*/)
{
checkState(NOT_STARTED, INVALID_STATE_START);
setState(NEGOTIATING);
//TODO: verify that desired mechanism and locale are supported
string response = ((char)0) + uid + ((char)0) + pwd;
proxy.startOk(properties, mechanism, response, locale);
}
void ConnectionHandler::secure(const std::string& /*challenge*/)
{
throw NotImplementedException("Challenge-response cycle not yet implemented in client");
}
void ConnectionHandler::tune(uint16_t channelMax, uint16_t /*frameMax*/, uint16_t /*heartbeatMin*/, uint16_t /*heartbeatMax*/)
{
checkState(NEGOTIATING, INVALID_STATE_TUNE);
//TODO: verify that desired heartbeat and max frame size are valid
maxChannels = channelMax;
proxy.tuneOk(maxChannels, maxFrameSize, heartbeat);
setState(OPENING);
proxy.open(vhost, capabilities, insist);
}
void ConnectionHandler::openOk(const framing::Array& /*knownHosts*/)
{
checkState(OPENING, INVALID_STATE_OPEN_OK);
//TODO: store knownHosts for reconnection etc
setState(OPEN);
}
void ConnectionHandler::redirect(const std::string& /*host*/, const Array& /*knownHosts*/)
{
throw NotImplementedException("Redirection received from broker; not yet implemented in client");
}
void ConnectionHandler::close(uint16_t replyCode, const std::string& replyText)
{
proxy.closeOk();
setState(CLOSED);
errorCode = replyCode;
errorText = replyText;
QPID_LOG(warning, "Broker closed connection: " << replyCode << ", " << replyText);
if (onError) {
onError(replyCode, replyText);
}
}
void ConnectionHandler::closeOk()
{
checkState(CLOSING, INVALID_STATE_CLOSE_OK);
if (onClose) {
onClose();
}
setState(CLOSED);
}
|