1 /*
2  * hunt-amqp: AMQP library for D programming language, based on hunt-net.
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 module hunt.amqp.ProtonTransportOptions;
13 
14 //import io.vertx.codegen.annotations.DataObject;
15 //import io.vertx.core.json.JsonObject;
16 import std.json;
17 import hunt.amqp.generated.ProtonTransportOptionsConverter;
18 import hunt.collection.Map;
19 import hunt.collection.LinkedHashMap;
20 import std.variant;
21 /**
22  * Options for configuring transport layer
23  */
24 //@DataObject(generateConverter = true, publicConverter = false)
25 class ProtonTransportOptions {
26 
27   private int heartbeat;
28   private int maxFrameSize;
29 
30   this() {
31   }
32 
33   /**
34    * Create options from JSON
35    *
36    * @param json  the JSON
37    */
38   this(JSONValue json) {
39     Map!(string, Variant) mp = new LinkedHashMap!(string,Variant)();
40     foreach (string key, value; json)
41     {
42       Variant tmp = value;
43       mp.put(key,tmp);
44     }
45     ProtonTransportOptionsConverter.fromJson(mp, this);
46   }
47 
48   /**
49    * Convert to JSON
50    *
51    * @return the JSON
52    */
53   public JSONValue toJson() {
54     JSONValue json ;
55     ProtonTransportOptionsConverter.toJson(this, json);
56     return json;
57   }
58 
59   /**
60    * Set the heart beat as maximum delay between sending frames for the remote peers.
61    * If no frames are received within 2 * heart beat, the connection is closed
62    *
63    * @param heartbeat The maximum delay in milliseconds.
64    * @return current ProtonTransportOptions instance.
65    */
66   public ProtonTransportOptions setHeartbeat(int heartbeat) {
67     this.heartbeat = heartbeat;
68     return this;
69   }
70 
71   /**
72    * Returns the heart beat as maximum delay between sending frames for the remote peers.
73    *
74    * @return The maximum delay in milliseconds.
75    */
76   public int getHeartbeat() {
77     return this.heartbeat;
78   }
79 
80   /**
81    * Sets the maximum frame size for the connection.
82    * <p>
83    * If this property is not set explicitly, a reasonable default value is used.
84    * <p>
85    * Setting this property to a negative value will result in no maximum frame size being announced at all.
86    *
87    * @param maxFrameSize The frame size in bytes.
88    * @return This instance for setter chaining.
89    */
90   public ProtonTransportOptions setMaxFrameSize(int maxFrameSize) {
91     if (maxFrameSize < 0) {
92       this.maxFrameSize = -1;
93     } else {
94       this.maxFrameSize = maxFrameSize;
95     }
96     return this;
97   }
98 
99   /**
100    * Gets the maximum frame size for the connection.
101    * <p>
102    * If this property is not set explicitly, a reasonable default value is used.
103    *
104    * @return The frame size in bytes or -1 if no limit is set.
105    */
106   public int getMaxFrameSize() {
107     return maxFrameSize;
108   }
109 
110   override size_t toHash() @safe nothrow
111   {
112     int prime = 31;
113     int result = 1;
114     result = prime * result + heartbeat;
115     result = prime * result + maxFrameSize;
116     return cast(size_t)result;
117   }
118 
119   override
120   public bool opEquals (Object obj) {
121     if (this == obj) {
122       return true;
123     }
124 
125     if (obj is null ){
126       return false;
127     }
128 
129     ProtonTransportOptions other = cast(ProtonTransportOptions) obj;
130     if (this.heartbeat != other.heartbeat) {
131       return false;
132     }
133     if (this.maxFrameSize != other.maxFrameSize) {
134       return false;
135     }
136 
137     return true;
138   }
139 }