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