KickJava   Java API By Example, From Geeks To Geeks.

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


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 EDU.oswego.cs.dl.util.concurrent.LinkedQueue;
7
8 import com.tc.bytes.TCByteBuffer;
9 import com.tc.exception.ImplementMe;
10 import com.tc.io.TCByteBufferOutput;
11 import com.tc.io.TCByteBufferOutputStream;
12 import com.tc.net.TCSocketAddress;
13 import com.tc.net.protocol.NetworkLayer;
14 import com.tc.net.protocol.NetworkStackID;
15 import com.tc.net.protocol.TCNetworkMessage;
16 import com.tc.net.protocol.transport.MessageTransport;
17
18 import java.lang.reflect.Constructor JavaDoc;
19 import java.util.HashMap JavaDoc;
20 import java.util.Map JavaDoc;
21
22 public class MockMessageChannel implements MessageChannelInternal {
23
24   private final ChannelID channelId;
25   private NetworkLayer sendLayer;
26
27   LinkedQueue closedCalls = new LinkedQueue();
28   private long lastClosedCallTimestamp;
29
30   private Map JavaDoc knownMessageTypes;
31
32   private int numSends;
33   private TCNetworkMessage lastSentMessage;
34
35   public MockMessageChannel(ChannelID channelId) {
36     this.channelId = channelId;
37     this.knownMessageTypes = new HashMap JavaDoc();
38     reset();
39   }
40
41   public void registerType(TCMessageType messageType, Class JavaDoc theClass) {
42     this.knownMessageTypes.put(messageType, theClass);
43   }
44
45   public void reset() {
46     this.numSends = 0;
47     this.lastSentMessage = null;
48   }
49
50   public TCNetworkMessage getLastSentMessage() {
51     return lastSentMessage;
52   }
53
54   public int getNumSends() {
55     return numSends;
56   }
57
58   public void addListener(ChannelEventListener listener) {
59     throw new ImplementMe();
60   }
61
62   public boolean isConnected() {
63     throw new ImplementMe();
64   }
65
66   public boolean isOpen() {
67     throw new ImplementMe();
68   }
69
70   public boolean isClosed() {
71     throw new ImplementMe();
72   }
73
74   public void close() {
75     this.lastClosedCallTimestamp = System.currentTimeMillis();
76     try {
77       closedCalls.put(new Object JavaDoc());
78     } catch (InterruptedException JavaDoc e) {
79       e.printStackTrace();
80     }
81   }
82
83   public boolean waitForCloseCall(long timeout) throws InterruptedException JavaDoc {
84     return closedCalls.poll(timeout) != null;
85   }
86
87   public ChannelID getChannelID() {
88     return channelId;
89   }
90
91   public void setSendLayer(NetworkLayer layer) {
92     this.sendLayer = layer;
93   }
94
95   public void setReceiveLayer(NetworkLayer layer) {
96     throw new ImplementMe();
97   }
98
99   public void send(TCNetworkMessage message) {
100     ++this.numSends;
101     this.lastSentMessage = message;
102   }
103
104   public void receive(TCByteBuffer[] msgData) {
105     throw new ImplementMe();
106   }
107
108   public NetworkStackID open() {
109     throw new ImplementMe();
110   }
111
112   public TCMessage createMessage(TCMessageType type) {
113     Class JavaDoc theClass = (Class JavaDoc) this.knownMessageTypes.get(type);
114
115     if (theClass == null) throw new ImplementMe();
116
117     try {
118       Constructor JavaDoc constructor = theClass.getConstructor(new Class JavaDoc[] { MessageMonitor.class, TCByteBufferOutput.class,
119           MessageChannel.class, TCMessageType.class });
120       return (TCMessage) constructor.newInstance(new Object JavaDoc[] { new NullMessageMonitor(),
121           new TCByteBufferOutputStream(4, 4096, false), this, type });
122     } catch (Exception JavaDoc e) {
123       throw new ImplementMe("Failed", e);
124     }
125   }
126
127   public void notifyTransportConnected(MessageTransport transport) {
128     throw new ImplementMe();
129
130   }
131
132   public void notifyTransportDisconnected(MessageTransport transport) {
133     throw new ImplementMe();
134
135   }
136
137   public void notifyTransportConnectAttempt(MessageTransport transport) {
138     throw new ImplementMe();
139
140   }
141
142   public void notifyTransportClosed(MessageTransport transport) {
143     throw new ImplementMe();
144   }
145
146   public long getLastClosedCallTimestamp() {
147     return lastClosedCallTimestamp;
148   }
149
150   public NetworkLayer getSendLayer() {
151     return sendLayer;
152   }
153
154   public Object JavaDoc getAttachment(String JavaDoc key) {
155     throw new ImplementMe();
156   }
157
158   public void addAttachment(String JavaDoc key, Object JavaDoc value, boolean replace) {
159     throw new ImplementMe();
160   }
161
162   public Object JavaDoc removeAttachment(String JavaDoc key) {
163     throw new ImplementMe();
164   }
165
166   public TCSocketAddress getLocalAddress() {
167     throw new ImplementMe();
168   }
169
170   public TCSocketAddress getRemoteAddress() {
171     throw new ImplementMe();
172   }
173
174 }
175
Popular Tags