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