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 
12 
13 module hunt.proton.codec.messaging.HeaderType;
14 
15 import hunt.collection.AbstractList;
16 import hunt.collection.List;
17 import hunt.Object;
18 import hunt.Boolean;
19 import hunt.proton.amqp.Symbol;
20 import hunt.proton.amqp.UnsignedByte;
21 import hunt.proton.amqp.UnsignedInteger;
22 import hunt.proton.amqp.UnsignedLong;
23 import hunt.proton.amqp.messaging.Header;
24 import hunt.proton.codec.AbstractDescribedType;
25 import hunt.proton.codec.Decoder;
26 import hunt.proton.codec.DescribedTypeConstructor;
27 import hunt.proton.codec.EncoderImpl;
28 import hunt.Exceptions;
29 import std.concurrency : initOnce;
30 import std.conv : to;
31 
32 class HeaderWrapper : AbstractList!Object
33 {
34     private Header _impl;
35 
36     this(Header impl)
37     {
38         _impl = impl;
39     }
40 
41 
42     override
43     public Object get(int index)
44     {
45 
46         switch(index)
47         {
48             case 0:
49             return _impl.getDurable();
50             case 1:
51             return _impl.getPriority();
52             case 2:
53             return _impl.getTtl();
54             case 3:
55             return _impl.getFirstAcquirer();
56             case 4:
57             return _impl.getDeliveryCount();
58             default:
59             return null;
60         }
61 
62         //   throw new IllegalStateException("Unknown index " ~ to!string(index));
63 
64     }
65 
66     override
67     public int size()
68     {
69         return _impl.getDeliveryCount() !is null
70         ? 5
71         : _impl.getFirstAcquirer() !is null
72         ? 4
73         : _impl.getTtl() !is null
74         ? 3
75         : _impl.getPriority() !is null
76         ? 2
77         : _impl.getDurable() !is null
78         ? 1
79         : 0;
80 
81     }
82 
83 
84 }
85 
86 
87 class HeaderType : AbstractDescribedType!(Header,List!Object) , DescribedTypeConstructor!(Header)
88 {
89     //private static Object[] DESCRIPTORS =
90     //{
91     //    UnsignedLong.valueOf(0x0000000000000070L), Symbol.valueOf("amqp:header:list"),
92     //};
93 
94     //private static UnsignedLong DESCRIPTOR = UnsignedLong.valueOf(0x0000000000000070L);
95 
96     static Object[]  DESCRIPTORS() {
97         __gshared Object[]  inst;
98         return initOnce!inst([UnsignedLong.valueOf(0x0000000000000070L), Symbol.valueOf("amqp:header:list")]);
99     }
100 
101     static UnsignedLong  DESCRIPTOR() {
102         __gshared UnsignedLong  inst;
103         return initOnce!inst(UnsignedLong.valueOf(0x0000000000000070L));
104     }
105 
106 
107     this(EncoderImpl encoder)
108     {
109         super(encoder);
110     }
111 
112     override
113     protected UnsignedLong getDescriptor()
114     {
115         return DESCRIPTOR;
116     }
117 
118     override
119     protected List!Object wrap(Header val)
120     {
121         return new HeaderWrapper(val);
122     }
123 
124 
125 
126     public Header newInstance(Object described)
127     {
128         List!Object l = cast(List!Object) described;
129 
130         Header o = new Header();
131 
132 
133         switch(5 - l.size())
134         {
135 
136             case 0:
137                 o.setDeliveryCount( cast(UnsignedInteger) l.get( 4 ) );
138                 goto case;
139             case 1:
140                 o.setFirstAcquirer( (cast(Boolean) l.get( 3 )) );
141                 goto case;
142             case 2:
143                 o.setTtl( cast(UnsignedInteger) l.get( 2 ) );
144                 goto case;
145             case 3:
146                 o.setPriority( cast(UnsignedByte) l.get( 1 ) );
147                 goto case;
148             case 4:
149                 o.setDurable( (cast(Boolean) l.get( 0 )) );
150                 break;
151             default:
152                 break;
153         }
154 
155 
156         return o;
157     }
158 
159     public TypeInfo getTypeClass()
160     {
161         return typeid(Header);
162     }
163 
164     public static void register(Decoder decoder, EncoderImpl encoder)
165     {
166         HeaderType type = new HeaderType(encoder);
167     //    implementationMissing(false);
168         foreach(Object descriptor ; DESCRIPTORS)
169         {
170             decoder.registerDynamic(descriptor, type);
171         }
172         encoder.register(type);
173     }
174 }