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.GossipMember;
16
17 import hunt.io.Common;
18 import hunt.Integer;
19 import hunt.gossip.model.GossipState;
20 import std.conv;
21 import std.json;
22
23 public class GossipMember : Serializable {
24 private string cluster;
25 private string ipAddress;
26 private Integer port;
27 private string id;
28 private GossipState state;
29
30 public this() {
31 }
32
33 public this(string cluster, string ipAddress, Integer port, string id, GossipState state) {
34 this.cluster = cluster;
35 this.ipAddress = ipAddress;
36 this.port = port;
37 this.id = id;
38 this.state = state;
39 }
40
41 public GossipState getState() {
42 return state;
43 }
44
45 public void setState(GossipState state) {
46 this.state = state;
47 }
48
49 public string getCluster() {
50 return cluster;
51 }
52
53 public void setCluster(string cluster) {
54 this.cluster = cluster;
55 }
56
57 public string getIpAddress() {
58 return ipAddress;
59 }
60
61 public void setIpAddress(string ipAddress) {
62 this.ipAddress = ipAddress;
63 }
64
65 public Integer getPort() {
66 return port;
67 }
68
69 public void setPort(Integer port) {
70 this.port = port;
71 }
72
73 public string getId() {
74 if (id is null) {
75 setId(ipAndPort());
76 }
77 return id;
78 }
79
80 public void setId(string id) {
81 this.id = id;
82 }
83
84 override
85 public string toString() {
86 return "GossipMember{" ~
87 "cluster='" ~ cluster ~ '\'' ~
88 ", ipAddress='" ~ ipAddress ~ '\'' ~
89 ", port=" ~ port.to!string ~
90 ", id='" ~ id ~ '\'' ~
91 ", state=" ~ state.state() ~
92 '}';
93 }
94
95 override
96 public bool opEquals(Object o) {
97 if (this is o) return true;
98 if (o is null) return false;
99
100 GossipMember member = cast(GossipMember) o;
101 if(member is null) return false;
102 if (!(cluster == member.cluster)) return false;
103 if (!(ipAddress == member.ipAddress)) return false;
104 return port.intValue() == member.port.intValue();
105 }
106
107 override
108 public size_t toHash() @trusted nothrow {
109 size_t result = cluster.hashOf();
110 result = 31 * result + ipAddress.hashOf();
111 result = 31 * result + port.hashOf();
112 return result;
113 }
114
115 public string ipAndPort() {
116 return ipAddress ~ (":") ~ (to!string(port));
117 }
118
119 public string eigenvalue(){
120 return getCluster() ~ (":") ~ (getIpAddress()) ~ (":") ~ (getPort().toString());
121 }
122
123 public JSONValue encode()
124 {
125 JSONValue data;
126 data["cluster"] = cluster;
127 data["ipAddress"] = ipAddress;
128 data["port"] = port.intValue();
129 data["id"] = id;
130 data["state"] = state.state();
131 return data;
132 }
133
134 public static GossipMember decode(JSONValue data)
135 {
136 try
137 {
138 GossipMember gm = new GossipMember(data["cluster"].str,data["ipAddress"].str,new Integer(cast(int)(data["port"].integer)),data["id"].str,GossipState(data["state"].str));
139 return gm;
140 }catch(Exception e)
141 {}
142 return null;
143 }
144 }