1 /*
2 * Copyright (c) 1998-2002 The Jgroup Team.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU Lesser General Public License version 2 as
6 * published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU Lesser General Public License for more details.
12 *
13 * You should have received a copy of the GNU Lesser General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
16 *
17 */
18
19 package jgroup.relacs.mss;
20
21 import java.io.Externalizable;
22 import java.io.IOException;
23 import java.io.ObjectInput;
24 import java.io.ObjectOutput;
25
26 import jgroup.core.EndPoint;
27 import jgroup.relacs.types.EndPointImpl;
28
29
30 /**
31 * The <code>RoutingTableEntry</code> class.
32 *
33 * @author Salvatore Cammarata
34 * @since Jgroup 1.2
35 */
36 final class RoutingTableEntry
37 implements Externalizable
38 {
39
40 ////////////////////////////////////////////////////////////////////////////////////////////
41 // Constants
42 ////////////////////////////////////////////////////////////////////////////////////////////
43
44 /** Size of this object in bytes (those that are marshalled) */
45 public static final int SIZE = EndPointImpl.SIZE + EndPointImpl.SIZE + 4;
46
47
48 ////////////////////////////////////////////////////////////////////////////////////////////
49 // Fields
50 ////////////////////////////////////////////////////////////////////////////////////////////
51
52 /** Unique identifier for the cluster */
53 EndPoint key;
54
55 /** Route for the cluster */
56 EndPoint route;
57
58 /** Cost for this route */
59 int cost;
60
61 /** TTL */
62 int TTL;
63
64
65 ////////////////////////////////////////////////////////////////////////////////////////////
66 // Constructors
67 ////////////////////////////////////////////////////////////////////////////////////////////
68
69 /**
70 * Default constructor for externalization.
71 */
72 public RoutingTableEntry()
73 {
74 }
75
76 /**
77 * Builds a new routing table entry by setting the specified
78 * endpoint, the initial route to be used for this endpoint,
79 * the initial cost for this endpoint.
80 */
81 RoutingTableEntry(EndPoint id, EndPoint route, int cost)
82 {
83 this.key = id;
84 this.route = route;
85 this.cost = cost;
86 }
87
88
89 ////////////////////////////////////////////////////////////////////////////////////////////
90 // Marshaling / unmarshaling
91 ////////////////////////////////////////////////////////////////////////////////////////////
92
93 /**
94 * Restores the content of this object from the marshalled data contained
95 * in the specified input stream.
96 *
97 * @param in the stream to be read
98 */
99 public void readExternal(ObjectInput in)
100 throws IOException, ClassNotFoundException
101 {
102 key = new EndPointImpl();
103 key.readExternal(in);
104 route = new EndPointImpl();
105 route.readExternal(in);
106 cost = in.readInt();
107 }
108
109
110 /**
111 * Marshals the content of this object to the specified output stream.
112 *
113 * @param out the stream to be written
114 */
115 public void writeExternal(ObjectOutput out)
116 throws IOException
117 {
118 key.writeExternal(out);
119 route.writeExternal(out);
120 if (cost < 0) {
121 out.writeInt(-cost);
122 } else {
123 out.writeInt(cost);
124 }
125 }
126
127
128 ////////////////////////////////////////////////////////////////////////////////////////////
129 // Methods
130 ////////////////////////////////////////////////////////////////////////////////////////////
131
132
133 /**
134 * Returns a string representation of this object
135 */
136 public String toString()
137 {
138 return "[" + key + ", " + route + ", " + cost + "]";
139 }
140
141
142 /**
143 * Compares two objects for content equality.
144 *
145 * @param obj the object to compare with
146 * @return true if these objects are equal; false otherwise.
147 */
148 public boolean equals(Object obj)
149 {
150 if (this == obj)
151 return true;
152 else
153 if (obj instanceof RoutingTableEntry)
154 return key.equals(((RoutingTableEntry) obj).key);
155 else
156 return false;
157 }
158
159 } // END RoutingTableEntry