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.TargetType;
14 
15 import hunt.collection.AbstractList;
16 
17 import hunt.collection.List;
18 import hunt.collection.Map;
19 import hunt.Object;
20 import hunt.Boolean;
21 import hunt.String;
22 import hunt.Exceptions;
23 import hunt.proton.amqp.Symbol;
24 import hunt.proton.amqp.UnsignedInteger;
25 import hunt.proton.amqp.UnsignedLong;
26 import hunt.proton.amqp.messaging.Target;
27 import hunt.proton.codec.AbstractDescribedType;
28 import hunt.proton.codec.Decoder;
29 import hunt.proton.codec.DescribedTypeConstructor;
30 import hunt.proton.codec.EncoderImpl;
31 import hunt.proton.amqp.messaging.TerminusDurability;
32 import hunt.proton.amqp.messaging.TerminusExpiryPolicy;
33 import std.concurrency : initOnce;
34 import std.conv : to;
35 
36 class TargetWrapper : AbstractList!Object
37 {
38     private Target _impl;
39 
40     this(Target impl)
41     {
42         _impl = impl;
43     }
44 
45     override
46     public Object get(int index)
47     {
48 
49         switch(index)
50         {
51             case 0:
52             return _impl.getAddress();
53             case 1:
54             return _impl.getDurable().getValue();
55             case 2:
56             return _impl.getExpiryPolicy().getPolicy();
57             case 3:
58             return _impl.getTimeout();
59             case 4:
60             return _impl.getDynamic();
61             case 5:
62             return cast(Object)(_impl.getDynamicNodeProperties());
63             case 6:
64             return cast(Object)_impl.getCapabilities();
65             default:
66             return null;
67         }
68 
69         //   throw new IllegalStateException("Unknown index " ~ to!string(index));
70 
71     }
72 
73     override
74     public int size()
75     {
76         return _impl.getCapabilities() !is null
77         ? 7
78         : _impl.getDynamicNodeProperties() !is null
79         ? 6
80         : _impl.getDynamic().booleanValue
81         ? 5
82         : (_impl.getTimeout() !is null && _impl.getTimeout() != (UnsignedInteger.ZERO))
83         ? 4
84         : _impl.getExpiryPolicy() !is (TerminusExpiryPolicy.SESSION_END)
85         ? 3
86         : _impl.getDurable() !is (TerminusDurability.NONE)
87         ? 2
88         : _impl.getAddress() !is null
89         ? 1
90         : 0;
91 
92     }
93 }
94 
95 class TargetType : AbstractDescribedType!(Target,List!Object) , DescribedTypeConstructor!(Target)
96 {
97     //private static Object[] DESCRIPTORS =
98     //{
99     //    UnsignedLong.valueOf(0x0000000000000029L), Symbol.valueOf("amqp:target:list"),
100     //};
101 
102    // private static UnsignedLong DESCRIPTOR = UnsignedLong.valueOf(0x0000000000000029L);
103 
104 
105     static Object[]  DESCRIPTORS() {
106         __gshared Object[]  inst;
107         return initOnce!inst([UnsignedLong.valueOf(0x0000000000000029L), Symbol.valueOf("amqp:target:list")]);
108     }
109 
110     static UnsignedLong  DESCRIPTOR() {
111         __gshared UnsignedLong  inst;
112         return initOnce!inst(UnsignedLong.valueOf(0x0000000000000029L));
113     }
114 
115 
116     this(EncoderImpl encoder)
117     {
118         super(encoder);
119     }
120 
121     override
122     public UnsignedLong getDescriptor()
123     {
124         return DESCRIPTOR;
125     }
126 
127     override
128     protected List!Object wrap(Target val)
129     {
130         return new TargetWrapper(val);
131     }
132 
133 
134 
135 
136     public Target newInstance(Object described)
137     {
138         List!Object l = cast(List!Object) described;
139 
140         Target o = new Target();
141 
142 
143         switch(7 - l.size())
144         {
145 
146             case 0:
147                 Object val0 = l.get( 6 );
148                 //if( val0 is null || val0.getClass().isArray() )
149                 //{
150                 //    o.setCapabilities( (Symbol[]) val0 );
151                 //}
152                 //else
153                 {
154                     o.setCapabilities( cast(List!Symbol) val0 );
155                 }
156                 goto case;
157             case 1:
158                 o.setDynamicNodeProperties( cast(IObject) l.get( 5 ) );
159                 goto case;
160             case 2:
161                 Boolean dynamic = cast(Boolean) l.get(4);
162                 o.setDynamic(dynamic is null ? new Boolean( false) : dynamic);
163                 goto case;
164             case 3:
165                 UnsignedInteger timeout = cast(UnsignedInteger) l.get(3);
166                 o.setTimeout(timeout is null ? UnsignedInteger.ZERO : timeout);
167                 goto case;
168             case 4:
169                 Symbol expiryPolicy = cast(Symbol) l.get(2);
170                 o.setExpiryPolicy(expiryPolicy is null ? TerminusExpiryPolicy.SESSION_END : TerminusExpiryPolicy.valueOf(expiryPolicy));
171                 goto case;
172             case 5:
173                 UnsignedInteger durable = cast(UnsignedInteger) l.get(1);
174                 o.setDurable(durable is null ? TerminusDurability.NONE : TerminusDurability.get(durable));
175                 goto case;
176             case 6:
177                 o.setAddress( cast(String) l.get( 0 ) );
178                 break;
179             default:
180                 break;
181         }
182 
183 
184         return o;
185     }
186 
187     public TypeInfo getTypeClass()
188     {
189         return typeid(Target);
190     }
191 
192 
193 
194     public static void register(Decoder decoder, EncoderImpl encoder)
195     {
196         TargetType type = new TargetType(encoder);
197         //implementationMissing(false);
198         foreach(Object descriptor ; DESCRIPTORS)
199         {
200             decoder.registerDynamic(descriptor, type);
201         }
202         encoder.register(type);
203     }
204 
205 }
206