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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
|
/*
* 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 "qmf/ConsoleEngineImpl.h"
#include "qmf/MessageImpl.h"
#include "qmf/SchemaImpl.h"
#include "qmf/Typecode.h"
#include "qmf/ObjectImpl.h"
#include "qmf/ObjectIdImpl.h"
#include "qmf/QueryImpl.h"
#include "qmf/ValueImpl.h"
#include "qmf/Protocol.h"
#include "qmf/SequenceManager.h"
#include "qmf/BrokerProxyImpl.h"
#include <qpid/framing/Buffer.h>
#include <qpid/framing/Uuid.h>
#include <qpid/framing/FieldTable.h>
#include <qpid/framing/FieldValue.h>
#include <qpid/log/Statement.h>
#include <qpid/sys/Time.h>
#include <qpid/sys/SystemInfo.h>
#include <string.h>
#include <iostream>
#include <fstream>
using namespace std;
using namespace qmf;
using namespace qpid::framing;
using namespace qpid::sys;
namespace {
const char* QMF_EXCHANGE = "qpid.management";
}
#define STRING_REF(s) {if (!s.empty()) item.s = const_cast<char*>(s.c_str());}
ConsoleEvent ConsoleEventImpl::copy()
{
ConsoleEvent item;
::memset(&item, 0, sizeof(ConsoleEvent));
item.kind = kind;
item.agent = agent.get();
item.classKey = classKey.get();
item.object = object;
item.context = context;
item.event = event;
item.timestamp = timestamp;
STRING_REF(name);
return item;
}
ConsoleEngineImpl::ConsoleEngineImpl(const ConsoleSettings& s) : settings(s)
{
bindingList.push_back(pair<string, string>(string(), "schema.#"));
if (settings.rcvObjects && settings.rcvEvents && settings.rcvHeartbeats && !settings.userBindings) {
bindingList.push_back(pair<string, string>(string(), "console.#"));
} else {
if (settings.rcvObjects && !settings.userBindings)
bindingList.push_back(pair<string, string>(string(), "console.obj.#"));
else
bindingList.push_back(pair<string, string>(string(), "console.obj.*.*.org.apache.qpid.broker.agent"));
if (settings.rcvEvents)
bindingList.push_back(pair<string, string>(string(), "console.event.#"));
if (settings.rcvHeartbeats)
bindingList.push_back(pair<string, string>(string(), "console.heartbeat.#"));
}
}
ConsoleEngineImpl::~ConsoleEngineImpl()
{
// This function intentionally left blank.
}
bool ConsoleEngineImpl::getEvent(ConsoleEvent& event) const
{
Mutex::ScopedLock _lock(lock);
if (eventQueue.empty())
return false;
event = eventQueue.front()->copy();
return true;
}
void ConsoleEngineImpl::popEvent()
{
Mutex::ScopedLock _lock(lock);
if (!eventQueue.empty())
eventQueue.pop_front();
}
void ConsoleEngineImpl::addConnection(BrokerProxy& broker, void* /*context*/)
{
Mutex::ScopedLock _lock(lock);
brokerList.push_back(broker.impl);
}
void ConsoleEngineImpl::delConnection(BrokerProxy& broker)
{
Mutex::ScopedLock _lock(lock);
for (vector<BrokerProxyImpl*>::iterator iter = brokerList.begin();
iter != brokerList.end(); iter++)
if (*iter == broker.impl) {
brokerList.erase(iter);
break;
}
}
uint32_t ConsoleEngineImpl::packageCount() const
{
Mutex::ScopedLock _lock(lock);
return packages.size();
}
const string& ConsoleEngineImpl::getPackageName(uint32_t idx) const
{
const static string empty;
Mutex::ScopedLock _lock(lock);
if (idx >= packages.size())
return empty;
PackageList::const_iterator iter = packages.begin();
for (uint32_t i = 0; i < idx; i++) iter++;
return iter->first;
}
uint32_t ConsoleEngineImpl::classCount(const char* packageName) const
{
Mutex::ScopedLock _lock(lock);
PackageList::const_iterator pIter = packages.find(packageName);
if (pIter == packages.end())
return 0;
const ObjectClassList& oList = pIter->second.first;
const EventClassList& eList = pIter->second.second;
return oList.size() + eList.size();
}
const SchemaClassKey* ConsoleEngineImpl::getClass(const char* packageName, uint32_t idx) const
{
Mutex::ScopedLock _lock(lock);
PackageList::const_iterator pIter = packages.find(packageName);
if (pIter == packages.end())
return 0;
const ObjectClassList& oList = pIter->second.first;
const EventClassList& eList = pIter->second.second;
uint32_t count = 0;
for (ObjectClassList::const_iterator oIter = oList.begin();
oIter != oList.end(); oIter++) {
if (count == idx)
return oIter->second->getClassKey();
count++;
}
for (EventClassList::const_iterator eIter = eList.begin();
eIter != eList.end(); eIter++) {
if (count == idx)
return eIter->second->getClassKey();
count++;
}
return 0;
}
ClassKind ConsoleEngineImpl::getClassKind(const SchemaClassKey* key) const
{
Mutex::ScopedLock _lock(lock);
PackageList::const_iterator pIter = packages.find(key->getPackageName());
if (pIter == packages.end())
return CLASS_OBJECT;
const EventClassList& eList = pIter->second.second;
if (eList.find(key) != eList.end())
return CLASS_EVENT;
return CLASS_OBJECT;
}
const SchemaObjectClass* ConsoleEngineImpl::getObjectClass(const SchemaClassKey* key) const
{
Mutex::ScopedLock _lock(lock);
PackageList::const_iterator pIter = packages.find(key->getPackageName());
if (pIter == packages.end())
return 0;
const ObjectClassList& oList = pIter->second.first;
ObjectClassList::const_iterator iter = oList.find(key);
if (iter == oList.end())
return 0;
return iter->second;
}
const SchemaEventClass* ConsoleEngineImpl::getEventClass(const SchemaClassKey* key) const
{
Mutex::ScopedLock _lock(lock);
PackageList::const_iterator pIter = packages.find(key->getPackageName());
if (pIter == packages.end())
return 0;
const EventClassList& eList = pIter->second.second;
EventClassList::const_iterator iter = eList.find(key);
if (iter == eList.end())
return 0;
return iter->second;
}
void ConsoleEngineImpl::bindPackage(const char* packageName)
{
stringstream key;
key << "console.obj.*.*." << packageName << ".#";
Mutex::ScopedLock _lock(lock);
bindingList.push_back(pair<string, string>(string(), key.str()));
for (vector<BrokerProxyImpl*>::iterator iter = brokerList.begin();
iter != brokerList.end(); iter++)
(*iter)->addBinding(QMF_EXCHANGE, key.str());
}
void ConsoleEngineImpl::bindClass(const SchemaClassKey* classKey)
{
stringstream key;
key << "console.obj.*.*." << classKey->getPackageName() << "." << classKey->getClassName() << ".#";
Mutex::ScopedLock _lock(lock);
bindingList.push_back(pair<string, string>(string(), key.str()));
for (vector<BrokerProxyImpl*>::iterator iter = brokerList.begin();
iter != brokerList.end(); iter++)
(*iter)->addBinding(QMF_EXCHANGE, key.str());
}
void ConsoleEngineImpl::bindClass(const char* packageName, const char* className)
{
stringstream key;
key << "console.obj.*.*." << packageName << "." << className << ".#";
Mutex::ScopedLock _lock(lock);
bindingList.push_back(pair<string, string>(string(), key.str()));
for (vector<BrokerProxyImpl*>::iterator iter = brokerList.begin();
iter != brokerList.end(); iter++)
(*iter)->addBinding(QMF_EXCHANGE, key.str());
}
/*
void ConsoleEngineImpl::startSync(const Query& query, void* context, SyncQuery& sync)
{
}
void ConsoleEngineImpl::touchSync(SyncQuery& sync)
{
}
void ConsoleEngineImpl::endSync(SyncQuery& sync)
{
}
*/
void ConsoleEngineImpl::learnPackage(const string& packageName)
{
Mutex::ScopedLock _lock(lock);
if (packages.find(packageName) == packages.end())
packages.insert(pair<string, pair<ObjectClassList, EventClassList> >
(packageName, pair<ObjectClassList, EventClassList>(ObjectClassList(), EventClassList())));
}
void ConsoleEngineImpl::learnClass(SchemaObjectClass* cls)
{
Mutex::ScopedLock _lock(lock);
const SchemaClassKey* key = cls->getClassKey();
PackageList::iterator pIter = packages.find(key->getPackageName());
if (pIter == packages.end())
return;
ObjectClassList& list = pIter->second.first;
if (list.find(key) == list.end())
list[key] = cls;
}
void ConsoleEngineImpl::learnClass(SchemaEventClass* cls)
{
Mutex::ScopedLock _lock(lock);
const SchemaClassKey* key = cls->getClassKey();
PackageList::iterator pIter = packages.find(key->getPackageName());
if (pIter == packages.end())
return;
EventClassList& list = pIter->second.second;
if (list.find(key) == list.end())
list[key] = cls;
}
bool ConsoleEngineImpl::haveClass(const SchemaClassKey* key) const
{
Mutex::ScopedLock _lock(lock);
PackageList::const_iterator pIter = packages.find(key->getPackageName());
if (pIter == packages.end())
return false;
const ObjectClassList& oList = pIter->second.first;
const EventClassList& eList = pIter->second.second;
return oList.find(key) != oList.end() || eList.find(key) != eList.end();
}
SchemaObjectClass* ConsoleEngineImpl::getSchema(const SchemaClassKey* key) const
{
Mutex::ScopedLock _lock(lock);
PackageList::const_iterator pIter = packages.find(key->getPackageName());
if (pIter == packages.end())
return 0;
const ObjectClassList& oList = pIter->second.first;
ObjectClassList::const_iterator iter = oList.find(key);
if (iter == oList.end())
return 0;
return iter->second;
}
//==================================================================
// Wrappers
//==================================================================
ConsoleEngine::ConsoleEngine(const ConsoleSettings& settings) : impl(new ConsoleEngineImpl(settings)) {}
ConsoleEngine::~ConsoleEngine() { delete impl; }
bool ConsoleEngine::getEvent(ConsoleEvent& event) const { return impl->getEvent(event); }
void ConsoleEngine::popEvent() { impl->popEvent(); }
void ConsoleEngine::addConnection(BrokerProxy& broker, void* context) { impl->addConnection(broker, context); }
void ConsoleEngine::delConnection(BrokerProxy& broker) { impl->delConnection(broker); }
uint32_t ConsoleEngine::packageCount() const { return impl->packageCount(); }
const char* ConsoleEngine::getPackageName(uint32_t idx) const { return impl->getPackageName(idx).c_str(); }
uint32_t ConsoleEngine::classCount(const char* packageName) const { return impl->classCount(packageName); }
const SchemaClassKey* ConsoleEngine::getClass(const char* packageName, uint32_t idx) const { return impl->getClass(packageName, idx); }
ClassKind ConsoleEngine::getClassKind(const SchemaClassKey* key) const { return impl->getClassKind(key); }
const SchemaObjectClass* ConsoleEngine::getObjectClass(const SchemaClassKey* key) const { return impl->getObjectClass(key); }
const SchemaEventClass* ConsoleEngine::getEventClass(const SchemaClassKey* key) const { return impl->getEventClass(key); }
void ConsoleEngine::bindPackage(const char* packageName) { impl->bindPackage(packageName); }
void ConsoleEngine::bindClass(const SchemaClassKey* key) { impl->bindClass(key); }
void ConsoleEngine::bindClass(const char* packageName, const char* className) { impl->bindClass(packageName, className); }
//void ConsoleEngine::startSync(const Query& query, void* context, SyncQuery& sync) { impl->startSync(query, context, sync); }
//void ConsoleEngine::touchSync(SyncQuery& sync) { impl->touchSync(sync); }
//void ConsoleEngine::endSync(SyncQuery& sync) { impl->endSync(sync); }
|