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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
|
/*
*
* 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 "ManagementBroker.h"
#include "qpid/broker/DeliverableMessage.h"
#include "qpid/log/Statement.h"
#include <qpid/broker/Message.h>
#include <qpid/broker/MessageDelivery.h>
#include "qpid/framing/MessageTransferBody.h"
#include "qpid/sys/Time.h"
#include <list>
#include <iostream>
#include <fstream>
using boost::intrusive_ptr;
using qpid::framing::Uuid;
using namespace qpid::framing;
using namespace qpid::management;
using namespace qpid::broker;
using namespace qpid::sys;
using namespace std;
ManagementAgent::shared_ptr ManagementBroker::agent;
bool ManagementBroker::enabled = 0;
ManagementBroker::RemoteAgent::~RemoteAgent ()
{
if (mgmtObject.get () != 0)
mgmtObject->resourceDestroy ();
}
ManagementBroker::ManagementBroker (string _dataDir, uint16_t _interval, Manageable* _broker) :
dataDir (_dataDir), interval (_interval), broker (_broker)
{
timer.add (intrusive_ptr<TimerTask> (new Periodic(*this, interval)));
localBank = 5;
nextObjectId = 1;
bootSequence = 1;
nextRemoteBank = 10;
clientWasAdded = false;
// Get from file or generate and save to file.
if (dataDir.empty ())
{
uuid.generate ();
QPID_LOG (info, "ManagementBroker has no data directory, generated new broker ID: "
<< uuid);
}
else
{
string filename (dataDir + "/.mbrokerdata");
ifstream inFile (filename.c_str ());
if (inFile.good ())
{
inFile >> uuid;
inFile >> bootSequence;
inFile >> nextRemoteBank;
inFile.close ();
QPID_LOG (debug, "ManagementBroker restored broker ID: " << uuid);
bootSequence++;
writeData ();
}
else
{
uuid.generate ();
QPID_LOG (info, "ManagementBroker generated broker ID: " << uuid);
writeData ();
}
QPID_LOG (debug, "ManagementBroker boot sequence: " << bootSequence);
}
}
ManagementBroker::~ManagementBroker () {}
void ManagementBroker::writeData ()
{
string filename (dataDir + "/.mbrokerdata");
ofstream outFile (filename.c_str ());
if (outFile.good ())
{
outFile << uuid << " " << bootSequence << " " << nextRemoteBank << endl;
outFile.close ();
}
}
void ManagementBroker::enableManagement (string dataDir, uint16_t interval, Manageable* broker)
{
enabled = 1;
if (agent.get () == 0)
agent = shared_ptr (new ManagementBroker (dataDir, interval, broker));
}
ManagementAgent::shared_ptr ManagementAgent::getAgent (void)
{
return ManagementBroker::agent;
}
void ManagementBroker::shutdown (void)
{
if (agent.get () != 0)
{
ManagementBroker* broker = (ManagementBroker*) agent.get();
broker->mExchange.reset ();
broker->dExchange.reset ();
agent.reset ();
}
}
void ManagementBroker::setExchange (broker::Exchange::shared_ptr _mexchange,
broker::Exchange::shared_ptr _dexchange)
{
mExchange = _mexchange;
dExchange = _dexchange;
}
void ManagementBroker::RegisterClass (string packageName,
string className,
uint8_t* md5Sum,
ManagementObject::writeSchemaCall_t schemaCall)
{
Mutex::ScopedLock lock (userLock);
PackageMap::iterator pIter = FindOrAddPackage (packageName);
AddClassLocal (pIter, className, md5Sum, schemaCall);
}
void ManagementBroker::addObject (ManagementObject::shared_ptr object,
uint32_t persistId,
uint32_t persistBank)
{
Mutex::ScopedLock lock (addLock);
uint64_t objectId;
if (persistId == 0)
{
objectId = ((uint64_t) bootSequence) << 48 |
((uint64_t) localBank) << 24 | nextObjectId++;
if ((nextObjectId & 0xFF000000) != 0)
{
nextObjectId = 1;
localBank++;
}
}
else
objectId = ((uint64_t) persistBank) << 24 | persistId;
object->setObjectId (objectId);
newManagementObjects[objectId] = object;
}
ManagementBroker::Periodic::Periodic (ManagementBroker& _broker, uint32_t _seconds)
: TimerTask (qpid::sys::Duration ((_seconds ? _seconds : 1) * qpid::sys::TIME_SEC)), broker(_broker) {}
ManagementBroker::Periodic::~Periodic () {}
void ManagementBroker::Periodic::fire ()
{
broker.timer.add (intrusive_ptr<TimerTask> (new Periodic (broker, broker.interval)));
broker.PeriodicProcessing ();
}
void ManagementBroker::clientAdded (void)
{
Mutex::ScopedLock lock (userLock);
clientWasAdded = true;
}
void ManagementBroker::EncodeHeader (Buffer& buf, uint8_t opcode, uint32_t seq)
{
buf.putOctet ('A');
buf.putOctet ('M');
buf.putOctet ('1');
buf.putOctet (opcode);
buf.putLong (seq);
}
bool ManagementBroker::CheckHeader (Buffer& buf, uint8_t *opcode, uint32_t *seq)
{
uint8_t h1 = buf.getOctet ();
uint8_t h2 = buf.getOctet ();
uint8_t h3 = buf.getOctet ();
*opcode = buf.getOctet ();
*seq = buf.getLong ();
return h1 == 'A' && h2 == 'M' && h3 == '1';
}
void ManagementBroker::SendBuffer (Buffer& buf,
uint32_t length,
broker::Exchange::shared_ptr exchange,
string routingKey)
{
if (exchange.get() == 0)
return;
intrusive_ptr<Message> msg (new Message ());
AMQFrame method (in_place<MessageTransferBody>(
ProtocolVersion(), exchange->getName (), 0, 0));
AMQFrame header (in_place<AMQHeaderBody>());
AMQFrame content(in_place<AMQContentBody>());
content.castBody<AMQContentBody>()->decode(buf, length);
method.setEof (false);
header.setBof (false);
header.setEof (false);
content.setBof (false);
msg->getFrames().append(method);
msg->getFrames().append(header);
MessageProperties* props =
msg->getFrames().getHeaders()->get<MessageProperties>(true);
props->setContentLength(length);
msg->getFrames().append(content);
DeliverableMessage deliverable (msg);
exchange->route (deliverable, routingKey, 0);
}
void ManagementBroker::moveNewObjectsLH()
{
Mutex::ScopedLock lock (addLock);
for (ManagementObjectMap::iterator iter = newManagementObjects.begin ();
iter != newManagementObjects.end ();
iter++)
managementObjects[iter->first] = iter->second;
newManagementObjects.clear();
}
void ManagementBroker::PeriodicProcessing (void)
{
#define BUFSIZE 65536
Mutex::ScopedLock lock (userLock);
char msgChars[BUFSIZE];
uint32_t contentSize;
string routingKey;
std::list<uint64_t> deleteList;
{
Buffer msgBuffer(msgChars, BUFSIZE);
EncodeHeader(msgBuffer, 'h');
msgBuffer.putLongLong(uint64_t(Duration(now())));
contentSize = BUFSIZE - msgBuffer.available ();
msgBuffer.reset ();
routingKey = "mgmt." + uuid.str() + ".heartbeat";
SendBuffer (msgBuffer, contentSize, mExchange, routingKey);
}
moveNewObjectsLH();
if (clientWasAdded)
{
clientWasAdded = false;
for (ManagementObjectMap::iterator iter = managementObjects.begin ();
iter != managementObjects.end ();
iter++)
{
ManagementObject::shared_ptr object = iter->second;
object->setAllChanged ();
}
}
if (managementObjects.empty ())
return;
for (ManagementObjectMap::iterator iter = managementObjects.begin ();
iter != managementObjects.end ();
iter++)
{
ManagementObject::shared_ptr object = iter->second;
if (object->getConfigChanged () || object->isDeleted ())
{
Buffer msgBuffer (msgChars, BUFSIZE);
EncodeHeader (msgBuffer, 'c');
object->writeConfig (msgBuffer);
contentSize = BUFSIZE - msgBuffer.available ();
msgBuffer.reset ();
routingKey = "mgmt." + uuid.str() + ".config." + object->getClassName ();
SendBuffer (msgBuffer, contentSize, mExchange, routingKey);
}
if (object->getInstChanged ())
{
Buffer msgBuffer (msgChars, BUFSIZE);
EncodeHeader (msgBuffer, 'i');
object->writeInstrumentation (msgBuffer);
contentSize = BUFSIZE - msgBuffer.available ();
msgBuffer.reset ();
routingKey = "mgmt." + uuid.str () + ".inst." + object->getClassName ();
SendBuffer (msgBuffer, contentSize, mExchange, routingKey);
}
if (object->isDeleted ())
deleteList.push_back (iter->first);
}
// Delete flagged objects
for (std::list<uint64_t>::reverse_iterator iter = deleteList.rbegin ();
iter != deleteList.rend ();
iter++)
managementObjects.erase (*iter);
deleteList.clear ();
}
void ManagementBroker::sendCommandComplete (string replyToKey, uint32_t sequence,
uint32_t code, string text)
{
Buffer outBuffer (outputBuffer, MA_BUFFER_SIZE);
uint32_t outLen;
EncodeHeader (outBuffer, 'z', sequence);
outBuffer.putLong (code);
outBuffer.putShortString (text);
outLen = MA_BUFFER_SIZE - outBuffer.available ();
outBuffer.reset ();
SendBuffer (outBuffer, outLen, dExchange, replyToKey);
}
void ManagementBroker::dispatchCommand (Deliverable& deliverable,
const string& routingKey,
const FieldTable* /*args*/)
{
Mutex::ScopedLock lock (userLock);
Message& msg = ((DeliverableMessage&) deliverable).getMessage ();
if (routingKey.compare (0, 13, "agent.method.") == 0)
dispatchMethodLH (msg, routingKey, 13);
else if (routingKey.length () == 5 &&
routingKey.compare (0, 5, "agent") == 0)
dispatchAgentCommandLH (msg);
else
{
QPID_LOG (debug, "Illegal routing key for dispatch: " << routingKey);
return;
}
}
void ManagementBroker::dispatchMethodLH (Message& msg,
const string& routingKey,
size_t first)
{
size_t pos, start = first;
uint32_t contentSize;
if (routingKey.length () == start)
{
QPID_LOG (debug, "Missing package-name in routing key: " << routingKey);
return;
}
pos = routingKey.find ('.', start);
if (pos == string::npos || routingKey.length () == pos + 1)
{
QPID_LOG (debug, "Missing class-name in routing key: " << routingKey);
return;
}
string packageName = routingKey.substr (start, pos - start);
start = pos + 1;
pos = routingKey.find ('.', start);
if (pos == string::npos || routingKey.length () == pos + 1)
{
QPID_LOG (debug, "Missing method-name in routing key: " << routingKey);
return;
}
string className = routingKey.substr (start, pos - start);
start = pos + 1;
string methodName = routingKey.substr (start, routingKey.length () - start);
contentSize = msg.encodedContentSize ();
if (contentSize < 8 || contentSize > MA_BUFFER_SIZE)
return;
Buffer inBuffer (inputBuffer, MA_BUFFER_SIZE);
Buffer outBuffer (outputBuffer, MA_BUFFER_SIZE);
uint32_t outLen, sequence;
uint8_t opcode;
if (msg.encodedSize() > MA_BUFFER_SIZE) {
QPID_LOG(debug, "ManagementBroker::dispatchMethodLH: Message too large: " <<
msg.encodedSize());
return;
}
msg.encodeContent (inBuffer);
inBuffer.reset ();
if (!CheckHeader (inBuffer, &opcode, &sequence))
{
QPID_LOG (debug, " Invalid content header");
return;
}
if (opcode != 'M')
{
QPID_LOG (debug, " Unexpected opcode " << opcode);
return;
}
uint64_t objId = inBuffer.getLongLong ();
string replyToKey;
const framing::MessageProperties* p =
msg.getFrames().getHeaders()->get<framing::MessageProperties>();
if (p && p->hasReplyTo())
{
const framing::ReplyTo& rt = p->getReplyTo ();
replyToKey = rt.getRoutingKey ();
}
else
{
QPID_LOG (debug, " Reply-to missing");
return;
}
EncodeHeader (outBuffer, 'm', sequence);
ManagementObjectMap::iterator iter = managementObjects.find (objId);
if (iter == managementObjects.end () || iter->second->isDeleted ())
{
outBuffer.putLong (Manageable::STATUS_UNKNOWN_OBJECT);
outBuffer.putShortString (Manageable::StatusText (Manageable::STATUS_UNKNOWN_OBJECT));
}
else
{
iter->second->doMethod (methodName, inBuffer, outBuffer);
}
outLen = MA_BUFFER_SIZE - outBuffer.available ();
outBuffer.reset ();
SendBuffer (outBuffer, outLen, dExchange, replyToKey);
}
void ManagementBroker::handleBrokerRequestLH (Buffer&, string replyToKey, uint32_t sequence)
{
Buffer outBuffer (outputBuffer, MA_BUFFER_SIZE);
uint32_t outLen;
EncodeHeader (outBuffer, 'b', sequence);
uuid.encode (outBuffer);
outLen = MA_BUFFER_SIZE - outBuffer.available ();
outBuffer.reset ();
SendBuffer (outBuffer, outLen, dExchange, replyToKey);
}
void ManagementBroker::handlePackageQueryLH (Buffer&, string replyToKey, uint32_t sequence)
{
for (PackageMap::iterator pIter = packages.begin ();
pIter != packages.end ();
pIter++)
{
Buffer outBuffer (outputBuffer, MA_BUFFER_SIZE);
uint32_t outLen;
EncodeHeader (outBuffer, 'p', sequence);
EncodePackageIndication (outBuffer, pIter);
outLen = MA_BUFFER_SIZE - outBuffer.available ();
outBuffer.reset ();
SendBuffer (outBuffer, outLen, dExchange, replyToKey);
}
sendCommandComplete (replyToKey, sequence);
}
void ManagementBroker::handlePackageIndLH (Buffer& /*inBuffer*/, string /*replyToKey*/, uint32_t /*sequence*/)
{
}
void ManagementBroker::handleClassQueryLH (Buffer& inBuffer, string replyToKey, uint32_t sequence)
{
std::string packageName;
inBuffer.getShortString (packageName);
PackageMap::iterator pIter = packages.find (packageName);
if (pIter != packages.end ())
{
ClassMap cMap = pIter->second;
for (ClassMap::iterator cIter = cMap.begin ();
cIter != cMap.end ();
cIter++)
{
if (cIter->second.hasSchema ())
{
Buffer outBuffer (outputBuffer, MA_BUFFER_SIZE);
uint32_t outLen;
EncodeHeader (outBuffer, 'q', sequence);
EncodeClassIndication (outBuffer, pIter, cIter);
outLen = MA_BUFFER_SIZE - outBuffer.available ();
outBuffer.reset ();
SendBuffer (outBuffer, outLen, dExchange, replyToKey);
}
}
}
sendCommandComplete (replyToKey, sequence);
}
void ManagementBroker::SchemaClass::appendSchema (Buffer& buf)
{
// If the management package is attached locally (embedded in the broker or
// linked in via plug-in), call the schema handler directly. If the package
// is from a remote management agent, send the stored schema information.
if (writeSchemaCall != 0)
writeSchemaCall (buf);
else
buf.putRawData (buffer, bufferLen);
}
void ManagementBroker::handleSchemaRequestLH (Buffer& inBuffer, string replyToKey, uint32_t sequence)
{
string packageName;
SchemaClassKey key;
inBuffer.getShortString (packageName);
inBuffer.getShortString (key.name);
inBuffer.getBin128 (key.hash);
PackageMap::iterator pIter = packages.find (packageName);
if (pIter != packages.end ())
{
ClassMap cMap = pIter->second;
ClassMap::iterator cIter = cMap.find (key);
if (cIter != cMap.end ())
{
Buffer outBuffer (outputBuffer, MA_BUFFER_SIZE);
uint32_t outLen;
SchemaClass classInfo = cIter->second;
if (classInfo.hasSchema())
{
EncodeHeader (outBuffer, 's', sequence);
classInfo.appendSchema (outBuffer);
outLen = MA_BUFFER_SIZE - outBuffer.available ();
outBuffer.reset ();
SendBuffer (outBuffer, outLen, dExchange, replyToKey);
}
else
sendCommandComplete (replyToKey, sequence, 1, "Schema not available");
}
else
sendCommandComplete (replyToKey, sequence, 1, "Class key not found");
}
else
sendCommandComplete (replyToKey, sequence, 1, "Package not found");
}
bool ManagementBroker::bankInUse (uint32_t bank)
{
for (RemoteAgentMap::iterator aIter = remoteAgents.begin();
aIter != remoteAgents.end();
aIter++)
if (aIter->second->objIdBank == bank)
return true;
return false;
}
uint32_t ManagementBroker::allocateNewBank ()
{
while (bankInUse (nextRemoteBank))
nextRemoteBank++;
uint32_t allocated = nextRemoteBank++;
writeData ();
return allocated;
}
uint32_t ManagementBroker::assignBankLH (uint32_t requestedBank)
{
if (requestedBank == 0 || bankInUse (requestedBank))
return allocateNewBank ();
return requestedBank;
}
void ManagementBroker::handleAttachRequestLH (Buffer& inBuffer, string replyToKey, uint32_t sequence)
{
string label;
uint32_t requestedBank;
uint32_t assignedBank;
Uuid sessionId;
Uuid systemId;
inBuffer.getShortString (label);
sessionId.decode (inBuffer);
systemId.decode (inBuffer);
requestedBank = inBuffer.getLong ();
assignedBank = assignBankLH (requestedBank);
RemoteAgentMap::iterator aIter = remoteAgents.find (sessionId);
if (aIter != remoteAgents.end())
{
// There already exists an agent on this session. Reject the request.
sendCommandComplete (replyToKey, sequence, 1, "Session already has remote agent");
return;
}
RemoteAgent* agent = new RemoteAgent;
agent->objIdBank = assignedBank;
agent->mgmtObject = management::Agent::shared_ptr
(new management::Agent (agent));
agent->mgmtObject->set_sessionId (sessionId);
agent->mgmtObject->set_label (label);
agent->mgmtObject->set_registeredTo (broker->GetManagementObject()->getObjectId());
agent->mgmtObject->set_systemId (systemId);
addObject (agent->mgmtObject);
remoteAgents[sessionId] = agent;
Buffer outBuffer (outputBuffer, MA_BUFFER_SIZE);
uint32_t outLen;
EncodeHeader (outBuffer, 'a', sequence);
outBuffer.putLong (assignedBank);
outLen = MA_BUFFER_SIZE - outBuffer.available ();
outBuffer.reset ();
SendBuffer (outBuffer, outLen, dExchange, replyToKey);
}
void ManagementBroker::handleGetQueryLH (Buffer& inBuffer, string replyToKey, uint32_t sequence)
{
FieldTable ft;
FieldTable::ValuePtr value;
moveNewObjectsLH();
ft.decode(inBuffer);
value = ft.get("_class");
if (value.get() == 0 || !value->convertsTo<string>())
{
// TODO: Send completion with an error code
return;
}
string className (value->get<string>());
for (ManagementObjectMap::iterator iter = managementObjects.begin ();
iter != managementObjects.end ();
iter++)
{
ManagementObject::shared_ptr object = iter->second;
if (object->getClassName () == className)
{
Buffer outBuffer (outputBuffer, MA_BUFFER_SIZE);
uint32_t outLen;
EncodeHeader (outBuffer, 'g', sequence);
object->writeConfig (outBuffer);
object->writeInstrumentation (outBuffer, true);
outLen = MA_BUFFER_SIZE - outBuffer.available ();
outBuffer.reset ();
SendBuffer (outBuffer, outLen, dExchange, replyToKey);
}
}
sendCommandComplete (replyToKey, sequence);
}
void ManagementBroker::dispatchAgentCommandLH (Message& msg)
{
Buffer inBuffer (inputBuffer, MA_BUFFER_SIZE);
uint8_t opcode;
uint32_t sequence;
string replyToKey;
const framing::MessageProperties* p =
msg.getFrames().getHeaders()->get<framing::MessageProperties>();
if (p && p->hasReplyTo())
{
const framing::ReplyTo& rt = p->getReplyTo ();
replyToKey = rt.getRoutingKey ();
}
else
return;
if (msg.encodedSize() > MA_BUFFER_SIZE) {
QPID_LOG(debug, "ManagementBroker::dispatchAgentCommandLH: Message too large: " <<
msg.encodedSize());
return;
}
msg.encodeContent (inBuffer);
inBuffer.reset ();
if (!CheckHeader (inBuffer, &opcode, &sequence))
return;
if (opcode == 'B') handleBrokerRequestLH (inBuffer, replyToKey, sequence);
else if (opcode == 'P') handlePackageQueryLH (inBuffer, replyToKey, sequence);
else if (opcode == 'p') handlePackageIndLH (inBuffer, replyToKey, sequence);
else if (opcode == 'Q') handleClassQueryLH (inBuffer, replyToKey, sequence);
else if (opcode == 'S') handleSchemaRequestLH (inBuffer, replyToKey, sequence);
//else if (opcode == 'A') handleAttachRequestLH (inBuffer, replyToKey, sequence);
else if (opcode == 'G') handleGetQueryLH (inBuffer, replyToKey, sequence);
}
ManagementBroker::PackageMap::iterator ManagementBroker::FindOrAddPackage (std::string name)
{
PackageMap::iterator pIter = packages.find (name);
if (pIter != packages.end ())
return pIter;
// No such package found, create a new map entry.
pair<PackageMap::iterator, bool> result =
packages.insert (pair<string, ClassMap> (name, ClassMap ()));
QPID_LOG (debug, "ManagementBroker added package " << name);
// Publish a package-indication message
Buffer outBuffer (outputBuffer, MA_BUFFER_SIZE);
uint32_t outLen;
EncodeHeader (outBuffer, 'p');
EncodePackageIndication (outBuffer, result.first);
outLen = MA_BUFFER_SIZE - outBuffer.available ();
outBuffer.reset ();
SendBuffer (outBuffer, outLen, mExchange, "mgmt." + uuid.str() + ".schema.package");
return result.first;
}
void ManagementBroker::AddClassLocal (PackageMap::iterator pIter,
string className,
uint8_t* md5Sum,
ManagementObject::writeSchemaCall_t schemaCall)
{
SchemaClassKey key;
ClassMap& cMap = pIter->second;
key.name = className;
memcpy (&key.hash, md5Sum, 16);
ClassMap::iterator cIter = cMap.find (key);
if (cIter != cMap.end ())
return;
// No such class found, create a new class with local information.
QPID_LOG (debug, "ManagementBroker added class " << pIter->first << "." <<
key.name);
SchemaClass classInfo;
classInfo.writeSchemaCall = schemaCall;
cMap[key] = classInfo;
// TODO: Publish a class-indication message
}
void ManagementBroker::EncodePackageIndication (Buffer& buf,
PackageMap::iterator pIter)
{
buf.putShortString ((*pIter).first);
}
void ManagementBroker::EncodeClassIndication (Buffer& buf,
PackageMap::iterator pIter,
ClassMap::iterator cIter)
{
SchemaClassKey key = (*cIter).first;
buf.putShortString ((*pIter).first);
buf.putShortString (key.name);
buf.putBin128 (key.hash);
}
|