KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > net > protocol > delivery > GuaranteedDeliveryProtocol


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.delivery;
5
6 import EDU.oswego.cs.dl.util.concurrent.LinkedQueue;
7
8 import com.tc.async.api.Sink;
9 import com.tc.net.protocol.TCNetworkMessage;
10
11 /**
12  * This implements an asynchronous Once and only once protocol. Sent messages go out on the sent queue received messages
13  * come in to the ProtocolMessageDelivery instance.
14  */

15 class GuaranteedDeliveryProtocol implements DeliveryProtocol {
16   private final StateMachineRunner send;
17   private final StateMachineRunner receive;
18   private final LinkedQueue sendQueue;
19
20   public GuaranteedDeliveryProtocol(OOOProtocolMessageDelivery delivery, Sink workSink, LinkedQueue sendQueue) {
21     this.send = new StateMachineRunner(new SendStateMachine(delivery, sendQueue), workSink);
22     this.receive = new StateMachineRunner(new ReceiveStateMachine(delivery), workSink);
23     this.sendQueue = sendQueue;
24   }
25
26   public void send(TCNetworkMessage message) {
27     try {
28       sendQueue.put(message);
29       send.addEvent(new OOOProtocolEvent());
30     } catch (InterruptedException JavaDoc e) {
31       throw new AssertionError JavaDoc(e);
32     }
33   }
34
35   public void receive(OOOProtocolMessage protocolMessage) {
36     if (protocolMessage.isSend() || protocolMessage.isAckRequest()) {
37       receive.addEvent(new OOOProtocolEvent(protocolMessage));
38     } else {
39       send.addEvent(new OOOProtocolEvent(protocolMessage));
40     }
41   }
42
43   public void start() {
44     send.start();
45     receive.start();
46   }
47
48   public void pause() {
49     send.pause();
50     receive.pause();
51   }
52
53   public void resume() {
54     send.resume();
55     receive.resume();
56   }
57 }
Popular Tags