This commit is contained in:
Edward Capriolo
2016-04-29 13:35:51 -04:00
parent a040e2cd36
commit a7f90aae08
18 changed files with 674 additions and 356 deletions

View File

@ -24,158 +24,176 @@ import org.json.JSONObject;
/**
* A abstract class representing a gossip member.
*
*
* @author joshclemm, harmenw
*/
public abstract class GossipMember implements Comparable<GossipMember>{
public abstract class GossipMember implements Comparable<GossipMember> {
public static final String JSON_HOST = "host";
public static final String JSON_PORT = "port";
public static final String JSON_HEARTBEAT = "heartbeat";
public static final String JSON_ID = "id";
public static final String JSON_CLUSTER = "cluster";
protected final String _host;
protected final int _port;
protected volatile long _heartbeat;
protected final String _clusterName;
/**
* The purpose of the id field is to be able for nodes to identify themselves beyond there 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;
public static final String JSON_HOST = "host";
/**
* Constructor.
* @param host The hostname or IP address.
* @param port The port number.
* @param heartbeat The current heartbeat.
* @param id an id that may be replaced after contact
*/
public GossipMember(String clusterName, String host, int port, String id, long heartbeat) {
_clusterName = clusterName;
_host = host;
_port = port;
_id = id;
_heartbeat = heartbeat;
}
public static final String JSON_PORT = "port";
/**
* Get the name of the cluster the member belongs to.
*
* @return The cluster name
*/
public String getClusterName(){
return _clusterName;
}
public static final String JSON_HEARTBEAT = "heartbeat";
/**
* Get the hostname or IP address of the remote gossip member.
* @return The hostname or IP address.
*/
public String getHost() {
return _host;
}
public static final String JSON_ID = "id";
/**
* Get the port number of the remote gossip member.
* @return The port number.
*/
public int getPort() {
return _port;
}
public static final String JSON_CLUSTER = "cluster";
/**
* The member address in the form IP/host:port
* Similar to the toString in {@link InetSocketAddress}
*/
public String getAddress() {
return _host+":"+_port;
}
protected final String host;
/**
* Get the heartbeat of this gossip member.
* @return The current heartbeat.
*/
public long getHeartbeat() {
return _heartbeat;
}
protected final int port;
/**
* Set the heartbeat of this gossip member.
* @param heartbeat The new heartbeat.
*/
public void setHeartbeat(long heartbeat) {
this._heartbeat = heartbeat;
}
protected volatile long heartbeat;
protected final String clusterName;
public String getId() {
return _id;
/**
* The purpose of the id field is to be able for nodes to identify themselves beyond there
* 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;
/**
* Constructor.
*
* @param host
* The hostname or IP address.
* @param port
* The port number.
* @param heartbeat
* The current heartbeat.
* @param id
* an id that may be replaced after contact
*/
public GossipMember(String clusterName, String host, int port, String id, long heartbeat) {
this.clusterName = clusterName;
this.host = host;
this.port = port;
this.id = id;
this.heartbeat = heartbeat;
}
/**
* Get the name of the cluster the member belongs to.
*
* @return The cluster name
*/
public String getClusterName() {
return clusterName;
}
/**
* Get the hostname or IP address of the remote gossip member.
*
* @return The hostname or IP address.
*/
public String getHost() {
return host;
}
/**
* Get the port number of the remote gossip member.
*
* @return The port number.
*/
public int getPort() {
return port;
}
/**
* The member address in the form IP/host:port Similar to the toString in
* {@link InetSocketAddress}
*/
public String getAddress() {
return host + ":" + port;
}
/**
* 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;
this.id = _id;
}
public String toString() {
return "Member [address=" + getAddress() + ", id=" + _id + ", heartbeat=" + _heartbeat + "]";
}
public String toString() {
return "Member [address=" + getAddress() + ", id=" + id + ", heartbeat=" + heartbeat + "]";
}
/**
* @see java.lang.Object#hashCode()
*/
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
String address = getAddress();
result = prime * result
+ ((address == null) ? 0 : address.hashCode())
+ _clusterName == null ? 0 : _clusterName.hashCode();
return result;
}
/**
* @see java.lang.Object#hashCode()
*/
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
String address = getAddress();
result = prime * result + ((address == null) ? 0 : address.hashCode()) + clusterName == null ? 0
: clusterName.hashCode();
return result;
}
/**
* @see java.lang.Object#equals(java.lang.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 GossipMember) ) {
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 getAddress().equals(((LocalGossipMember) obj).getAddress()) &&
getClusterName().equals(((LocalGossipMember) obj).getClusterName());
}
/**
* @see java.lang.Object#equals(java.lang.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 GossipMember)) {
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 getAddress().equals(((LocalGossipMember) obj).getAddress())
&& getClusterName().equals(((LocalGossipMember) obj).getClusterName());
}
/**
* Get the JSONObject which is the JSON representation of this GossipMember.
* @return The JSONObject of this GossipMember.
*/
public JSONObject toJSONObject() {
try {
JSONObject jsonObject = new JSONObject();
jsonObject.put(JSON_CLUSTER, _clusterName);
jsonObject.put(JSON_HOST, _host);
jsonObject.put(JSON_PORT, _port);
jsonObject.put(JSON_ID, _id);
jsonObject.put(JSON_HEARTBEAT, _heartbeat);
return jsonObject;
} catch (JSONException e) {
throw new RuntimeException(e);
}
}
/**
* Get the JSONObject which is the JSON representation of this GossipMember.
*
* @return The JSONObject of this GossipMember.
*/
public JSONObject toJSONObject() {
try {
JSONObject jsonObject = new JSONObject();
jsonObject.put(JSON_CLUSTER, clusterName);
jsonObject.put(JSON_HOST, host);
jsonObject.put(JSON_PORT, port);
jsonObject.put(JSON_ID, id);
jsonObject.put(JSON_HEARTBEAT, heartbeat);
return jsonObject;
} catch (JSONException e) {
throw new RuntimeException(e);
}
}
public int compareTo(GossipMember other){
return this.getAddress().compareTo(other.getAddress());
}
public int compareTo(GossipMember other) {
return this.getAddress().compareTo(other.getAddress());
}
}