KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > net > protocol > transport > ServerHandshakeMessageResponder


1 /*
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
3  */

4 package com.tc.net.protocol.transport;
5
6 import EDU.oswego.cs.dl.util.concurrent.LinkedQueue;
7 import EDU.oswego.cs.dl.util.concurrent.SynchronizedRef;
8
9 import com.tc.net.protocol.transport.TransportHandshakeMessage;
10
11 import junit.framework.Assert;
12
13 class ServerHandshakeMessageResponder extends HandshakeMessageResponderBase {
14
15   private LinkedQueue synAckErrors = new LinkedQueue();
16
17   protected ServerHandshakeMessageResponder(LinkedQueue sentQueue, LinkedQueue receivedQueue,
18                                             TransportHandshakeMessageFactory messageFactory,
19                                             ConnectionID assignedConnectionId, MessageTransportBase transport,
20                                             SynchronizedRef errorRef) {
21     super(sentQueue, receivedQueue, messageFactory, assignedConnectionId, transport, errorRef);
22   }
23
24   public void handleHandshakeMessage(final TransportHandshakeMessage message) {
25     try {
26       if (message.isSynAck()) {
27         Assert.assertNotNull(message.getConnectionId());
28         Assert.assertEquals(this.assignedConnectionId, message.getConnectionId());
29         final SynAckMessage synAck = (SynAckMessage)message;
30         if (synAck.hasErrorContext()) {
31           this.synAckErrors.put(synAck.getErrorContext());
32         } else {
33           TransportHandshakeMessage ack = messageFactory.createAck(this.assignedConnectionId, synAck.getSource());
34           this.sendResponseMessage(ack);
35         }
36       } else {
37         Assert.fail("Recieved an unexpected message type: " + message);
38       }
39     } catch (Exception JavaDoc e) {
40       setError(e);
41     }
42   }
43
44   public boolean wasSynAckReceived(long timeout) throws Exception JavaDoc {
45     TransportHandshakeMessage message = (TransportHandshakeMessage) this.receivedQueue.poll(timeout);
46     return message != null && message.isSynAck();
47   }
48
49   public String JavaDoc waitForSynAckErrorToBeReceived(long timeout) throws InterruptedException JavaDoc {
50     return (String JavaDoc) this.synAckErrors.poll(timeout);
51   }
52
53 }
54
Popular Tags