From 150352f64cdb49a27b110bd32e049c307fcf3486 Mon Sep 17 00:00:00 2001 From: Leif Åstrand Date: Fri, 5 Apr 2013 18:33:55 +0300 Subject: Implemented poll interval for UI (#11495) Change-Id: Ic56b0123970f18e282c75d67863569ac55c72ea8 --- .../com/vaadin/client/ApplicationConnection.java | 17 ++++ .../src/com/vaadin/client/ui/ui/UIConnector.java | 38 +++++++++ server/src/com/vaadin/ui/UI.java | 34 ++++++++ .../shared/communication/MethodInvocation.java | 27 +++++++ .../src/com/vaadin/shared/ui/ui/UIServerRpc.java | 7 ++ shared/src/com/vaadin/shared/ui/ui/UIState.java | 1 + .../com/vaadin/tests/components/ui/UIPolling.html | 53 +++++++++++++ .../com/vaadin/tests/components/ui/UIPolling.java | 90 ++++++++++++++++++++++ 8 files changed, 267 insertions(+) create mode 100644 uitest/src/com/vaadin/tests/components/ui/UIPolling.html create mode 100644 uitest/src/com/vaadin/tests/components/ui/UIPolling.java diff --git a/client/src/com/vaadin/client/ApplicationConnection.java b/client/src/com/vaadin/client/ApplicationConnection.java index 2fa82c6004..2cc5a85996 100644 --- a/client/src/com/vaadin/client/ApplicationConnection.java +++ b/client/src/com/vaadin/client/ApplicationConnection.java @@ -2387,6 +2387,23 @@ public class ApplicationConnection { } } + /** + * Removes any pending invocation of the given method from the queue + * + * @param invocation + * The invocation to remove + */ + public void removePendingInvocations(MethodInvocation invocation) { + Iterator iter = pendingInvocations.values() + .iterator(); + while (iter.hasNext()) { + MethodInvocation mi = iter.next(); + if (mi.equals(invocation)) { + iter.remove(); + } + } + } + /** * This method sends currently queued variable changes to server. It is * called when immediate variable update must happen. diff --git a/client/src/com/vaadin/client/ui/ui/UIConnector.java b/client/src/com/vaadin/client/ui/ui/UIConnector.java index 07481063c3..f3b4f36670 100644 --- a/client/src/com/vaadin/client/ui/ui/UIConnector.java +++ b/client/src/com/vaadin/client/ui/ui/UIConnector.java @@ -37,6 +37,7 @@ import com.google.gwt.user.client.DOM; import com.google.gwt.user.client.Element; import com.google.gwt.user.client.Event; import com.google.gwt.user.client.History; +import com.google.gwt.user.client.Timer; import com.google.gwt.user.client.Window; import com.google.gwt.user.client.ui.RootPanel; import com.google.gwt.user.client.ui.Widget; @@ -61,6 +62,7 @@ import com.vaadin.client.ui.layout.MayScrollChildren; import com.vaadin.client.ui.window.WindowConnector; import com.vaadin.server.Page.Styles; import com.vaadin.shared.MouseEventDetails; +import com.vaadin.shared.communication.MethodInvocation; import com.vaadin.shared.ui.ComponentStateUtil; import com.vaadin.shared.ui.Connect; import com.vaadin.shared.ui.Connect.LoadStyle; @@ -417,6 +419,8 @@ public class UIConnector extends AbstractSingleComponentContainerConnector }; + private Timer pollTimer = null; + @Override public void updateCaption(ComponentConnector component) { // NOP The main view never draws caption for its layout @@ -580,5 +584,39 @@ public class UIConnector extends AbstractSingleComponentContainerConnector getConnection().getLoadingIndicator().setDelayStateDelay( getState().loadingIndicatorConfiguration.delayStateDelay); } + + if (stateChangeEvent.hasPropertyChanged("pollInterval")) { + configurePolling(); + } + } + + private void configurePolling() { + if (pollTimer != null) { + pollTimer.cancel(); + pollTimer = null; + } + if (getState().pollInterval >= 0) { + pollTimer = new Timer() { + @Override + public void run() { + /* + * Verify that polling has not recently been canceled. This + * is needed because Timer.cancel() does not always work + * properly in IE 8 until GWT issue 8101 has been fixed. + */ + if (pollTimer != null) { + getRpcProxy(UIServerRpc.class).poll(); + // Send changes even though poll is @Delayed + getConnection().sendPendingVariableChanges(); + } + } + }; + pollTimer.scheduleRepeating(getState().pollInterval); + } else { + // Ensure no more polls are sent as polling has been disabled + getConnection().removePendingInvocations( + new MethodInvocation(getConnectorId(), UIServerRpc.class + .getName(), "poll")); + } } } diff --git a/server/src/com/vaadin/ui/UI.java b/server/src/com/vaadin/ui/UI.java index 2d9f49b122..6160978542 100644 --- a/server/src/com/vaadin/ui/UI.java +++ b/server/src/com/vaadin/ui/UI.java @@ -148,6 +148,14 @@ public abstract class UI extends AbstractSingleComponentContainer implements UI.this.scrollTop = scrollTop; UI.this.scrollLeft = scrollLeft; } + + @Override + public void poll() { + /* + * No-op. This is only called to cause a server visit to check for + * changes. + */ + } }; /** @@ -1171,4 +1179,30 @@ public abstract class UI extends AbstractSingleComponentContainer implements public void setPushConnection(PushConnection connection) { pushConnection = connection; } + + /** + * Sets the interval with which the UI should poll the server to see if + * there are any changes. Polling is disabled by default. + *

+ * Note that it is possible to enable push and polling at the same time but + * it should not be done to avoid excessive server traffic. + *

+ * + * @param intervalInMillis + * The interval (in ms) with which the UI should poll the server + * or -1 to disable polling + */ + public void setPollInterval(int intervalInMillis) { + getState().pollInterval = intervalInMillis; + } + + /** + * Returns the interval with which the UI polls the server. + * + * @return The interval (in ms) with which the UI polls the server or -1 if + * polling is disabled + */ + public int getPollInterval() { + return getState(false).pollInterval; + } } diff --git a/shared/src/com/vaadin/shared/communication/MethodInvocation.java b/shared/src/com/vaadin/shared/communication/MethodInvocation.java index 417ced76be..d5bf8324ef 100644 --- a/shared/src/com/vaadin/shared/communication/MethodInvocation.java +++ b/shared/src/com/vaadin/shared/communication/MethodInvocation.java @@ -19,6 +19,8 @@ package com.vaadin.shared.communication; import java.io.Serializable; import java.util.Arrays; +import com.vaadin.shared.util.SharedUtil; + /** * Information needed by the framework to send an RPC method invocation from the * client to the server or vice versa. @@ -85,4 +87,29 @@ public class MethodInvocation implements Serializable { return connectorId + "-" + getInterfaceName() + "-" + getMethodName(); } + @Override + public boolean equals(Object obj) { + if (!(obj instanceof MethodInvocation)) { + return false; + } + MethodInvocation other = (MethodInvocation) obj; + if (!SharedUtil.equals(getConnectorId(), other.getConnectorId())) { + return false; + } + + if (!SharedUtil.equals(getInterfaceName(), other.getInterfaceName())) { + return false; + } + + if (!SharedUtil.equals(getMethodName(), other.getMethodName())) { + return false; + } + + if (!SharedUtil.equals(getParameters(), other.getParameters())) { + return false; + } + + return true; + + } } \ No newline at end of file diff --git a/shared/src/com/vaadin/shared/ui/ui/UIServerRpc.java b/shared/src/com/vaadin/shared/ui/ui/UIServerRpc.java index 358ba2e24e..576ee83980 100644 --- a/shared/src/com/vaadin/shared/ui/ui/UIServerRpc.java +++ b/shared/src/com/vaadin/shared/ui/ui/UIServerRpc.java @@ -26,4 +26,11 @@ public interface UIServerRpc extends ClickRpc, ServerRpc { @Delayed(lastOnly = true) public void scroll(int scrollTop, int scrollLeft); + + @Delayed(lastOnly = true) + /* + * @Delayed just to get lastOnly semantics, sendPendingVariableChanges() + * should always be called to ensure the message is flushed right away. + */ + public void poll(); } \ No newline at end of file diff --git a/shared/src/com/vaadin/shared/ui/ui/UIState.java b/shared/src/com/vaadin/shared/ui/ui/UIState.java index d5ee4c30e6..fbb6427c6f 100644 --- a/shared/src/com/vaadin/shared/ui/ui/UIState.java +++ b/shared/src/com/vaadin/shared/ui/ui/UIState.java @@ -22,6 +22,7 @@ import com.vaadin.shared.ui.TabIndexState; public class UIState extends TabIndexState { public TooltipConfiguration tooltipConfiguration = new TooltipConfiguration(); public LoadingIndicatorConfiguration loadingIndicatorConfiguration = new LoadingIndicatorConfiguration(); + public int pollInterval = -1; public static class LoadingIndicatorConfiguration implements Serializable { public int initialDelay = 300; diff --git a/uitest/src/com/vaadin/tests/components/ui/UIPolling.html b/uitest/src/com/vaadin/tests/components/ui/UIPolling.html new file mode 100644 index 0000000000..0e3cc2bc6e --- /dev/null +++ b/uitest/src/com/vaadin/tests/components/ui/UIPolling.html @@ -0,0 +1,53 @@ + + + + + + +WindowMaximizeRestoreTest + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
WindowMaximizeRestoreTest
open/run/UIPolling?restartApplication
typeid=gwt-uid-5500
pause2000
assertTextPresent2. 1000ms has passed
typeid=gwt-uid-5-1
pause2000
assertTextNotPresent8. 4000ms has passed
+ + diff --git a/uitest/src/com/vaadin/tests/components/ui/UIPolling.java b/uitest/src/com/vaadin/tests/components/ui/UIPolling.java new file mode 100644 index 0000000000..a7add63801 --- /dev/null +++ b/uitest/src/com/vaadin/tests/components/ui/UIPolling.java @@ -0,0 +1,90 @@ +/* + * Copyright 2000-2013 Vaadin Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not + * use this file except in compliance with the License. You may obtain a copy of + * the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations under + * the License. + */ +package com.vaadin.tests.components.ui; + +import com.vaadin.data.Property.ValueChangeEvent; +import com.vaadin.data.Property.ValueChangeListener; +import com.vaadin.data.util.MethodProperty; +import com.vaadin.server.VaadinRequest; +import com.vaadin.tests.components.AbstractTestUIWithLog; +import com.vaadin.tests.util.Log; +import com.vaadin.ui.TextField; + +public class UIPolling extends AbstractTestUIWithLog { + + protected static final long SLEEP_TIME = 500; + + private class BackgroundLogger extends Thread { + + @Override + public void run() { + int i = 0; + while (true) { + i++; + try { + Thread.sleep(SLEEP_TIME); + } catch (InterruptedException e) { + } + final int iteration = i; + runSafely(new Runnable() { + @Override + public void run() { + log.log((iteration * SLEEP_TIME) + "ms has passed"); + } + }); + } + } + } + + private BackgroundLogger logger = null; + + @Override + protected void setup(VaadinRequest request) { + log = new Log(20); + log.setNumberLogRows(true); + TextField pollingInterval = new TextField("Poll interval", + new MethodProperty(this, "pollInterval")); + pollingInterval.setImmediate(true); + pollingInterval.setValue("-1"); + pollingInterval.addValueChangeListener(new ValueChangeListener() { + + @Override + public void valueChange(ValueChangeEvent event) { + if (logger != null) { + logger.stop(); + logger = null; + } + if (getPollInterval() >= 0) { + logger = new BackgroundLogger(); + logger.start(); + } + } + }); + addComponent(pollingInterval); + + } + + @Override + protected String getTestDescription() { + return "Tests the polling feature of UI. Set the polling interval using the text field. Enabling polling will at the same time start a background thread which logs every 500ms"; + } + + @Override + protected Integer getTicketNumber() { + return 11495; + } + +} -- cgit v1.2.3