summaryrefslogtreecommitdiff
path: root/cpp/src/qpid/amqp/MessageReader.cpp
blob: ab90472067b3e72bddff3e146b1792bd76e21255 (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
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
/*
 *
 * 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/amqp/MessageReader.h"
#include "qpid/amqp/Descriptor.h"
#include "qpid/amqp/descriptors.h"
#include "qpid/amqp/typecodes.h"
#include "qpid/types/Uuid.h"
#include "qpid/types/Variant.h"
#include "qpid/log/Statement.h"

using namespace qpid::amqp::message;

namespace qpid {
namespace amqp {
namespace {

//header fields:
const size_t DURABLE(0);
const size_t PRIORITY(1);
const size_t TTL(2);
const size_t FIRST_ACQUIRER(3);
const size_t DELIVERY_COUNT(4);

//properties fields:
const size_t MESSAGE_ID(0);
const size_t USER_ID(1);
const size_t TO(2);
const size_t SUBJECT(3);
const size_t REPLY_TO(4);
const size_t CORRELATION_ID(5);
const size_t CONTENT_TYPE(6);
const size_t CONTENT_ENCODING(7);
const size_t ABSOLUTE_EXPIRY_TIME(8);
const size_t CREATION_TIME(9);
const size_t GROUP_ID(10);
const size_t GROUP_SEQUENCE(11);
const size_t REPLY_TO_GROUP_ID(12);


const Descriptor* nested(const Descriptor* d)
{
    if (d && d->nested) return d->nested.get();
    else return 0;
}
}

MessageReader::HeaderReader::HeaderReader(MessageReader& p) : parent(p), index(0) {}
void MessageReader::HeaderReader::onBoolean(bool v, const Descriptor*)  // durable, first-acquirer
{
    if (index == DURABLE) {
        parent.onDurable(v);
    } else if (index == FIRST_ACQUIRER) {
        parent.onFirstAcquirer(v);
    } else {
        QPID_LOG(warning, "Unexpected message format, got boolean at index " << index << " of headers");
    }
    ++index;
}
void MessageReader::HeaderReader::onUByte(uint8_t v, const Descriptor*) // priority
{
    if (index == PRIORITY) {
        parent.onPriority(v);
    } else {
        QPID_LOG(warning, "Unexpected message format, got ubyte at index " << index << " of headers");
    }
    ++index;
}
void MessageReader::HeaderReader::onUInt(uint32_t v, const Descriptor*) // ttl, delivery-count
{
    if (index == TTL) {
        parent.onTtl(v);
    } else if (index == DELIVERY_COUNT) {
        parent.onDeliveryCount(v);
    } else {
        QPID_LOG(warning, "Unexpected message format, got uint at index " << index << " of headers");
    }
    ++index;
}
void MessageReader::HeaderReader::onNull(const Descriptor*)
{
    ++index;
}

MessageReader::PropertiesReader::PropertiesReader(MessageReader& p) : parent(p), index(0) {}
void MessageReader::PropertiesReader::onUuid(const CharSequence& v, const Descriptor*) // message-id, correlation-id
{
    if (index == MESSAGE_ID) {
        parent.onMessageId(v, qpid::types::VAR_UUID);
    } else if (index == CORRELATION_ID) {
        parent.onCorrelationId(v, qpid::types::VAR_UUID);
    } else {
        QPID_LOG(warning, "Unexpected message format, got uuid at index " << index << " of properties");
    }
    ++index;
}
void MessageReader::PropertiesReader::onULong(uint64_t v, const Descriptor*) // message-id, correlation-id
{
    if (index == MESSAGE_ID) {
        parent.onMessageId(v);
    } else if (index == CORRELATION_ID) {
        parent.onCorrelationId(v);
    } else {
        QPID_LOG(warning, "Unexpected message format, got long at index " << index << " of properties");
    }
    ++index;
}
void MessageReader::PropertiesReader::onBinary(const CharSequence& v, const Descriptor*) // message-id, correlation-id, user-id
{
    if (index == MESSAGE_ID) {
        parent.onMessageId(v, qpid::types::VAR_STRING);
    } else if (index == CORRELATION_ID) {
        parent.onCorrelationId(v, qpid::types::VAR_STRING);
    } else if (index == USER_ID) {
        parent.onUserId(v);
    } else {
        QPID_LOG(warning, "Unexpected message format, got binary at index " << index << " of properties");
    }
    ++index;
}
void MessageReader::PropertiesReader::onString(const CharSequence& v, const Descriptor*) // message-id, correlation-id, group-id, reply-to-group-id, subject, to, reply-to
{
    if (index == MESSAGE_ID) {
        parent.onMessageId(v, qpid::types::VAR_STRING);
    } else if (index == CORRELATION_ID) {
        parent.onCorrelationId(v, qpid::types::VAR_STRING);
    } else if (index == GROUP_ID) {
        parent.onGroupId(v);
    } else if (index == REPLY_TO_GROUP_ID) {
        parent.onReplyToGroupId(v);
    } else if (index == SUBJECT) {
        parent.onSubject(v);
    } else if (index == TO) {
        parent.onTo(v);
    } else if (index == REPLY_TO) {
        parent.onReplyTo(v);
    } else {
        QPID_LOG(warning, "Unexpected message format, got string at index " << index << " of properties");
    }
    ++index;
}
void MessageReader::PropertiesReader::onSymbol(const CharSequence& v, const Descriptor*) // content-type, content-encoding
{
    if (index == CONTENT_TYPE) {
        parent.onContentType(v);
    } else if (index == CONTENT_ENCODING) {
        parent.onContentEncoding(v);
    } else {
        QPID_LOG(warning, "Unexpected message format, got symbol at index " << index << " of properties");
    }
    ++index;
}
void MessageReader::PropertiesReader::onTimestamp(int64_t v, const Descriptor*) // absolute-expiry-time, creation-time
{
    if (index == ABSOLUTE_EXPIRY_TIME) {
        parent.onAbsoluteExpiryTime(v);
    } else if (index == CREATION_TIME) {
        parent.onCreationTime(v);
    } else {
        QPID_LOG(warning, "Unexpected message format, got timestamp at index " << index << " of properties");
    }
    ++index;
}
void MessageReader::PropertiesReader::onUInt(uint32_t v, const Descriptor*) // group-sequence
{
    if (index == GROUP_SEQUENCE) {
        parent.onGroupSequence(v);
    } else {
        QPID_LOG(warning, "Unexpected message format, got uint at index " << index << " of properties");
    }
    ++index;
}
void MessageReader::PropertiesReader::onNull(const Descriptor*)
{
    ++index;
}
void MessageReader::PropertiesReader::onBoolean(bool, const Descriptor*)
{
    QPID_LOG(info, "skipping message property at index " << index << " unexpected type (boolean)");
    ++index;
}
void MessageReader::PropertiesReader::onUByte(uint8_t, const Descriptor*)
{
    QPID_LOG(info, "skipping message property at index " << index << " unexpected type (ubyte)");
    ++index;
}
void MessageReader::PropertiesReader::onUShort(uint16_t, const Descriptor*)
{
    QPID_LOG(info, "skipping message property at index " << index << " unexpected type (ushort)");
    ++index;
}
void MessageReader::PropertiesReader::onByte(int8_t, const Descriptor*)
{
    QPID_LOG(info, "skipping message property at index " << index << " unexpected type (byte)");
    ++index;
}
void MessageReader::PropertiesReader::onShort(int16_t, const Descriptor*)
{
    QPID_LOG(info, "skipping message property at index " << index << " unexpected type (short)");
    ++index;
}
void MessageReader::PropertiesReader::onInt(int32_t, const Descriptor*)
{
    QPID_LOG(info, "skipping message property at index " << index << " unexpected type (int)");
    ++index;
}
void MessageReader::PropertiesReader::onLong(int64_t, const Descriptor*)
{
    QPID_LOG(info, "skipping message property at index " << index << " unexpected type (long)");
    ++index;
}
void MessageReader::PropertiesReader::onFloat(float, const Descriptor*)
{
    QPID_LOG(info, "skipping message property at index " << index << " unexpected type (float)");
    ++index;
}
void MessageReader::PropertiesReader::onDouble(double, const Descriptor*)
{
    QPID_LOG(info, "skipping message property at index " << index << " unexpected type (double)");
    ++index;
}
bool MessageReader::PropertiesReader::onStartList(uint32_t /*count*/, const CharSequence& /*elements*/, const CharSequence& /*complete*/, const Descriptor*)
{
    QPID_LOG(info, "skipping message property at index " << index << " unexpected type (list)");
    ++index;
    return false;
}
bool MessageReader::PropertiesReader::onStartMap(uint32_t /*count*/, const CharSequence& /*elements*/, const CharSequence& /*complete*/, const Descriptor*)
{
    QPID_LOG(info, "skipping message property at index " << index << " unexpected type (map)");
    ++index;
    return false;
}
bool MessageReader::PropertiesReader::onStartArray(uint32_t /*count*/, const CharSequence&, const Constructor&, const Descriptor*)
{
    QPID_LOG(info, "skipping message property at index " << index << " unexpected type (array)");
    ++index;
    return false;
}


//header, properties, amqp-sequence, amqp-value
bool MessageReader::onStartList(uint32_t count, const CharSequence& elements, const CharSequence& raw, const Descriptor* descriptor)
{
    if (delegate) {
        return delegate->onStartList(count, elements, raw, descriptor);
    } else {
        if (!descriptor) {
            QPID_LOG(warning, "Expected described type but got no descriptor for list.");
            return false;
        } else if (descriptor->match(HEADER_SYMBOL, HEADER_CODE)) {
            delegate = &headerReader;
            return true;
        } else if (descriptor->match(PROPERTIES_SYMBOL, PROPERTIES_CODE)) {
            delegate = &propertiesReader;
            return true;
        } else if (descriptor->match(AMQP_SEQUENCE_SYMBOL, AMQP_SEQUENCE_CODE)) {
            onAmqpSequence(raw);
            return false;
        } else if (descriptor->match(AMQP_VALUE_SYMBOL, AMQP_VALUE_CODE)) {
            onAmqpValue(elements, qpid::amqp::typecodes::LIST_NAME, nested(descriptor));
            return false;
        } else {
            QPID_LOG(warning, "Unexpected described list: " << *descriptor);
            return false;
        }
    }
}
void MessageReader::onEndList(uint32_t count, const Descriptor* descriptor)
{
    if (delegate) {
        if (descriptor && (descriptor->match(HEADER_SYMBOL, HEADER_CODE) || descriptor->match(PROPERTIES_SYMBOL, PROPERTIES_CODE))) {
            delegate = 0;
        } else {
            delegate->onEndList(count, descriptor);
        }
    }
}

//delivery-annotations, message-annotations, application-properties, amqp-value
bool MessageReader::onStartMap(uint32_t count, const CharSequence& elements, const CharSequence& raw, const Descriptor* descriptor)
{
    if (delegate) {
        return delegate->onStartMap(count, elements, raw, descriptor);
    } else {
        if (!descriptor) {
            QPID_LOG(warning, "Expected described type but got no descriptor for map.");
            return false;
        } else if (descriptor->match(DELIVERY_ANNOTATIONS_SYMBOL, DELIVERY_ANNOTATIONS_CODE)) {
            onDeliveryAnnotations(elements, raw);
            return false;
        } else if (descriptor->match(MESSAGE_ANNOTATIONS_SYMBOL, MESSAGE_ANNOTATIONS_CODE)) {
            onMessageAnnotations(elements, raw);
            return false;
        } else if (descriptor->match(FOOTER_SYMBOL, FOOTER_CODE)) {
            onFooter(elements, raw);
            return false;
        } else if (descriptor->match(APPLICATION_PROPERTIES_SYMBOL, APPLICATION_PROPERTIES_CODE)) {
            onApplicationProperties(elements, raw);
            return false;
        } else if (descriptor->match(AMQP_VALUE_SYMBOL, AMQP_VALUE_CODE)) {
            onAmqpValue(elements, qpid::amqp::typecodes::MAP_NAME, nested(descriptor));
            return false;
        } else {
            QPID_LOG(warning, "Unexpected described map: " << *descriptor);
            return false;
        }
    }
}

void MessageReader::onEndMap(uint32_t count, const Descriptor* descriptor)
{
    if (delegate) {
        delegate->onEndMap(count, descriptor);
    }
}

//data, amqp-value
void MessageReader::onBinary(const CharSequence& bytes, const Descriptor* descriptor)
{
    if (delegate) {
        delegate->onBinary(bytes, descriptor);
    } else {
        if (!descriptor) {
            QPID_LOG(warning, "Expected described type but got binary value with no descriptor.");
        } else if (descriptor->match(DATA_SYMBOL, DATA_CODE)) {
            onData(bytes);
        } else if (descriptor->match(AMQP_VALUE_SYMBOL, AMQP_VALUE_CODE)) {
            onAmqpValue(bytes, qpid::amqp::typecodes::BINARY_NAME, nested(descriptor));
        } else {
            QPID_LOG(warning, "Unexpected binary value with descriptor: " << *descriptor);
        }
    }

}

//amqp-value
void MessageReader::onNull(const Descriptor* descriptor)
{
    if (delegate) {
        delegate->onNull(descriptor);
    } else {
        if (descriptor && descriptor->match(AMQP_VALUE_SYMBOL, AMQP_VALUE_CODE)) {
            qpid::types::Variant v;
            onAmqpValue(v, nested(descriptor));
        } else {
            if (!descriptor) {
                QPID_LOG(warning, "Expected described type but got null value with no descriptor.");
            } else {
                QPID_LOG(warning, "Unexpected null value with descriptor: " << *descriptor);
            }
        }
    }
}
void MessageReader::onString(const CharSequence& v, const Descriptor* descriptor)
{
    if (delegate) {
        delegate->onString(v, descriptor);
    } else {
        if (descriptor && descriptor->match(AMQP_VALUE_SYMBOL, AMQP_VALUE_CODE)) {
            onAmqpValue(v, qpid::amqp::typecodes::STRING_NAME, nested(descriptor));
        } else {
            if (!descriptor) {
                QPID_LOG(warning, "Expected described type but got string value with no descriptor.");
            } else {
                QPID_LOG(warning, "Unexpected string value with descriptor: " << *descriptor);
            }
        }
    }
}
void MessageReader::onSymbol(const CharSequence& v, const Descriptor* descriptor)
{
    if (delegate) {
        delegate->onSymbol(v, descriptor);
    } else {
        if (descriptor && descriptor->match(AMQP_VALUE_SYMBOL, AMQP_VALUE_CODE)) {
            onAmqpValue(v, qpid::amqp::typecodes::SYMBOL_NAME, nested(descriptor));
        } else {
            if (!descriptor) {
                QPID_LOG(warning, "Expected described type but got symbol value with no descriptor.");
            } else {
                QPID_LOG(warning, "Unexpected symbol value with descriptor: " << *descriptor);
            }
        }
    }
}

void MessageReader::onBoolean(bool v, const Descriptor* descriptor)
{
    if (delegate) {
        delegate->onBoolean(v, descriptor);
    } else {
        if (descriptor && descriptor->match(AMQP_VALUE_SYMBOL, AMQP_VALUE_CODE)) {
            qpid::types::Variant body = v;
            onAmqpValue(body, nested(descriptor));
        } else {
            if (!descriptor) {
                QPID_LOG(warning, "Expected described type but got boolean value with no descriptor.");
            } else {
                QPID_LOG(warning, "Unexpected boolean value with descriptor: " << *descriptor);
            }
        }
    }
}

void MessageReader::onUByte(uint8_t v, const Descriptor* descriptor)
{
    if (delegate) {
        delegate->onUByte(v, descriptor);
    } else {
        if (descriptor && descriptor->match(AMQP_VALUE_SYMBOL, AMQP_VALUE_CODE)) {
            qpid::types::Variant body = v;
            onAmqpValue(body, nested(descriptor));
        } else {
            if (!descriptor) {
                QPID_LOG(warning, "Expected described type but got ubyte value with no descriptor.");
            } else {
                QPID_LOG(warning, "Unexpected ubyte value with descriptor: " << *descriptor);
            }
        }
    }
}

void MessageReader::onUShort(uint16_t v, const Descriptor* descriptor)
{
    if (delegate) {
        delegate->onUShort(v, descriptor);
    } else {
        if (descriptor && descriptor->match(AMQP_VALUE_SYMBOL, AMQP_VALUE_CODE)) {
            qpid::types::Variant body = v;
            onAmqpValue(body, nested(descriptor));
        } else {
            if (!descriptor) {
                QPID_LOG(warning, "Expected described type but got ushort value with no descriptor.");
            } else {
                QPID_LOG(warning, "Unexpected ushort value with descriptor: " << *descriptor);
            }
        }
    }
}

void MessageReader::onUInt(uint32_t v, const Descriptor* descriptor)
{
    if (delegate) {
        delegate->onUInt(v, descriptor);
    } else {
        if (descriptor && descriptor->match(AMQP_VALUE_SYMBOL, AMQP_VALUE_CODE)) {
            qpid::types::Variant body = v;
            onAmqpValue(body, nested(descriptor));
        } else {
            if (!descriptor) {
                QPID_LOG(warning, "Expected described type but got uint value with no descriptor.");
            } else {
                QPID_LOG(warning, "Unexpected uint value with descriptor: " << *descriptor);
            }
        }
    }
}

void MessageReader::onULong(uint64_t v, const Descriptor* descriptor)
{
    if (delegate) {
        delegate->onULong(v, descriptor);
    } else {
        if (descriptor && descriptor->match(AMQP_VALUE_SYMBOL, AMQP_VALUE_CODE)) {
            qpid::types::Variant body = v;
            onAmqpValue(body, nested(descriptor));
        } else {
            if (!descriptor) {
                QPID_LOG(warning, "Expected described type but got ulong value with no descriptor.");
            } else {
                QPID_LOG(warning, "Unexpected ulong value with descriptor: " << *descriptor);
            }
        }
    }
}

void MessageReader::onByte(int8_t v, const Descriptor* descriptor)
{
    if (delegate) {
        delegate->onByte(v, descriptor);
    } else {
        if (descriptor && descriptor->match(AMQP_VALUE_SYMBOL, AMQP_VALUE_CODE)) {
            qpid::types::Variant body = v;
            onAmqpValue(body, nested(descriptor));
        } else {
            if (!descriptor) {
                QPID_LOG(warning, "Expected described type but got byte value with no descriptor.");
            } else {
                QPID_LOG(warning, "Unexpected byte value with descriptor: " << *descriptor);
            }
        }
    }
}

void MessageReader::onShort(int16_t v, const Descriptor* descriptor)
{
    if (delegate) {
        delegate->onShort(v, descriptor);
    } else {
        if (descriptor && descriptor->match(AMQP_VALUE_SYMBOL, AMQP_VALUE_CODE)) {
            qpid::types::Variant body = v;
            onAmqpValue(body, nested(descriptor));
        } else {
            if (!descriptor) {
                QPID_LOG(warning, "Expected described type but got short value with no descriptor.");
            } else {
                QPID_LOG(warning, "Unexpected short value with descriptor: " << *descriptor);
            }
        }
    }
}

void MessageReader::onInt(int32_t v, const Descriptor* descriptor)
{
    if (delegate) {
        delegate->onInt(v, descriptor);
    } else {
        if (descriptor && descriptor->match(AMQP_VALUE_SYMBOL, AMQP_VALUE_CODE)) {
            qpid::types::Variant body = v;
            onAmqpValue(body, nested(descriptor));
        } else {
            if (!descriptor) {
                QPID_LOG(warning, "Expected described type but got int value with no descriptor.");
            } else {
                QPID_LOG(warning, "Unexpected int value with descriptor: " << *descriptor);
            }
        }
    }
}

void MessageReader::onLong(int64_t v, const Descriptor* descriptor)
{
    if (delegate) {
        delegate->onLong(v, descriptor);
    } else {
        if (descriptor && descriptor->match(AMQP_VALUE_SYMBOL, AMQP_VALUE_CODE)) {
            qpid::types::Variant body = v;
            onAmqpValue(body, nested(descriptor));
        } else {
            if (!descriptor) {
                QPID_LOG(warning, "Expected described type but got long value with no descriptor.");
            } else {
                QPID_LOG(warning, "Unexpected long value with descriptor: " << *descriptor);
            }
        }
    }
}

void MessageReader::onFloat(float v, const Descriptor* descriptor)
{
    if (delegate) {
        delegate->onFloat(v, descriptor);
    } else {
        if (descriptor && descriptor->match(AMQP_VALUE_SYMBOL, AMQP_VALUE_CODE)) {
            qpid::types::Variant body = v;
            onAmqpValue(body, nested(descriptor));
        } else {
            if (!descriptor) {
                QPID_LOG(warning, "Expected described type but got float value with no descriptor.");
            } else {
                QPID_LOG(warning, "Unexpected float value with descriptor: " << *descriptor);
            }
        }
    }
}

void MessageReader::onDouble(double v, const Descriptor* descriptor)
{
    if (delegate) {
        delegate->onDouble(v, descriptor);
    } else {
        if (descriptor && descriptor->match(AMQP_VALUE_SYMBOL, AMQP_VALUE_CODE)) {
            qpid::types::Variant body = v;
            onAmqpValue(body, nested(descriptor));
        } else {
            if (!descriptor) {
                QPID_LOG(warning, "Expected described type but got double value with no descriptor.");
            } else {
                QPID_LOG(warning, "Unexpected double value with descriptor: " << *descriptor);
            }
        }
    }
}

void MessageReader::onUuid(const CharSequence& v, const Descriptor* descriptor)
{
    if (delegate) {
        delegate->onUuid(v, descriptor);
    } else {
        if (descriptor && descriptor->match(AMQP_VALUE_SYMBOL, AMQP_VALUE_CODE)) {
            onAmqpValue(v, qpid::amqp::typecodes::UUID_NAME, nested(descriptor));
        } else {
            if (!descriptor) {
                QPID_LOG(warning, "Expected described type but got uuid value with no descriptor.");
            } else {
                QPID_LOG(warning, "Unexpected uuid value with descriptor: " << *descriptor);
            }
        }
    }
}

void MessageReader::onTimestamp(int64_t v, const Descriptor* descriptor)
{
    if (delegate) {
        delegate->onTimestamp(v, descriptor);
    } else {
        if (descriptor && descriptor->match(AMQP_VALUE_SYMBOL, AMQP_VALUE_CODE)) {
            qpid::types::Variant body = v;
            onAmqpValue(body, nested(descriptor));
        } else {
            if (!descriptor) {
                QPID_LOG(warning, "Expected described type but got timestamp value with no descriptor.");
            } else {
                QPID_LOG(warning, "Unexpected timestamp value with descriptor: " << *descriptor);
            }
        }
    }
}

bool MessageReader::onStartArray(uint32_t count, const CharSequence& raw, const Constructor& constructor, const Descriptor* descriptor)
{
    if (delegate) {
        return delegate->onStartArray(count, raw, constructor, descriptor);
    } else {
        if (descriptor && descriptor->match(AMQP_VALUE_SYMBOL, AMQP_VALUE_CODE)) {
            //TODO: might be better to decode this here
            onAmqpValue(raw, qpid::amqp::typecodes::ARRAY_NAME, nested(descriptor));
        } else {
            if (!descriptor) {
                QPID_LOG(warning, "Expected described type but got array with no descriptor.");
            } else {
                QPID_LOG(warning, "Unexpected array with descriptor: " << *descriptor);
            }
        }
        return false;
    }
}

void MessageReader::onEndArray(uint32_t v, const Descriptor* descriptor)
{
    if (delegate) {
        delegate->onEndArray(v, descriptor);
    }
}

MessageReader::MessageReader() : headerReader(*this), propertiesReader(*this), delegate(0)
{
    bare.init();
}

void MessageReader::onDescriptor(const Descriptor& descriptor, const char* position)
{
    if (bare.data) {
        if (descriptor.match(FOOTER_SYMBOL, FOOTER_CODE)) {
            bare.size = position - bare.data;
        }
    } else {
        if (descriptor.match(PROPERTIES_SYMBOL, PROPERTIES_CODE) || descriptor.match(APPLICATION_PROPERTIES_SYMBOL, APPLICATION_PROPERTIES_CODE)
            || descriptor.match(AMQP_SEQUENCE_SYMBOL, AMQP_SEQUENCE_CODE) || descriptor.match(AMQP_VALUE_SYMBOL, AMQP_VALUE_CODE) || descriptor.match(DATA_SYMBOL, DATA_CODE)) {
            bare.data = position;
        }
    }
}

CharSequence MessageReader::getBareMessage() const { return bare; }

}} // namespace qpid::amqp