summaryrefslogtreecommitdiff
path: root/qpid/cpp/examples/qmf-console/cluster-qmon.cpp
blob: 4ee2ce1edd0a86963dfed2b9974e3e4d14311f0b (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
/*
 *
 * 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 "qpid/console/ConsoleListener.h"
#include "qpid/console/SessionManager.h"
#include "qpid/sys/Time.h"
#include "qpid/sys/Mutex.h"
#include <signal.h>
#include <map>

using namespace std;
using namespace qpid::console;
using qpid::sys::Mutex;

//
//  This example maintains connections to a number of brokers (assumed
//  to be running on localhost and at ports listed in the command line
//  arguments).
//
//  The program then periodically polls queue information from a
//  single operational broker.  This is a useful illustration of how
//  one might monitor statistics on a cluster of brokers.
//

//==============================================================
// Main program
//==============================================================
class Main : public ConsoleListener {
    bool  stopping;  // Used to tell the program to exit
    Mutex lock;      // Mutex to protect the broker-map
    map<Broker*, bool> brokerMap; // Map of broker-pointers to boolean "operational" status

public:
    Main() : stopping(false) {}

    /** Invoked when a connection is established to a broker
     */
    void brokerConnected(const Broker& broker)
    {
        Mutex::ScopedLock l(lock);
        brokerMap[const_cast<Broker*>(&broker)] = true;
    }

    /** Invoked when the connection to a broker is lost
     */
    void brokerDisconnected(const Broker& broker)
    {
        Mutex::ScopedLock l(lock);
        brokerMap[const_cast<Broker*>(&broker)] = false;
    }

    int run(int argc, char** argv)
    {
        //
        // Tune the settings for this application:  We will operate synchronously only, we don't
        // wish to use the bandwidth needed to aysnchronously receive objects or events.
        //
        SessionManager::Settings sessionSettings;
        sessionSettings.rcvObjects = false;
        sessionSettings.rcvEvents = false;
        sessionSettings.rcvHeartbeats = false;

        SessionManager sm(this, sessionSettings);

        //
        // Connect to the brokers.
        //
        for (int idx = 1; idx < argc; idx++) {
            Mutex::ScopedLock l(lock);
            qpid::client::ConnectionSettings connSettings;
            connSettings.host = "localhost";
            connSettings.port = atoi(argv[idx]);
            Broker* broker = sm.addBroker(connSettings);
            brokerMap[broker] = false; // initially assume broker is disconnected
        }

        //
        // Periodically poll the first connected broker.
        //
        while (!stopping) {
            //
            // Find an operational broker
            //
            Broker* operationalBroker = 0;
            for (map<Broker*, bool>::iterator iter = brokerMap.begin();
                 iter != brokerMap.end(); iter++) {
                if (iter->second) {
                    operationalBroker = iter->first;
                    break;
                }
            }

            if (operationalBroker != 0) {
                Object::Vector list;
                sm.getObjects(list, "queue", operationalBroker);
                for (Object::Vector::iterator i = list.begin(); i != list.end(); i++) {
                    cout << "queue: " << i->attrString("name");
                    cout << "  bindingCount=" << i->attrUint64("bindingCount") << endl;
                }
            } else {
                cout << "No operational brokers" << endl;
            }

            qpid::sys::sleep(10);
            if (stopping)
                break;
        }

        {
            //
            //  The following code structure uses the mutex to protect the broker map while
            //  ensuring that sm.delBroker is called without the mutex held (which leads to
            //  a deadlock).
            //
            Mutex::ScopedLock l(lock);
            map<Broker*, bool>::iterator iter = brokerMap.begin();
            while (iter != brokerMap.end()) {
                Broker* broker = iter->first;
                brokerMap.erase(iter);
                {
                    Mutex::ScopedUnlock ul(lock);
                    sm.delBroker(broker);
                }
                iter = brokerMap.begin();
            }
        }

        return 0;
    }

    void stop() {
        stopping = true;
    }
};

Main main_program;

void signal_handler(int)
{
    main_program.stop();
}

int main(int argc, char** argv)
{
    signal(SIGINT, signal_handler);
    try {
        return main_program.run(argc, argv);
    } catch(std::exception& e) {
        cout << "Top Level Exception: " << e.what() << endl;
    }
}