summaryrefslogtreecommitdiff
path: root/qpid/cpp/src/qpid/messaging/ProtocolRegistry.cpp
blob: dbb0d6dfc2ad09bf2b9b663e33ef326bf089083d (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
/*
 *
 * 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 "ProtocolRegistry.h"
#include "qpid/messaging/exceptions.h"
#include "qpid/client/amqp0_10/ConnectionImpl.h"
#include "qpid/client/LoadPlugins.h"
#include "qpid/log/Statement.h"
#include "qpid/Options.h"
#include "qpid/StringUtils.h"
#include "config.h"
#include <map>
#include <sstream>
#include <boost/bind.hpp>

using qpid::types::Variant;

namespace qpid {
namespace messaging {
namespace {
struct ProtocolOptions : qpid::Options
{
    std::string protocolDefaults;

    ProtocolOptions() : qpid::Options("Protocol Settings")
    {
        addOptions()
            ("protocol-defaults", optValue(protocolDefaults, "PROTOCOLS"), "Protocols to use when none are specified");
    }
};
const std::string SEPARATOR(", ");
const std::string EMPTY;
std::string join(const std::vector<std::string>& in, const std::string& base=EMPTY, const std::string& separator = SEPARATOR)
{
    std::stringstream out;
    if (!base.empty()) out << base;
    for (std::vector<std::string>::const_iterator i = in.begin(); i != in.end(); ++i) {
        if (i != in.begin()) out << separator;
        out << *i;
    }
    return out.str();
}

typedef std::map<std::string, ProtocolRegistry::Factory*> Factories;

ConnectionImpl* create_0_10(const std::string& url, const qpid::types::Variant::Map& options)
{
    return new qpid::client::amqp0_10::ConnectionImpl(url, options);
}

class Registry
{
  public:
    Registry()
    {
        factories["amqp0-10"] = &create_0_10;
        CommonOptions common("", "", QPIDC_CONF_FILE);
        ProtocolOptions options;
        try {
            common.parse(0, 0, common.clientConfig, true);
            options.parse (0, 0, common.clientConfig, true);
        } catch (const std::exception& e) {
            throw qpid::types::Exception(QPID_MSG("Failed to parse options while initialising Protocol Registry: " << e.what()));
        }
        QPID_LOG(debug, "Protocol defaults: " << options.protocolDefaults);
        if (!options.protocolDefaults.empty()) {
            split(versions, options.protocolDefaults, ", ");
        }
    }
    ProtocolRegistry::Factory* find(const std::string& name) const
    {
        Factories::const_iterator i = factories.find(name);
        if (i == factories.end()) {
            std::stringstream error;
            error << "Unsupported protocol: " << name;
            error << " (valid values are " << getNames() << ")";
            throw MessagingException(error.str());
        } else {
            return i->second;
        }
    }
    void add(const std::string& name, ProtocolRegistry::Factory* factory)
    {
        factories[name] = factory;
    }
    std::string getNames() const
    {
        std::stringstream names;
        for (Factories::const_iterator i = factories.begin(); i != factories.end(); ++i) {
            if (i != factories.begin()) names << ", ";
            names << i->first;
        }
        return names.str();
    }
    void collectNames(std::vector<std::string>& names) const
    {
        for (std::vector< std::string >::const_iterator i = versions.begin(); i != versions.end(); ++i) {
            Factories::const_iterator j = factories.find(*i);
            if (j == factories.end()) {
                QPID_LOG(notice, "Unsupported protocol specified in defaults " << *i);
            } else {
                names.push_back(*i);
            }
        }
        if (names.empty()) {
            if (!versions.empty()) {
                QPID_LOG(warning, "Protocol defaults specified are not valid (" << join(versions) << ") falling back to  " << getNames());
            }
            for (Factories::const_iterator i = factories.begin(); i != factories.end(); ++i) {
                names.push_back(i->first);
            }
        }
    }
  private:
    Factories factories;
    std::vector<std::string> versions;
};

Registry& theRegistry()
{
    static Registry factories;
    return factories;
}

bool extract(const std::string& key, Variant& value, const Variant::Map& in, Variant::Map& out)
{
    bool matched = false;
    for (Variant::Map::const_iterator i = in.begin(); i != in.end(); ++i) {
        if (i->first == key) {
            value = i->second;
            matched = true;
        } else {
            out.insert(*i);
        }
    }
    return matched;
}
}

ConnectionImpl* ProtocolRegistry::create(const std::string& url, const Variant::Map& options)
{
    qpid::client::theModuleLoader();//ensure modules are loaded
    Variant name;
    Variant::Map stripped;
    std::vector<std::string> versions;
    if (extract("protocol", name, options, stripped)) {
        split(versions, name.asString(), ", ");
    } else {
        theRegistry().collectNames(versions);
    }
    bool debugOn;
    QPID_LOG_TEST(debug, debugOn);
    if (debugOn) {
        QPID_LOG(debug, "Trying versions " << join(versions));
    }
    return createInternal(versions, url, stripped, join(versions, "No suitable protocol version supported by peer, tried "));
}

ConnectionImpl* ProtocolRegistry::createInternal(const std::vector<std::string>& requested, const std::string& url, const Variant::Map& options, const std::string& error)
{
    std::vector<std::string>::const_iterator i = requested.begin();
    if (i == requested.end())
        throw MessagingException(error);
    std::string name = *i;
    ConnectionImpl* result = theRegistry().find(name)(url, options);
    result->next = boost::bind(&ProtocolRegistry::createInternal, std::vector<std::string>(++i, requested.end()), url, options, error);
    return result;
 }

ConnectionImpl* ProtocolRegistry::next(ConnectionImpl* last)
{
    if (last->next) {
        return last->next();
    }
    throw MessagingException("No suitable protocol version supported by peer");
}

void ProtocolRegistry::add(const std::string& name, Factory* factory)
{
    theRegistry().add(name, factory);
}

}} // namespace qpid::messaging