Skip to content

Commit

Permalink
Remove clientId argument where obsolete
Browse files Browse the repository at this point in the history
  • Loading branch information
planger committed Sep 16, 2021
1 parent 119d57f commit a0d82f2
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 51 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,10 @@ protected void executeOperation(final ApplyTaskEditOperation operation, final GM
String text = operation.getExpression();
if (text.startsWith(TaskEditContextActionProvider.DURATION_PREFIX)) {
String durationString = text.substring(TaskEditContextActionProvider.DURATION_PREFIX.length());
actionProcessor.dispatch(modelState.getClientId(),
new EditTaskOperation(operation.getTaskId(), "duration", durationString));
actionProcessor.dispatch(new EditTaskOperation(operation.getTaskId(), "duration", durationString));
} else if (text.startsWith(TaskEditContextActionProvider.TYPE_PREFIX)) {
String typeString = text.substring(TaskEditContextActionProvider.TYPE_PREFIX.length());
actionProcessor.dispatch(modelState.getClientId(),
new EditTaskOperation(operation.getTaskId(), "taskType", typeString));
actionProcessor.dispatch(new EditTaskOperation(operation.getTaskId(), "taskType", typeString));
} else {
throw new GLSPServerException(
"Cannot process 'ApplyTaskEditOperation' expression: " + operation.getExpression());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,14 @@
public interface ActionDispatcher extends IDisposable {

/**
* @see ActionDispatcher#dispatch(String, Action)
* @see ActionDispatcher#dispatch(Action)
*
* @param message ActionMessage received from the client
* @return
* A {@link CompletableFuture} indicating when the action processing is complete
*/
default CompletableFuture<Void> dispatch(final ActionMessage message) {
return dispatch(message.getClientId(), message.getAction());
return dispatch(message.getAction());
}

/**
Expand All @@ -40,22 +40,20 @@ default CompletableFuture<Void> dispatch(final ActionMessage message) {
* </p>
*
* @param clientId The client from which the action was received
* @param action The action to dispatch
* @return
* A {@link CompletableFuture} indicating when the action processing is complete
*/
CompletableFuture<Void> dispatch(String clientId, Action action);
CompletableFuture<Void> dispatch(Action action);

/**
* <p>
* Processes all given actions, received from the specified clientId, by dispatching to the corresponding handlers.
* </p>
*
* @param clientId
* @param actions
* @param actions Actions to dispatch
* @return A list of {@link CompletableFuture CompletableFutures}; one for each dispatched action
*/
default List<CompletableFuture<Void>> dispatchAll(final String clientId, final List<Action> actions) {
return actions.stream().map(action -> dispatch(clientId, action)).collect(Collectors.toList());
default List<CompletableFuture<Void>> dispatchAll(final List<Action> actions) {
return actions.stream().map(action -> dispatch(action)).collect(Collectors.toList());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,13 @@ public List<Action> executeAction(final RequestModelAction action, final GModelS
}

protected void notifyStartLoading(final GModelState modelState) {
actionDispatcher.dispatch(modelState.getClientId(), ServerStatusUtil.info("Model loading in progress!"));
actionDispatcher.dispatch(modelState.getClientId(), ServerMessageUtil.info("Model loading in progress!"));
actionDispatcher.dispatch(ServerStatusUtil.info("Model loading in progress!"));
actionDispatcher.dispatch(ServerMessageUtil.info("Model loading in progress!"));
}

protected void notifyFinishedLoading(final GModelState modelState) {
actionDispatcher.dispatch(modelState.getClientId(), ServerStatusUtil.clear());
actionDispatcher.dispatch(modelState.getClientId(), ServerMessageUtil.clear());
actionDispatcher.dispatch(ServerStatusUtil.clear());
actionDispatcher.dispatch(ServerMessageUtil.clear());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,7 @@ protected void scheduleClientNotification(final String clientId, final Path file
}

protected void notifyClient(final ClientNotification clientNotification) {
actionDispatcher.dispatch(clientNotification.clientId,
new ModelSourceChangedAction(clientNotification.modelSourceName));
actionDispatcher.dispatch(new ModelSourceChangedAction(clientNotification.modelSourceName));
}

class FileWatchWorker extends Thread {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ public class DefaultActionDispatcher extends Disposable implements ActionDispatc
@Inject
protected ActionHandlerRegistry actionHandlerRegistry;

@Inject
@ClientId
protected String clientId;

Expand Down Expand Up @@ -90,7 +89,7 @@ public DefaultActionDispatcher() {
}

@Override
public CompletableFuture<Void> dispatch(final String clientId, final Action action) {
public CompletableFuture<Void> dispatch(final Action action) {
return dispatch(new ActionMessage(clientId, action));
}

Expand Down Expand Up @@ -166,14 +165,13 @@ private void handleNextMessage()
protected void handleMessage(final ActionMessage message) {
checkThread();
final Action action = message.getAction();
final String clientId = message.getClientId();
if (action == null) {
LOG.warn(String.format("Received an action message without an action for client %s", clientId));
return;
}

try {
List<CompletableFuture<Void>> results = runAction(action, clientId);
List<CompletableFuture<Void>> results = runAction(action);
CompletableFuture<Void> result = FutureUtil.aggregateResults(results);
result.thenAccept(any -> {
this.results.remove(message).complete(null);
Expand All @@ -186,7 +184,7 @@ protected void handleMessage(final ActionMessage message) {
}
}

protected List<CompletableFuture<Void>> runAction(final Action action, final String clientId) {
protected List<CompletableFuture<Void>> runAction(final Action action) {
final List<ActionHandler> actionHandlers = actionHandlerRegistry.get(action);
if (actionHandlers.isEmpty()) {
throw new IllegalArgumentException("No handler registered for action: " + action);
Expand All @@ -197,7 +195,7 @@ protected List<CompletableFuture<Void>> runAction(final Action action, final Str
final List<Action> responses = actionHandler.execute(action, modelState).stream()
.map(response -> ResponseAction.respond(action, response))
.collect(Collectors.toList());
results.addAll(dispatchAll(clientId, responses));
results.addAll(dispatchAll(responses));
}
return results;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public class CutOperationHandler extends BasicOperationHandler<CutOperation> {
public void executeOperation(final CutOperation operation, final GModelState modelState) {
List<String> cutableElementIds = getElementToCut(operation, modelState);
if (!cutableElementIds.isEmpty()) {
actionDispatcher.dispatch(modelState.getClientId(), new DeleteOperation(cutableElementIds));
actionDispatcher.dispatch(new DeleteOperation(cutableElementIds));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,8 @@ public void removeDirectory()
@Test
void changingWatchedFileNotifiesClient()
throws IOException, InterruptedException {
final String clientId = "1";
final File file = createFile("test.txt");
final GModelState modelState = modelState(clientId, fileUri(file));
final GModelState modelState = modelState("1", fileUri(file));

final FileWatcher fileWatcher = new FileWatcher(sessionManager, actionDispatcher);
fileWatcher.setDebounceDelay(0);
Expand All @@ -85,15 +84,14 @@ void changingWatchedFileNotifiesClient()
sleep();
fileWatcher.stopWatching(modelState);

assertNotifications(clientId, 1);
assertNotifications(1);
}

@Test
void deletingWatchedFileNotifiesClient()
throws IOException, InterruptedException {
final String clientId = "1";
final File file = createFile("test.txt");
final GModelState modelState = modelState(clientId, fileUri(file));
final GModelState modelState = modelState("1", fileUri(file));

final FileWatcher fileWatcher = new FileWatcher(sessionManager, actionDispatcher);
fileWatcher.setDebounceDelay(0);
Expand All @@ -103,15 +101,14 @@ void deletingWatchedFileNotifiesClient()
sleep();
fileWatcher.stopWatching(modelState);

assertNotifications(clientId, 1);
assertNotifications(1);
}

@Test
void changingWatchedFileWhilePausedDoesntNotifyClient()
throws IOException, InterruptedException {
final String clientId = "1";
final File file = createFile("test.txt");
final GModelState modelState = modelState(clientId, fileUri(file));
final GModelState modelState = modelState("1", fileUri(file));

final FileWatcher fileWatcher = new FileWatcher(sessionManager, actionDispatcher);
fileWatcher.setDebounceDelay(0);
Expand All @@ -123,15 +120,14 @@ void changingWatchedFileWhilePausedDoesntNotifyClient()
sleep();
fileWatcher.stopWatching(modelState);

assertNoNotification(clientId);
assertNoNotification();
}

@Test
void changingWatchedFileAfterPauseAndContinueNotifiesClient()
throws IOException, InterruptedException {
final String clientId = "1";
final File file = createFile("test.txt");
final GModelState modelState = modelState(clientId, fileUri(file));
final GModelState modelState = modelState("1", fileUri(file));

final FileWatcher fileWatcher = new FileWatcher(sessionManager, actionDispatcher);
fileWatcher.setDebounceDelay(0);
Expand All @@ -145,20 +141,17 @@ void changingWatchedFileAfterPauseAndContinueNotifiesClient()
sleep();
fileWatcher.stopWatching(modelState);

assertNotifications(clientId, 1);
assertNotifications(1);
}

private void assertNoNotification(final String clientId) {
if (actionDispatcher.dispatchedActions.get(clientId) != null) {
assertEquals(actionDispatcher.dispatchedActions.get(clientId).size(), 0);
}
private void assertNoNotification() {
assertEquals(actionDispatcher.dispatchedActions.size(), 0);
}

private void assertNotifications(final String clientId, final int size) throws InterruptedException {
final List<Action> actionsDispatchedToClient1 = actionDispatcher.dispatchedActions.get(clientId);
assertEquals(actionsDispatchedToClient1.size(), size);
private void assertNotifications(final int size) throws InterruptedException {
assertEquals(actionDispatcher.dispatchedActions.size(), size);
for (int i = 0; i < size; i++) {
assertTrue(actionsDispatchedToClient1.get(i) instanceof ModelSourceChangedAction);
assertTrue(actionDispatcher.dispatchedActions.get(i) instanceof ModelSourceChangedAction);
}
}

Expand Down Expand Up @@ -201,14 +194,11 @@ private void sleep()

@SuppressWarnings("checkstyle:VisibilityModifier")
class RecordingActionDispatcher extends Disposable implements ActionDispatcher {
Map<String, List<Action>> dispatchedActions = new HashMap<>();
List<Action> dispatchedActions = new ArrayList<>();

@Override
public CompletableFuture<Void> dispatch(final String clientId, final Action action) {
if (!dispatchedActions.containsKey(clientId)) {
dispatchedActions.put(clientId, new ArrayList<Action>());
}
dispatchedActions.get(clientId).add(action);
public CompletableFuture<Void> dispatch(final Action action) {
dispatchedActions.add(action);
return CompletableFuture.completedFuture(null);
}
}
Expand Down

0 comments on commit a0d82f2

Please sign in to comment.