001: // $Id: WanPipeAddress.java,v 1.9 2005/08/08 12:45:46 belaban Exp $
002:
003: package org.jgroups.protocols;
004:
005: import org.jgroups.Address;
006: import org.jgroups.util.Util;
007: import org.apache.commons.logging.Log;
008: import org.apache.commons.logging.LogFactory;
009:
010: import java.io.*;
011:
012: /**
013: * Logical address for a WAN pipe (logical link)
014: */
015: public class WanPipeAddress implements Address {
016: String logical_name = null;
017: static final Log log = LogFactory.getLog(WanPipeAddress.class);
018:
019: // Used only by Externalization
020: public WanPipeAddress() {
021: }
022:
023: public WanPipeAddress(String logical_name) {
024: this .logical_name = logical_name;
025: }
026:
027: public boolean isMulticastAddress() {
028: return true;
029: }
030:
031: public int size() {
032: return logical_name != null ? logical_name.length() + 2 : 22;
033: }
034:
035: /**
036: * Establishes an order between 2 addresses. Assumes other contains non-null WanPipeAddress.
037: *
038: * @return 0 for equality, value less than 0 if smaller, greater than 0 if greater.
039: */
040: public int compareTo(Object other) throws ClassCastException {
041: if (other == null) {
042: log
043: .error("WanPipeAddress.compareTo(): other address is null !");
044: return -1;
045: }
046:
047: if (!(other instanceof WanPipeAddress)) {
048: log
049: .error("WanPipeAddress.compareTo(): other address is not of type WanPipeAddress !");
050: return -1;
051: }
052:
053: if (((WanPipeAddress) other).logical_name == null) {
054: log
055: .error("WanPipeAddress.compareTo(): other address is null !");
056: return -1;
057: }
058:
059: return logical_name
060: .compareTo(((WanPipeAddress) other).logical_name);
061: }
062:
063: public boolean equals(Object obj) {
064: return compareTo(obj) == 0;
065: }
066:
067: public int hashCode() {
068: return logical_name.hashCode();
069: }
070:
071: public String toString() {
072: return logical_name;
073: }
074:
075: public void writeExternal(ObjectOutput out) throws IOException {
076: out.writeObject(logical_name);
077: }
078:
079: public void readExternal(ObjectInput in) throws IOException,
080: ClassNotFoundException {
081: logical_name = (String) in.readObject();
082: }
083:
084: public static void main(String args[]) {
085:
086: WanPipeAddress a = new WanPipeAddress("daddy");
087: System.out.println(a);
088:
089: WanPipeAddress b = new WanPipeAddress(
090: "daddy.nms.fnc.fujitsu.com");
091: System.out.println(b);
092:
093: if (a.equals(b))
094: System.out.println("equals");
095: else
096: System.out.println("does not equal");
097: }
098:
099: public void writeTo(DataOutputStream outstream) throws IOException {
100: Util.writeString(logical_name, outstream);
101: }
102:
103: public void readFrom(DataInputStream instream) throws IOException,
104: IllegalAccessException, InstantiationException {
105: logical_name = Util.readString(instream);
106: }
107: }
|