1 2 package org.jgroups; 3 4 import org.jgroups.util.Streamable; 5 import org.jgroups.util.Util; 6 7 import java.io.*; 8 9 10 /** 11 * ViewIds are used for ordering views (each view has a ViewId and a list of members). 12 * Ordering between views is important for example in a virtual synchrony protocol where 13 * all views seen by a member have to be ordered. 14 */ 15 public class ViewId implements Externalizable, Comparable, Cloneable, Streamable { 16 Address coord_addr=null; // Address of the issuer of this view 17 long id=0; // Lamport time of the view 18 19 ViewId()20 public ViewId() { // used for externalization 21 } 22 23 24 /** 25 * Creates a ViewID with the coordinator address and a Lamport timestamp of 0. 26 * 27 * @param coord_addr the address of the member that issued this view 28 */ ViewId(Address coord_addr)29 public ViewId(Address coord_addr) { 30 this.coord_addr=coord_addr; 31 } 32 33 /** 34 * Creates a ViewID with the coordinator address and the given Lamport timestamp. 35 * 36 * @param coord_addr - the address of the member that issued this view 37 * @param id - the Lamport timestamp of the view 38 */ ViewId(Address coord_addr, long id)39 public ViewId(Address coord_addr, long id) { 40 this.coord_addr=coord_addr; 41 this.id=id; 42 } 43 44 /** 45 * returns the lamport time of the view 46 * 47 * @return the lamport time timestamp 48 */ getId()49 public long getId() { 50 return id; 51 } 52 53 54 /** 55 * returns the address of the member that issued this view 56 * 57 * @return the Address of the the issuer 58 */ getCoordAddress()59 public Address getCoordAddress() { 60 return coord_addr; 61 } 62 63 toString()64 public String toString() { 65 return "[" + coord_addr + '|' + id + ']'; 66 } 67 68 /** 69 * Cloneable interface 70 * Returns a new ViewID object containing the same address and lamport timestamp as this view 71 */ clone()72 public Object clone() { 73 return new ViewId(coord_addr, id); 74 } 75 76 /** 77 * Old Copy method, deprecated because it is substituted by clone() 78 */ copy()79 public ViewId copy() { 80 return (ViewId)clone(); 81 } 82 83 /** 84 * Establishes an order between 2 ViewIds. First compare on id. <em>Compare on coord_addr 85 * only if necessary</em> (i.e. ids are equal) ! 86 * 87 * @return 0 for equality, value less than 0 if smaller, greater than 0 if greater. 88 */ compareTo(Object other)89 public int compareTo(Object other) { 90 if(other == null) return 1; //+++ Maybe necessary to throw an exception 91 92 if(!(other instanceof ViewId)) { 93 throw new ClassCastException("ViewId.compareTo(): view id is not comparable with different Objects"); 94 } 95 return id > ((ViewId)other).id ? 1 : id < ((ViewId)other).id ? -1 : 0; 96 } 97 98 /** 99 * Old Compare 100 */ compare(Object o)101 public int compare(Object o) { 102 return compareTo(o); 103 } 104 105 equals(Object other_view)106 public boolean equals(Object other_view) { 107 return compareTo(other_view) == 0; 108 } 109 110 hashCode()111 public int hashCode() { 112 return (int)id; 113 } 114 115 writeExternal(ObjectOutput out)116 public void writeExternal(ObjectOutput out) throws IOException { 117 out.writeObject(coord_addr); 118 out.writeLong(id); 119 } 120 121 readExternal(ObjectInput in)122 public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException { 123 coord_addr=(Address)in.readObject(); 124 id=in.readLong(); 125 } 126 writeTo(DataOutputStream out)127 public void writeTo(DataOutputStream out) throws IOException { 128 Util.writeAddress(coord_addr, out); 129 out.writeLong(id); 130 } 131 readFrom(DataInputStream in)132 public void readFrom(DataInputStream in) throws IOException, IllegalAccessException, InstantiationException { 133 coord_addr=Util.readAddress(in); 134 id=in.readLong(); 135 } 136 serializedSize()137 public int serializedSize() { 138 int retval=Global.LONG_SIZE; // for the id 139 retval+=Util.size(coord_addr); 140 return retval; 141 } 142 143 } 144