summaryrefslogtreecommitdiff
path: root/qpid/cpp/src/qpid/broker/ManagementAgent.cpp
blob: 71b027c7df1d46c1d9b2e4122a8df041084cdd15 (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
/*
 *
 * 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 "ManagementAgent.h"
#include "DeliverableMessage.h"
#include "qpid/log/Statement.h"
#include <qpid/broker/Message.h>
#include <qpid/broker/MessageDelivery.h>
#include <qpid/framing/AMQFrame.h>

using namespace qpid::framing;
using namespace qpid::broker;
using namespace qpid::sys;

ManagementAgent::ManagementAgent (uint16_t _interval) : interval (_interval)
{
    timer.add (TimerTask::shared_ptr (new Periodic(*this, interval)));
}

void ManagementAgent::setExchange (Exchange::shared_ptr _exchange)
{
    exchange = _exchange;
}

void ManagementAgent::addObject (ManagementObject::shared_ptr object)
{
    managementObjects.push_back (object);
    QPID_LOG(info, "Management Object Added");
}

ManagementAgent::Periodic::Periodic (ManagementAgent& _agent, uint32_t _seconds)
    : TimerTask (qpid::sys::Duration (_seconds * qpid::sys::TIME_SEC)), agent(_agent) {}

void ManagementAgent::Periodic::fire ()
{
    agent.timer.add (TimerTask::shared_ptr (new Periodic (agent, agent.interval)));
    agent.PeriodicProcessing ();
}

void ManagementAgent::clientAdded (void)
{
    for (ManagementObjectList::iterator iter = managementObjects.begin ();
         iter != managementObjects.end ();
         iter++)
    {
        ManagementObject::shared_ptr object = *iter;
        object->setAllChanged   ();
        object->setSchemaNeeded ();
    }
}

void ManagementAgent::PeriodicProcessing (void)
{
#define BUFSIZE   65536
#define THRESHOLD 16384
    char      msgChars[BUFSIZE];
    Buffer    msgBuffer (msgChars, BUFSIZE);
    uint32_t  contentSize;

    if (managementObjects.empty ())
        return;
        
    Message::shared_ptr msg (new Message ());

    // Build the magic number for the management message.
    msgBuffer.putOctet ('A');
    msgBuffer.putOctet ('M');
    msgBuffer.putOctet ('0');
    msgBuffer.putOctet ('1');

    for (ManagementObjectList::iterator iter = managementObjects.begin ();
         iter != managementObjects.end ();
         iter++)
    {
        ManagementObject::shared_ptr object = *iter;

        if (object->getSchemaNeeded ())
        {
            uint32_t startAvail = msgBuffer.available ();
            uint32_t recordLength;
            
            msgBuffer.putOctet ('S');  // opcode = Schema Record
            msgBuffer.putOctet (0);    // content-class = N/A
            msgBuffer.putShort (object->getObjectType ());
            msgBuffer.record   (); // Record the position of the length field
            msgBuffer.putLong  (0xFFFFFFFF); // Placeholder for length

            object->writeSchema (msgBuffer);
            recordLength = startAvail - msgBuffer.available ();
            msgBuffer.restore (true);         // Restore pointer to length field
            msgBuffer.putLong (recordLength);
            msgBuffer.restore ();             // Re-restore to get to the end of the buffer
        }

        if (object->getConfigChanged ())
        {
            uint32_t startAvail = msgBuffer.available ();
            uint32_t recordLength;
            
            msgBuffer.putOctet ('C');  // opcode = Content Record
            msgBuffer.putOctet ('C');  // content-class = Configuration
            msgBuffer.putShort (object->getObjectType ());
            msgBuffer.record   (); // Record the position of the length field
            msgBuffer.putLong  (0xFFFFFFFF); // Placeholder for length

            object->writeConfig (msgBuffer);
            recordLength = startAvail - msgBuffer.available ();
            msgBuffer.restore (true);         // Restore pointer to length field
            msgBuffer.putLong (recordLength);
            msgBuffer.restore ();             // Re-restore to get to the end of the buffer
        }
        
        if (object->getInstChanged ())
        {
            uint32_t startAvail = msgBuffer.available ();
            uint32_t recordLength;
            
            msgBuffer.putOctet ('C');  // opcode = Content Record
            msgBuffer.putOctet ('I');  // content-class = Instrumentation
            msgBuffer.putShort (object->getObjectType ());
            msgBuffer.record   (); // Record the position of the length field
            msgBuffer.putLong  (0xFFFFFFFF); // Placeholder for length

            object->writeInstrumentation (msgBuffer);
            recordLength = startAvail - msgBuffer.available ();
            msgBuffer.restore (true);         // Restore pointer to length field
            msgBuffer.putLong (recordLength);
            msgBuffer.restore ();             // Re-restore to get to the end of the buffer
        }

        if (object->isDeleted ())
        {
            managementObjects.remove (object);
            QPID_LOG (debug, "Management Object Removed");
        }

        // Temporary protection against buffer overrun.
        // This needs to be replaced with frame fragmentation.
        if (msgBuffer.available () < THRESHOLD)
            break;
    }
    
    msgBuffer.putOctet ('X');  // End-of-message
    msgBuffer.putOctet (0);
    msgBuffer.putShort (0);
    msgBuffer.putLong  (8);

    contentSize = BUFSIZE - msgBuffer.available ();
    msgBuffer.reset ();

    AMQFrame method  (0, MessageTransferBody(ProtocolVersion(),
                                             0, "qpid.management", 0, 0));
    AMQFrame header  (0, AMQHeaderBody());
    AMQFrame content;

    content.setBody(AMQContentBody());
    content.castBody<AMQContentBody>()->decode(msgBuffer, contentSize);

    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(contentSize);
    //msg->getFrames().getHeaders()->get<DeliveryProperties>(true)->setRoutingKey("mgmt");
    msg->getFrames().append(content);

    DeliverableMessage deliverable (msg);
    exchange->route (deliverable, "mgmt", 0);
}