More API improvements

This commit is contained in:
Michael Jumper
2011-01-02 14:27:44 -08:00
parent 1430bed43e
commit a54c413420
8 changed files with 147 additions and 145 deletions

View File

@@ -1,28 +0,0 @@
package net.sourceforge.guacamole;
/*
* 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 abstract class Client {
public abstract void write(char[] chunk, int off, int len) throws GuacamoleException;
public abstract char[] read() throws GuacamoleException;
public abstract void disconnect() throws GuacamoleException;
}

View File

@@ -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);
}

View File

@@ -0,0 +1,126 @@
package net.sourceforge.guacamole;
/*
* 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/>.
*/
import java.io.IOException;
import java.net.InetAddress;
import java.net.Socket;
import java.io.Reader;
import java.io.InputStreamReader;
import java.io.Writer;
import java.io.OutputStreamWriter;
public class GuacamoleTCPClient extends GuacamoleClient {
private Socket sock;
private Reader input;
private Writer output;
public GuacamoleTCPClient(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);
}
}
}

View File

@@ -49,14 +49,6 @@ public class GuacamoleProperties {
}
public static String getProxyHostname() throws GuacamoleException {
return GuacamoleProperties.getProperty("guacd-hostname");
}
public static int getProxyPort() throws GuacamoleException {
return GuacamoleProperties.getIntProperty("guacd-port", null);
}
public static GuacamoleClientProvider getClientProvider() throws GuacamoleException {
// Get client provider instance

View File

@@ -23,8 +23,8 @@ import java.util.concurrent.locks.ReentrantLock;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionBindingEvent;
import javax.servlet.http.HttpSessionBindingListener;
import net.sourceforge.guacamole.Client;
import net.sourceforge.guacamole.GuacamoleClient;
import net.sourceforge.guacamole.GuacamoleTCPClient;
import net.sourceforge.guacamole.GuacamoleException;
public class GuacamoleSession {
@@ -33,11 +33,11 @@ public class GuacamoleSession {
private SessionClient client;
private ReentrantLock instructionStreamLock;
public class SessionClient extends Client implements HttpSessionBindingListener {
public class SessionClient extends GuacamoleClient implements HttpSessionBindingListener {
private Client client;
private GuacamoleClient client;
public SessionClient(Client client) {
public SessionClient(GuacamoleClient client) {
this.client = client;
}
@@ -84,7 +84,7 @@ public class GuacamoleSession {
}
public void attachClient(GuacamoleClient client) throws GuacamoleException {
public void attachClient(GuacamoleTCPClient client) throws GuacamoleException {
synchronized (session) {

View File

@@ -2,7 +2,7 @@
package net.sourceforge.guacamole.net.authentication;
import javax.servlet.http.HttpSession;
import net.sourceforge.guacamole.GuacamoleClient;
import net.sourceforge.guacamole.GuacamoleTCPClient;
import net.sourceforge.guacamole.GuacamoleException;
/*
@@ -25,6 +25,6 @@ import net.sourceforge.guacamole.GuacamoleException;
public interface GuacamoleClientProvider {
public GuacamoleClient createClient(HttpSession session) throws GuacamoleException;
public GuacamoleTCPClient createClient(HttpSession session) throws GuacamoleException;
}

View File

@@ -2,7 +2,7 @@
package net.sourceforge.guacamole.net.authentication;
import javax.servlet.http.HttpSession;
import net.sourceforge.guacamole.GuacamoleClient;
import net.sourceforge.guacamole.GuacamoleTCPClient;
import net.sourceforge.guacamole.GuacamoleException;
/*
@@ -25,7 +25,7 @@ import net.sourceforge.guacamole.GuacamoleException;
public class NullGuacamoleClientProvider implements GuacamoleClientProvider {
public GuacamoleClient createClient(HttpSession session) throws GuacamoleException {
public GuacamoleTCPClient createClient(HttpSession session) throws GuacamoleException {
throw new GuacamoleException("Null provider will not create clients.");
}

View File

@@ -27,7 +27,7 @@ import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import net.sourceforge.guacamole.Client;
import net.sourceforge.guacamole.GuacamoleClient;
import net.sourceforge.guacamole.GuacamoleException;
import net.sourceforge.guacamole.net.GuacamoleSession;
@@ -54,7 +54,7 @@ public class Outbound extends HttpServlet {
try {
// Query new update from server
Client client = session.getClient();
GuacamoleClient client = session.getClient();
// For all messages, until another stream is ready (we send at least one message)
char[] message;