summaryrefslogtreecommitdiff
path: root/qpid/cpp/src/qpid/broker/amqp_0_10/Connection.h
blob: 1bf6b2cee30ae074d8111342b4caa3aa235e1301 (plain)
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
#ifndef QPID_BROKER_AMQP_0_10_CONNECTION_H
#define QPID_BROKER_AMQP_0_10_CONNECTION_H

/*
 *
 * 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 <memory>
#include <sstream>
#include <vector>
#include <queue>

#include "qpid/broker/BrokerImportExport.h"

#include "qpid/broker/ConnectionHandler.h"
#include "qpid/broker/Connection.h"
#include "qpid/broker/OwnershipToken.h"
#include "qpid/management/Manageable.h"
#include "qpid/sys/AggregateOutput.h"
#include "qpid/sys/ConnectionInputHandler.h"
#include "qpid/sys/SecuritySettings.h"
#include "qpid/sys/Mutex.h"
#include "qpid/types/Variant.h"
#include "qpid/RefCounted.h"
#include "qpid/Url.h"
#include "qpid/ptr_map.h"

#include "qmf/org/apache/qpid/broker/Connection.h"

#include <boost/scoped_ptr.hpp>
#include <boost/bind.hpp>

#include <algorithm>

namespace qpid {
namespace sys {
class ConnectionOutputHandler;
class Timer;
class TimerTask;
}
namespace broker {

class Broker;
class LinkRegistry;
class Queue;
class SecureConnection;
class SessionHandler;

namespace amqp_0_10 {
struct ConnectionTimeoutTask;

class Connection : public sys::ConnectionInputHandler, public qpid::broker::Connection,
                   public management::Manageable,
                   public RefCounted
{
  public:
    uint32_t getFrameMax() const { return framemax; }
    uint16_t getHeartbeat() const { return heartbeat; }
    uint16_t getHeartbeatMax() const { return heartbeatmax; }

    void setFrameMax(uint32_t fm) { framemax = std::max(fm, (uint32_t) 4096); }
    void setHeartbeat(uint16_t hb) { heartbeat = hb; }
    void setHeartbeatMax(uint16_t hbm) { heartbeatmax = hbm; }


    const management::ObjectId getObjectId() const { return GetManagementObject()->getObjectId(); };
    const std::string& getUserId() const { return userId; }

    bool isFederationLink() const { return federationPeerTag.size() > 0; }
    void setFederationPeerTag(const std::string& tag) { federationPeerTag = std::string(tag); }
    const std::string& getFederationPeerTag() const { return federationPeerTag; }
    std::vector<Url>& getKnownHosts() { return knownHosts; }

    /**@return true if user is the authenticated user on this connection.
     * If id has the default realm will also compare plain username.
     */
    bool isAuthenticatedUser(const std::string& id) const {
        return (id == userId || (isDefaultRealm && id == userName));
    }

    Broker& getBroker() { return broker; }

    sys::ConnectionOutputHandler& getOutput() { return *out; }
    void activateOutput();
    void addOutputTask(OutputTask*);
    void removeOutputTask(OutputTask*);
    framing::ProtocolVersion getVersion() const { return version; }

    Connection(sys::ConnectionOutputHandler* out,
               Broker& broker,
               const std::string& mgmtId,
               const qpid::sys::SecuritySettings&,
               bool isLink = false,
               uint64_t objectId = 0);

    ~Connection ();

    /** Get the SessionHandler for channel. Create if it does not already exist */
    SessionHandler& getChannel(framing::ChannelId channel);

    /** Close the connection. Waits for the client to respond with close-ok
     * before actually destroying the connection.
     */
    QPID_BROKER_EXTERN void close(
        framing::connection::CloseCode code, const std::string& text);

    /** Abort the connection. Close abruptly and immediately. */
    QPID_BROKER_EXTERN void abort();

    // ConnectionInputHandler methods
    void received(framing::AMQFrame& frame);
    bool doOutput();
    void closed();

    void closeChannel(framing::ChannelId channel);

    // Manageable entry points
    management::ManagementObject::shared_ptr GetManagementObject(void) const;
    management::Manageable::status_t
        ManagementMethod (uint32_t methodId, management::Args& args, std::string&);

    void requestIOProcessing (boost::function0<void>);
    void recordFromServer (const framing::AMQFrame& frame);
    void recordFromClient (const framing::AMQFrame& frame);

    // gets for configured federation links
    std::string getAuthMechanism();
    std::string getAuthCredentials();
    std::string getUsername();
    std::string getPassword();
    std::string getHost();
    uint16_t    getPort();

    void notifyConnectionForced(const std::string& text);
    void setUserId(const std::string& uid);

    // credentials for connected client
    const std::string& getMgmtId() const { return mgmtId; }
    management::ManagementAgent* getAgent() const { return agent; }

    void setHeartbeatInterval(uint16_t heartbeat);
    void sendHeartbeat();
    void restartTimeout();

    void setSecureConnection(SecureConnection* secured);

    const qpid::sys::SecuritySettings& getExternalSecuritySettings() const
    {
        return securitySettings;
    }

    /** @return true if the initial connection negotiation is complete. */
    bool isOpen();

    bool isLink() const { return link; }
    void startLinkHeartbeatTimeoutTask();

    void setClientProperties(const types::Variant::Map& cp) { clientProperties = cp; }
    const types::Variant::Map& getClientProperties() const { return clientProperties; }

  private:
    // Management object is used in the constructor so must be early
    qmf::org::apache::qpid::broker::Connection::shared_ptr mgmtObject;

    //contained output tasks
    sys::AggregateOutput outputTasks;

    boost::scoped_ptr<framing::FrameHandler> outboundTracker;
    boost::scoped_ptr<sys::ConnectionOutputHandler> out;

    Broker& broker;

    framing::ProtocolVersion version;
    uint32_t framemax;
    uint16_t heartbeat;
    uint16_t heartbeatmax;
    std::string userId;
    std::string federationPeerTag;
    std::vector<Url> knownHosts;
    std::string userName;
    bool isDefaultRealm;

    typedef boost::ptr_map<framing::ChannelId, SessionHandler> ChannelMap;

    ChannelMap channels;
    qpid::sys::SecuritySettings securitySettings;
    const bool link;
    ConnectionHandler adapter;
    bool mgmtClosing;
    const std::string mgmtId;
    sys::Mutex ioCallbackLock;
    std::queue<boost::function0<void> > ioCallbacks;
    LinkRegistry& links;
    management::ManagementAgent* agent;
    sys::Timer& timer;
    boost::intrusive_ptr<sys::TimerTask> heartbeatTimer, linkHeartbeatTimer;
    boost::intrusive_ptr<ConnectionTimeoutTask> timeoutTimer;
    uint64_t objectId;
    types::Variant::Map clientProperties;

    void raiseConnectEvent();

friend class OutboundFrameTracker;

    void sent(const framing::AMQFrame& f);
    void doIoCallbacks();

  public:

    qmf::org::apache::qpid::broker::Connection::shared_ptr getMgmtObject() { return mgmtObject; }
};

}}}

#endif  /*!QPID_BROKER_AMQP_0_10_CONNECTION_H*/