summaryrefslogtreecommitdiff
path: root/cpp/src/qpid/console/Object.cpp
blob: 6570e293ab26591870261fa1193d7e84e4094f71 (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
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
/*
 *
 * 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/SessionManager.h"
#include "qpid/console/Broker.h"
#include "qpid/console/Object.h"
#include "qpid/console/Schema.h"
#include "qpid/console/ClassKey.h"
#include "qpid/console/Value.h"
#include "qpid/framing/Buffer.h"
#include "qpid/sys/Mutex.h"

using namespace qpid::console;
using namespace qpid::sys;
using namespace qpid;
using namespace std;
using qpid::framing::Uuid;
using qpid::framing::FieldTable;

void Object::AttributeMap::addRef(const string& key, const ObjectId& val)
{
    (*this)[key] = Value::Ptr(new RefValue(val));
}

void Object::AttributeMap::addUint(const string& key, uint32_t val)
{
    (*this)[key] = Value::Ptr(new UintValue(val));
}

void Object::AttributeMap::addInt(const string& key, int32_t val)
{
    (*this)[key] = Value::Ptr(new IntValue(val));
}

void Object::AttributeMap::addUint64(const string& key, uint64_t val)
{
    (*this)[key] = Value::Ptr(new Uint64Value(val));
}

void Object::AttributeMap::addInt64(const string& key, int64_t val)
{
    (*this)[key] = Value::Ptr(new Int64Value(val));
}

void Object::AttributeMap::addString(const string& key, const string& val)
{
    (*this)[key] = Value::Ptr(new StringValue(val));
}

void Object::AttributeMap::addBool(const string& key, bool val)
{
    (*this)[key] = Value::Ptr(new BoolValue(val));
}

void Object::AttributeMap::addFloat(const string& key, float val)
{
    (*this)[key] = Value::Ptr(new FloatValue(val));
}

void Object::AttributeMap::addDouble(const string& key, double val)
{
    (*this)[key] = Value::Ptr(new DoubleValue(val));
}

void Object::AttributeMap::addUuid(const string& key, const Uuid& val)
{
    (*this)[key] = Value::Ptr(new UuidValue(val));
}

void Object::AttributeMap::addMap(const string& key, const FieldTable& val)
{
    (*this)[key] = Value::Ptr(new MapValue(val));
}

Object::Object(Broker* b, SchemaClass* s, framing::Buffer& buffer, bool prop, bool stat) :
    broker(b), schema(s), pendingMethod(0)
{
    currentTime = buffer.getLongLong();
    createTime = buffer.getLongLong();
    deleteTime = buffer.getLongLong();
    objectId.decode(buffer);

    if (prop) {
        set<string> excludes;
        parsePresenceMasks(buffer, excludes);
        for (vector<SchemaProperty*>::const_iterator pIter = schema->properties.begin();
             pIter != schema->properties.end(); pIter++) {
            SchemaProperty* property = *pIter;
            if (excludes.count(property->name) != 0) {
                attributes[property->name] = Value::Ptr(new NullValue());
            } else {
                attributes[property->name] = property->decodeValue(buffer);
            }
        }
    }

    if (stat) {
        for (vector<SchemaStatistic*>::const_iterator sIter = schema->statistics.begin();
             sIter != schema->statistics.end(); sIter++) {
            SchemaStatistic* statistic = *sIter;
            attributes[statistic->name] = statistic->decodeValue(buffer);
        }
    }
}

Object::~Object() {}

const ClassKey& Object::getClassKey() const
{
    return schema->getClassKey();
}

string Object::getIndex() const
{
    string result;

    for (vector<SchemaProperty*>::const_iterator pIter = schema->properties.begin();
         pIter != schema->properties.end(); pIter++) {
        SchemaProperty* property = *pIter;
        if (property->isIndex) {
            AttributeMap::const_iterator vIter = attributes.find(property->name);
            if (vIter != attributes.end()) {
                if (!result.empty())
                    result += ":";
                result += vIter->second->str();
            }
        }
    }
    return result;
}

void Object::mergeUpdate(const Object& /*updated*/)
{
    // TODO
}

void Object::invokeMethod(const string name, const AttributeMap& args, MethodResponse& result)
{
    for (vector<SchemaMethod*>::const_iterator iter = schema->methods.begin();
         iter != schema->methods.end(); iter++) {
        if ((*iter)->name == name) {
            SchemaMethod* method = *iter;
            char    rawbuffer[65536];
            framing::Buffer  buffer(rawbuffer, 65536);
            uint32_t sequence = broker->sessionManager.sequenceManager.reserve("method");
            pendingMethod = method;
            broker->methodObject = this;
            broker->encodeHeader(buffer, 'M', sequence);
            objectId.encode(buffer);
            schema->key.encode(buffer);
            buffer.putShortString(name);

            for (vector<SchemaArgument*>::const_iterator aIter = method->arguments.begin();
                 aIter != method->arguments.end(); aIter++) {
                SchemaArgument* arg = *aIter;
                if (arg->dirInput) {
                    AttributeMap::const_iterator attr = args.find(arg->name);
                    if (attr != args.end()) {
                        ValueFactory::encodeValue(arg->typeCode, attr->second, buffer);
                    } else {
                        // TODO Use the default value instead of throwing
                        throw Exception("Missing arguments in method call");
                    }
                }
            }

            uint32_t length = buffer.getPosition();
            buffer.reset();
            stringstream routingKey;
            routingKey << "agent." << objectId.getBrokerBank() << "." << objectId.getAgentBank();
            broker->connThreadBody.sendBuffer(buffer, length, "qpid.management", routingKey.str());

            {
                Mutex::ScopedLock l(broker->lock);
                bool ok = true;
                while (pendingMethod != 0 && ok) {
                    ok = broker->cond.wait(broker->lock, AbsTime(now(), broker->sessionManager.settings.methodTimeout * TIME_SEC));
                }

                if (!ok) {
                    result.code = 0x1001;
                    result.text.assign("Method call timed out");
                    result.arguments.clear();
                } else {
                    result = methodResponse;
                }
            }
        }
    }
}

void Object::handleMethodResp(framing::Buffer& buffer, uint32_t sequence)
{
    broker->sessionManager.sequenceManager.release(sequence);
    methodResponse.code = buffer.getLong();
    buffer.getMediumString(methodResponse.text);
    methodResponse.arguments.clear();

    for (vector<SchemaArgument*>::const_iterator aIter = pendingMethod->arguments.begin();
         aIter != pendingMethod->arguments.end(); aIter++) {
        SchemaArgument* arg = *aIter;
        if (arg->dirOutput) {
            methodResponse.arguments[arg->name] = arg->decodeValue(buffer);
        }
    }
    
    {
        Mutex::ScopedLock l(broker->lock);
        pendingMethod = 0;
        broker->cond.notify();
    }
}

ObjectId Object::attrRef(const string& key) const
{
    AttributeMap::const_iterator iter = attributes.find(key);
    if (iter == attributes.end())
        return ObjectId();
    Value::Ptr val = iter->second;
    if (!val->isObjectId())
        return ObjectId();
    return val->asObjectId();
}

uint32_t Object::attrUint(const string& key) const
{
    AttributeMap::const_iterator iter = attributes.find(key);
    if (iter == attributes.end())
        return 0;
    Value::Ptr val = iter->second;
    if (!val->isUint())
        return 0;
    return val->asUint();
}

int32_t Object::attrInt(const string& key) const
{
    AttributeMap::const_iterator iter = attributes.find(key);
    if (iter == attributes.end())
        return 0;
    Value::Ptr val = iter->second;
    if (!val->isInt())
        return 0;
    return val->asInt();
}

uint64_t Object::attrUint64(const string& key) const
{
    AttributeMap::const_iterator iter = attributes.find(key);
    if (iter == attributes.end())
        return 0;
    Value::Ptr val = iter->second;
    if (!val->isUint64())
        return 0;
    return val->asUint64();
}

int64_t Object::attrInt64(const string& key) const
{
    AttributeMap::const_iterator iter = attributes.find(key);
    if (iter == attributes.end())
        return 0;
    Value::Ptr val = iter->second;
    if (!val->isInt64())
        return 0;
    return val->asInt64();
}

string Object::attrString(const string& key) const
{
    AttributeMap::const_iterator iter = attributes.find(key);
    if (iter == attributes.end())
        return string();
    Value::Ptr val = iter->second;
    if (!val->isString())
        return string();
    return val->asString();
}

bool Object::attrBool(const string& key) const
{
    AttributeMap::const_iterator iter = attributes.find(key);
    if (iter == attributes.end())
        return false;
    Value::Ptr val = iter->second;
    if (!val->isBool())
        return false;
    return val->asBool();
}

float Object::attrFloat(const string& key) const
{
    AttributeMap::const_iterator iter = attributes.find(key);
    if (iter == attributes.end())
        return 0.0;
    Value::Ptr val = iter->second;
    if (!val->isFloat())
        return 0.0;
    return val->asFloat();
}

double Object::attrDouble(const string& key) const
{
    AttributeMap::const_iterator iter = attributes.find(key);
    if (iter == attributes.end())
        return 0.0;
    Value::Ptr val = iter->second;
    if (!val->isDouble())
        return 0.0;
    return val->asDouble();
}

Uuid Object::attrUuid(const string& key) const
{
    AttributeMap::const_iterator iter = attributes.find(key);
    if (iter == attributes.end())
        return Uuid();
    Value::Ptr val = iter->second;
    if (!val->isUuid())
        return Uuid();
    return val->asUuid();
}

FieldTable Object::attrMap(const string& key) const
{
    AttributeMap::const_iterator iter = attributes.find(key);
    if (iter == attributes.end())
        return FieldTable();
    Value::Ptr val = iter->second;
    if (!val->isMap())
        return FieldTable();
    return val->asMap();
}

void Object::parsePresenceMasks(framing::Buffer& buffer, set<string>& excludeList)
{
    excludeList.clear();
    uint8_t bit = 0;
    uint8_t mask = 0;

    for (vector<SchemaProperty*>::const_iterator pIter = schema->properties.begin();
         pIter != schema->properties.end(); pIter++) {
        SchemaProperty* property = *pIter;
        if (property->isOptional) {
            if (bit == 0) {
                mask = buffer.getOctet();
                bit = 1;
            }
            if ((mask & bit) == 0)
                excludeList.insert(property->name);
            if (bit == 0x80)
                bit = 0;
            else
                bit = bit << 1;
        }
    }
}

ostream& qpid::console::operator<<(ostream& o, const Object& object)
{
    const ClassKey& key = object.getClassKey();
    o << key.getPackageName() << ":" << key.getClassName() << "[" << object.getObjectId() << "] " <<
        object.getIndex();
    return o;
}