PageRenderTime 38ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/gemfire-jgroups/src/main/java/com/gemstone/org/jgroups/stack/LogicalAddress.java

https://gitlab.com/kidaa/incubator-geode
Java | 370 lines | 224 code | 78 blank | 68 comment | 43 complexity | fde8c2c0ac443e6ab7e5f343a10f1f52 MD5 | raw file
  1. /** Notice of modification as required by the LGPL
  2. * This file was modified by Gemstone Systems Inc. on
  3. * $Date$
  4. **/
  5. // $Id: LogicalAddress.java,v 1.9 2005/07/17 11:34:20 chrislott Exp $
  6. package com.gemstone.org.jgroups.stack;
  7. import com.gemstone.org.jgroups.Address;
  8. import com.gemstone.org.jgroups.util.Util;
  9. import java.io.*;
  10. import java.net.InetAddress;
  11. import java.net.SocketAddress;
  12. import java.util.ArrayList;
  13. import java.util.Date;
  14. import java.util.List;
  15. /**
  16. * Logical address that spans the lifetime of a member. Assigned at member (JVM) startup, and
  17. * retained until member is shutdown. Note that the address does <em>not</em> change on
  18. * disconnect-connect sequences. For example, when a member is shunned and subsequently
  19. * readmitted to the group, the member's address (LogicalAddress) remains the same.<br/>
  20. * An instance of LogicalAddress is generated by the transport protocol. Currently, only
  21. * UDP_NIO generates LogicalAddresses.<br/>
  22. * Note that host, timestamp and id are supposed to make LogicalAddress as unique as possible.
  23. * However, there is a remote chance that 2 instances started on the same machine create their
  24. * address at exactly the same time, resulting in identical addresses (leading to problems).
  25. * In the future, I will try to make this totally unique, by for example using the PID of the current
  26. * process (once available though the JDK, or by locking on a common resource (e.g. /dev/random)
  27. * to serialize creation. However, as for now, chances are you will never experience this problem.
  28. * @author Bela Ban, Dec 23 2003
  29. */
  30. public class LogicalAddress implements Address {
  31. static int count=1;
  32. protected String host=null;
  33. protected long timestamp=0;
  34. protected int id=0;
  35. protected boolean multicast_addr=false;
  36. // GemStoneAddition
  37. public boolean preferredForCoordinator() {
  38. return true;
  39. }
  40. public boolean splitBrainEnabled() {
  41. return false;
  42. }
  43. @Override
  44. public int getBirthViewId() {
  45. return -1;
  46. }
  47. @Override
  48. public short getVersionOrdinal() {
  49. return -1;
  50. }
  51. /** Address of the primary physical address. This is set to the sender when a message is received.
  52. * If this field is set, we will send unicast messages only to this address, not to all addresses listed
  53. * in physical_addrs; this reduces the number of msgs we have to send.<br/>
  54. * Note that this field is not shipped across the wire.
  55. */
  56. transient SocketAddress primary_physical_addr=null;
  57. /** List<SocketAddress> of physical addresses */
  58. protected ArrayList physical_addrs=null;
  59. /** To tack on some additional data */
  60. byte[] additional_data=null;
  61. // Used only by Externalization
  62. public LogicalAddress() {
  63. }
  64. /** Use this constructor to create an instance, not the null-constructor */
  65. public LogicalAddress(String host_name, List physical_addrs) {
  66. init(host_name, physical_addrs);
  67. }
  68. protected void init(String host_name, List physical_addrs) {
  69. if(host_name != null) {
  70. this.host=host_name;
  71. }
  72. else {
  73. try {
  74. host=InetAddress.getLocalHost().getHostName();
  75. }
  76. catch(Exception e) {
  77. host="localhost";
  78. }
  79. }
  80. timestamp=System.currentTimeMillis();
  81. synchronized(LogicalAddress.class) {
  82. id=count++;
  83. }
  84. if(physical_addrs != null) {
  85. this.physical_addrs=new ArrayList(physical_addrs);
  86. }
  87. }
  88. public String getHost() {
  89. return host;
  90. }
  91. public long getTimestamp() {
  92. return timestamp;
  93. }
  94. public long getId() {
  95. return id;
  96. }
  97. public SocketAddress getPrimaryPhysicalAddress() {
  98. return primary_physical_addr;
  99. }
  100. public void setPrimaryPhysicalAddress(SocketAddress primary_physical_addr) {
  101. this.primary_physical_addr=primary_physical_addr;
  102. }
  103. /**
  104. * Returns a <em>copy</em> of the list of physical addresses. Reason for the copy is that the list is not supposed
  105. * to be modified (should be immutable).
  106. * @return List of physical addresses (return value maybe null)
  107. */
  108. public ArrayList getPhysicalAddresses() {
  109. return physical_addrs != null? (ArrayList)physical_addrs.clone() : null;
  110. }
  111. /**
  112. * For internal use only. Don't use this method!
  113. * @param addr
  114. */
  115. public void addPhysicalAddress(SocketAddress addr) {
  116. if(addr != null) {
  117. if(physical_addrs == null)
  118. physical_addrs=new ArrayList();
  119. if(!physical_addrs.contains(addr))
  120. physical_addrs.add(addr);
  121. }
  122. }
  123. /**
  124. * For internal use only. Don't use this method !
  125. * @param addr
  126. */
  127. public void removePhysicalAddress(SocketAddress addr) {
  128. if(addr != null && physical_addrs != null)
  129. physical_addrs.remove(addr);
  130. }
  131. /**
  132. * For internal use only. Don't use this method !
  133. */
  134. public void removeAllPhysicalAddresses() {
  135. if(physical_addrs != null)
  136. physical_addrs.clear();
  137. }
  138. public boolean isMulticastAddress() {
  139. return false; // LogicalAddresses can never be multicast
  140. }
  141. public int size(short version) {
  142. return 22;
  143. }
  144. /**
  145. * Returns the additional_data.
  146. * @return byte[]
  147. */
  148. public byte[] getAdditionalData() {
  149. return additional_data;
  150. }
  151. /**
  152. * Sets the additional_data.
  153. * @param additional_data The additional_data to set
  154. */
  155. public void setAdditionalData(byte[] additional_data) {
  156. this.additional_data = additional_data;
  157. }
  158. /**
  159. * Establishes an order between 2 addresses. Assumes other contains non-null IpAddress.
  160. * Excludes channel_name from comparison.
  161. * @return 0 for equality, value less than 0 if smaller, greater than 0 if greater.
  162. */
  163. public int compare(LogicalAddress other) {
  164. return compareTo(other);
  165. }
  166. /**
  167. * implements the java.lang.Comparable interface
  168. * @see Comparable
  169. * @param o - the Object to be compared
  170. * @return a negative integer, zero, or a positive integer as this object is less than,
  171. * equal to, or greater than the specified object.
  172. * @exception ClassCastException - if the specified object's type prevents it
  173. * from being compared to this Object.
  174. */
  175. public int compareTo(Object o) {
  176. int rc;
  177. if ((o == null) || !(o instanceof LogicalAddress))
  178. throw new ClassCastException("LogicalAddress.compareTo(): comparison between different classes");
  179. LogicalAddress other = (LogicalAddress) o;
  180. rc=this.host.compareTo(other.host);
  181. if(rc != 0) return rc;
  182. if(this.timestamp != other.timestamp)
  183. return this.timestamp < other.timestamp? -1 : 1;
  184. if(this.id != other.id)
  185. return this.id < other.id? -1 : 1;
  186. return 0;
  187. }
  188. @Override // GemStoneAddition
  189. public boolean equals(Object obj) {
  190. if(obj == null) return false;
  191. if (!(obj instanceof LogicalAddress)) return false; // GemStoneAddition
  192. return compareTo(obj) == 0 ? true : false;
  193. }
  194. @Override // GemStoneAddition
  195. public int hashCode() {
  196. int retval=(int)(host.hashCode() + timestamp + id);
  197. return retval;
  198. }
  199. @Override // GemStoneAddition
  200. public String toString() {
  201. return toString(false);
  202. }
  203. public String toString(boolean print_details) {
  204. StringBuffer sb=new StringBuffer();
  205. sb.append(host);
  206. sb.append(':').append(id);
  207. if(print_details) {
  208. sb.append(" (created ").append(new Date(timestamp)).append(')');
  209. if(physical_addrs != null)
  210. sb.append("\nphysical addrs: ").append(physical_addrs);
  211. }
  212. if(additional_data != null)
  213. sb.append(" (additional data: ").append(additional_data.length).append(" bytes)");
  214. return sb.toString();
  215. }
  216. public void writeExternal(ObjectOutput out) throws IOException {
  217. out.writeObject(host);
  218. out.writeLong(timestamp);
  219. out.writeInt(id);
  220. if(physical_addrs != null) {
  221. out.writeInt(physical_addrs.size());
  222. out.writeObject(physical_addrs);
  223. }
  224. else
  225. out.writeInt(0);
  226. if(additional_data != null) {
  227. out.writeInt(additional_data.length);
  228. out.write(additional_data, 0, additional_data.length);
  229. }
  230. else
  231. out.writeInt(0);
  232. }
  233. public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
  234. int len;
  235. host=(String)in.readObject();
  236. timestamp=in.readLong();
  237. id=in.readInt();
  238. len=in.readInt();
  239. if(len > 0) {
  240. physical_addrs=(ArrayList)in.readObject();
  241. }
  242. len=in.readInt();
  243. if(len > 0) {
  244. additional_data=new byte[len];
  245. in.readFully(additional_data, 0, additional_data.length);
  246. }
  247. }
  248. public void writeTo(DataOutputStream out) throws IOException {
  249. Util.writeString(host, out);
  250. out.writeLong(timestamp);
  251. out.writeInt(id);
  252. out.writeBoolean(multicast_addr);
  253. ObjectOutputStream oos=new ObjectOutputStream(out);
  254. oos.writeObject(physical_addrs);
  255. oos.close();
  256. Util.writeByteBuffer(additional_data, out);
  257. }
  258. public void readFrom(DataInputStream in) throws IOException, IllegalAccessException, InstantiationException {
  259. host=Util.readString(in);
  260. timestamp=in.readLong();
  261. id=in.readInt();
  262. multicast_addr=in.readBoolean();
  263. ObjectInputStream ois=new ObjectInputStream(in);
  264. try {
  265. physical_addrs=(ArrayList)ois.readObject();
  266. }
  267. catch(ClassNotFoundException e) {
  268. }
  269. additional_data=Util.readByteBuffer(in);
  270. }
  271. @Override // GemStoneAddition
  272. public Object clone() throws CloneNotSupportedException {
  273. LogicalAddress ret=new LogicalAddress();
  274. ret.host=host;
  275. ret.timestamp=timestamp;
  276. ret.id=id;
  277. ret.multicast_addr=multicast_addr;
  278. ret.additional_data=additional_data;
  279. ret.primary_physical_addr=primary_physical_addr;
  280. if(physical_addrs != null)
  281. ret.physical_addrs=(ArrayList)physical_addrs.clone();
  282. return ret;
  283. }
  284. public LogicalAddress copy() {
  285. try {
  286. return (LogicalAddress)clone();
  287. }
  288. catch(CloneNotSupportedException e) {
  289. return null;
  290. }
  291. }
  292. }