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

/interpreter/branches/multinetwork/src/edu/vub/at/actors/net/comm/Address.java

http://ambienttalk.googlecode.com/
Java | 202 lines | 108 code | 22 blank | 72 comment | 14 complexity | daab5ef55151f01be4a05015fcdd06dd MD5 | raw file
Possible License(s): Apache-2.0, LGPL-2.0, LGPL-2.1
  1. /**
  2. * AmbientTalk/2 Project
  3. * Address.java created on 2-apr-2007 at 10:52:36
  4. * (c) Programming Technology Lab, 2006 - 2007
  5. * Authors: Tom Van Cutsem & Stijn Mostinckx
  6. *
  7. * Permission is hereby granted, free of charge, to any person
  8. * obtaining a copy of this software and associated documentation
  9. * files (the "Software"), to deal in the Software without
  10. * restriction, including without limitation the rights to use,
  11. * copy, modify, merge, publish, distribute, sublicense, and/or
  12. * sell copies of the Software, and to permit persons to whom the
  13. * Software is furnished to do so, subject to the following
  14. * conditions:
  15. *
  16. * The above copyright notice and this permission notice shall be
  17. * included in all copies or substantial portions of the Software.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  20. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  21. * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  22. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  23. * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  24. * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  25. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  26. * OTHER DEALINGS IN THE SOFTWARE.
  27. */
  28. package edu.vub.at.actors.net.comm;
  29. import edu.vub.at.util.logging.Logging;
  30. import java.io.ByteArrayInputStream;
  31. import java.io.ByteArrayOutputStream;
  32. import java.io.DataInputStream;
  33. import java.io.DataOutputStream;
  34. import java.io.IOException;
  35. import java.io.Serializable;
  36. import java.net.InetAddress;
  37. import java.net.ServerSocket;
  38. /**
  39. * Instances of this class represent low-level network addresses of AmbientTalk
  40. * virtual machines.
  41. *
  42. * An <tt>Address</tt> is frequently multicast across the network via UDP, serving
  43. * as the contents of a VM's <i>heartbeat</i>.
  44. *
  45. * Implementation-wise, an <tt>Address</tt> encapsulates an IP address and a port number,
  46. * which is the address on which the {@link ServerSocket} of the {@link MasterConnectionThread}
  47. * is listening for incoming connections.
  48. *
  49. * @author tvcutsem
  50. */
  51. public class Address implements Serializable, Comparable {
  52. /** the maximum size of a serialized address to be transported over UDP */
  53. public static final int MAX_ADDRESS_BYTE_SIZE = 256;
  54. public final InetAddress ipAddress_;
  55. public final int port_;
  56. /** virtual network interface name used to distinguish local communication **/
  57. public final String virtualNetworkName_;
  58. /** subnet mask length **/
  59. public final short networkPrefixLength_;
  60. /** the name of the overlay network to which this AmbientTalk VM belongs */
  61. public final String ambientTalkNetworkName_;
  62. /** an address is serialized at construction time and cached because it is constant */
  63. public final byte[] serializedForm_;
  64. /** the string form is cached because it is frequently used in the compareTo method */
  65. private final String stringForm_;
  66. /**
  67. * @throws RuntimeException if the network name is so long that the serialized form
  68. * of this <tt>Address</tt> would exceed {@link Address#MAX_ADDRESS_BYTE_SIZE}.
  69. */
  70. public Address(InetAddress ipAddress, int port, String ambientTalkNetworkName, short networkPrefixLength,
  71. String virtualNetworkName) {
  72. ipAddress_ = ipAddress;
  73. port_ = port;
  74. ambientTalkNetworkName_ = ambientTalkNetworkName;
  75. networkPrefixLength_ = networkPrefixLength;
  76. stringForm_ = ipAddress.toString() + port;
  77. if (virtualNetworkName == null) {
  78. // this is not virtual network
  79. virtualNetworkName_ = "";
  80. } else {
  81. virtualNetworkName_ = virtualNetworkName;
  82. }
  83. serializedForm_ = toBytes();
  84. // check for overflow
  85. if (serializedForm_.length > MAX_ADDRESS_BYTE_SIZE) {
  86. throw new RuntimeException("Address too long: " + this.toString());
  87. }
  88. }
  89. /**
  90. * Deserialize a network address manually from a byte stream.
  91. */
  92. public static Address fromBytes(byte[] serializedForm) throws IOException {
  93. ByteArrayInputStream bin = new ByteArrayInputStream(serializedForm);
  94. DataInputStream din = new DataInputStream(bin);
  95. String address = din.readUTF();
  96. InetAddress ipAddress = InetAddress.getByName(address);
  97. int port = din.readInt();
  98. String groupName = din.readUTF();
  99. String virtualNetworkName = din.readUTF();
  100. din.close();
  101. return new Address(ipAddress, port, groupName, (short)24, virtualNetworkName);
  102. }
  103. /**
  104. * @return whether the receiver address and the parameter denote VMs that are
  105. * connected to the same AmbientTalk overlay network.
  106. */
  107. public boolean inSameNetwork(Address other) {
  108. return ambientTalkNetworkName_.equals(other.ambientTalkNetworkName_);
  109. }
  110. /**
  111. * @return whether the receiver address and the parameter denote VMs that are
  112. * connected to the same IP subnet, assuming networkPrefixLength is 8, 16 or 24
  113. */
  114. public boolean inSameSubnet(Address other) {
  115. byte[] b1 = this.ipAddress_.getAddress();
  116. byte[] b2 = other.ipAddress_.getAddress();
  117. Boolean ret = true;
  118. switch (networkPrefixLength_ / 8) {
  119. case 3:
  120. ret &= b1[2] == b2[2];
  121. case 2: // fall through
  122. ret &= b1[1] == b2[1];
  123. case 1: // fall through
  124. case 0: // fall through
  125. ret &= b1[0] == b2[0];
  126. break;
  127. default:
  128. /* 127.0.0.1 has network prefix 0? */
  129. Logging.Network_LOG.debug("Strange network prefix length(subnetmask) " + networkPrefixLength_);
  130. }
  131. return ret;
  132. }
  133. public boolean isVirtualNetwork() {
  134. return virtualNetworkName_.length() != 0;
  135. }
  136. public boolean isSameVirtualNetwork(Address other) {
  137. return virtualNetworkName_.equals(other.virtualNetworkName_);
  138. }
  139. /**
  140. * Compares two addresses based on their internal representation. There is no
  141. * logical ordering defined on addresses. However, it is guaranteed that
  142. * if <code>adr1.compareTo(adr2) > 0</code> then <code>adr2.compareTo(adr1) < 0</code>.
  143. */
  144. public int compareTo(Object otherAddress) {
  145. return stringForm_.compareTo(((Address) otherAddress).stringForm_);
  146. }
  147. public boolean equals(Object other) {
  148. if (other instanceof Address) {
  149. Address otherAddress = (Address) other;
  150. return (ipAddress_.equals(otherAddress.ipAddress_) && port_ == otherAddress.port_)
  151. && (ambientTalkNetworkName_.equals(otherAddress.ambientTalkNetworkName_));
  152. } else {
  153. return false;
  154. }
  155. }
  156. /**
  157. * It is guaranteed that if two addresses are equal, then they map to the same hash code.
  158. */
  159. public int hashCode() {
  160. return ipAddress_.hashCode() | port_;
  161. }
  162. public String toString() {
  163. return ipAddress_.toString() + ":" + port_ + "[" + ambientTalkNetworkName_ + "]";
  164. }
  165. private byte[] toBytes() {
  166. try {
  167. ByteArrayOutputStream bout = new ByteArrayOutputStream();
  168. DataOutputStream dout = new DataOutputStream(bout);
  169. dout.writeUTF(ipAddress_.getHostAddress());
  170. dout.writeInt(port_);
  171. dout.writeUTF(ambientTalkNetworkName_);
  172. dout.writeUTF(virtualNetworkName_);
  173. dout.close();
  174. return bout.toByteArray();
  175. } catch (IOException e) {
  176. Logging.VirtualMachine_LOG.fatal("Could not construct serialized address:", e);
  177. return null;
  178. }
  179. }
  180. }