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