01: /*
02: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
03: */
04: package com.tc.net.protocol;
05:
06: import com.tc.net.core.TCConnection;
07: import com.tc.net.protocol.tcm.ChannelID;
08: import com.tc.net.protocol.tcm.MessageChannelInternal;
09: 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: public abstract class AbstractNetworkStackHarness implements
16: NetworkStackHarness {
17: protected MessageTransport transport;
18: protected MessageChannelInternal channel;
19: private final ServerMessageChannelFactory channelFactory;
20: private final MessageTransportFactory transportFactory;
21: private final boolean isClientStack;
22: private final SetOnceFlag finalized = new SetOnceFlag();
23:
24: protected AbstractNetworkStackHarness(
25: ServerMessageChannelFactory channelFactory,
26: MessageTransport transport) {
27: this .channelFactory = channelFactory;
28: this .transportFactory = null;
29: this .transport = transport;
30: this .isClientStack = false;
31: }
32:
33: protected AbstractNetworkStackHarness(
34: MessageTransportFactory transportFactory,
35: MessageChannelInternal channel) {
36: this .transportFactory = transportFactory;
37: this .channelFactory = null;
38: this .channel = channel;
39: this .isClientStack = true;
40: }
41:
42: /**
43: * Connects a new transport to an existing stack (server-side).
44: */
45: public final MessageTransport attachNewConnection(
46: TCConnection connection) throws IllegalReconnectException {
47: Assert
48: .eval(
49: "Attempt to connect a transport to a stack that hasn't been finalized.",
50: finalized.isSet());
51: this .transport.attachNewConnection(connection);
52: return this .transport;
53: }
54:
55: /**
56: * Creates and connects a new stack.
57: */
58: public final void finalizeStack() {
59: if (finalized.attemptSet()) {
60: if (isClientStack) {
61: Assert.assertNotNull(this .channel);
62: Assert.assertNotNull(this .transportFactory);
63: this .transport = transportFactory.createNewTransport();
64: } else {
65: Assert.assertNotNull(this .transport);
66: Assert.assertNotNull(this .channelFactory);
67: this .channel = channelFactory
68: .createNewChannel(new ChannelID(this .transport
69: .getConnectionId().getChannelID()));
70: }
71: createIntermediateLayers();
72: connectStack();
73: } else {
74: throw Assert
75: .failure("Attempt to finalize an already finalized stack");
76: }
77: }
78:
79: protected abstract void createIntermediateLayers();
80:
81: protected abstract void connectStack();
82: }
|