From 573f92af025e876547dd1121eb93e4b6e3ec878b Mon Sep 17 00:00:00 2001 From: Michael Jumper Date: Sun, 2 Jan 2011 15:16:21 -0800 Subject: [PATCH] Added opcode enum --- .../guacamole/GuacamoleClient.java | 21 +++- .../guacamole/GuacamoleInstruction.java | 95 +++++++++++++++++++ 2 files changed, 113 insertions(+), 3 deletions(-) create mode 100644 guacamole-common/src/main/java/net/sourceforge/guacamole/GuacamoleInstruction.java diff --git a/guacamole-common/src/main/java/net/sourceforge/guacamole/GuacamoleClient.java b/guacamole-common/src/main/java/net/sourceforge/guacamole/GuacamoleClient.java index 80f0c1bf5..c9e18afc9 100644 --- a/guacamole-common/src/main/java/net/sourceforge/guacamole/GuacamoleClient.java +++ b/guacamole-common/src/main/java/net/sourceforge/guacamole/GuacamoleClient.java @@ -1,6 +1,7 @@ package net.sourceforge.guacamole; +import net.sourceforge.guacamole.GuacamoleInstruction.Operation; import net.sourceforge.guacamole.net.Configuration; /* @@ -24,14 +25,28 @@ import net.sourceforge.guacamole.net.Configuration; public abstract class GuacamoleClient { public abstract void write(char[] chunk, int off, int len) throws GuacamoleException; + + public void write(char[] chunk) throws GuacamoleException { + write(chunk, 0, chunk.length); + } + + public void write(GuacamoleInstruction instruction) throws GuacamoleException { + write(instruction.toString().toCharArray()); + } + public abstract char[] read() throws GuacamoleException; + public abstract void disconnect() throws GuacamoleException; public void connect(Configuration config) throws GuacamoleException { - // TODO: Send "select" and "connect" messages in client connect function (based on config) ... to be implemented. - char[] initMessages = "select:vnc;connect:localhost,5901,potato;".toCharArray(); - write(initMessages, 0, initMessages.length); + // Send protocol + write(new GuacamoleInstruction(Operation.CLIENT_SELECT, config.getProtocol())); + + // TODO: Wait for and read args message + + // Send args + write(new GuacamoleInstruction(Operation.CLIENT_CONNECT, "localhost", "5901", "potato")); } diff --git a/guacamole-common/src/main/java/net/sourceforge/guacamole/GuacamoleInstruction.java b/guacamole-common/src/main/java/net/sourceforge/guacamole/GuacamoleInstruction.java new file mode 100644 index 000000000..e86ab6467 --- /dev/null +++ b/guacamole-common/src/main/java/net/sourceforge/guacamole/GuacamoleInstruction.java @@ -0,0 +1,95 @@ + +package net.sourceforge.guacamole; + +import java.util.HashMap; + +/* + * Guacamole - Clientless Remote Desktop + * Copyright (C) 2010 Michael Jumper + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +public class GuacamoleInstruction { + + public enum Operation { + + CLIENT_SELECT("select"), + CLIENT_CONNECT("connect"), + + SERVER_ARGS("args"); + + private String opcode; + private Operation(String opcode) { this.opcode = opcode; } + + public String getOpcode() { + return opcode; + } + + // Maintain static hash of all opcodes + private static final HashMap opcodeToOperation; + static { + + opcodeToOperation = new HashMap(); + + for (Operation operation : Operation.values()) + opcodeToOperation.put(operation.getOpcode(), operation); + + } + + public Operation fromOpcode(String opcode) { + return opcodeToOperation.get(opcode); + } + + } + + private Operation operation; + private String[] args; + + public GuacamoleInstruction(Operation operation, String... args) { + this.operation = operation; + this.args = args; + } + + public Operation getOperation() { + return operation; + } + + public String[] getArgs() { + return args; + } + + @Override + public String toString() { + + StringBuilder buff = new StringBuilder(); + + buff.append(operation); + + if (args.length >= 1) + buff.append(':'); + + for (int i=0; i 0) + buff.append(','); + buff.append(args[i]); + } + + buff.append(';'); + + return buff.toString(); + + } + +}