KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > net > protocol > transport > TransportHandshakeMessageFactoryImpl


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.transport;
5
6 import com.tc.exception.TCInternalError;
7 import com.tc.io.TCByteBufferOutputStream;
8 import com.tc.net.core.TCConnection;
9 import com.tc.net.protocol.TCProtocolException;
10
11 public class TransportHandshakeMessageFactoryImpl implements TransportHandshakeMessageFactory {
12   public static final ConnectionID DEFAULT_ID = ConnectionID.NULL_ID;
13
14   public TransportHandshakeMessage createSyn(ConnectionID connectionId, TCConnection source) {
15     return createNewMessage(TransportHandshakeMessageImpl.SYN, connectionId, null, source, false, 0);
16   }
17
18   public TransportHandshakeMessage createAck(ConnectionID connectionId, TCConnection source) {
19     return createNewMessage(TransportHandshakeMessageImpl.ACK, connectionId, null, source, false, 0);
20   }
21
22   public TransportHandshakeMessage createSynAck(ConnectionID connectionId, TCConnection source,
23                                                 boolean isMaxConnectionsExceeded, int maxConnections) {
24     return createSynAck(connectionId, null, source, isMaxConnectionsExceeded, maxConnections);
25   }
26
27   public TransportHandshakeMessage createSynAck(ConnectionID connectionId, TransportHandshakeErrorContext errorContext,
28                                                 TCConnection source, boolean isMaxConnectionsExceeded,
29                                                 int maxConnections) {
30     return createNewMessage(TransportHandshakeMessageImpl.SYN_ACK, connectionId, errorContext, source,
31                             isMaxConnectionsExceeded, maxConnections);
32   }
33
34   private TransportHandshakeMessage createNewMessage(byte type, ConnectionID connectionId,
35                                                      TransportHandshakeErrorContext errorContext, TCConnection source,
36                                                      boolean isMaxConnectionsExceeded, int maxConnections) {
37     TCByteBufferOutputStream bbos = new TCByteBufferOutputStream();
38
39     bbos.write(TransportHandshakeMessageImpl.VERSION_1);
40     bbos.write(type);
41     bbos.writeString(connectionId.getID());
42     bbos.writeBoolean(isMaxConnectionsExceeded);
43     bbos.writeInt(maxConnections);
44     bbos.writeBoolean(errorContext != null);
45     if (errorContext != null) bbos.writeString(errorContext.toString());
46
47     final WireProtocolHeader header = new WireProtocolHeader();
48     header.setProtocol(WireProtocolHeader.PROTOCOL_TRANSPORT_HANDSHAKE);
49
50     final TransportHandshakeMessageImpl message;
51     try {
52       message = new TransportHandshakeMessageImpl(source, header, bbos.toArray());
53     } catch (TCProtocolException e) {
54       throw new TCInternalError(e);
55     }
56     return message;
57   }
58 }
Popular Tags