Repository navigation
Expand file tree
/
Copy pathMember.java
More file actions
executable file
·165 lines (140 loc) · 4.21 KB
/
Copy pathMember.java
File metadata and controls
executable file
·165 lines (140 loc) · 4.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.tron.gossip;
import java.net.InetSocketAddress;
import java.net.URI;
import java.util.Map;
/**
* An abstract class representing a gossip member.
*/
public abstract class Member implements Comparable<Member> {
protected URI uri;
protected volatile long heartbeat;
protected String clusterName;
/**
* The purpose of the id field is to be able for nodes to identify themselves beyond their
* host/port. For example an application might generate a persistent id so if they rejoin the
* cluster at a different host and port we are aware it is the same node.
*/
protected String id;
/* properties provided at startup time */
protected Map<String, String> properties;
/**
* Constructor.
*
* @param clusterName The name of the cluster
* @param uri A URI object containing IP/hostname and port
* @param heartbeat The current heartbeat
* @param id An id that may be replaced after contact
*/
public Member(String clusterName, URI uri, String id, long heartbeat,
Map<String, String> properties) {
this.clusterName = clusterName;
this.id = id;
this.heartbeat = heartbeat;
this.uri = uri;
this.properties = properties;
}
protected Member() {
}
/**
* Get the name of the cluster the member belongs to.
*
* @return The cluster name
*/
public String getClusterName() {
return clusterName;
}
/**
* @return The member address in the form IP/host:port Similar to the toString in
* {@link InetSocketAddress}
*/
public String computeAddress() {
return uri.getHost() + ":" + uri.getPort();
}
/**
* Get the heartbeat of this gossip member.
*
* @return The current heartbeat.
*/
public long getHeartbeat() {
return heartbeat;
}
/**
* Set the heartbeat of this gossip member.
*
* @param heartbeat The new heartbeat.
*/
public void setHeartbeat(long heartbeat) {
this.heartbeat = heartbeat;
}
public String getId() {
return id;
}
public void setId(String _id) {
this.id = _id;
}
public Map<String, String> getProperties() {
return properties;
}
public void setProperties(Map<String, String> properties) {
this.properties = properties;
}
public String toString() {
return "Member [address=" + computeAddress() + ", id=" + id + ", heartbeat=" + heartbeat + "]";
}
/**
* @see Object#hashCode()
*/
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
String address = computeAddress();
result =
prime * result + ((address == null) ? 0 : address.hashCode()) + (clusterName == null ? 0
: clusterName.hashCode());
return result;
}
public URI getUri() {
return uri;
}
/**
* @see Object#equals(Object)
*/
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
System.err.println("equals(): obj is null.");
return false;
}
if (!(obj instanceof Member)) {
System.err.println("equals(): obj is not of type GossipMember.");
return false;
}
// The object is the same of they both have the same address (hostname and port).
return computeAddress().equals(((LocalMember) obj).computeAddress())
&& getClusterName().equals(((LocalMember) obj).getClusterName());
}
public int compareTo(Member other) {
return this.computeAddress().compareTo(other.computeAddress());
}
}