comparison src/main/java/com/glavsoft/rfb/protocol/state/HandshakeState.java @ 0:4689cc86d6cb

create TreeViewer2 Repository
author Yu Taninari <you@cr.ie.u-ryukyu.ac.jp>
date Tue, 03 Jul 2012 13:20:49 +0900
parents
children e7ce2b2ffed8 472a9bcacb21
comparison
equal deleted inserted replaced
-1:000000000000 0:4689cc86d6cb
1 // Copyright (C) 2010, 2011 GlavSoft LLC.
2 // All rights reserved.
3 //
4 //-------------------------------------------------------------------------
5 // This file is part of the TightVNC software. Please visit our Web site:
6 //
7 // http://www.tightvnc.com/
8 //
9 // This program is free software; you can redistribute it and/or modify
10 // it under the terms of the GNU General Public License as published by
11 // the Free Software Foundation; either version 2 of the License, or
12 // (at your option) any later version.
13 //
14 // This program is distributed in the hope that it will be useful,
15 // but WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 // GNU General Public License for more details.
18 //
19 // You should have received a copy of the GNU General Public License along
20 // with this program; if not, write to the Free Software Foundation, Inc.,
21 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 //-------------------------------------------------------------------------
23 //
24
25 package com.glavsoft.rfb.protocol.state;
26
27 import com.glavsoft.exceptions.TransportException;
28 import com.glavsoft.exceptions.UnsupportedProtocolVersionException;
29 import com.glavsoft.rfb.protocol.ProtocolContext;
30
31 import java.util.regex.Matcher;
32 import java.util.regex.Pattern;
33
34 public class HandshakeState extends ProtocolState {
35
36 public static final String PROTOCOL_VERSION_3_8 = "3.8";
37 public static final String PROTOCOL_VERSION_3_7 = "3.7";
38 public static final String PROTOCOL_VERSION_3_3 = "3.3";
39 private static final int PROTOCOL_STRING_LENGTH = 12;
40 private static final String PROTOCOL_STRING_REGEXP = "^RFB (\\d\\d\\d).(\\d\\d\\d)\n$";
41
42 private static final int MIN_SUPPORTED_VERSION_MAJOR = 3;
43 private static final int MIN_SUPPORTED_VERSION_MINOR = 3;
44
45 private static final int MAX_SUPPORTED_VERSION_MAJOR = 3;
46 private static final int MAX_SUPPORTED_VERSION_MINOR = 8;
47
48 public HandshakeState(ProtocolContext context) {
49 super(context);
50 }
51
52 @Override
53 public boolean next() throws UnsupportedProtocolVersionException, TransportException {
54 handshake();
55 return true;
56 }
57
58 private void handshake() throws TransportException, UnsupportedProtocolVersionException {
59 String protocolString = reader.readString(PROTOCOL_STRING_LENGTH);
60 logger.info("Server sent protocol string: " + protocolString.substring(0, protocolString.length() - 1));
61 Pattern pattern = Pattern.compile(PROTOCOL_STRING_REGEXP);
62 final Matcher matcher = pattern.matcher(protocolString);
63 if ( ! matcher.matches())
64 throw new UnsupportedProtocolVersionException(
65 "Unsupported protocol version: " + protocolString);
66 int major = Integer.parseInt(matcher.group(1));
67 int minor = Integer.parseInt(matcher.group(2));
68 if (major < MIN_SUPPORTED_VERSION_MAJOR ||
69 MIN_SUPPORTED_VERSION_MAJOR == major && minor <MIN_SUPPORTED_VERSION_MINOR)
70 throw new UnsupportedProtocolVersionException(
71 "Unsupported protocol version: " + major + "." + minor);
72 if (major > MAX_SUPPORTED_VERSION_MAJOR) {
73 major = MAX_SUPPORTED_VERSION_MAJOR;
74 minor = MAX_SUPPORTED_VERSION_MINOR;
75 }
76
77 if (minor >= MIN_SUPPORTED_VERSION_MINOR && minor < 7) {
78 changeStateTo(new SecurityType33State(context));
79 context.getSettings().setProtocolVersion(PROTOCOL_VERSION_3_3);
80 minor = 3;
81 } else if (7 == minor) {
82 changeStateTo(new SecurityType37State(context));
83 context.getSettings().setProtocolVersion(PROTOCOL_VERSION_3_7);
84 minor = 7;
85 } else if (minor >= MAX_SUPPORTED_VERSION_MINOR) {
86 changeStateTo(new SecurityTypeState(context));
87 context.getSettings().setProtocolVersion(PROTOCOL_VERSION_3_8);
88 minor = 8;
89 } else
90 throw new UnsupportedProtocolVersionException(
91 "Unsupported protocol version: " + protocolString);
92 writer.write(("RFB 00" + major + ".00" + minor + "\n").getBytes());
93 logger.info("Set protocol version to: " + context.getSettings().getProtocolVersion());
94 }
95
96 }