Skip to content

Commit

Permalink
GLSP-77: Allow WebSocket connections to reconnect after interrupt (#208)
Browse files Browse the repository at this point in the history
* GLSP-77: Allow WebSocket connections to reconnect after interrupt

If Websocket connections reconnect after an interrupt, ensure that the last modelState is restored on RequestModelAction if available

Part of eclipse-glsp/glsp#77
  • Loading branch information
ndoschek authored Jul 24, 2023
1 parent 355cb4d commit 8587e82
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import java.util.List;
import java.util.Optional;

import org.eclipse.glsp.graph.GModelRoot;
import org.eclipse.glsp.server.actions.AbstractActionHandler;
import org.eclipse.glsp.server.actions.Action;
import org.eclipse.glsp.server.actions.ActionDispatcher;
Expand All @@ -26,6 +27,7 @@
import org.eclipse.glsp.server.features.progress.ProgressService;
import org.eclipse.glsp.server.features.sourcemodelwatcher.SourceModelWatcher;
import org.eclipse.glsp.server.model.GModelState;
import org.eclipse.glsp.server.utils.ClientOptionsUtil;
import org.eclipse.glsp.server.utils.ServerStatusUtil;

import com.google.inject.Inject;
Expand Down Expand Up @@ -64,15 +66,34 @@ public class RequestModelActionHandler extends AbstractActionHandler<RequestMode
public List<Action> executeAction(final RequestModelAction action) {
modelState.setClientOptions(action.getOptions());

boolean isReconnecting = ClientOptionsUtil.isReconnecting(action.getOptions());

ProgressMonitor monitor = notifyStartLoading();
sourceModelStorage.loadSourceModel(action);
if (isReconnecting) {
handleReconnect(action);
} else {
sourceModelStorage.loadSourceModel(action);
}
notifyFinishedLoading(monitor);

sourceModelWatcher.ifPresent(watcher -> watcher.startWatching());
if (!isReconnecting) {
sourceModelWatcher.ifPresent(watcher -> watcher.startWatching());
}

return modelSubmissionHandler.submitModel();
}

protected void handleReconnect(final RequestModelAction action) {
GModelRoot oldModel = modelState.getRoot();
if (oldModel != null) {
// decrease revision by one, as each submit will increase it by one;
// the next save would produce warning that source model was changed otherwise
modelState.getRoot().setRevision(oldModel.getRevision() - 1);
} else {
sourceModelStorage.loadSourceModel(action);
}
}

protected ProgressMonitor notifyStartLoading() {
String message = "Model loading in progress";
actionDispatcher.dispatch(ServerStatusUtil.info(message));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/********************************************************************************
* Copyright (c) 2019-2021 EclipseSource and others.
* Copyright (c) 2019-2023 EclipseSource and others.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
Expand All @@ -25,6 +25,7 @@
public final class ClientOptionsUtil {
public static final String DIAGRAM_TYPE = "diagramType";
public static final String SOURCE_URI = "sourceUri";
public static final String IS_RECONNECTING = "isReconnecting";
private static final String FILE_PREFIX = "file://";

private ClientOptionsUtil() {}
Expand All @@ -35,13 +36,16 @@ public static Optional<String> getSourceUri(final Map<String, String> options) {

public static Optional<String> getDiagramType(final Map<String, String> options) {
return MapUtil.getValue(options, DIAGRAM_TYPE);

}

public static Optional<File> getSourceUriAsFile(final Map<String, String> options) {
return MapUtil.getValue(options, SOURCE_URI).map(ClientOptionsUtil::getAsFile);
}

public static Boolean isReconnecting(final Map<String, String> options) {
return MapUtil.getBoolValue(options, IS_RECONNECTING);
}

public static String adaptUri(final String uri) {
return uri.replace(FILE_PREFIX, "");
}
Expand Down

0 comments on commit 8587e82

Please sign in to comment.