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.registry;
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.IID;
27 import jgroup.core.registry.DependableRegistry.RegistryEntry;
28 import jgroup.relacs.types.IIDImpl;
29 import jgroup.util.OrderedList;
30
31 /**
32 * Objects of this class are used to maintain a set of bindings
33 * associated to one service name. <p>
34 *
35 * FIXME HEIN Oct 5, 2002: Currently it extends the OrderedList
36 * class; we should avoid to use OrderedList and use hash tables
37 * instead; no need for ordering. In this sense, the correct name
38 * for the class should be just Binding.
39 *
40 * @author Alberto Montresor
41 * @since Jgroup 0.4
42 */
43 public final class BindingList
44 extends OrderedList
45 implements Externalizable
46 {
47
48 ////////////////////////////////////////////////////////////////////////////////////////////
49 // Fields
50 ////////////////////////////////////////////////////////////////////////////////////////////
51
52 private static final long serialVersionUID = -8269573883688508547L;
53
54 /** The name of the service of this binding */
55 String servicename;
56
57 /** The remote reference entry of the replica of this binding */
58 RegistryEntry entry;
59
60 /** The implementation class associated to this binding */
61 Class serverClass;
62
63 /** Timestamp for this node */
64 long timestamp;
65
66 /** Leasetime for this node */
67 long leasetime;
68
69 /** IID for this node */
70 IID iid;
71
72
73 ////////////////////////////////////////////////////////////////////////////////////////////
74 // Constructors
75 ////////////////////////////////////////////////////////////////////////////////////////////
76
77 /**
78 * Empty constructor needed for construction of an empty
79 * <code>OrderedList</code> list. Also needed for externalization.
80 */
81 public BindingList() { }
82
83
84 /**
85 * Construct a new binding between a service name and a remote
86 * object. Additional information are a unique numeric identifier
87 * for the binding, created progressively by the server to which
88 * this binding is associated.
89 *
90 * @param servicename
91 * the string identifier of the service
92 * @param entry
93 * the remote reference entry of the replica of this binding
94 * @param serverClass
95 * the implementation class associated to this binding
96 * @param iid
97 * the binding identifier for this replica
98 */
99 BindingList(String servicename, RegistryEntry entry,
100 Class serverClass, IID iid)
101 {
102 this(servicename, entry, serverClass, iid, -1);
103 }
104
105
106 /**
107 * Construct a new binding between a service name and a remote
108 * object. Additional information are a unique numeric identifier
109 * for the binding, created progressively by the server to which
110 * this binding is associated.
111 *
112 * This constructor is used in conjunction with the LeaseLayer.
113 *
114 * @param servicename
115 * the string identifier of the service
116 * @param entry
117 * the remote reference entry of the replica of this binding
118 * @param serverClass
119 * the implementation class associated to this binding
120 * @param iid
121 * the binding identifier for this replica
122 * @param leasetime
123 * the leasetime associated with this BindingList
124 */
125 BindingList(String servicename, RegistryEntry entry,
126 Class serverClass, IID iid, long leasetime)
127 {
128 // The key is a numeric progressive identifier of the binding.
129 this.key = iid.getCounter();
130 this.servicename = servicename;
131 this.entry = entry;
132 this.serverClass = serverClass;
133 this.timestamp = System.currentTimeMillis();
134 this.leasetime = leasetime;
135 this.iid = iid;
136 }
137
138
139 ////////////////////////////////////////////////////////////////////////////////////////////
140 // Object methods
141 ////////////////////////////////////////////////////////////////////////////////////////////
142
143 /**
144 * Returns a string representation of this object
145 */
146 public String toString()
147 {
148 StringBuilder buf = new StringBuilder();
149 buf.append("key: ");
150 buf.append(key);
151 buf.append(", service: ");
152 buf.append(servicename);
153 // buf.append(", entry: ");
154 // buf.append(entry);
155 // buf.append(", class: ");
156 // buf.append(serverClass.getName());
157 return buf.toString();
158 }
159
160
161 ////////////////////////////////////////////////////////////////////////////////////////////
162 // Externalization
163 ////////////////////////////////////////////////////////////////////////////////////////////
164
165 /**
166 * Marshals the content of this object to the specified output stream.
167 *
168 * @param out the stream to be written
169 */
170 public void writeExternal(ObjectOutput out)
171 throws IOException
172 {
173 out.writeInt(key);
174 out.writeUTF(servicename);
175 out.writeObject(entry);
176 out.writeObject(serverClass);
177 out.writeLong(leasetime);
178 /*
179 * Compute the remaining time of the lease for this server
180 * (i.e. the delta time) and send this to the other registry
181 * replicas.
182 */
183 out.writeLong(System.currentTimeMillis() - timestamp);
184 iid.writeExternal(out);
185 }
186
187 /**
188 * Restores the content of this object from the marshalled data contained
189 * in the specified input stream.
190 *
191 * @param in the stream to be read
192 */
193 public void readExternal(ObjectInput in)
194 throws IOException, ClassNotFoundException
195 {
196 key = in.readInt();
197 servicename = in.readUTF();
198 entry = (RegistryEntry) in.readObject();
199 serverClass = (Class) in.readObject();
200 leasetime = in.readLong();
201 /*
202 * Compute a new timestamp for this server as the delta time plus
203 * the current time. That is, we will give the server a little
204 * extra time (the communicatio delay) to perform the refresh.
205 */
206 timestamp = in.readLong() + System.currentTimeMillis();
207 iid = new IIDImpl();
208 iid.readExternal(in);
209 }
210
211 } // END BindingList