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.Ack2Message;
16 
17 import hunt.io.Common;
18 import hunt.collection.Map;
19 import hunt.collection.HashMap;
20 import hunt.gossip.model.GossipMember;
21 import hunt.gossip.model.HeartbeatState;
22 import std.json;
23 
24 public class Ack2Message : Serializable {
25 
26     private Map!(GossipMember, HeartbeatState) endpoints;
27 
28     public this() {
29     }
30 
31     public this(Map!(GossipMember, HeartbeatState) endpoints) {
32 
33         this.endpoints = endpoints;
34     }
35 
36     override
37     public string toString() {
38         return "GossipDigestAck2Message{" ~
39                 "endpoints=" ~ endpoints.toString ~
40                 '}';
41     }
42 
43     public Map!(GossipMember, HeartbeatState) getEndpoints() {
44         return endpoints;
45     }
46 
47     public void setEndpoints(Map!(GossipMember, HeartbeatState) endpoints) {
48         this.endpoints = endpoints;
49     }
50 
51     public JSONValue encode()
52     {
53         JSONValue data;
54         foreach(GossipMember k, HeartbeatState v; endpoints) {
55             data[k.encode.toString] = v.encode();
56         }
57         return data;
58     }
59 
60     public static Ack2Message decode(JSONValue data)
61     {
62         try
63         {
64             Map!(GossipMember, HeartbeatState) es = new HashMap!(GossipMember, HeartbeatState)();
65             foreach(string k, JSONValue v; data) {
66                 es.put(GossipMember.decode(parseJSON(k)),HeartbeatState.decode(v));
67             }
68             Ack2Message ack2m = new Ack2Message(es);
69             return ack2m;
70         }catch(Exception e)
71         {}
72         return null;
73     }
74 }