summaryrefslogtreecommitdiff
path: root/qpid/cpp/src/qpid/ha/ConnectionObserver.cpp
blob: a824adb871cd1e736f28cd5a4abc082b0169b658 (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
/*
 *
 * 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 "ConnectionObserver.h"
#include "BrokerInfo.h"
#include "HaBroker.h"
#include "qpid/Url.h"
#include "qpid/types/Variant.h"
#include "qpid/broker/Connection.h"
#include "qpid/log/Statement.h"

namespace qpid {
namespace ha {

ConnectionObserver::ConnectionObserver(HaBroker& hb, const types::Uuid& uuid)
    : haBroker(hb), logPrefix(hb.logPrefix), self(uuid) {}

bool ConnectionObserver::getBrokerInfo(const broker::Connection& connection, BrokerInfo& info) {
    qpid::types::Variant::Map::const_iterator i = connection.getClientProperties().find(ConnectionObserver::BACKUP_TAG);
    if (i != connection.getClientProperties().end() && i->second.getType() == qpid::types::VAR_MAP) {
        info = BrokerInfo(i->second.asMap());
        return true;
    }
    return false;
}

bool ConnectionObserver::getAddress(const broker::Connection& connection, Address& addr) {
    qpid::types::Variant::Map::const_iterator i = connection.getClientProperties().find(ConnectionObserver::ADDRESS_TAG);
    if (i != connection.getClientProperties().end()) {
        Url url;
        url.parseNoThrow(i->second.asString().c_str());
        if (!url.empty()) {
            addr = url[0];
            return true;
        }
    }
    return false;
}

void ConnectionObserver::setObserver(const ObserverPtr& o)
{
    sys::Mutex::ScopedLock l(lock);
    observer = o;
}

ConnectionObserver::ObserverPtr ConnectionObserver::getObserver() {
    sys::Mutex::ScopedLock l(lock);
    return observer;
}

void ConnectionObserver::reset() {
    sys::Mutex::ScopedLock l(lock);
    observer.reset();
}

bool ConnectionObserver::isSelf(const broker::Connection& connection) {
    BrokerInfo info;
    return getBrokerInfo(connection, info) && info.getSystemId() == self;
}

void ConnectionObserver::opened(broker::Connection& connection) {
    try {
        if (isSelf(connection)) { // Reject self connections
            // Set my own address if there is an address header.
            Address addr;
            if (getAddress(connection, addr)) haBroker.setAddress(addr);
            QPID_LOG(trace, logPrefix << "Rejected self connection "+connection.getMgmtId());
            connection.abort();
            return;
        }
        if (connection.isLink()) return; // Allow outgoing links.
        if (connection.getClientProperties().find(ADMIN_TAG) != connection.getClientProperties().end()) {
            QPID_LOG(trace, logPrefix << "Accepted admin connection: " << connection.getMgmtId());
            return;                 // No need to call observer, always allow admins.
        }
        ObserverPtr o(getObserver());
        if (o) o->opened(connection);
    }
    catch (const std::exception& e) {
        QPID_LOG(error, logPrefix << "Error on incoming connection " << connection.getMgmtId()
                 << ": " << e.what());
        throw;
    }
}

void ConnectionObserver::closed(broker::Connection& connection) {
    if (isSelf(connection)) return; // Ignore closing of self connections.
    try {
        ObserverPtr o(getObserver());
        if (o) o->closed(connection);
    }
    catch (const std::exception& e) {
        QPID_LOG(error, logPrefix << "Error closing incoming connection " << connection.getMgmtId()
                 << ": " << e.what());
        throw;
    }
}

const std::string ConnectionObserver::ADMIN_TAG="qpid.ha-admin";
const std::string ConnectionObserver::BACKUP_TAG="qpid.ha-backup";
const std::string ConnectionObserver::ADDRESS_TAG="qpid.ha-address";

}} // namespace qpid::ha