1 // Copyright (C) 2018-2019 HuntLabs. All rights reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 module hunt.gossip.model.AckMessage;
16 
17 import hunt.io.Common;
18 import hunt.collection.List;
19 import hunt.collection.ArrayList;
20 import hunt.collection.Map;
21 import hunt.collection.HashMap;
22 import hunt.gossip.model.GossipDigest;
23 import hunt.gossip.model.GossipMember;
24 import hunt.gossip.model.HeartbeatState;
25 import std.json;
26 
27 
28 public class AckMessage : Serializable {
29     private List!(GossipDigest) olders;
30 
31     private Map!(GossipMember, HeartbeatState) newers;
32 
33     public this() {
34     }
35 
36     public this(List!(GossipDigest) olders, Map!(GossipMember, HeartbeatState) newers) {
37         this.olders = olders;
38         this.newers = newers;
39     }
40 
41     public List!(GossipDigest) getOlders() {
42         return olders;
43     }
44 
45     public void setOlders(List!(GossipDigest) olders) {
46         this.olders = olders;
47     }
48 
49     public Map!(GossipMember, HeartbeatState) getNewers() {
50         return newers;
51     }
52 
53     public void setNewers(Map!(GossipMember, HeartbeatState) newers) {
54         this.newers = newers;
55     }
56 
57     override
58     public string toString() {
59         return "AckMessage{" ~
60                 "olders=" ~ olders.toString ~
61                 ", newers=" ~ newers.toString ~
62                 '}';
63     }
64 
65     public JSONValue encode()
66     {
67         JSONValue data;
68         JSONValue[] olders;
69         foreach(old; this.olders) {
70             olders ~= old.encode();
71         }
72         JSONValue newers;
73         foreach(GossipMember k, HeartbeatState v; this.newers) {
74             newers[k.encode.toString] = v.encode();
75         }
76         data["olders"] = JSONValue(olders);
77         data["newers"] = newers;
78         return data;
79     }
80 
81     public static AckMessage decode(JSONValue data)
82     {
83         try
84         {
85             List!(GossipDigest) olders = new ArrayList!(GossipDigest)();
86             foreach(value; data["olders"].array) {
87                 olders.add(GossipDigest.decode(value));
88             }
89             
90             Map!(GossipMember, HeartbeatState) newers = new HashMap!(GossipMember, HeartbeatState)();
91             foreach(string k, JSONValue v; data["newers"]) {
92                 newers.put(GossipMember.decode(parseJSON(k)),HeartbeatState.decode(v));
93             }
94             AckMessage ackm = new AckMessage(olders,newers);
95             return ackm;
96         }catch(Exception e)
97         {}
98         return null;
99     }
100 
101 }