Added opcode enum

This commit is contained in:
Michael Jumper
2011-01-02 15:16:21 -08:00
parent a54c413420
commit 573f92af02
2 changed files with 113 additions and 3 deletions

View File

@@ -1,6 +1,7 @@
package net.sourceforge.guacamole; package net.sourceforge.guacamole;
import net.sourceforge.guacamole.GuacamoleInstruction.Operation;
import net.sourceforge.guacamole.net.Configuration; import net.sourceforge.guacamole.net.Configuration;
/* /*
@@ -24,14 +25,28 @@ import net.sourceforge.guacamole.net.Configuration;
public abstract class GuacamoleClient { public abstract class GuacamoleClient {
public abstract void write(char[] chunk, int off, int len) throws GuacamoleException; 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 char[] read() throws GuacamoleException;
public abstract void disconnect() throws GuacamoleException; public abstract void disconnect() throws GuacamoleException;
public void connect(Configuration config) 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. // Send protocol
char[] initMessages = "select:vnc;connect:localhost,5901,potato;".toCharArray(); write(new GuacamoleInstruction(Operation.CLIENT_SELECT, config.getProtocol()));
write(initMessages, 0, initMessages.length);
// TODO: Wait for and read args message
// Send args
write(new GuacamoleInstruction(Operation.CLIENT_CONNECT, "localhost", "5901", "potato"));
} }

View File

@@ -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 <http://www.gnu.org/licenses/>.
*/
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<String, Operation> opcodeToOperation;
static {
opcodeToOperation = new HashMap<String, Operation>();
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<args.length; i++) {
if (i > 0)
buff.append(',');
buff.append(args[i]);
}
buff.append(';');
return buff.toString();
}
}