Change build scripts
[jvcard.git] / src / com / googlecode / lanterna / terminal / ansi / TelnetProtocol.java
1 /*
2 * This file is part of lanterna (http://code.google.com/p/lanterna/).
3 *
4 * lanterna is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU Lesser General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 *
17 * Copyright (C) 2010-2015 Martin
18 */
19 package com.googlecode.lanterna.terminal.ansi;
20
21 import java.lang.reflect.Field;
22 import java.util.Collections;
23 import java.util.HashMap;
24 import java.util.Map;
25
26 /**
27 * Contains the telnet protocol commands, although not a complete set.
28 * @author Martin
29 */
30 class TelnetProtocol {
31 public static final byte COMMAND_SUBNEGOTIATION_END = (byte)0xf0; //SE
32 public static final byte COMMAND_NO_OPERATION = (byte)0xf1; //NOP
33 public static final byte COMMAND_DATA_MARK = (byte)0xf2; //DM
34 public static final byte COMMAND_BREAK = (byte)0xf3; //BRK
35 public static final byte COMMAND_INTERRUPT_PROCESS = (byte)0xf4; //IP
36 public static final byte COMMAND_ABORT_OUTPUT = (byte)0xf5; //AO
37 public static final byte COMMAND_ARE_YOU_THERE = (byte)0xf6; //AYT
38 public static final byte COMMAND_ERASE_CHARACTER = (byte)0xf7; //EC
39 public static final byte COMMAND_ERASE_LINE = (byte)0xf8; //WL
40 public static final byte COMMAND_GO_AHEAD = (byte)0xf9; //GA
41 public static final byte COMMAND_SUBNEGOTIATION = (byte)0xfa; //SB
42 public static final byte COMMAND_WILL = (byte)0xfb;
43 public static final byte COMMAND_WONT = (byte)0xfc;
44 public static final byte COMMAND_DO = (byte)0xfd;
45 public static final byte COMMAND_DONT = (byte)0xfe;
46 public static final byte COMMAND_IAC = (byte)0xff;
47
48 public static final byte OPTION_TRANSMIT_BINARY = (byte)0x00;
49 public static final byte OPTION_ECHO = (byte)0x01;
50 public static final byte OPTION_SUPPRESS_GO_AHEAD = (byte)0x03;
51 public static final byte OPTION_STATUS = (byte)0x05;
52 public static final byte OPTION_TIMING_MARK = (byte)0x06;
53 public static final byte OPTION_NAOCRD = (byte)0x0a;
54 public static final byte OPTION_NAOHTS = (byte)0x0b;
55 public static final byte OPTION_NAOHTD = (byte)0x0c;
56 public static final byte OPTION_NAOFFD = (byte)0x0d;
57 public static final byte OPTION_NAOVTS = (byte)0x0e;
58 public static final byte OPTION_NAOVTD = (byte)0x0f;
59 public static final byte OPTION_NAOLFD = (byte)0x10;
60 public static final byte OPTION_EXTEND_ASCII = (byte)0x01;
61 public static final byte OPTION_TERMINAL_TYPE = (byte)0x18;
62 public static final byte OPTION_NAWS = (byte)0x1f;
63 public static final byte OPTION_TERMINAL_SPEED = (byte)0x20;
64 public static final byte OPTION_TOGGLE_FLOW_CONTROL = (byte)0x21;
65 public static final byte OPTION_LINEMODE = (byte)0x22;
66 public static final byte OPTION_AUTHENTICATION = (byte)0x25;
67
68 public static final Map<String, Byte> NAME_TO_CODE = createName2CodeMap();
69 public static final Map<Byte, String> CODE_TO_NAME = reverseMap(NAME_TO_CODE);
70
71 private static Map<String, Byte> createName2CodeMap() {
72 Map<String, Byte> result = new HashMap<String, Byte>();
73 for(Field field: TelnetProtocol.class.getDeclaredFields()) {
74 if(field.getType() != byte.class || (!field.getName().startsWith("COMMAND_") && !field.getName().startsWith("OPTION_"))) {
75 continue;
76 }
77 try {
78 String namePart = field.getName().substring(field.getName().indexOf("_") + 1);
79 result.put(namePart, (Byte)field.get(null));
80 }
81 catch(IllegalAccessException ignored) {
82 }
83 catch(IllegalArgumentException ignored) {
84 }
85 }
86 return Collections.unmodifiableMap(result);
87 }
88
89 private static <V,K> Map<V,K> reverseMap(Map<K,V> n2c) {
90 Map<V, K> result = new HashMap<V,K>();
91 for (Map.Entry<K, V> e : n2c.entrySet()) {
92 result.put(e.getValue(), e.getKey());
93 }
94 return Collections.unmodifiableMap(result);
95 }
96 /** Cannot instantiate. */
97 private TelnetProtocol() {}
98 }