KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > net > protocol > tcm > ClientMessageChannelImpl


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.tcm;
5
6 import com.tc.logging.TCLogger;
7 import com.tc.logging.TCLogging;
8 import com.tc.net.MaxConnectionsExceededException;
9 import com.tc.net.core.ConnectionInfo;
10 import com.tc.net.protocol.NetworkStackID;
11 import com.tc.net.protocol.transport.MessageTransport;
12 import com.tc.util.TCTimeoutException;
13
14 import java.io.IOException JavaDoc;
15 import java.net.UnknownHostException JavaDoc;
16
17 /**
18  * @author orion
19  */

20
21 public class ClientMessageChannelImpl extends AbstractMessageChannel implements ClientMessageChannel {
22   private static final TCLogger logger = TCLogging.getLogger(ClientMessageChannel.class);
23   private final ConnectionInfo connInfo;
24   private final TCMessageFactory msgFactory;
25   private int connectAttemptCount;
26   private int connectCount;
27   private ChannelID channelID;
28   private final ChannelIDProviderImpl cidProvider;
29
30   protected ClientMessageChannelImpl(ConnectionInfo connInfo, TCMessageFactory msgFactory, TCMessageRouter router) {
31     super(router, logger, msgFactory);
32     this.connInfo = connInfo;
33     this.msgFactory = msgFactory;
34     this.cidProvider = new ChannelIDProviderImpl();
35   }
36
37   public NetworkStackID open() throws TCTimeoutException, UnknownHostException JavaDoc, IOException JavaDoc, MaxConnectionsExceededException {
38     final ChannelStatus status = getStatus();
39
40     synchronized (status) {
41       if (status.isOpen()) { throw new IllegalStateException JavaDoc("Channel already open"); }
42       NetworkStackID id = this.sendLayer.open();
43       getStatus().open();
44       this.channelID = new ChannelID(id.toLong());
45       this.cidProvider.setChannelID(this.channelID);
46       return id;
47     }
48   }
49
50   public void addClassMapping(TCMessageType type, Class JavaDoc msgClass) {
51     msgFactory.addClassMapping(type, msgClass);
52   }
53
54   public String JavaDoc getHostname() {
55     return this.connInfo.getHostname();
56   }
57
58   public int getPort() {
59     return this.connInfo.getPort();
60   }
61
62   public ChannelID getChannelID() {
63     final ChannelStatus status = getStatus();
64     synchronized (status) {
65       if (!status.isOpen()) {
66         throw new IllegalStateException JavaDoc("Attempt to get the channel ID of an unopened channel.");
67       } else {
68         return this.channelID;
69       }
70     }
71   }
72
73   public int getConnectCount() {
74     return connectCount;
75   }
76
77   public int getConnectAttemptCount() {
78     return this.connectAttemptCount;
79   }
80
81   public void notifyTransportConnected(MessageTransport transport) {
82     super.notifyTransportConnected(transport);
83     connectCount++;
84   }
85
86   public void notifyTransportDisconnected(MessageTransport transport) {
87     this.fireTransportDisconnectedEvent();
88   }
89
90   public void notifyTransportConnectAttempt(MessageTransport transport) {
91     super.notifyTransportConnectAttempt(transport);
92     connectAttemptCount++;
93   }
94
95   public void notifyTransportClosed(MessageTransport transport) {
96     //
97
}
98
99   public ChannelIDProvider getChannelIDProvider() {
100     return cidProvider;
101   }
102
103   private static class ChannelIDProviderImpl implements ChannelIDProvider {
104
105     private ChannelID channelID = ChannelID.NULL_ID;
106     
107     private synchronized void setChannelID(ChannelID channelID) {
108       this.channelID = channelID;
109     }
110     
111     public synchronized ChannelID getChannelID() {
112       return this.channelID;
113     }
114     
115   }
116   
117 }
Popular Tags