Mercurial > hg > blitz_condensed
comparison src/org/dancres/blitz/remote/transport/StubImpl.java @ 0:3dc0c5604566
Initial checkin of blitz 2.0 fcs - no installer yet.
author | Dan Creswell <dan.creswell@gmail.com> |
---|---|
date | Sat, 21 Mar 2009 11:00:06 +0000 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:3dc0c5604566 |
---|---|
1 package org.dancres.blitz.remote.transport; | |
2 | |
3 import java.io.IOException; | |
4 import java.net.InetSocketAddress; | |
5 import java.rmi.MarshalledObject; | |
6 import java.rmi.RemoteException; | |
7 import java.util.List; | |
8 | |
9 import net.jini.core.discovery.LookupLocator; | |
10 import net.jini.core.entry.Entry; | |
11 import net.jini.core.event.EventRegistration; | |
12 import net.jini.core.event.RemoteEventListener; | |
13 import net.jini.core.lease.LeaseDeniedException; | |
14 import net.jini.core.lease.UnknownLeaseException; | |
15 import net.jini.core.transaction.Transaction; | |
16 import net.jini.core.transaction.TransactionException; | |
17 import net.jini.core.transaction.UnknownTransactionException; | |
18 import net.jini.core.transaction.server.TransactionManager; | |
19 import net.jini.space.JavaSpace; | |
20 | |
21 import org.dancres.blitz.EntryChit; | |
22 import org.dancres.blitz.lease.SpaceUID; | |
23 import org.dancres.blitz.mangler.MangledEntry; | |
24 import org.dancres.blitz.remote.BlitzServer; | |
25 import org.dancres.blitz.remote.LeaseImpl; | |
26 import org.dancres.blitz.remote.LeaseResults; | |
27 import org.dancres.blitz.remote.ViewResult; | |
28 import org.dancres.blitz.remote.view.EntryViewUID; | |
29 import org.dancres.blitz.stats.Stat; | |
30 import org.dancres.blitz.stats.Switch; | |
31 import org.apache.mina.common.*; | |
32 import org.apache.mina.filter.codec.ProtocolCodecFilter; | |
33 import org.apache.mina.filter.codec.serialization.ObjectSerializationCodecFactory; | |
34 import org.apache.mina.filter.LoggingFilter; | |
35 import org.apache.mina.transport.socket.nio.SocketConnector; | |
36 import org.apache.mina.transport.socket.nio.SocketConnectorConfig; | |
37 import org.apache.mina.transport.socket.nio.SocketSessionConfig; | |
38 import edu.emory.mathcs.backport.java.util.concurrent.Executors; | |
39 | |
40 /** | |
41 */ | |
42 public class StubImpl implements BlitzServer { | |
43 private static final String HOSTNAME = "192.168.0.54"; | |
44 private static final int PORT = 8080; | |
45 private static final int CONNECT_TIMEOUT = 30; // seconds | |
46 | |
47 private IoSession _session; | |
48 private ClientSessionHandler _handler; | |
49 | |
50 private Object _lock = new Object(); | |
51 private int _nextConversationId; | |
52 | |
53 private int nextConversationId() { | |
54 synchronized (_lock) { | |
55 return _nextConversationId++; | |
56 } | |
57 } | |
58 | |
59 private synchronized IoSession getSession() throws RemoteException { | |
60 if (_session == null) { | |
61 SocketConnector connector = new SocketConnector(); | |
62 // new SocketConnector(2, Executors.newFixedThreadPool(2)); | |
63 | |
64 // Change the worker timeout to 1 second to make the I/O thread quit soon | |
65 // when there's no connection to manage. | |
66 connector.setWorkerTimeout(1); | |
67 | |
68 // Configure the service. | |
69 SocketConnectorConfig cfg = new SocketConnectorConfig(); | |
70 cfg.setConnectTimeout(CONNECT_TIMEOUT); | |
71 cfg.getFilterChain().addLast( | |
72 "codec", | |
73 new ProtocolCodecFilter(new MessageCodecFactory())); | |
74 | |
75 SocketSessionConfig mySConfig = (SocketSessionConfig) | |
76 cfg.getSessionConfig(); | |
77 mySConfig.setTcpNoDelay(true); | |
78 | |
79 // cfg.getFilterChain().addLast( | |
80 // "codec", | |
81 // new ProtocolCodecFilter(new ObjectSerializationCodecFactory())); | |
82 // cfg.getFilterChain().addLast("logger", new LoggingFilter()); | |
83 | |
84 _handler = new ClientSessionHandler(); | |
85 | |
86 for (; ;) { | |
87 try { | |
88 ConnectFuture future = connector.connect( | |
89 new InetSocketAddress(HOSTNAME, PORT), | |
90 _handler, cfg); | |
91 | |
92 future.join(); | |
93 _session = future.getSession(); | |
94 break; | |
95 } | |
96 catch (RuntimeIOException anRIOE) { | |
97 throw new RemoteException("Failed to connect", anRIOE); | |
98 } | |
99 } | |
100 } | |
101 | |
102 return _session; | |
103 } | |
104 | |
105 private synchronized ClientSessionHandler getHandler() | |
106 throws RemoteException { | |
107 | |
108 if (_handler == null) { | |
109 getSession(); | |
110 } | |
111 | |
112 return _handler; | |
113 } | |
114 | |
115 public void ping() throws RemoteException { | |
116 Message myMessage = MarshallUtil.marshall(new Ping(), 0); | |
117 | |
118 getSession().write(myMessage); | |
119 } | |
120 | |
121 public LeaseImpl write(MangledEntry anEntry, Transaction aTxn, | |
122 long aLeaseTime) throws RemoteException, | |
123 TransactionException { | |
124 | |
125 int myConversationId = nextConversationId(); | |
126 | |
127 Message myMessage = | |
128 MarshallUtil.marshall(new Write(anEntry, aTxn, aLeaseTime), | |
129 myConversationId); | |
130 | |
131 ClientSessionHandler.Ticket myTicket = | |
132 getHandler().getTicket(myConversationId); | |
133 | |
134 getSession().write(myMessage); | |
135 | |
136 myMessage = myTicket.getResponse(Long.MAX_VALUE); | |
137 | |
138 Object myResult = MarshallUtil.unmarshall(myMessage); | |
139 | |
140 if (myResult instanceof Exception) { | |
141 if (myResult instanceof RemoteException) | |
142 throw (RemoteException) myResult; | |
143 else | |
144 throw (TransactionException) myResult; | |
145 } else { | |
146 return (LeaseImpl) myResult; | |
147 } | |
148 } | |
149 | |
150 public MangledEntry take(MangledEntry anEntry, Transaction aTxn, | |
151 long aWaitTime) throws RemoteException, | |
152 TransactionException { | |
153 | |
154 int myConversationId = nextConversationId(); | |
155 | |
156 Message myMessage = | |
157 MarshallUtil.marshall(new Take(anEntry, aTxn, aWaitTime), | |
158 myConversationId); | |
159 | |
160 ClientSessionHandler.Ticket myTicket = | |
161 getHandler().getTicket(myConversationId); | |
162 | |
163 getSession().write(myMessage); | |
164 | |
165 myMessage = myTicket.getResponse(aWaitTime); | |
166 | |
167 if (myMessage == null) | |
168 return null; | |
169 | |
170 Object myResult = MarshallUtil.unmarshall(myMessage); | |
171 | |
172 if (myResult instanceof Exception) { | |
173 if (myResult instanceof RemoteException) | |
174 throw (RemoteException) myResult; | |
175 else | |
176 throw (TransactionException) myResult; | |
177 } else { | |
178 return (MangledEntry) myResult; | |
179 } | |
180 } | |
181 | |
182 public MangledEntry read(MangledEntry anEntry, Transaction aTxn, | |
183 long aWaitTime) throws RemoteException, | |
184 TransactionException { | |
185 | |
186 int myConversationId = nextConversationId(); | |
187 | |
188 Message myMessage = | |
189 MarshallUtil.marshall(new Read(anEntry, aTxn, aWaitTime), | |
190 myConversationId); | |
191 | |
192 ClientSessionHandler.Ticket myTicket = | |
193 getHandler().getTicket(myConversationId); | |
194 | |
195 getSession().write(myMessage); | |
196 | |
197 myMessage = myTicket.getResponse(Long.MAX_VALUE); | |
198 | |
199 if (myMessage == null) | |
200 return null; | |
201 | |
202 Object myResult = MarshallUtil.unmarshall(myMessage); | |
203 | |
204 if (myResult instanceof Exception) { | |
205 if (myResult instanceof RemoteException) | |
206 throw (RemoteException) myResult; | |
207 else | |
208 throw (TransactionException) myResult; | |
209 } else { | |
210 return (MangledEntry) myResult; | |
211 } | |
212 } | |
213 | |
214 public MangledEntry takeIfExists(MangledEntry anEntry, Transaction aTxn, | |
215 long aWaitTime) throws RemoteException, | |
216 TransactionException { | |
217 return null; //To change body of implemented methods use File | Settings | File Templates. | |
218 } | |
219 | |
220 public MangledEntry readIfExists(MangledEntry anEntry, Transaction aTxn, | |
221 long aWaitTime) throws RemoteException, | |
222 TransactionException { | |
223 return null; //To change body of implemented methods use File | Settings | File Templates. | |
224 } | |
225 | |
226 public EventRegistration notify(MangledEntry anEntry, Transaction aTxn, | |
227 RemoteEventListener aListener, | |
228 long aLeaseTime, | |
229 MarshalledObject aHandback) throws | |
230 RemoteException, TransactionException { | |
231 return null; //To change body of implemented methods use File | Settings | File Templates. | |
232 } | |
233 | |
234 public Entry[] getLookupAttributes() throws RemoteException { | |
235 return new Entry[0]; //To change body of implemented methods use File | Settings | File Templates. | |
236 } | |
237 | |
238 public void addLookupAttributes(Entry[] entries) throws RemoteException { | |
239 //To change body of implemented methods use File | Settings | File Templates. | |
240 } | |
241 | |
242 public void modifyLookupAttributes(Entry[] entries, Entry[] entries1) throws | |
243 RemoteException { | |
244 //To change body of implemented methods use File | Settings | File Templates. | |
245 } | |
246 | |
247 public String[] getLookupGroups() throws RemoteException { | |
248 return new String[0]; //To change body of implemented methods use File | Settings | File Templates. | |
249 } | |
250 | |
251 public void addLookupGroups(String[] strings) throws RemoteException { | |
252 //To change body of implemented methods use File | Settings | File Templates. | |
253 } | |
254 | |
255 public void removeLookupGroups(String[] strings) throws RemoteException { | |
256 //To change body of implemented methods use File | Settings | File Templates. | |
257 } | |
258 | |
259 public void setLookupGroups(String[] strings) throws RemoteException { | |
260 //To change body of implemented methods use File | Settings | File Templates. | |
261 } | |
262 | |
263 public LookupLocator[] getLookupLocators() throws RemoteException { | |
264 return new LookupLocator[0]; //To change body of implemented methods use File | Settings | File Templates. | |
265 } | |
266 | |
267 public void addLookupLocators(LookupLocator[] lookupLocators) throws | |
268 RemoteException { | |
269 //To change body of implemented methods use File | Settings | File Templates. | |
270 } | |
271 | |
272 public void removeLookupLocators(LookupLocator[] lookupLocators) throws | |
273 RemoteException { | |
274 //To change body of implemented methods use File | Settings | File Templates. | |
275 } | |
276 | |
277 public void setLookupLocators(LookupLocator[] lookupLocators) throws | |
278 RemoteException { | |
279 //To change body of implemented methods use File | Settings | File Templates. | |
280 } | |
281 | |
282 public Stat[] getStats() throws RemoteException { | |
283 return new Stat[0]; //To change body of implemented methods use File | Settings | File Templates. | |
284 } | |
285 | |
286 public void setSwitches(Switch[] aListOfSwitches) throws RemoteException { | |
287 //To change body of implemented methods use File | Settings | File Templates. | |
288 } | |
289 | |
290 public void backup(String aBackupDir) throws RemoteException, IOException { | |
291 //To change body of implemented methods use File | Settings | File Templates. | |
292 } | |
293 | |
294 public void requestSnapshot() throws RemoteException, TransactionException, | |
295 IOException { | |
296 //To change body of implemented methods use File | Settings | File Templates. | |
297 } | |
298 | |
299 public void shutdown() throws RemoteException, IOException { | |
300 //To change body of implemented methods use File | Settings | File Templates. | |
301 } | |
302 | |
303 public void clean() throws RemoteException, IOException { | |
304 //To change body of implemented methods use File | Settings | File Templates. | |
305 } | |
306 | |
307 public void reap() throws RemoteException { | |
308 //To change body of implemented methods use File | Settings | File Templates. | |
309 } | |
310 | |
311 public void destroy() throws RemoteException { | |
312 //To change body of implemented methods use File | Settings | File Templates. | |
313 } | |
314 | |
315 public JavaSpace getJavaSpaceProxy() throws RemoteException { | |
316 return null; //To change body of implemented methods use File | Settings | File Templates. | |
317 }/* | |
318 * @param isJavaSpace05 if <code>true</code> enforces any defined lease bounds | |
319 * and asserts locks when performing the scan/acquire internally. | |
320 * This is used internally to differentiate between old and new contents | |
321 * methods as JavaSpaceAdmin::contents does not do leases. | |
322 */ | |
323 public ViewResult newView(MangledEntry[] aTemplates, Transaction aTxn, | |
324 long aLeaseDuration, boolean isJavaSpace05, | |
325 long aLimit, int anInitialChunk) throws | |
326 RemoteException, TransactionException { | |
327 return null; //To change body of implemented methods use File | Settings | File Templates. | |
328 } | |
329 | |
330 public EntryChit[] getNext(EntryViewUID aEntryViewUID, | |
331 int aChunkSize) throws RemoteException { | |
332 return new EntryChit[0]; //To change body of implemented methods use File | Settings | File Templates. | |
333 } | |
334 | |
335 public void delete(Object aCookie) throws RemoteException { | |
336 //To change body of implemented methods use File | Settings | File Templates. | |
337 } | |
338 | |
339 public void close(EntryViewUID aEntryViewUID) throws RemoteException { | |
340 //To change body of implemented methods use File | Settings | File Templates. | |
341 } | |
342 | |
343 public Object getServiceProxy() throws RemoteException { | |
344 return null; //To change body of implemented methods use File | Settings | File Templates. | |
345 } | |
346 | |
347 public int prepare(TransactionManager transactionManager, long l) throws | |
348 UnknownTransactionException, RemoteException { | |
349 return 0; //To change body of implemented methods use File | Settings | File Templates. | |
350 } | |
351 | |
352 public void commit(TransactionManager transactionManager, long l) throws | |
353 UnknownTransactionException, RemoteException { | |
354 //To change body of implemented methods use File | Settings | File Templates. | |
355 } | |
356 | |
357 public void abort(TransactionManager transactionManager, long l) throws | |
358 UnknownTransactionException, RemoteException { | |
359 //To change body of implemented methods use File | Settings | File Templates. | |
360 } | |
361 | |
362 public int prepareAndCommit(TransactionManager transactionManager, | |
363 long l) throws UnknownTransactionException, | |
364 RemoteException { | |
365 return 0; //To change body of implemented methods use File | Settings | File Templates. | |
366 } | |
367 | |
368 public List write(List aMangledEntries, Transaction aTxn, | |
369 List aLeaseTimes) throws RemoteException, | |
370 TransactionException { | |
371 return null; //To change body of implemented methods use File | Settings | File Templates. | |
372 } | |
373 | |
374 public List take(MangledEntry[] aTemplates, Transaction aTxn, | |
375 long aWaitTime, long aLimit) throws RemoteException, | |
376 TransactionException { | |
377 return null; //To change body of implemented methods use File | Settings | File Templates. | |
378 } | |
379 | |
380 public EventRegistration registerForVisibility(MangledEntry[] aTemplates, | |
381 Transaction aTxn, | |
382 RemoteEventListener aListener, | |
383 long aLeaseTime, | |
384 MarshalledObject aHandback, | |
385 boolean visibilityOnly) throws | |
386 RemoteException, TransactionException { | |
387 return null; //To change body of implemented methods use File | Settings | File Templates. | |
388 } | |
389 | |
390 public LeaseResults renew(SpaceUID[] aLeases, long[] aDurations) throws | |
391 RemoteException { | |
392 return null; //To change body of implemented methods use File | Settings | File Templates. | |
393 } | |
394 | |
395 public LeaseResults cancel(SpaceUID[] aLeases) throws RemoteException { | |
396 return null; //To change body of implemented methods use File | Settings | File Templates. | |
397 } | |
398 | |
399 public long renew(SpaceUID aUID, long aDuration) throws | |
400 UnknownLeaseException, LeaseDeniedException, RemoteException { | |
401 return 0; //To change body of implemented methods use File | Settings | File Templates. | |
402 } | |
403 | |
404 public void cancel(SpaceUID aUID) throws UnknownLeaseException, | |
405 RemoteException { | |
406 //To change body of implemented methods use File | Settings | File Templates. | |
407 } | |
408 | |
409 public Object getAdmin() throws RemoteException { | |
410 return null; //To change body of implemented methods use File | Settings | File Templates. | |
411 } | |
412 } |