Browse Source

Simplify UIDL message handling (#11733)

Pass only JSONObject around instead of multiple versions

Change-Id: Ibf747df366e384df8e3cc6f5153100168514f63a
tags/7.6.0.alpha5
Artur Signell 9 years ago
parent
commit
ba2efda9d3

+ 4
- 6
client/src/com/vaadin/client/ApplicationConnection.java View File



package com.vaadin.client; package com.vaadin.client;


import java.util.Date;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.logging.Logger; import java.util.logging.Logger;


// Hack to avoid logging an error in endRequest() // Hack to avoid logging an error in endRequest()
getServerCommunicationHandler().startRequest(); getServerCommunicationHandler().startRequest();
getServerMessageHandler().handleJSONText(jsonText, -1);
getServerMessageHandler().handleMessage(jsonText);
} }


// Tooltip can't be created earlier because the // Tooltip can't be created earlier because the


static final int MAX_CSS_WAITS = 100; static final int MAX_CSS_WAITS = 100;


public void handleWhenCSSLoaded(final String jsonText, final ValueMap json) {
public void executeWhenCSSLoaded(final Command c) {
if (!isCSSLoaded() && cssWaits < MAX_CSS_WAITS) { if (!isCSSLoaded() && cssWaits < MAX_CSS_WAITS) {
(new Timer() { (new Timer() {
@Override @Override
public void run() { public void run() {
handleWhenCSSLoaded(jsonText, json);
executeWhenCSSLoaded(c);
} }
}).schedule(50); }).schedule(50);


getLogger().severe("CSS files may have not loaded properly."); getLogger().severe("CSS files may have not loaded properly.");
} }


getServerMessageHandler().handleUIDLMessage(new Date(), jsonText,
json);
c.execute();
} }
} }



+ 1
- 1
client/src/com/vaadin/client/communication/AtmospherePushConnection.java View File

getLogger().info("Received push message: " + message); getLogger().info("Received push message: " + message);
// "for(;;);[{json}]" -> "{json}" // "for(;;);[{json}]" -> "{json}"
message = message.substring(9, message.length() - 1); message = message.substring(9, message.length() - 1);
connection.getServerMessageHandler().handleJSONText(message, 200);
connection.getServerMessageHandler().handleMessage(message);
} }
} }



+ 4
- 4
client/src/com/vaadin/client/communication/ServerCommunicationHandler.java View File

return Logger.getLogger(ServerCommunicationHandler.class.getName()); return Logger.getLogger(ServerCommunicationHandler.class.getName());
} }


public void doSendPendingVariableChanges() {
public void sendInvocationsToServer() {
if (!connection.isApplicationRunning()) { if (!connection.isApplicationRunning()) {
getLogger() getLogger()
.warning( .warning(
// There is an active request or push is enabled but not active // There is an active request or push is enabled but not active
// -> send when current request completes or push becomes active // -> send when current request completes or push becomes active
} else { } else {
sendInvocationsToServer();
doSendInvocationsToServer();
} }
} }


* changes) to the server. * changes) to the server.
* *
*/ */
public void sendInvocationsToServer() {
private void doSendInvocationsToServer() {
ServerRpcQueue serverRpcQueue = getServerRpcQueue(); ServerRpcQueue serverRpcQueue = getServerRpcQueue();
if (serverRpcQueue.isEmpty()) { if (serverRpcQueue.isEmpty()) {
return; return;
responseText.length() responseText.length()
- JSON_COMMUNICATION_SUFFIX.length()); - JSON_COMMUNICATION_SUFFIX.length());


getServerMessageHandler().handleJSONText(jsonText, statusCode);
getServerMessageHandler().handleMessage(jsonText);
} }
}; };
if (push != null) { if (push != null) {

+ 18
- 33
client/src/com/vaadin/client/communication/ServerMessageHandler.java View File

* Data structure holding information about pending UIDL messages. * Data structure holding information about pending UIDL messages.
*/ */
private static class PendingUIDLMessage { private static class PendingUIDLMessage {
private Date start;
private String jsonText;
private ValueMap json; private ValueMap json;


public PendingUIDLMessage(Date start, String jsonText, ValueMap json) {
this.start = start;
this.jsonText = jsonText;
public PendingUIDLMessage(ValueMap json) {
this.json = json; this.json = json;
} }


public Date getStart() {
return start;
}

public String getJsonText() {
return jsonText;
}

public ValueMap getJson() { public ValueMap getJson() {
return json; return json;
} }
} }


/** /**
* Handles received UIDL JSON text, parsing it, and passing it on to the
* Handles a received UIDL JSON text, parsing it, and passing it on to the
* appropriate handlers, while logging timing information. * appropriate handlers, while logging timing information.
* *
* @param jsonText * @param jsonText
* @param statusCode
* The JSON to handle
*/ */
public void handleJSONText(String jsonText, int statusCode) {
public void handleMessage(String jsonText) {
final Date start = new Date(); final Date start = new Date();
final ValueMap json; final ValueMap json;
try { try {
// FIXME // FIXME
getServerCommunicationHandler().endRequest(); getServerCommunicationHandler().endRequest();
connection.showCommunicationError(e.getMessage() connection.showCommunicationError(e.getMessage()
+ " - Original JSON-text:" + jsonText, statusCode);
+ " - Original JSON-text:" + jsonText, 200);
return; return;
} }


"JSON parsing took " + (new Date().getTime() - start.getTime()) "JSON parsing took " + (new Date().getTime() - start.getTime())
+ "ms"); + "ms");
if (connection.getState() == State.RUNNING) { if (connection.getState() == State.RUNNING) {
handleUIDLMessage(start, jsonText, json);
handleJSON(json);
} else if (connection.getState() == State.INITIALIZING) { } else if (connection.getState() == State.INITIALIZING) {
// Application is starting up for the first time // Application is starting up for the first time
connection.setApplicationRunning(true); connection.setApplicationRunning(true);
connection.handleWhenCSSLoaded(jsonText, json);
connection.executeWhenCSSLoaded(new Command() {
@Override
public void execute() {
handleJSON(json);
}
});
} else { } else {
getLogger() getLogger()
.warning( .warning(


private static native ValueMap parseJSONResponse(String jsonText) private static native ValueMap parseJSONResponse(String jsonText)
/*-{ /*-{
try {
return JSON.parse(jsonText);
} catch (ignored) {
return eval('(' + jsonText + ')');
}
return JSON.parse(jsonText);
}-*/; }-*/;


public void handleUIDLMessage(final Date start, final String jsonText,
final ValueMap json) {
protected void handleJSON(final ValueMap json) {
if (!responseHandlingLocks.isEmpty()) { if (!responseHandlingLocks.isEmpty()) {
// Some component is doing something that can't be interrupted // Some component is doing something that can't be interrupted
// (e.g. animation that should be smooth). Enqueue the UIDL // (e.g. animation that should be smooth). Enqueue the UIDL
// message for later processing. // message for later processing.
getLogger().info("Postponing UIDL handling due to lock..."); getLogger().info("Postponing UIDL handling due to lock...");
pendingUIDLMessages.add(new PendingUIDLMessage(start, jsonText,
json));
pendingUIDLMessages.add(new PendingUIDLMessage(json));
if (!forceHandleMessage.isRunning()) { if (!forceHandleMessage.isRunning()) {
forceHandleMessage.schedule(MAX_SUSPENDED_TIMEOUT); forceHandleMessage.schedule(MAX_SUSPENDED_TIMEOUT);
} }
return; return;
} }


final Date start = new Date();
/* /*
* Lock response handling to avoid a situation where something pushed * Lock response handling to avoid a situation where something pushed
* from the server gets processed while waiting for e.g. lazily loaded * from the server gets processed while waiting for e.g. lazily loaded


getLogger().info( getLogger().info(
" Processing time was " " Processing time was "
+ String.valueOf(lastProcessingTime)
+ "ms for " + jsonText.length()
+ " characters of JSON");
+ String.valueOf(lastProcessingTime) + "ms");
getLogger().info( getLogger().info(
"Referenced paintables: " + getConnectorMap().size()); "Referenced paintables: " + getConnectorMap().size());


pendingUIDLMessages = new ArrayList<PendingUIDLMessage>(); pendingUIDLMessages = new ArrayList<PendingUIDLMessage>();


for (PendingUIDLMessage pending : pendingMessages) { for (PendingUIDLMessage pending : pendingMessages) {
handleUIDLMessage(pending.getStart(), pending.getJsonText(),
pending.getJson());
handleJSON(pending.getJson());
} }
} }
} }

+ 1
- 1
client/src/com/vaadin/client/communication/ServerRpcQueue.java View File

return; return;
} }
connection.getServerCommunicationHandler() connection.getServerCommunicationHandler()
.doSendPendingVariableChanges();
.sendInvocationsToServer();
} }
}; };



+ 2
- 4
uitest/src/com/vaadin/tests/widgetset/client/MockServerMessageHandler.java View File

*/ */
package com.vaadin.tests.widgetset.client; package com.vaadin.tests.widgetset.client;


import java.util.Date;

import com.vaadin.client.ValueMap; import com.vaadin.client.ValueMap;
import com.vaadin.client.communication.ServerMessageHandler; import com.vaadin.client.communication.ServerMessageHandler;
import com.vaadin.shared.ApplicationConstants; import com.vaadin.shared.ApplicationConstants;
protected String lastCsrfTokenReceiver; protected String lastCsrfTokenReceiver;


@Override @Override
public void handleUIDLMessage(Date start, String jsonText, ValueMap json) {
public void handleJSON(ValueMap json) {
lastCsrfTokenReceiver = json lastCsrfTokenReceiver = json
.getString(ApplicationConstants.UIDL_SECURITY_TOKEN_ID); .getString(ApplicationConstants.UIDL_SECURITY_TOKEN_ID);


super.handleUIDLMessage(start, jsonText, json);
super.handleJSON(json);
} }


} }

Loading…
Cancel
Save