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.GossipDigest; 16 17 import hunt.io.Common; 18 import hunt.util.Common; 19 import hunt.gossip.Common; 20 import hunt.gossip.model.GossipMember; 21 import std.conv; 22 import std.json; 23 24 public class GossipDigest : Serializable, Comparable!(GossipDigest) { 25 26 alias opCmp = Object.opCmp; 27 28 private InetSocketAddress endpoint; 29 private long heartbeatTime; 30 private long _version; 31 private string id; 32 33 // override 34 public int compareTo(GossipDigest o) { 35 if (heartbeatTime != o.heartbeatTime) { 36 return cast(int) (heartbeatTime - o.heartbeatTime); 37 } 38 return cast(int) (_version - o._version); 39 } 40 41 public int opCmp(GossipDigest o) 42 { 43 return compareTo(o); 44 } 45 46 public this() { 47 } 48 49 public this(GossipMember endpoint, long heartbeatTime, long _version) /* throws UnknownHostException */ { 50 this.endpoint = new InetSocketAddress(endpoint.getIpAddress(), endpoint.getPort().intValue()); 51 this.heartbeatTime = heartbeatTime; 52 this._version = _version; 53 this.id = endpoint.getId(); 54 } 55 56 public InetSocketAddress getEndpoint() { 57 return endpoint; 58 } 59 60 public void setEndpoint(InetSocketAddress endpoint) { 61 this.endpoint = endpoint; 62 } 63 64 public long getHeartbeatTime() { 65 return heartbeatTime; 66 } 67 68 public void setHeartbeatTime(long heartbeatTime) { 69 this.heartbeatTime = heartbeatTime; 70 } 71 72 public long getVersion() { 73 return _version; 74 } 75 76 public void setVersion(long _version) { 77 this._version = _version; 78 } 79 80 public string getId() { 81 return id; 82 } 83 84 public void setId(string id) { 85 this.id = id; 86 } 87 88 override 89 public string toString() { 90 return "GossipDigest{" ~ 91 "endpoint=" ~ endpoint.toString() ~ 92 ", heartbeatTime=" ~ heartbeatTime.to!string ~ 93 ", _version=" ~ _version.to!string ~ 94 '}'; 95 } 96 97 public JSONValue encode() 98 { 99 JSONValue data; 100 data["heartbeatTime"] = heartbeatTime; 101 data["version"] = _version; 102 data["id"] = id; 103 JSONValue endpoint; 104 endpoint["ip"] = this.endpoint.getIp(); 105 endpoint["port"] = this.endpoint.getPort(); 106 data["endpoint"] = endpoint; 107 return data; 108 } 109 110 public static GossipDigest decode(JSONValue data) 111 { 112 try 113 { 114 GossipDigest gd = new GossipDigest(); 115 gd.setHeartbeatTime(data["heartbeatTime"].integer); 116 gd.setVersion(data["version"].integer); 117 gd.setId(data["id"].str); 118 gd.setEndpoint(new InetSocketAddress(data["endpoint"]["ip"].str,cast(int)(data["endpoint"]["port"].integer))); 119 return gd; 120 }catch(Exception e) 121 {} 122 return null; 123 } 124 }