KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > net > protocol > GenericProtocolAdaptor


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;
5
6 import com.tc.bytes.TCByteBuffer;
7 import com.tc.logging.TCLogger;
8 import com.tc.logging.TCLogging;
9 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.getLogger(GenericProtocolAdaptor.class);
18   private final GenericNetworkMessageSink sink;
19
20   public GenericProtocolAdaptor(GenericNetworkMessageSink sink) {
21     super(logger);
22     this.sink = sink;
23   }
24
25   protected TCNetworkMessage createMessage(TCConnection conn, TCNetworkHeader hdr, TCByteBuffer[] data) {
26     GenericNetworkMessage rv = new GenericNetworkMessage(conn, hdr, data);
27     return rv;
28   }
29
30   protected AbstractTCNetworkHeader getNewProtocolHeader() {
31     return new GenericNetworkHeader();
32   }
33
34   protected int computeDataLength(TCNetworkHeader hdr) {
35     return ((GenericNetworkHeader) hdr).getMessageDataLength();
36   }
37
38   public void addReadData(TCConnection source, TCByteBuffer[] data, int length) throws TCProtocolException {
39     final boolean msgDone = processIncomingData(source, data, length);
40
41     if (msgDone) {
42       try {
43         GenericNetworkMessage msg = (GenericNetworkMessage) collectMessage();
44         sink.putMessage(msg);
45       } finally {
46         init();
47       }
48     }
49
50     return;
51   }
52 }
Popular Tags