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.transport.TransferType;
14 
15 import hunt.collection.AbstractList;
16 import hunt.collection.List;
17 import hunt.proton.amqp.Binary;
18 import hunt.proton.amqp.Symbol;
19 import hunt.proton.amqp.UnsignedByte;
20 import hunt.proton.amqp.UnsignedInteger;
21 import hunt.proton.amqp.UnsignedLong;
22 import hunt.proton.amqp.transport.DeliveryState;
23 import hunt.proton.amqp.transport.ReceiverSettleMode;
24 import hunt.proton.amqp.transport.Transfer;
25 import hunt.proton.codec.AbstractDescribedType;
26 import hunt.proton.codec.DecodeException;
27 import hunt.proton.codec.Decoder;
28 import hunt.proton.codec.DescribedTypeConstructor;
29 import hunt.proton.codec.EncoderImpl;
30 import hunt.Boolean;
31 import std.concurrency : initOnce;
32 import hunt.logging;
33 
34 class TransferWrapper : AbstractList!Object
35 {
36 
37     private Transfer _transfer;
38 
39     this(Transfer transfer)
40     {
41         _transfer = transfer;
42     }
43 
44     override
45     public Object get(int index)
46     {
47 
48         switch(index)
49         {
50             case 0:
51             return _transfer.getHandle();
52             case 1:
53             return _transfer.getDeliveryId();
54             case 2:
55             return _transfer.getDeliveryTag();
56             case 3:
57             return _transfer.getMessageFormat();
58             case 4:
59             return _transfer.getSettled();
60             case 5:
61             return _transfer.getMore();
62             case 6:
63             return _transfer.getRcvSettleMode() is null ? null : _transfer.getRcvSettleMode().getValue();
64             case 7:
65             return cast(Object)_transfer.getState();
66             case 8:
67             return _transfer.getResume();
68             case 9:
69             return _transfer.getAborted();
70             case 10:
71             return _transfer.getBatchable();
72             default:
73             return null;
74         }
75 
76         // throw new IllegalStateException("Unknown index " ~ index);
77 
78     }
79 
80     override
81     public int size()
82     {
83         return _transfer.getBatchable().booleanValue
84         ? 11
85         : _transfer.getAborted().booleanValue
86         ? 10
87         : _transfer.getResume().booleanValue
88         ? 9
89         : _transfer.getState() !is null
90         ? 8
91         : _transfer.getRcvSettleMode() !is null
92         ? 7
93         : _transfer.getMore().booleanValue
94         ? 6
95         : _transfer.getSettled() !is null
96         ? 5
97         : _transfer.getMessageFormat() !is null
98         ? 4
99         : _transfer.getDeliveryTag() !is null
100         ? 3
101         : _transfer.getDeliveryId() !is null
102         ? 2
103         : 1;
104 
105     }
106 
107 }
108 
109 
110 class TransferType : AbstractDescribedType!(Transfer,List!Object) , DescribedTypeConstructor!(Transfer)
111 {
112     //private static Object[] DESCRIPTORS =
113     //{
114     //    UnsignedLong.valueOf(0x0000000000000014L), Symbol.valueOf("amqp:transfer:list"),
115     //};
116     //
117     //private static UnsignedLong DESCRIPTOR = UnsignedLong.valueOf(0x0000000000000014L);
118 
119     static Object[]  DESCRIPTORS() {
120         __gshared Object[]  inst;
121         return initOnce!inst([UnsignedLong.valueOf(0x0000000000000014L), Symbol.valueOf("amqp:transfer:list")]);
122     }
123 
124          static UnsignedLong  DESCRIPTOR() {
125              __gshared UnsignedLong  inst;
126              return initOnce!inst(UnsignedLong.valueOf(0x0000000000000014L));
127          }
128 
129     this(EncoderImpl encoder)
130     {
131         super(encoder);
132     }
133 
134 
135     override
136     public UnsignedLong getDescriptor()
137     {
138         return DESCRIPTOR;
139     }
140 
141     override
142     protected List!Object wrap(Transfer val)
143     {
144         return new TransferWrapper(val);
145     }
146 
147 
148 
149         public Transfer newInstance(Object described)
150         {
151             List!Object l = cast(List!Object) described;
152 
153             Transfer o = new Transfer();
154 
155             if(l.isEmpty())
156             {
157                 logError("The handle field cannot be omitted");
158                 return null;
159                // throw new DecodeException("The handle field cannot be omitted");
160             }
161 
162             switch(11 - l.size())
163             {
164 
165                 case 0:
166                     Boolean batchable = cast(Boolean) l.get(10);
167                     o.setBatchable(batchable is null ? null : batchable);
168                     goto case;
169                 case 1:
170                     Boolean aborted = cast(Boolean) l.get(9);
171                     o.setAborted(aborted is null ? null : aborted);
172                     goto case;
173                 case 2:
174                     Boolean resume = cast(Boolean) l.get(8);
175                     o.setResume(resume is null ? null : resume);
176                     goto case;
177                 case 3:
178                     o.setState( cast(DeliveryState) l.get( 7 ) );
179                     goto case;
180                 case 4:
181                     UnsignedByte receiverSettleMode = cast(UnsignedByte) l.get(6);
182                     o.setRcvSettleMode(receiverSettleMode is null ? null : ReceiverSettleMode.valueOf(receiverSettleMode));
183                     goto case;
184                 case 5:
185                     Boolean more = cast(Boolean) l.get(5);
186                     o.setMore(more is null ? null : more );
187                     goto case;
188                 case 6:
189                     o.setSettled( cast(Boolean) l.get( 4 ) );
190                     goto case;
191                 case 7:
192                     o.setMessageFormat( cast(UnsignedInteger) l.get( 3 ) );
193                     goto case;
194                 case 8:
195                     o.setDeliveryTag( cast(Binary) l.get( 2 ) );
196                     goto case;
197                 case 9:
198                     o.setDeliveryId( cast(UnsignedInteger) l.get( 1 ) );
199                     goto case;
200                 case 10:
201                     o.setHandle( cast(UnsignedInteger) l.get( 0 ) );
202                     break;
203                 default:
204                     break;
205             }
206 
207 
208             return o;
209         }
210 
211         public TypeInfo getTypeClass()
212         {
213             return typeid(Transfer);
214         }
215 
216     public static void register(Decoder decoder, EncoderImpl encoder)
217     {
218         TransferType type = new TransferType(encoder);
219         foreach(Object descriptor ; DESCRIPTORS)
220         {
221             decoder.registerDynamic(descriptor, type);
222         }
223         encoder.register(type);
224     }
225 }