You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

MessageSender.java 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  1. /*
  2. * Copyright 2000-2021 Vaadin Ltd.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5. * use this file except in compliance with the License. You may obtain a copy of
  6. * the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. * License for the specific language governing permissions and limitations under
  14. * the License.
  15. */
  16. package com.vaadin.client.communication;
  17. import java.util.logging.Logger;
  18. import com.google.gwt.core.client.GWT;
  19. import com.google.gwt.core.client.Scheduler;
  20. import com.vaadin.client.ApplicationConfiguration;
  21. import com.vaadin.client.ApplicationConnection;
  22. import com.vaadin.client.ApplicationConnection.RequestStartingEvent;
  23. import com.vaadin.client.ApplicationConnection.ResponseHandlingEndedEvent;
  24. import com.vaadin.client.Util;
  25. import com.vaadin.client.VLoadingIndicator;
  26. import com.vaadin.shared.ApplicationConstants;
  27. import com.vaadin.shared.Version;
  28. import com.vaadin.shared.ui.ui.UIState.PushConfigurationState;
  29. import elemental.json.Json;
  30. import elemental.json.JsonArray;
  31. import elemental.json.JsonObject;
  32. import elemental.json.JsonValue;
  33. /**
  34. * MessageSender is responsible for sending messages to the server.
  35. * <p>
  36. * Internally uses {@link XhrConnection} and/or {@link PushConnection} for
  37. * delivering messages, depending on the application configuration.
  38. *
  39. * @since 7.6
  40. * @author Vaadin Ltd
  41. */
  42. public class MessageSender {
  43. private ApplicationConnection connection;
  44. private boolean hasActiveRequest = false;
  45. private boolean resynchronizeRequested = false;
  46. /**
  47. * Counter for the messages send to the server. First sent message has id 0.
  48. */
  49. private int clientToServerMessageId = 0;
  50. private XhrConnection xhrConnection;
  51. private PushConnection push;
  52. public MessageSender() {
  53. xhrConnection = GWT.create(XhrConnection.class);
  54. }
  55. /**
  56. * Sets the application connection this instance is connected to. Called
  57. * internally by the framework.
  58. *
  59. * @param connection
  60. * the application connection this instance is connected to
  61. */
  62. public void setConnection(ApplicationConnection connection) {
  63. this.connection = connection;
  64. xhrConnection.setConnection(connection);
  65. }
  66. private static Logger getLogger() {
  67. return Logger.getLogger(MessageSender.class.getName());
  68. }
  69. public void sendInvocationsToServer() {
  70. if (!connection.isApplicationRunning()) {
  71. getLogger().warning(
  72. "Trying to send RPC from not yet started or stopped application");
  73. return;
  74. }
  75. if (hasActiveRequest() || (push != null && !push.isActive())) {
  76. // There is an active request or push is enabled but not active
  77. // -> send when current request completes or push becomes active
  78. } else {
  79. doSendInvocationsToServer();
  80. }
  81. }
  82. /**
  83. * Sends all pending method invocations (server RPC and legacy variable
  84. * changes) to the server.
  85. *
  86. */
  87. private void doSendInvocationsToServer() {
  88. ServerRpcQueue serverRpcQueue = getServerRpcQueue();
  89. if (serverRpcQueue.isEmpty() && !resynchronizeRequested) {
  90. return;
  91. }
  92. if (ApplicationConfiguration.isDebugMode()) {
  93. Util.logMethodInvocations(connection, serverRpcQueue.getAll());
  94. }
  95. boolean showLoadingIndicator = serverRpcQueue.showLoadingIndicator();
  96. JsonArray reqJson = serverRpcQueue.toJson();
  97. serverRpcQueue.clear();
  98. if (reqJson.length() == 0 && !resynchronizeRequested) {
  99. // Nothing to send, all invocations were filtered out (for
  100. // non-existing connectors)
  101. getLogger().warning(
  102. "All RPCs filtered out, not sending anything to the server");
  103. return;
  104. }
  105. JsonObject extraJson = Json.createObject();
  106. if (!connection.getConfiguration().isWidgetsetVersionSent()) {
  107. extraJson.put(ApplicationConstants.WIDGETSET_VERSION_ID,
  108. Version.getFullVersion());
  109. connection.getConfiguration().setWidgetsetVersionSent();
  110. }
  111. if (resynchronizeRequested) {
  112. getLogger().info("Resynchronizing from server");
  113. getMessageHandler().onResynchronize();
  114. extraJson.put(ApplicationConstants.RESYNCHRONIZE_ID, true);
  115. resynchronizeRequested = false;
  116. }
  117. if (showLoadingIndicator) {
  118. connection.getLoadingIndicator().trigger();
  119. }
  120. send(reqJson, extraJson);
  121. }
  122. private ServerRpcQueue getServerRpcQueue() {
  123. return connection.getServerRpcQueue();
  124. }
  125. /**
  126. * Makes an UIDL request to the server.
  127. *
  128. * @param reqInvocations
  129. * Data containing RPC invocations and all related information.
  130. * @param extraJson
  131. * The JsonObject whose parameters are added to the payload
  132. */
  133. protected void send(final JsonArray reqInvocations,
  134. final JsonObject extraJson) {
  135. startRequest();
  136. JsonObject payload = Json.createObject();
  137. String csrfToken = getMessageHandler().getCsrfToken();
  138. if (!csrfToken.equals(ApplicationConstants.CSRF_TOKEN_DEFAULT_VALUE)) {
  139. payload.put(ApplicationConstants.CSRF_TOKEN, csrfToken);
  140. }
  141. payload.put(ApplicationConstants.RPC_INVOCATIONS, reqInvocations);
  142. payload.put(ApplicationConstants.SERVER_SYNC_ID,
  143. getMessageHandler().getLastSeenServerSyncId());
  144. payload.put(ApplicationConstants.CLIENT_TO_SERVER_ID,
  145. clientToServerMessageId++);
  146. if (extraJson != null) {
  147. for (String key : extraJson.keys()) {
  148. JsonValue value = extraJson.get(key);
  149. payload.put(key, value);
  150. }
  151. }
  152. send(payload);
  153. }
  154. /**
  155. * Sends an asynchronous or synchronous UIDL request to the server using the
  156. * given URI.
  157. *
  158. * @param payload
  159. * The contents of the request to send
  160. */
  161. public void send(final JsonObject payload) {
  162. if (push != null && push.isBidirectional()) {
  163. push.push(payload);
  164. } else {
  165. xhrConnection.send(payload);
  166. }
  167. }
  168. /**
  169. * Sets the status for the push connection.
  170. *
  171. * @param enabled
  172. * <code>true</code> to enable the push connection;
  173. * <code>false</code> to disable the push connection.
  174. */
  175. public void setPushEnabled(boolean enabled) {
  176. final PushConfigurationState pushState = connection.getUIConnector()
  177. .getState().pushConfiguration;
  178. if (enabled && push == null) {
  179. push = GWT.create(PushConnection.class);
  180. push.init(connection, pushState);
  181. } else if (!enabled && push != null && push.isActive()) {
  182. push.disconnect(() -> {
  183. push = null;
  184. /*
  185. * If push has been enabled again while we were waiting for the
  186. * old connection to disconnect, now is the right time to open a
  187. * new connection
  188. */
  189. if (pushState.mode.isEnabled()) {
  190. setPushEnabled(true);
  191. }
  192. /*
  193. * Send anything that was enqueued while we waited for the
  194. * connection to close
  195. */
  196. if (getServerRpcQueue().isFlushPending()) {
  197. getServerRpcQueue().flush();
  198. }
  199. });
  200. }
  201. }
  202. public void startRequest() {
  203. if (hasActiveRequest) {
  204. getLogger().severe(
  205. "Trying to start a new request while another is active");
  206. }
  207. hasActiveRequest = true;
  208. connection.fireEvent(new RequestStartingEvent(connection));
  209. }
  210. public void endRequest() {
  211. if (!hasActiveRequest) {
  212. getLogger().severe("No active request");
  213. }
  214. // After sendInvocationsToServer() there may be a new active
  215. // request, so we must set hasActiveRequest to false before, not after,
  216. // the call.
  217. hasActiveRequest = false;
  218. if (connection.isApplicationRunning()) {
  219. if (getServerRpcQueue().isFlushPending()
  220. || resynchronizeRequested) {
  221. sendInvocationsToServer();
  222. }
  223. runPostRequestHooks(connection.getConfiguration().getRootPanelId());
  224. }
  225. // deferring to avoid flickering
  226. Scheduler.get().scheduleDeferred(() -> {
  227. if (!connection.isApplicationRunning() || !(hasActiveRequest()
  228. || getServerRpcQueue().isFlushPending())) {
  229. getLoadingIndicator().hide();
  230. // If on Liferay and session expiration management is in
  231. // use, extend session duration on each request.
  232. // Doing it here rather than before the request to improve
  233. // responsiveness.
  234. // Postponed until the end of the next request if other
  235. // requests still pending.
  236. extendLiferaySession();
  237. }
  238. });
  239. connection.fireEvent(new ResponseHandlingEndedEvent(connection));
  240. }
  241. /**
  242. * Runs possibly registered client side post request hooks. This is expected
  243. * to be run after each uidl request made by Vaadin application.
  244. *
  245. * @param appId
  246. */
  247. public static native void runPostRequestHooks(String appId)
  248. /*-{
  249. if ($wnd.vaadin.postRequestHooks) {
  250. for ( var hook in $wnd.vaadin.postRequestHooks) {
  251. if (typeof ($wnd.vaadin.postRequestHooks[hook]) == "function") {
  252. try {
  253. $wnd.vaadin.postRequestHooks[hook](appId);
  254. } catch (e) {
  255. }
  256. }
  257. }
  258. }
  259. }-*/;
  260. /**
  261. * If on Liferay and logged in, ask the client side session management
  262. * JavaScript to extend the session duration.
  263. *
  264. * Otherwise, Liferay client side JavaScript will explicitly expire the
  265. * session even though the server side considers the session to be active.
  266. * See ticket #8305 for more information.
  267. */
  268. public static native void extendLiferaySession()
  269. /*-{
  270. if ($wnd.Liferay && $wnd.Liferay.Session) {
  271. $wnd.Liferay.Session.extend();
  272. // if the extend banner is visible, hide it
  273. if ($wnd.Liferay.Session.banner) {
  274. $wnd.Liferay.Session.banner.remove();
  275. }
  276. }
  277. }-*/;
  278. /**
  279. * Indicates whether or not there are currently active UIDL requests. Used
  280. * internally to sequence requests properly, seldom needed in Widgets.
  281. *
  282. * @return true if there are active requests
  283. */
  284. public boolean hasActiveRequest() {
  285. return hasActiveRequest;
  286. }
  287. /**
  288. * Returns a human readable string representation of the method used to
  289. * communicate with the server.
  290. *
  291. * @return A string representation of the current transport type
  292. */
  293. public String getCommunicationMethodName() {
  294. String clientToServer = "XHR";
  295. String serverToClient = "-";
  296. if (push != null) {
  297. serverToClient = push.getTransportType();
  298. if (push.isBidirectional()) {
  299. clientToServer = serverToClient;
  300. }
  301. }
  302. return "Client to server: " + clientToServer + ", "
  303. + "server to client: " + serverToClient;
  304. }
  305. private ConnectionStateHandler getConnectionStateHandler() {
  306. return connection.getConnectionStateHandler();
  307. }
  308. private MessageHandler getMessageHandler() {
  309. return connection.getMessageHandler();
  310. }
  311. private VLoadingIndicator getLoadingIndicator() {
  312. return connection.getLoadingIndicator();
  313. }
  314. /**
  315. * Resynchronize the client side, i.e. reload all component hierarchy and
  316. * state from the server
  317. */
  318. public void resynchronize() {
  319. getLogger().info("Resynchronize from server requested");
  320. resynchronizeRequested = true;
  321. sendInvocationsToServer();
  322. }
  323. /**
  324. * Used internally to update what the server expects.
  325. *
  326. * @param nextExpectedId
  327. * the new client id to set
  328. * @param force
  329. * true if the id must be updated, false otherwise
  330. */
  331. public void setClientToServerMessageId(int nextExpectedId, boolean force) {
  332. if (nextExpectedId == clientToServerMessageId) {
  333. // No op as everything matches they way it should
  334. return;
  335. }
  336. if (force) {
  337. getLogger().info(
  338. "Forced update of clientId to " + clientToServerMessageId);
  339. clientToServerMessageId = nextExpectedId;
  340. return;
  341. }
  342. if (nextExpectedId > clientToServerMessageId) {
  343. if (clientToServerMessageId == 0) {
  344. // We have never sent a message to the server, so likely the
  345. // server knows better (typical case is that we refreshed a
  346. // @PreserveOnRefresh UI)
  347. getLogger().info("Updating client-to-server id to "
  348. + nextExpectedId + " based on server");
  349. } else {
  350. getLogger().warning(
  351. "Server expects next client-to-server id to be "
  352. + nextExpectedId + " but we were going to use "
  353. + clientToServerMessageId + ". Will use "
  354. + nextExpectedId + ".");
  355. }
  356. clientToServerMessageId = nextExpectedId;
  357. } else {
  358. // Server has not yet seen all our messages
  359. // Do nothing as they will arrive eventually
  360. }
  361. }
  362. }