diff --git a/guacamole/src/main/java/org/glyptodon/guacamole/net/basic/CaptureClipboard.java b/guacamole/src/main/java/org/glyptodon/guacamole/net/basic/CaptureClipboard.java index 0aaec2f8b..322f088bf 100644 --- a/guacamole/src/main/java/org/glyptodon/guacamole/net/basic/CaptureClipboard.java +++ b/guacamole/src/main/java/org/glyptodon/guacamole/net/basic/CaptureClipboard.java @@ -58,17 +58,11 @@ public class CaptureClipboard extends AuthenticatingHttpServlet { // Get clipboard final HttpSession session = request.getSession(true); final ClipboardState clipboard = getClipboardState(session); - - // FIXME: Don't do this - try { - Thread.sleep(CLIPBOARD_TIMEOUT); - } - catch (InterruptedException e) { /* ignore */ } - + // Send clipboard contents try { response.setContentType("text/plain"); - response.getWriter().print(clipboard.getContents()); + response.getWriter().print(clipboard.waitForContents(CLIPBOARD_TIMEOUT)); } catch (IOException e) { throw new GuacamoleServerException("Unable to send clipboard contents", e); diff --git a/guacamole/src/main/java/org/glyptodon/guacamole/net/basic/ClipboardState.java b/guacamole/src/main/java/org/glyptodon/guacamole/net/basic/ClipboardState.java index 1aebd2206..f5892c568 100644 --- a/guacamole/src/main/java/org/glyptodon/guacamole/net/basic/ClipboardState.java +++ b/guacamole/src/main/java/org/glyptodon/guacamole/net/basic/ClipboardState.java @@ -36,6 +36,11 @@ public class ClipboardState { */ private String contents = ""; + /** + * The timestamp of the last contents update. + */ + private long last_update = 0; + /** * Returns the current clipboard contents. * @return The current clipboard contents @@ -48,8 +53,31 @@ public class ClipboardState { * Sets the current clipboard contents. * @param contents The contents to assign to the clipboard. */ - public void setContents(String contents) { + public synchronized void setContents(String 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(); + } }