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.HeartbeatState; 16 17 import hunt.gossip.VersionHelper; 18 import hunt.util.DateTime; 19 import std.conv; 20 import std.json; 21 22 public class HeartbeatState { 23 private long heartbeatTime; 24 private long _version; 25 26 public long getHeartbeatTime() { 27 return heartbeatTime; 28 } 29 30 public void setHeartbeatTime(long heartbeatTime) { 31 this.heartbeatTime = heartbeatTime; 32 } 33 34 public long getVersion() { 35 return _version; 36 } 37 38 public void setVersion(long _version) { 39 this._version = _version; 40 } 41 42 public this() { 43 this.heartbeatTime = DateTimeHelper.currentTimeMillis(); 44 this._version = VersionHelper.getInstance().nextVersion(); 45 } 46 47 public long updateVersion() { 48 setHeartbeatTime(DateTimeHelper.currentTimeMillis()); 49 this._version = VersionHelper.getInstance().nextVersion(); 50 return _version; 51 } 52 53 override 54 public string toString() { 55 return "HeartbeatState{" ~ 56 "heartbeatTime=" ~ heartbeatTime.to!string ~ 57 ", version=" ~ _version.to!string ~ 58 '}'; 59 } 60 61 public JSONValue encode() 62 { 63 JSONValue data; 64 data["heartbeatTime"] = heartbeatTime; 65 data["version"] = _version; 66 return data; 67 } 68 69 public static HeartbeatState decode(JSONValue data) 70 { 71 try 72 { 73 HeartbeatState hs = new HeartbeatState(); 74 hs.setHeartbeatTime(data["heartbeatTime"].integer); 75 hs.setVersion(data["version"].integer); 76 return hs; 77 }catch(Exception e) 78 {} 79 return null; 80 } 81 }