1 /*
2 * Copyright 2014 The Netty Project
3 *
4 * The Netty Project licenses this file to you under the Apache License,
5 * version 2.0 (the "License"); you may not use this file except in compliance
6 * with the License. You may obtain a copy of the License at:
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 * License for the specific language governing permissions and limitations
14 * under the License.
15 */
16 package io.netty.handler.codec.haproxy;
17
18 import static io.netty.handler.codec.haproxy.HAProxyConstants.*;
19
20 /**
21 * The HAProxy proxy protocol specification version.
22 */
23 public enum HAProxyProtocolVersion {
24 /**
25 * The ONE proxy protocol version represents a version 1 (human-readable) header.
26 */
27 V1(VERSION_ONE_BYTE),
28 /**
29 * The TWO proxy protocol version represents a version 2 (binary) header.
30 */
31 V2(VERSION_TWO_BYTE);
32
33 /**
34 * The highest 4 bits of the protocol version and command byte contain the version
35 */
36 private static final byte VERSION_MASK = (byte) 0xf0;
37
38 private final byte byteValue;
39
40 /**
41 * Creates a new instance
42 */
43 HAProxyProtocolVersion(byte byteValue) {
44 this.byteValue = byteValue;
45 }
46
47 /**
48 * Returns the {@link HAProxyProtocolVersion} represented by the highest 4 bits of the specified byte.
49 *
50 * @param verCmdByte protocol version and command byte
51 */
52 public static HAProxyProtocolVersion valueOf(byte verCmdByte) {
53 int version = verCmdByte & VERSION_MASK;
54 switch ((byte) version) {
55 case VERSION_TWO_BYTE:
56 return V2;
57 case VERSION_ONE_BYTE:
58 return V1;
59 default:
60 throw new IllegalArgumentException("unknown version: " + version);
61 }
62 }
63
64 /**
65 * Returns the byte value of this version.
66 */
67 public byte byteValue() {
68 return byteValue;
69 }
70 }