Implemented parsing of instructions

This commit is contained in:
Michael Jumper
2011-01-02 17:08:27 -08:00
parent 573f92af02
commit adb577ca8e
2 changed files with 85 additions and 4 deletions

View File

@@ -1,6 +1,7 @@
package net.sourceforge.guacamole;
import java.util.LinkedList;
import net.sourceforge.guacamole.GuacamoleInstruction.Operation;
import net.sourceforge.guacamole.net.Configuration;
@@ -36,6 +37,68 @@ public abstract class GuacamoleClient {
public abstract char[] read() throws GuacamoleException;
private int instructionStart;
private char[] buffer;
public GuacamoleInstruction readInstruction() throws GuacamoleException {
// Fill buffer if not already filled
if (buffer == null) {
buffer = read();
instructionStart = 0;
}
// Locate end-of-opcode and end-of-instruction
int opcodeEnd = -1;
int instructionEnd = -1;
for (int i=instructionStart; i<buffer.length; i++) {
char c = buffer[i];
if (c == ':')
opcodeEnd = i;
else if (c == ';') {
instructionEnd = i;
break;
}
}
// If no end-of-instruction marker, malformed.
if (instructionEnd == -1)
throw new GuacamoleException("Malformed instruction.");
// If no end-of-opcode marker, end is end-of-instruction
if (opcodeEnd == -1)
opcodeEnd = instructionEnd;
// Parse opcode
String opcode = new String(buffer, instructionStart, opcodeEnd - instructionStart);
// Parse args
String[] args;
if (instructionEnd > opcodeEnd)
args = new String(buffer, opcodeEnd+1, instructionEnd - opcodeEnd - 1).split(",");
else
args = new String[0];
// Create instruction
GuacamoleInstruction instruction = new GuacamoleInstruction(
Operation.fromOpcode(opcode),
args
);
// Advance buffer
instructionStart = instructionEnd + 1;
if (instructionStart >= buffer.length)
buffer = null;
return instruction;
}
public abstract void disconnect() throws GuacamoleException;
public void connect(Configuration config) throws GuacamoleException {
@@ -43,10 +106,28 @@ public abstract class GuacamoleClient {
// Send protocol
write(new GuacamoleInstruction(Operation.CLIENT_SELECT, config.getProtocol()));
// TODO: Wait for and read args message
// Wait for server args
GuacamoleInstruction instruction;
do {
instruction = readInstruction();
} while (instruction.getOperation() != Operation.SERVER_ARGS);
// Build args list off provided names and config
String[] args = new String[instruction.getArgs().length];
for (int i=0; i<instruction.getArgs().length; i++) {
String requiredArg = instruction.getArgs()[i];
String value = config.getParameter(requiredArg);
if (value != null)
args[i] = value;
else
args[i] = "";
}
// Send args
write(new GuacamoleInstruction(Operation.CLIENT_CONNECT, "localhost", "5901", "potato"));
write(new GuacamoleInstruction(Operation.CLIENT_CONNECT, args));
}

View File

@@ -48,7 +48,7 @@ public class GuacamoleInstruction {
}
public Operation fromOpcode(String opcode) {
public static Operation fromOpcode(String opcode) {
return opcodeToOperation.get(opcode);
}
@@ -75,7 +75,7 @@ public class GuacamoleInstruction {
StringBuilder buff = new StringBuilder();
buff.append(operation);
buff.append(operation.getOpcode());
if (args.length >= 1)
buff.append(':');