KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > net > protocol > AbstractNetworkStackHarness


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;
5
6 import com.tc.net.core.TCConnection;
7 import com.tc.net.protocol.tcm.ChannelID;
8 import com.tc.net.protocol.tcm.MessageChannelInternal;
9 import com.tc.net.protocol.tcm.ServerMessageChannelFactory;
10 import com.tc.net.protocol.transport.MessageTransport;
11 import com.tc.net.protocol.transport.MessageTransportFactory;
12 import com.tc.util.Assert;
13 import com.tc.util.concurrent.SetOnceFlag;
14
15 abstract class AbstractNetworkStackHarness implements NetworkStackHarness {
16   protected MessageTransport transport;
17   protected MessageChannelInternal channel;
18   private final ServerMessageChannelFactory channelFactory;
19   private final MessageTransportFactory transportFactory;
20   private final boolean isClientStack;
21   private final SetOnceFlag finalized = new SetOnceFlag();
22
23   AbstractNetworkStackHarness(ServerMessageChannelFactory channelFactory, MessageTransport transport) {
24     this.channelFactory = channelFactory;
25     this.transportFactory = null;
26     this.transport = transport;
27     this.isClientStack = false;
28   }
29
30   AbstractNetworkStackHarness(MessageTransportFactory transportFactory, MessageChannelInternal channel) {
31     this.transportFactory = transportFactory;
32     this.channelFactory = null;
33     this.channel = channel;
34     this.isClientStack = true;
35   }
36
37   /**
38    * Connects a new transport to an existing stack (server-side).
39    */

40   public MessageTransport attachNewConnection(TCConnection connection) {
41     Assert.eval("Attempt to connect a transport to a stack that hasn't been finalized.", finalized.isSet());
42     this.transport.attachNewConnection(connection);
43     return this.transport;
44   }
45
46   /**
47    * Creates and connects a new stack.
48    */

49   public void finalizeStack() {
50     if (finalized.attemptSet()) {
51       if (isClientStack) {
52         Assert.assertNotNull(this.channel);
53         Assert.assertNotNull(this.transportFactory);
54         this.transport = transportFactory.createNewTransport();
55       } else {
56         Assert.assertNotNull(this.transport);
57         Assert.assertNotNull(this.channelFactory);
58         this.channel = channelFactory.createNewChannel(new ChannelID(this.transport.getConnectionId().getChannelID()));
59       }
60       createIntermediateLayers();
61       connectStack();
62     } else {
63       throw Assert.failure("Attempt to finalize an already finalized stack");
64     }
65   }
66
67   protected abstract void createIntermediateLayers();
68
69   protected abstract void connectStack();
70 }
71
Popular Tags