1 4 package com.tc.net.protocol.transport; 5 6 import com.tc.bytes.TCByteBuffer; 7 import com.tc.io.TCByteBufferInputStream; 8 import com.tc.net.core.TCConnection; 9 import com.tc.net.protocol.TCNetworkHeader; 10 import com.tc.net.protocol.TCProtocolException; 11 import com.tc.util.Assert; 12 13 import java.io.IOException ; 14 15 class TransportHandshakeMessageImpl extends WireProtocolMessageImpl implements SynMessage, SynAckMessage, AckMessage { 16 static final byte VERSION_1 = 1; 17 18 static final byte SYN = 1; 19 static final byte ACK = 2; 20 static final byte SYN_ACK = 3; 21 22 private final byte version; 23 private final byte type; 24 private final ConnectionID connectionId; 25 private final String errorContext; 26 private final boolean hasErrorContext; 27 private final int maxConnections; 28 private final boolean isMaxConnectionsExceeded; 29 30 TransportHandshakeMessageImpl(TCConnection source, TCNetworkHeader header, TCByteBuffer[] payload) 31 throws TCProtocolException { 32 super(source, header, payload); 33 34 try { 35 TCByteBufferInputStream in = new TCByteBufferInputStream(payload); 36 this.version = in.readByte(); 37 38 if (version != VERSION_1) { throw new TCProtocolException("Bad Version: " + version + " != " + VERSION_1); } 39 40 this.type = in.readByte(); 41 42 try { 43 this.connectionId = ConnectionID.parse(in.readString()); 44 } catch (InvalidConnectionIDException e) { 45 throw new TCProtocolException(e); 46 } 47 48 this.isMaxConnectionsExceeded = in.readBoolean(); 49 this.maxConnections = in.readInt(); 50 this.hasErrorContext = in.readBoolean(); 51 52 if (this.hasErrorContext) { 53 this.errorContext = in.readString(); 54 } else { 55 this.errorContext = null; 56 } 57 58 } catch (IOException e) { 59 throw new TCProtocolException("IOException reading data: " + e.getMessage()); 60 } 61 } 62 63 public void doRecycleOnWrite() { 64 recycle(); 65 } 66 67 protected String describePayload() { 68 return "type: " + typeToString() + ", connectionId: " + connectionId + ", errorContext " + errorContext + "\n"; 69 } 70 71 private String typeToString() { 72 switch (type) { 73 case SYN: 74 return "SYN"; 75 case ACK: 76 return "ACK"; 77 case SYN_ACK: 78 return "SYN_ACK"; 79 default: 80 return "UNKNOWN"; 81 } 82 } 83 84 public ConnectionID getConnectionId() { 85 return this.connectionId; 86 } 87 88 public boolean hasErrorContext() { 89 return this.hasErrorContext; 90 } 91 92 public String getErrorContext() { 93 Assert.eval(hasErrorContext()); 94 return this.errorContext; 95 } 96 97 public boolean isSynAck() { 98 return type == SYN_ACK; 99 } 100 101 public boolean isSyn() { 102 return type == SYN; 103 } 104 105 public boolean isAck() { 106 return type == ACK; 107 } 108 109 public boolean hasDefaultConnectionId() { 110 Assert.assertNotNull(connectionId); 111 return this.connectionId.isNull(); 112 } 113 114 public boolean isMaxConnectionsExceeded() { 115 return this.isMaxConnectionsExceeded; 116 } 117 118 public int getMaxConnections() { 119 return this.maxConnections; 120 } 121 122 } | Popular Tags |