Use monitor to wait for changes, rather than universally waiting for entire timeout period.

This commit is contained in:
Michael Jumper
2014-03-03 09:32:45 -08:00
parent 4334cf7d76
commit 65451a37f8
2 changed files with 31 additions and 9 deletions

View File

@@ -58,17 +58,11 @@ public class CaptureClipboard extends AuthenticatingHttpServlet {
// Get clipboard // Get clipboard
final HttpSession session = request.getSession(true); final HttpSession session = request.getSession(true);
final ClipboardState clipboard = getClipboardState(session); final ClipboardState clipboard = getClipboardState(session);
// FIXME: Don't do this
try {
Thread.sleep(CLIPBOARD_TIMEOUT);
}
catch (InterruptedException e) { /* ignore */ }
// Send clipboard contents // Send clipboard contents
try { try {
response.setContentType("text/plain"); response.setContentType("text/plain");
response.getWriter().print(clipboard.getContents()); response.getWriter().print(clipboard.waitForContents(CLIPBOARD_TIMEOUT));
} }
catch (IOException e) { catch (IOException e) {
throw new GuacamoleServerException("Unable to send clipboard contents", e); throw new GuacamoleServerException("Unable to send clipboard contents", e);

View File

@@ -36,6 +36,11 @@ public class ClipboardState {
*/ */
private String contents = ""; private String contents = "";
/**
* The timestamp of the last contents update.
*/
private long last_update = 0;
/** /**
* Returns the current clipboard contents. * Returns the current clipboard contents.
* @return The current clipboard contents * @return The current clipboard contents
@@ -48,8 +53,31 @@ public class ClipboardState {
* Sets the current clipboard contents. * Sets the current clipboard contents.
* @param contents The contents to assign to the clipboard. * @param contents The contents to assign to the clipboard.
*/ */
public void setContents(String contents) { public synchronized void setContents(String contents) {
this.contents = contents; this.contents = contents;
last_update = System.currentTimeMillis();
this.notifyAll();
}
/**
* Wait up to the given timeout for new clipboard data. If data more recent
* than the timeout period is available, return that.
*
* @param timeout The amount of time to wait, in milliseconds.
* @return The current clipboard contents.
*/
public synchronized String waitForContents(int timeout) {
// Wait for new contents if it's been a while
if (System.currentTimeMillis() - last_update > timeout) {
try {
this.wait(timeout);
}
catch (InterruptedException e) { /* ignore */ }
}
return getContents();
} }
} }