More API improvements

This commit is contained in:
Michael Jumper committed 2011-01-02 14:27:44 -08:00
1 parent 1430bed43e
commit a54c413420
8 files changed
+147 -145

No files matched your search

@@ -1,6 +1,8 @@
package net.sourceforge.guacamole;
import net.sourceforge.guacamole.net.Configuration;
/*
* Guacamole - Clientless Remote Desktop
* Copyright (C) 2010 Michael Jumper
@@ -19,107 +21,17 @@ package net.sourceforge.guacamole;
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import java.io.IOException;
import java.net.InetAddress;
import java.net.Socket;
public abstract class GuacamoleClient {
import java.io.Reader;
import java.io.InputStreamReader;
public abstract void write(char[] chunk, int off, int len) throws GuacamoleException;
public abstract char[] read() throws GuacamoleException;
public abstract void disconnect() throws GuacamoleException;
import java.io.Writer;
import java.io.OutputStreamWriter;
public void connect(Configuration config) throws GuacamoleException {
public class GuacamoleClient extends Client {
private Socket sock;
private Reader input;
private Writer output;
public GuacamoleClient(String hostname, int port) throws GuacamoleException {
try {
sock = new Socket(InetAddress.getByName(hostname), port);
input = new InputStreamReader(sock.getInputStream());
output = new OutputStreamWriter(sock.getOutputStream());
}
catch (IOException e) {
throw new GuacamoleException(e);
}
}
public void write(char[] chunk, int off, int len) throws GuacamoleException {
try {
output.write(chunk, off, len);
output.flush();
}
catch (IOException e) {
throw new GuacamoleException(e);
}
}
public void disconnect() throws GuacamoleException {
try {
sock.close();
}
catch (IOException e) {
throw new GuacamoleException(e);
}
}
private int usedLength = 0;
private char[] buffer = new char[20000];
public char[] read() throws GuacamoleException {
try {
// While we're blocking, or input is available
for (;;) {
// If past threshold, resize buffer before reading
if (usedLength > buffer.length/2) {
char[] biggerBuffer = new char[buffer.length*2];
System.arraycopy(buffer, 0, biggerBuffer, 0, usedLength);
buffer = biggerBuffer;
}
// Attempt to fill buffer
int numRead = input.read(buffer, usedLength, buffer.length - usedLength);
if (numRead == -1)
return null;
int prevLength = usedLength;
usedLength += numRead;
for (int i=usedLength-1; i>=prevLength; i--) {
char readChar = buffer[i];
// If end of instruction, return it.
if (readChar == ';') {
// Get instruction
char[] chunk = new char[i+1];
System.arraycopy(buffer, 0, chunk, 0, i+1);
// Reset buffer
usedLength -= i+1;
System.arraycopy(buffer, i+1, buffer, 0, usedLength);
// Return instruction string
return chunk;
}
}
} // End read loop
}
catch (IOException e) {
throw new GuacamoleException(e);
}
// 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);
}