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 c9e18afc9..ac1176975 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 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 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= 1) buff.append(':');