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.bytes.TCByteBuffer;
07: import com.tc.logging.TCLogger;
08: import com.tc.logging.TCLogging;
09: import com.tc.net.core.TCConnection;
10:
11: /**
12: * A generic protocol adaptor (only useful for testing)
13: *
14: * @author teck
15: */
16: public class GenericProtocolAdaptor extends AbstractTCProtocolAdaptor {
17: private final static TCLogger logger = TCLogging
18: .getLogger(GenericProtocolAdaptor.class);
19: private final GenericNetworkMessageSink sink;
20:
21: public GenericProtocolAdaptor(GenericNetworkMessageSink sink) {
22: super (logger);
23: this .sink = sink;
24: }
25:
26: protected TCNetworkMessage createMessage(TCConnection conn,
27: TCNetworkHeader hdr, TCByteBuffer[] data) {
28: GenericNetworkMessage rv = new GenericNetworkMessage(conn, hdr,
29: data);
30: return rv;
31: }
32:
33: protected AbstractTCNetworkHeader getNewProtocolHeader() {
34: return new GenericNetworkHeader();
35: }
36:
37: protected int computeDataLength(TCNetworkHeader hdr) {
38: return ((GenericNetworkHeader) hdr).getMessageDataLength();
39: }
40:
41: public void addReadData(TCConnection source, TCByteBuffer[] data,
42: int length) throws TCProtocolException {
43: final boolean msgDone = processIncomingData(source, data,
44: length);
45:
46: if (msgDone) {
47: try {
48: GenericNetworkMessage msg = (GenericNetworkMessage) collectMessage();
49: sink.putMessage(msg);
50: } finally {
51: init();
52: }
53: }
54:
55: return;
56: }
57: }
|