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.OpenType;
14 
15 import hunt.collection.AbstractList;
16 import hunt.collection.List;
17 import hunt.collection.Map;
18 import hunt.proton.amqp.Symbol;
19 import hunt.proton.amqp.UnsignedInteger;
20 import hunt.proton.amqp.UnsignedLong;
21 import hunt.proton.amqp.UnsignedShort;
22 import hunt.proton.amqp.transport.Open;
23 import hunt.proton.codec.AbstractDescribedType;
24 import hunt.proton.codec.DecodeException;
25 import hunt.proton.codec.Decoder;
26 import hunt.proton.codec.DescribedTypeConstructor;
27 import hunt.proton.codec.EncoderImpl;
28 import std.concurrency : initOnce;
29 import hunt.Exceptions;
30 import hunt.logging;
31 import hunt.String;
32 
33 class OpenWrapper : AbstractList!Object
34 {
35 
36     private Open _open;
37 
38     this(Open open)
39     {
40         _open = open;
41     }
42 
43     override
44     public Object get(int index)
45     {
46 
47         switch(index)
48         {
49             case 0:
50             return _open.getContainerId();
51             case 1:
52             return _open.getHostname();
53             case 2:
54             return _open.getMaxFrameSize();
55             case 3:
56             return _open.getChannelMax();
57             case 4:
58             return _open.getIdleTimeOut();
59             case 5:
60             return cast(Object)_open.getOutgoingLocales();
61             case 6:
62             return cast(Object)_open.getIncomingLocales();
63             case 7:
64             return cast(Object)_open.getOfferedCapabilities();
65             case 8:
66             return cast(Object)_open.getDesiredCapabilities();
67             case 9:
68             return cast(Object)_open.getProperties();
69             default:
70             return null;
71         }
72 
73         //   throw new IllegalStateException("Unknown index " ~ index);
74 
75     }
76 
77     override
78     public int size()
79     {
80         return _open.getProperties() !is null
81         ? 10
82         : _open.getDesiredCapabilities() !is null
83         ? 9
84         : _open.getOfferedCapabilities() !is null
85         ? 8
86         : _open.getIncomingLocales() !is null
87         ? 7
88         : _open.getOutgoingLocales() !is null
89         ? 6
90         : _open.getIdleTimeOut() !is null
91         ? 5
92         : (_open.getChannelMax() !is null && _open.getChannelMax() != (UnsignedShort.MAX_VALUE))
93         ? 4
94         : (_open.getMaxFrameSize() !is null && _open.getMaxFrameSize() != (UnsignedInteger.MAX_VALUE))
95         ? 3
96         : _open.getHostname() !is null
97         ? 2
98         : 1;
99 
100     }
101 
102 }
103 
104 class OpenType : AbstractDescribedType!(Open,List!Object) , DescribedTypeConstructor!(Open)
105 {
106     //private static Object[] DESCRIPTORS =
107     //{
108     //    UnsignedLong.valueOf(0x0000000000000010L), Symbol.valueOf("amqp:open:list"),
109     //};
110 
111    // private static UnsignedLong DESCRIPTOR = UnsignedLong.valueOf(0x0000000000000010L);
112 
113    static Object[]  DESCRIPTORS() {
114        __gshared Object[]  inst;
115        return initOnce!inst([UnsignedLong.valueOf(0x0000000000000010L), Symbol.valueOf("amqp:open:list")]);
116    }
117 
118   static UnsignedLong  DESCRIPTOR() {
119       __gshared UnsignedLong  inst;
120       return initOnce!inst(UnsignedLong.valueOf(0x0000000000000010L));
121   }
122 
123 
124     this(EncoderImpl encoder)
125     {
126         super(encoder);
127     }
128 
129 
130     override
131     public UnsignedLong getDescriptor()
132     {
133         return DESCRIPTOR;
134     }
135 
136     override
137     protected List!Object wrap(Open val)
138     {
139         return new OpenWrapper(val);
140     }
141 
142 
143 
144 
145     public Open newInstance(Object described)
146     {
147         List!Object l = cast(List!Object) described;
148 
149         Open o = new Open();
150 
151         if(l.isEmpty())
152         {
153            // throw new DecodeException("The container-id field cannot be omitted");
154             logError("The container-id field cannot be omitted");
155         }
156 
157         switch(10 - l.size())
158         {
159 
160             case 0:
161                 o.setProperties( cast(Map!(Symbol,Object)) l.get( 9 ) );
162                 goto case;
163             case 1:
164                 //Object val1 = l.get( 8 );
165                 o.setDesiredCapabilities( cast(List!Symbol) l.get( 8 ) );
166                 goto case;
167             case 2:
168                 o.setOfferedCapabilities(cast(List!Symbol)l.get( 7 ) );
169                 goto case;
170             case 3:
171                 o.setIncomingLocales( cast(List!Symbol) l.get(6) );
172                 goto case;
173             case 4:
174                 o.setOutgoingLocales( cast(List!Symbol) l.get(5) );
175                 goto case;
176             case 5:
177                 o.setIdleTimeOut( cast(UnsignedInteger) l.get( 4 ) );
178                 goto case;
179             case 6:
180                 UnsignedShort channelMax = cast(UnsignedShort) l.get(3);
181                 o.setChannelMax(channelMax is null ? UnsignedShort.MAX_VALUE : channelMax);
182                 goto case;
183             case 7:
184                 UnsignedInteger maxFrameSize = cast(UnsignedInteger) l.get(2);
185                 o.setMaxFrameSize(maxFrameSize is null ? UnsignedInteger.MAX_VALUE : maxFrameSize);
186                 goto case;
187             case 8:
188                 o.setHostname( cast(String) l.get( 1 ) );
189                 goto case;
190             case 9:
191                 o.setContainerId( cast(String) l.get( 0 ) );
192                 break;
193             default:
194                 break;
195         }
196 
197 
198         return o;
199     }
200 
201     public TypeInfo getTypeClass()
202     {
203         return typeid(Open);
204     }
205 
206 
207     public static void register(Decoder decoder, EncoderImpl encoder)
208     {
209         OpenType type = new OpenType(encoder);
210         foreach(Object descriptor ; DESCRIPTORS)
211         {
212             decoder.registerDynamic(descriptor, type);
213         }
214         encoder.register(type);
215     }
216 
217 }