Initial work moving to reasonable session/client API.

This commit is contained in:
Michael Jumper
2011-01-01 20:32:52 -08:00
parent fc8ff9ea16
commit bda0d83ff1
6 changed files with 38 additions and 89 deletions

View File

@@ -19,8 +19,6 @@ package net.sourceforge.guacamole;
* along with this program. If not, see <http://www.gnu.org/licenses/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
import net.sourceforge.guacamole.GuacamoleException;
public abstract class Client { public abstract class Client {
public abstract void write(char[] chunk, int off, int len) throws GuacamoleException; public abstract void write(char[] chunk, int off, int len) throws GuacamoleException;

View File

@@ -23,15 +23,12 @@ import java.io.IOException;
import java.net.InetAddress; import java.net.InetAddress;
import java.net.Socket; import java.net.Socket;
import java.io.InputStream;
import java.io.Reader; import java.io.Reader;
import java.io.InputStreamReader; import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.Writer; import java.io.Writer;
import java.io.OutputStreamWriter; import java.io.OutputStreamWriter;
import net.sourceforge.guacamole.GuacamoleException;
public class GuacamoleClient extends Client { public class GuacamoleClient extends Client {

View File

@@ -19,8 +19,8 @@ package net.sourceforge.guacamole.net;
* along with this program. If not, see <http://www.gnu.org/licenses/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
import java.util.HashMap;
import java.util.concurrent.locks.ReentrantLock; import java.util.concurrent.locks.ReentrantLock;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpSession; import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionBindingEvent; import javax.servlet.http.HttpSessionBindingEvent;
import javax.servlet.http.HttpSessionBindingListener; import javax.servlet.http.HttpSessionBindingListener;
@@ -31,41 +31,36 @@ import net.sourceforge.guacamole.GuacamoleException;
public class GuacamoleSession { public class GuacamoleSession {
private GuacamoleConfiguration config; private GuacamoleConfiguration config;
private final HttpSession session; private final HttpSession session;
private SessionClient client; private SessionClient client;
private ReentrantLock instructionStreamLock; private ReentrantLock instructionStreamLock;
private String protocol; private String protocol;
private String hostname; private HashMap<String, String> parameters = new HashMap<String, String>();
private int port;
private String password; public String getProtocol() {
return protocol;
}
public void setProtocol(String protocol) {
this.protocol = protocol;
}
public String getParameter(String name) {
return parameters.get(name);
}
public void setParameter(String name, String value) {
parameters.put(name, value);
}
public class SessionClient extends Client implements HttpSessionBindingListener { public class SessionClient extends Client implements HttpSessionBindingListener {
private Client client; private Client client;
private ReentrantLock authorizedLock;
public SessionClient(Client client) { public SessionClient(Client client) {
this.client = client; this.client = client;
authorizedLock = new ReentrantLock();
authorizedLock.lock();
}
public void authorize() {
authorizedLock.unlock();
}
public void waitForAuthorization() {
if (authorizedLock.isLocked()) {
try {
authorizedLock.lock();
authorizedLock.unlock();
}
catch (Throwable t) {
throw new Error("Internal error waiting for authorization", t);
}
}
} }
public void valueBound(HttpSessionBindingEvent event) { public void valueBound(HttpSessionBindingEvent event) {
@@ -109,15 +104,16 @@ public class GuacamoleSession {
client = (SessionClient) session.getAttribute("CLIENT"); client = (SessionClient) session.getAttribute("CLIENT");
instructionStreamLock = (ReentrantLock) session.getAttribute("INSTRUCTION_STREAM_LOCK"); instructionStreamLock = (ReentrantLock) session.getAttribute("INSTRUCTION_STREAM_LOCK");
} }
} }
public void connect() throws GuacamoleException { public void connect() throws GuacamoleException {
synchronized (session) { synchronized (session) {
if (client != null) if (client != null)
client.disconnect(); client.disconnect();
client = new SessionClient( client = new SessionClient(
new GuacamoleClient ( new GuacamoleClient (
config.getProxyHostname(), config.getProxyHostname(),
@@ -125,12 +121,15 @@ public class GuacamoleSession {
) )
); );
// TODO: Send "select" and "connect" messages here.
session.setAttribute("CLIENT", client); session.setAttribute("CLIENT", client);
instructionStreamLock = new ReentrantLock(); instructionStreamLock = new ReentrantLock();
session.setAttribute("INSTRUCTION_STREAM_LOCK", instructionStreamLock); session.setAttribute("INSTRUCTION_STREAM_LOCK", instructionStreamLock);
} }
} }
public boolean isConnected() { public boolean isConnected() {
@@ -143,8 +142,12 @@ public class GuacamoleSession {
return config; return config;
} }
public SessionClient getClient() { public SessionClient getClient() throws GuacamoleException {
synchronized (session) { synchronized (session) {
if (client == null)
throw new GuacamoleException("Client not yet connected.");
return client; return client;
} }
} }
@@ -154,56 +157,21 @@ public class GuacamoleSession {
} }
public void disconnect() throws GuacamoleException { public void disconnect() throws GuacamoleException {
synchronized (session) {
if (client != null) { if (client != null) {
client.disconnect(); client.disconnect();
session.removeAttribute("CLIENT"); session.removeAttribute("CLIENT");
client = null; client = null;
} }
}
} }
public ReentrantLock getInstructionStreamLock() { public ReentrantLock getInstructionStreamLock() {
return instructionStreamLock; return instructionStreamLock;
} }
public void setConnection(String protocol, String hostname, int port) {
this.protocol = protocol;
this.hostname = hostname;
this.port = port;
}
public String getProtocol() {
return protocol;
}
public String getHostname() {
return hostname;
}
public int getPort() {
return port;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getConnectMessage() throws GuacamoleException {
if (getProtocol() == null)
throw new GuacamoleException("Protocol not specified");
if (getHostname() == null)
throw new GuacamoleException("Hostname not specified");
if (getPassword() == null)
return "connect:" + getProtocol() + "," + getHostname() + "," + getPort() + ";";
else
return "connect:" + getProtocol() + "," + getHostname() + "," + getPort() + "," + getPassword() + ";";
}
} }

View File

@@ -43,16 +43,6 @@ public class Connect extends GuacamoleServlet {
// Obtain new connection // Obtain new connection
session.connect(); session.connect();
// Send data
try {
char[] connect = session.getConnectMessage().toCharArray();
session.getClient().write(connect, 0, connect.length);
session.getClient().authorize();
}
catch (GuacamoleException e) {
throw new GuacamoleException("Error sending data to server: " + e.getMessage(), e);
}
} }
} }

View File

@@ -33,8 +33,6 @@ public class Inbound extends GuacamoleServlet {
@Override @Override
protected void handleRequest(GuacamoleSession session, HttpServletRequest request, HttpServletResponse response) throws GuacamoleException { protected void handleRequest(GuacamoleSession session, HttpServletRequest request, HttpServletResponse response) throws GuacamoleException {
session.getClient().waitForAuthorization();
// Send data // Send data
try { try {

View File

@@ -35,8 +35,6 @@ public class Outbound extends GuacamoleServlet {
@Override @Override
protected void handleRequest(GuacamoleSession session, HttpServletRequest request, HttpServletResponse response) throws GuacamoleException { protected void handleRequest(GuacamoleSession session, HttpServletRequest request, HttpServletResponse response) throws GuacamoleException {
session.getClient().waitForAuthorization();
ReentrantLock instructionStreamLock = session.getInstructionStreamLock(); ReentrantLock instructionStreamLock = session.getInstructionStreamLock();
instructionStreamLock.lock(); instructionStreamLock.lock();