comparison src/org/dancres/blitz/remote/transport/ClientSessionHandler.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.rmi.RemoteException;
4
5 import edu.emory.mathcs.backport.java.util.concurrent.ConcurrentHashMap;
6 import org.apache.mina.common.IoHandlerAdapter;
7 import org.apache.mina.common.IoSession;
8 import org.apache.mina.common.ByteBuffer;
9 import org.apache.mina.util.SessionLog;
10
11 /**
12 */
13 public class ClientSessionHandler extends IoHandlerAdapter {
14 private ConcurrentHashMap _requests = new ConcurrentHashMap();
15
16 public void messageReceived(IoSession session, Object aMessage) {
17 Message myMessage = (Message) aMessage;
18
19 int myConversationId = myMessage.getConversationId();
20
21 // System.err.println("Got response: " + myConversationId);
22
23 Ticket myTicket = (Ticket)
24 _requests.get(new Integer(myConversationId));
25
26 if (myTicket != null)
27 myTicket.postResponse(myMessage);
28 }
29
30 public void exceptionCaught(IoSession session, Throwable cause) {
31 SessionLog.warn(session, "Server error, disconnecting...", cause);
32 session.close();
33 }
34
35 Ticket getTicket(int aConversationId) {
36 Ticket myTicket = new Ticket(aConversationId);
37
38 _requests.put(new Integer(aConversationId), myTicket);
39 return myTicket;
40 }
41
42 class Ticket {
43 private int _conversationId;
44 private Message _message;
45
46 Ticket(int aConversationId) {
47 _conversationId = aConversationId;
48 }
49
50 void postResponse(Message aMessage) {
51 synchronized(this) {
52 _message = aMessage;
53 notify();
54 }
55 }
56
57 Message getResponse(long aWait) throws RemoteException {
58 try {
59 synchronized(this) {
60 if (_message == null) {
61 wait(aWait);
62 }
63
64 return _message;
65 }
66 } catch(InterruptedException anIE) {
67 throw new RemoteException("interrupted", anIE);
68 } finally {
69 _requests.remove(new Long(_conversationId));
70 }
71 }
72 }
73 }