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.transaction.TransactionalStateType; 14 15 import hunt.collection.AbstractList; 16 import hunt.collection.List; 17 import hunt.proton.amqp.Binary; 18 import hunt.proton.amqp.Symbol; 19 import hunt.proton.amqp.UnsignedLong; 20 import hunt.proton.amqp.messaging.Outcome; 21 import hunt.proton.amqp.transaction.TransactionalState; 22 import hunt.proton.codec.AbstractDescribedType; 23 import hunt.proton.codec.DecodeException; 24 import hunt.proton.codec.Decoder; 25 import hunt.proton.codec.DescribedTypeConstructor; 26 import hunt.proton.codec.EncoderImpl; 27 import hunt.Object; 28 import hunt.logging; 29 import hunt.Exceptions; 30 import std.conv : to; 31 32 import std.concurrency : initOnce; 33 34 35 class TransactionalStateWrapper : AbstractList!Object 36 { 37 38 private TransactionalState _transactionalState; 39 40 this(TransactionalState transactionalState) 41 { 42 _transactionalState = transactionalState; 43 } 44 45 override 46 public Object get(int index) 47 { 48 49 switch(index) 50 { 51 case 0: 52 return _transactionalState.getTxnId(); 53 case 1: 54 return cast(Object)(_transactionalState.getOutcome()); 55 default: 56 return null; 57 58 } 59 60 61 // throw new IllegalStateException("Unknown index " ~ to!string(index)); 62 63 } 64 65 override 66 public int size() 67 { 68 return _transactionalState.getOutcome() !is null 69 ? 2 70 : 1; 71 72 } 73 74 75 } 76 77 class TransactionalStateType : AbstractDescribedType!(TransactionalState,IObject) , DescribedTypeConstructor!(TransactionalState) 78 { 79 //private static Object[] DESCRIPTORS = 80 //{ 81 // UnsignedLong.valueOf(0x0000000000000034L), Symbol.valueOf("amqp:transactional-state:list"), 82 //}; 83 // 84 //private static UnsignedLong DESCRIPTOR = UnsignedLong.valueOf(0x0000000000000034L); 85 86 static Object[] DESCRIPTORS() { 87 __gshared Object[] inst; 88 return initOnce!inst([UnsignedLong.valueOf(0x0000000000000034L), Symbol.valueOf("amqp:transactional-state:list")]); 89 } 90 91 static UnsignedLong DESCRIPTOR() { 92 __gshared UnsignedLong inst; 93 return initOnce!inst(UnsignedLong.valueOf(0x0000000000000034L)); 94 } 95 96 this(EncoderImpl encoder) 97 { 98 super(encoder); 99 } 100 101 override 102 public UnsignedLong getDescriptor() 103 { 104 return DESCRIPTOR; 105 } 106 107 override 108 protected List!Object wrap(TransactionalState val) 109 { 110 return new TransactionalStateWrapper(val); 111 } 112 113 114 115 public TransactionalState newInstance(Object described) 116 { 117 List!Object l = cast(List!Object) described; 118 119 TransactionalState o = new TransactionalState(); 120 121 if(l.isEmpty()) 122 { 123 logError("The txn-id field cannot be omitted"); 124 //throw new DecodeException("The txn-id field cannot be omitted"); 125 } 126 127 switch(2 - l.size()) 128 { 129 130 case 0: 131 o.setOutcome( cast(Outcome) l.get( 1 ) ); 132 goto case; 133 case 1: 134 o.setTxnId( cast(Binary) l.get( 0 ) ); 135 break; 136 default: 137 break; 138 } 139 140 141 return o; 142 } 143 144 public TypeInfo getTypeClass() 145 { 146 return typeid(TransactionalState); 147 } 148 149 150 151 public static void register(Decoder decoder, EncoderImpl encoder) 152 { 153 TransactionalStateType type = new TransactionalStateType(encoder); 154 //implementationMissing(false); 155 foreach(Object descriptor ; DESCRIPTORS) 156 { 157 decoder.registerDynamic(descriptor, type); 158 } 159 encoder.register(type); 160 } 161 } 162