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.FastPathFooterType;
12 
13 import hunt.collection.Collection;
14 import hunt.proton.amqp.Symbol;
15 import hunt.proton.amqp.UnsignedLong;
16 import hunt.proton.amqp.messaging.Footer;
17 import hunt.proton.codec.AMQPType;
18 import hunt.proton.codec.Decoder;
19 import hunt.proton.codec.DecoderImpl;
20 import hunt.proton.codec.EncoderImpl;
21 import hunt.proton.codec.EncodingCodes;
22 import hunt.proton.codec.FastPathDescribedTypeConstructor;
23 import hunt.proton.codec.MapType;
24 import hunt.proton.codec.TypeEncoding;
25 import hunt.proton.codec.WritableBuffer;
26 import hunt.proton.codec.messaging.FooterType;
27 import std.concurrency : initOnce;
28 import hunt.Exceptions;
29 import hunt.collection.Map;
30 import hunt.String;
31 
32 class FastPathFooterType : AMQPType!(Footer), FastPathDescribedTypeConstructor!(Footer) {
33 
34     private static byte DESCRIPTOR_CODE = 0x78;
35 
36     //private static Object[] DESCRIPTORS =
37     //{
38     //    UnsignedLong.valueOf(DESCRIPTOR_CODE), Symbol.valueOf("amqp:footer:map"),
39     //};
40 
41     static Object[]  DESCRIPTORS() {
42         __gshared Object[]  inst;
43         return initOnce!inst([UnsignedLong.valueOf(DESCRIPTOR_CODE), Symbol.valueOf("amqp:footer:map")]);
44     }
45 
46     private FooterType footerType;
47 
48     this(EncoderImpl encoder) {
49         this.footerType = new FooterType(encoder);
50     }
51 
52     public EncoderImpl getEncoder() {
53         return footerType.getEncoder();
54     }
55 
56     public DecoderImpl getDecoder() {
57         return footerType.getDecoder();
58     }
59 
60     public bool encodesJavaPrimitive() {
61         return false;
62     }
63 
64     public TypeInfo getTypeClass() {
65         return typeid(Footer);
66     }
67 
68     public ITypeEncoding getEncoding(Object val) {
69         return footerType.getEncoding(cast(Footer)val);
70     }
71 
72     override
73     public TypeEncoding!(Footer) getCanonicalEncoding() {
74         return footerType.getCanonicalEncoding();
75     }
76 
77     override
78     public  Collection!(TypeEncoding!(Footer)) getAllEncodings() {
79         return footerType.getAllEncodings();
80     }
81 
82     override
83     public Footer readValue() {
84         return new Footer(cast(Map!(String,Object))(getDecoder().readMap()));
85        // implementationMissing(false);
86     }
87 
88     public void skipValue() {
89         //implementationMissing( false);
90         getDecoder().readConstructor().skipValue();
91     }
92 
93     override
94     public void write(Object v) {
95         Footer val = cast(Footer)v;
96         WritableBuffer buffer = getEncoder().getBuffer();
97 
98         buffer.put(EncodingCodes.DESCRIBED_TYPE_INDICATOR);
99         buffer.put(EncodingCodes.SMALLULONG);
100         buffer.put(DESCRIPTOR_CODE);
101 
102         //implementationMissing(false);
103         MapType mapType = cast(MapType) getEncoder().getType(cast(Object)(val.getValue()));
104 
105         mapType.write(cast(Object)(val.getValue()));
106     }
107 
108     public static void register(Decoder decoder, EncoderImpl encoder) {
109         FastPathFooterType type = new FastPathFooterType(encoder);
110         //implementationMissing( false);
111         foreach(Object descriptor ; DESCRIPTORS)
112         {
113             decoder.registerFastPath(descriptor, type);
114         }
115         encoder.register(type);
116     }
117 }