KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > net > protocol > delivery > OOOProtocolMessageImpl


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.delivery;
5
6 import com.tc.bytes.TCByteBuffer;
7 import com.tc.net.protocol.AbstractTCNetworkMessage;
8 import com.tc.net.protocol.TCNetworkMessage;
9 import com.tc.net.protocol.TCProtocolException;
10 import com.tc.net.protocol.delivery.OOOProtocolMessageHeader.ProtocolMessageHeaderFactory;
11 import com.tc.util.Assert;
12
13 class OOOProtocolMessageImpl extends AbstractTCNetworkMessage implements OOOProtocolMessage {
14
15   /**
16    * Create a header-only message (no payload). Useful for ack and ack request messages.
17    */

18   private OOOProtocolMessageImpl(OOOProtocolMessageHeader header) {
19     super(header);
20   }
21
22   /**
23    * Create a message with the given payload from the network. Useful for propogating messages up the network stack.
24    */

25   private OOOProtocolMessageImpl(OOOProtocolMessageHeader header, TCByteBuffer[] payload) {
26     super(header, payload);
27   }
28
29   /**
30    * Create a message with the given TCNetworkMessage payload. Useful for propogating messages down the network stack.
31    */

32   private OOOProtocolMessageImpl(OOOProtocolMessageHeader header, TCNetworkMessage msgPayload) {
33     super(header, msgPayload);
34   }
35
36   private OOOProtocolMessageHeader getOOOPHeader() {
37     return (OOOProtocolMessageHeader) getHeader();
38   }
39
40   public long getAckSequence() {
41     return getOOOPHeader().getSequence();
42   }
43
44   public long getSent() {
45     return getOOOPHeader().getSequence();
46   }
47
48   public boolean isAckRequest() {
49     return getOOOPHeader().isAckRequest();
50   }
51
52   public boolean isSend() {
53     return getOOOPHeader().isSend();
54   }
55
56   public static class ProtocolMessageParserImpl implements OOOProtocolMessageParser {
57     private final ProtocolMessageHeaderFactory headerFactory;
58     private final OOOProtocolMessageFactory messageFactory;
59
60     public ProtocolMessageParserImpl(ProtocolMessageHeaderFactory headerFactory, OOOProtocolMessageFactory messageFactory) {
61       this.headerFactory = headerFactory;
62       this.messageFactory = messageFactory;
63     }
64
65     public OOOProtocolMessage parseMessage(TCByteBuffer[] data) throws TCProtocolException {
66       int hdrLength = OOOProtocolMessageHeader.HEADER_LENGTH;
67       if (hdrLength > data[0].limit()) { throw new TCProtocolException("header not contained in first buffer: "
68                                                                        + hdrLength + " > " + data[0].limit()); }
69
70       OOOProtocolMessageHeader header = headerFactory.createNewHeader(data[0]);
71       header.validate();
72
73       TCByteBuffer msgData[];
74       if (header.getHeaderByteLength() < data[0].limit()) {
75         msgData = new TCByteBuffer[data.length];
76         System.arraycopy(data, 0, msgData, 0, msgData.length);
77
78         TCByteBuffer firstPayloadBuffer = msgData[0].duplicate();
79         firstPayloadBuffer.position(header.getHeaderByteLength());
80         msgData[0] = firstPayloadBuffer.slice();
81       } else {
82         Assert.eval(data.length >= 1);
83         msgData = new TCByteBuffer[data.length - 1];
84         System.arraycopy(data, 1, msgData, 0, msgData.length);
85       }
86
87       return messageFactory.createNewMessage(header, msgData);
88     }
89   }
90
91   public static class ProtocolMessageFactoryImpl implements OOOProtocolMessageFactory {
92
93     private final ProtocolMessageHeaderFactory headerFactory;
94
95     public ProtocolMessageFactoryImpl(ProtocolMessageHeaderFactory headerFactory) {
96       this.headerFactory = headerFactory;
97     }
98
99     public OOOProtocolMessage createNewAckRequestMessage() {
100       return new OOOProtocolMessageImpl(headerFactory.createNewAckRequest());
101     }
102
103     public OOOProtocolMessage createNewAckMessage(long sequence) {
104       return new OOOProtocolMessageImpl(headerFactory.createNewAck(sequence));
105     }
106
107     public OOOProtocolMessage createNewSendMessage(long sequence, TCNetworkMessage payload) {
108       return new OOOProtocolMessageImpl(headerFactory.createNewSend(sequence), payload);
109     }
110
111     public OOOProtocolMessage createNewMessage(OOOProtocolMessageHeader header, TCByteBuffer[] data) {
112       return new OOOProtocolMessageImpl(header, data);
113     }
114   }
115
116 }
Popular Tags