001: /*
002: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
003: */
004: package com.tc.net.protocol.tcm;
005:
006: import EDU.oswego.cs.dl.util.concurrent.LinkedQueue;
007:
008: import com.tc.bytes.TCByteBuffer;
009: import com.tc.exception.ImplementMe;
010: import com.tc.io.TCByteBufferOutput;
011: import com.tc.io.TCByteBufferOutputStream;
012: import com.tc.net.TCSocketAddress;
013: import com.tc.net.protocol.NetworkLayer;
014: import com.tc.net.protocol.NetworkStackID;
015: import com.tc.net.protocol.TCNetworkMessage;
016: import com.tc.net.protocol.transport.MessageTransport;
017:
018: import java.lang.reflect.Constructor;
019: import java.util.HashMap;
020: import java.util.Map;
021:
022: public class MockMessageChannel implements MessageChannelInternal {
023:
024: private final ChannelID channelId;
025: private NetworkLayer sendLayer;
026:
027: LinkedQueue closedCalls = new LinkedQueue();
028: private long lastClosedCallTimestamp;
029:
030: private Map knownMessageTypes;
031:
032: private int numSends;
033: private TCNetworkMessage lastSentMessage;
034:
035: public MockMessageChannel(ChannelID channelId) {
036: this .channelId = channelId;
037: this .knownMessageTypes = new HashMap();
038: reset();
039: }
040:
041: public void registerType(TCMessageType messageType, Class theClass) {
042: this .knownMessageTypes.put(messageType, theClass);
043: }
044:
045: public void reset() {
046: this .numSends = 0;
047: this .lastSentMessage = null;
048: }
049:
050: public TCNetworkMessage getLastSentMessage() {
051: return lastSentMessage;
052: }
053:
054: public int getNumSends() {
055: return numSends;
056: }
057:
058: public void addListener(ChannelEventListener listener) {
059: throw new ImplementMe();
060: }
061:
062: public boolean isConnected() {
063: throw new ImplementMe();
064: }
065:
066: public boolean isOpen() {
067: throw new ImplementMe();
068: }
069:
070: public boolean isClosed() {
071: throw new ImplementMe();
072: }
073:
074: public void close() {
075: this .lastClosedCallTimestamp = System.currentTimeMillis();
076: try {
077: closedCalls.put(new Object());
078: } catch (InterruptedException e) {
079: e.printStackTrace();
080: }
081: }
082:
083: public boolean waitForCloseCall(long timeout)
084: throws InterruptedException {
085: return closedCalls.poll(timeout) != null;
086: }
087:
088: public ChannelID getChannelID() {
089: return channelId;
090: }
091:
092: public void setSendLayer(NetworkLayer layer) {
093: this .sendLayer = layer;
094: }
095:
096: public void setReceiveLayer(NetworkLayer layer) {
097: throw new ImplementMe();
098: }
099:
100: public void send(TCNetworkMessage message) {
101: ++this .numSends;
102: this .lastSentMessage = message;
103: }
104:
105: public void receive(TCByteBuffer[] msgData) {
106: throw new ImplementMe();
107: }
108:
109: public NetworkStackID open() {
110: throw new ImplementMe();
111: }
112:
113: public Class getRegisteredMessageClass(TCMessageType type) {
114: return (Class) this .knownMessageTypes.get(type);
115: }
116:
117: public TCMessage createMessage(TCMessageType type) {
118: Class theClass = (Class) this .knownMessageTypes.get(type);
119:
120: if (theClass == null)
121: throw new ImplementMe();
122:
123: try {
124: Constructor constructor = theClass
125: .getConstructor(new Class[] { MessageMonitor.class,
126: TCByteBufferOutput.class,
127: MessageChannel.class, TCMessageType.class });
128: return (TCMessage) constructor.newInstance(new Object[] {
129: new NullMessageMonitor(),
130: new TCByteBufferOutputStream(4, 4096, false), this ,
131: type });
132: } catch (Exception e) {
133: throw new ImplementMe("Failed", e);
134: }
135: }
136:
137: public void notifyTransportConnected(MessageTransport transport) {
138: throw new ImplementMe();
139:
140: }
141:
142: public void notifyTransportDisconnected(MessageTransport transport) {
143: throw new ImplementMe();
144:
145: }
146:
147: public void notifyTransportConnectAttempt(MessageTransport transport) {
148: throw new ImplementMe();
149:
150: }
151:
152: public void notifyTransportClosed(MessageTransport transport) {
153: throw new ImplementMe();
154: }
155:
156: public long getLastClosedCallTimestamp() {
157: return lastClosedCallTimestamp;
158: }
159:
160: public NetworkLayer getSendLayer() {
161: return sendLayer;
162: }
163:
164: public Object getAttachment(String key) {
165: throw new ImplementMe();
166: }
167:
168: public void addAttachment(String key, Object value, boolean replace) {
169: throw new ImplementMe();
170: }
171:
172: public Object removeAttachment(String key) {
173: throw new ImplementMe();
174: }
175:
176: public TCSocketAddress getLocalAddress() {
177: throw new ImplementMe();
178: }
179:
180: public TCSocketAddress getRemoteAddress() {
181: throw new ImplementMe();
182: }
183:
184: }
|