Skip to content

Commit

Permalink
Implementation proposal for #3921. Please test and report !
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexisDrogoul committed Oct 23, 2023
1 parent 05fbad3 commit ab4bbdb
Showing 1 changed file with 9 additions and 7 deletions.
16 changes: 9 additions & 7 deletions msi.gama.core/src/msi/gama/runtime/server/CommandExecutor.java
Expand Up @@ -26,10 +26,9 @@
import static msi.gama.runtime.server.ISocketCommand.UPLOAD;

import java.util.AbstractMap;
import java.util.LinkedList;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Queue;
import java.util.concurrent.LinkedBlockingQueue;

import org.java_websocket.WebSocket;
import org.java_websocket.enums.ReadyState;
Expand All @@ -56,14 +55,17 @@ public class CommandExecutor {
protected final Map<String, ISocketCommand> commands;

/** The command queue. */
protected volatile Queue<Entry<WebSocket, IMap<String, Object>>> commandQueue;
protected volatile LinkedBlockingQueue<Entry<WebSocket, IMap<String, Object>>> commandQueue;

/** The command execution thread. */
protected final Thread commandExecutionThread = new Thread(() -> {
while (true) {
while (!commandQueue.isEmpty()) {
var cmd = commandQueue.poll();
Entry<WebSocket, IMap<String, Object>> cmd;
try {
cmd = commandQueue.take();
process(cmd.getKey(), cmd.getValue());
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
Expand All @@ -77,7 +79,7 @@ public class CommandExecutor {

public CommandExecutor() {
commands = GAMA.getGui().getServerCommands();
commandQueue = new LinkedList<>();
commandQueue = new LinkedBlockingQueue<>();
commandExecutionThread.setUncaughtExceptionHandler(GamaExecutorService.EXCEPTION_HANDLER);
commandExecutionThread.start();
}
Expand All @@ -93,7 +95,7 @@ public CommandExecutor() {
* @date 15 oct. 2023
*/
public void pushCommand(final WebSocket socket, final IMap<String, Object> map) {
commandQueue.add(new AbstractMap.SimpleEntry<>(socket, map));
commandQueue.offer(new AbstractMap.SimpleEntry<>(socket, map));
}

/**
Expand Down

0 comments on commit ab4bbdb

Please sign in to comment.