Add isOpen() to socket and tunnel. Add getSocket() to tunnel.

This commit is contained in:
Michael Jumper
2012-03-26 11:34:42 -07:00
parent 4b72a166ec
commit 762c84f25f
5 changed files with 47 additions and 2 deletions

View File

@@ -76,4 +76,12 @@ public interface GuacamoleSocket {
*/ */
public void close() throws GuacamoleException; public void close() throws GuacamoleException;
/**
* Returns whether this GuacamoleSocket is open and can be used for reading
* and writing.
*
* @return true if this GuacamoleSocket is open, false otherwise.
*/
public boolean isOpen();
} }

View File

@@ -57,6 +57,8 @@ public class GuacamoleTunnel {
private ReentrantLock readerLock; private ReentrantLock readerLock;
private ReentrantLock writerLock; private ReentrantLock writerLock;
private boolean open = true;
/** /**
* Creates a new GuacamoleTunnel which synchronizes access to the * Creates a new GuacamoleTunnel which synchronizes access to the
* Guacamole instruction stream associated with the given GuacamoleSocket. * Guacamole instruction stream associated with the given GuacamoleSocket.
@@ -146,6 +148,16 @@ public class GuacamoleTunnel {
return uuid; return uuid;
} }
/**
* Returns the GuacamoleSocket used by this GuacamoleTunnel for reading
* and writing.
*
* @return The GuacamoleSocket used by this GuacamoleTunnel.
*/
public GuacamoleSocket getSocket() {
return socket;
}
/** /**
* Release all resources allocated to this GuacamoleTunnel. * Release all resources allocated to this GuacamoleTunnel.
* *
@@ -156,4 +168,13 @@ public class GuacamoleTunnel {
socket.close(); socket.close();
} }
/**
* Returns whether this GuacamoleTunnel is open, or has been closed.
*
* @return true if this GuacamoleTunnel is open, false if it is closed.
*/
public boolean isOpen() {
return socket.isOpen();
}
} }

View File

@@ -132,5 +132,10 @@ public class InetGuacamoleSocket implements GuacamoleSocket {
return writer; return writer;
} }
@Override
public boolean isOpen() {
return !sock.isClosed();
}
} }

View File

@@ -121,4 +121,9 @@ public class ConfiguredGuacamoleSocket implements GuacamoleSocket {
socket.close(); socket.close();
} }
@Override
public boolean isOpen() {
return socket.isOpen();
}
} }

View File

@@ -227,10 +227,15 @@ public abstract class GuacamoleHTTPTunnelServlet extends HttpServlet {
HttpSession httpSession = request.getSession(false); HttpSession httpSession = request.getSession(false);
GuacamoleSession session = new GuacamoleSession(httpSession); GuacamoleSession session = new GuacamoleSession(httpSession);
// Get tunnel, ensure tunnel exists
GuacamoleTunnel tunnel = session.getTunnel(tunnelUUID); GuacamoleTunnel tunnel = session.getTunnel(tunnelUUID);
if (tunnel == null) if (tunnel == null)
throw new GuacamoleResourceNotFoundException("No such tunnel."); throw new GuacamoleResourceNotFoundException("No such tunnel.");
// Ensure tunnel is open
if (!tunnel.isOpen())
throw new GuacamoleResourceNotFoundException("Tunnel is closed.");
// Obtain exclusive read access // Obtain exclusive read access
GuacamoleReader reader = tunnel.acquireReader(); GuacamoleReader reader = tunnel.acquireReader();
@@ -265,7 +270,7 @@ public abstract class GuacamoleHTTPTunnelServlet extends HttpServlet {
if (tunnel.hasQueuedReaderThreads()) if (tunnel.hasQueuedReaderThreads())
break; break;
} while ((message = reader.read()) != null); } while (tunnel.isOpen() && (message = reader.read()) != null);
// Close tunnel immediately upon EOF // Close tunnel immediately upon EOF
if (message == null) if (message == null)
@@ -340,7 +345,8 @@ public abstract class GuacamoleHTTPTunnelServlet extends HttpServlet {
char[] buffer = new char[8192]; char[] buffer = new char[8192];
int length; int length;
while ((length = input.read(buffer, 0, buffer.length)) != -1) while (tunnel.isOpen() &&
(length = input.read(buffer, 0, buffer.length)) != -1)
writer.write(buffer, 0, length); writer.write(buffer, 0, length);
} }