1 /*
2 * hunt-proton: AMQP Protocol library for D programming language.
3 *
4 * Copyright (C) 2018-2019 HuntLabs
5 *
6 * Website: https://www.huntlabs.net/
7 *
8 * Licensed under the Apache-2.0 License.
9 *
10 */
11 module hunt.proton.codec.messaging.ReceivedType;
12
13 import hunt.collection.AbstractList;
14 import hunt.collection.List;
15 import hunt.Object;
16 import hunt.Exceptions;
17 import hunt.proton.amqp.Symbol;
18 import hunt.proton.amqp.UnsignedInteger;
19 import hunt.proton.amqp.UnsignedLong;
20 import hunt.proton.amqp.messaging.Received;
21 import hunt.proton.codec.AbstractDescribedType;
22 import hunt.proton.codec.Decoder;
23 import hunt.proton.codec.DescribedTypeConstructor;
24 import hunt.proton.codec.EncoderImpl;
25 import std.concurrency : initOnce;
26 import hunt.Exceptions;
27 import std.conv : to;
28
29 class ReceivedWrapper : AbstractList!Object
30 {
31 private Received _impl;
32
33 this(Received impl)
34 {
35 _impl = impl;
36 }
37
38 override
39 public Object get(int index)
40 {
41 switch(index)
42 {
43 case 0:
44 return _impl.getSectionNumber();
45 case 1:
46 return _impl.getSectionOffset();
47 default:
48 return null;
49 }
50
51 // throw new IllegalStateException("Unknown index " ~ to!string(index));
52 }
53
54 override
55 public int size()
56 {
57 return _impl.getSectionOffset() !is null
58 ? 2
59 : _impl.getSectionNumber() !is null
60 ? 1
61 : 0;
62 }
63 }
64
65 class ReceivedType : AbstractDescribedType!(Received, List!Object) , DescribedTypeConstructor!(Received)
66 {
67 //private static Object[] DESCRIPTORS =
68 //{
69 // UnsignedLong.valueOf(0x0000000000000023L), Symbol.valueOf("amqp:received:list"),
70 //};
71
72 //private static UnsignedLong DESCRIPTOR = UnsignedLong.valueOf(0x0000000000000023L);
73
74
75 static Object[] DESCRIPTORS() {
76 __gshared Object[] inst;
77 return initOnce!inst([UnsignedLong.valueOf(0x0000000000000023L), Symbol.valueOf("amqp:received:list")]);
78 }
79
80 static UnsignedLong DESCRIPTOR() {
81 __gshared UnsignedLong inst;
82 return initOnce!inst(UnsignedLong.valueOf(0x0000000000000023L));
83 }
84
85
86 this(EncoderImpl encoder)
87 {
88 super(encoder);
89 }
90
91 override
92 public UnsignedLong getDescriptor()
93 {
94 return DESCRIPTOR;
95 }
96
97 override
98 protected List!Object wrap(Received val)
99 {
100 return new ReceivedWrapper(val);
101 }
102
103
104
105 override
106 public Received newInstance(Object described)
107 {
108 List!Object l = cast(List!Object) described;
109
110 Received o = new Received();
111
112 switch(2 - l.size())
113 {
114 case 0:
115 o.setSectionOffset(cast(UnsignedLong) l.get( 1 ));
116 goto case;
117 case 1:
118 o.setSectionNumber(cast(UnsignedInteger) l.get( 0 ));
119 break;
120 default:
121 break;
122 }
123
124 return o;
125 }
126
127 override
128 public TypeInfo getTypeClass()
129 {
130 return typeid(Received);
131 }
132
133 public static void register(Decoder decoder, EncoderImpl encoder)
134 {
135 ReceivedType type = new ReceivedType(encoder);
136 //implementationMissing(false);
137 foreach(Object descriptor ; DESCRIPTORS)
138 {
139 decoder.registerDynamic(descriptor, type);
140 }
141 encoder.register(type);
142 }
143 }