summaryrefslogtreecommitdiff
path: root/qpid/cpp/src/qpid/broker/Protocol.cpp
blob: e9e7892499166e8f0ca9750b30609ebff0fa98b2 (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
/*
 *
 * 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 "Protocol.h"
#include "qpid/broker/RecoverableMessageImpl.h"
#include "qpid/amqp_0_10/Connection.h"
#include "qpid/broker/amqp_0_10/Connection.h"
#include "qpid/broker/amqp_0_10/MessageTransfer.h"
#include "qpid/broker/SecureConnection.h"
#include "qpid/log/Statement.h"

namespace qpid {
namespace broker {
namespace {
const std::string AMQP_0_10("amqp0-10");
}
ProtocolRegistry::ProtocolRegistry(const std::set<std::string>& e, Broker* b) : enabled(e), broker(b) {}

qpid::sys::ConnectionCodec* ProtocolRegistry::create(const qpid::framing::ProtocolVersion& v, qpid::sys::OutputControl& o, const std::string& id, const qpid::sys::SecuritySettings& s)
{
    if (v == qpid::framing::ProtocolVersion(0, 10)) {
        if (isEnabled(AMQP_0_10)) {
            return create_0_10(o, id, s, false);
        }
    }
    qpid::sys::ConnectionCodec* codec = 0;
    for (Protocols::const_iterator i = protocols.begin(); !codec && i != protocols.end(); ++i) {
        if (isEnabled(i->first)) {
            codec = i->second->create(v, o, id, s);
        }
    }
    return codec;
}
qpid::sys::ConnectionCodec* ProtocolRegistry::create(qpid::sys::OutputControl& o, const std::string& id, const qpid::sys::SecuritySettings& s)
{
    return create_0_10(o, id, s, true);
}

qpid::framing::ProtocolVersion ProtocolRegistry::supportedVersion() const
{
    if (isEnabled(AMQP_0_10)) {
        return qpid::framing::ProtocolVersion(0,10);
    } else {
        for (Protocols::const_iterator i = protocols.begin(); i != protocols.end(); ++i) {
            if (isEnabled(i->first)) {
                return i->second->supportedVersion();
            }
        }
    }
    QPID_LOG(error, "No enabled protocols!");
    return qpid::framing::ProtocolVersion(0,0);
}

bool ProtocolRegistry::isEnabled(const std::string& name) const
{
    return enabled.empty()/*if nothing is explicitly enabled, assume everything is*/ || enabled.find(name) != enabled.end();
}


typedef std::auto_ptr<qpid::amqp_0_10::Connection> CodecPtr;
typedef std::auto_ptr<SecureConnection> SecureConnectionPtr;
typedef std::auto_ptr<qpid::broker::amqp_0_10::Connection> ConnectionPtr;
typedef std::auto_ptr<sys::ConnectionInputHandler> InputPtr;

sys::ConnectionCodec*
ProtocolRegistry::create_0_10(qpid::sys::OutputControl& out, const std::string& id,
                              const qpid::sys::SecuritySettings& external, bool brokerActsAsClient)
{
    assert(broker);
    SecureConnectionPtr sc(new SecureConnection());
    CodecPtr c(new qpid::amqp_0_10::Connection(out, id, brokerActsAsClient));
    ConnectionPtr i(new broker::amqp_0_10::Connection(c.get(), *broker, id, external, brokerActsAsClient));
    i->setSecureConnection(sc.get());
    c->setInputHandler(InputPtr(i.release()));
    sc->setCodec(std::auto_ptr<sys::ConnectionCodec>(c));
    return sc.release();
}

boost::intrusive_ptr<const qpid::broker::amqp_0_10::MessageTransfer> ProtocolRegistry::translate(const Message& m)
{
    boost::intrusive_ptr<const qpid::broker::amqp_0_10::MessageTransfer> transfer;
    const qpid::broker::amqp_0_10::MessageTransfer* ptr = dynamic_cast<const qpid::broker::amqp_0_10::MessageTransfer*>(&m.getEncoding());
    if (ptr) transfer = boost::intrusive_ptr<const qpid::broker::amqp_0_10::MessageTransfer>(ptr);
    for (Protocols::const_iterator i = protocols.begin(); !transfer && i != protocols.end(); ++i) {
        transfer = i->second->translate(m);
    }
    if (!transfer) throw new Exception("Could not convert message into 0-10");
    return transfer;
}
boost::shared_ptr<RecoverableMessage> ProtocolRegistry::recover(qpid::framing::Buffer& b)
{
    uint32_t position = b.getPosition();
    for (Protocols::const_iterator i = protocols.begin(); i != protocols.end(); ++i) {
        boost::shared_ptr<RecoverableMessage> msg = i->second->recover(b);
        if (msg) return msg;
        else b.setPosition(position);
    }
    boost::intrusive_ptr<qpid::broker::amqp_0_10::MessageTransfer> transfer(new qpid::broker::amqp_0_10::MessageTransfer());
    transfer->decodeHeader(b);
    boost::shared_ptr<RecoverableMessage> msg(new RecoverableMessageImpl(Message(transfer, transfer)));
    return msg;
}

Message ProtocolRegistry::decode(qpid::framing::Buffer& buffer)
{
    boost::shared_ptr<RecoverableMessage> r = recover(buffer);
    r->decodeContent(buffer);
    return r->getMessage();
}

ProtocolRegistry::~ProtocolRegistry()
{
    for (Protocols::const_iterator i = protocols.begin(); i != protocols.end(); ++i) {
        delete i->second;
    }
    protocols.clear();
}
void ProtocolRegistry::add(const std::string& key, Protocol* protocol)
{
    protocols[key] = protocol;
    QPID_LOG(info, "Loaded protocol " << key);
}

}} // namespace qpid::broker