Skip to content

Commit

Permalink
GAMA listener, try to add dynamic output
Browse files Browse the repository at this point in the history
  • Loading branch information
hqnghi88 committed Mar 12, 2022
1 parent 2dd2405 commit 0f781fb
Show file tree
Hide file tree
Showing 7 changed files with 250 additions and 118 deletions.
3 changes: 1 addition & 2 deletions msi.gama.headless/SimpleGUI.html
Expand Up @@ -253,7 +253,6 @@ <h3>Create a new <code>Dialog</code> Widget</h3>
// message received - show the message in div#messages
return false;
};
var sk;
var interval_id;
// send message from the form
function run() {
Expand All @@ -263,7 +262,7 @@ <h3>Create a new <code>Dialog</code> Widget</h3>
socket.binaryType = "arraybuffer";
// socket.send(exp_compiled);
socket.send("run@" + $('#gaml_path').val() + "@" + $('#exp_name').val());
sk = new WebSocket("ws://localhost:6868/output");
var sk = new WebSocket("ws://localhost:6868/output");
sk.binaryType = "arraybuffer";
sk.addEventListener('open', (event) => {

Expand Down
Expand Up @@ -358,6 +358,11 @@ public void setExperimentID(final String experimentID) {
this.experimentID = experimentID;
}


public ListenedVariable[] getListenedVariables() {
return listenedVariables;
}

/**
* Export variables.
*/
Expand Down
30 changes: 1 addition & 29 deletions msi.gama.headless/src/msi/gama/headless/runtime/Application.java
Expand Up @@ -366,7 +366,7 @@ public Object start(final IApplicationContext context) throws Exception {
} else if (args.contains(BUILD_XML_PARAMETER)) {
buildXML(args);
} else if (args.contains(SOCKET_PARAMETER)) {
createSocketServer();
GamaWebSocketServer.newInstance(this.socket, this);
} else {
runSimulation(args);
}
Expand Down Expand Up @@ -632,32 +632,4 @@ public void runGamlSimulation(final List<String> args) throws IOException, GamaH
System.exit(0);
}

/**
* Creates the socket server.
*
* @throws UnknownHostException
* the unknown host exception
*/
public void createSocketServer() throws UnknownHostException {
socketServer = new GamaWebSocketServer(this.socket, this);
socketServer.endpoints.put("/compile", new CompileEndPoint());
socketServer.endpoints.put("/launch", new LaunchEndPoint());
socketServer.start();
System.out.println("ChatServer started on port: " + socketServer.getPort());
System.setOut(new WebSocketPrintStream(System.out, socketServer));
BufferedReader sysin = new BufferedReader(new InputStreamReader(System.in));
try {

while (true) {
String in = sysin.readLine();
socketServer.broadcast(in);
if ("exit".equals(in)) {
socketServer.stop(1000);
break;
}
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
@@ -1,13 +1,9 @@
package msi.gama.headless.runtime;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Arrays;
import java.nio.ByteBuffer;

import org.java_websocket.WebSocket;

import msi.gama.headless.core.GamaHeadlessException;

public class CompileEndPoint implements Endpoint{

@Override
Expand All @@ -20,5 +16,11 @@ public void onMessage(GamaWebSocketServer server, WebSocket socket, String messa
socket.send(message);

}

@Override
public void onMessage(GamaWebSocketServer server, WebSocket conn, ByteBuffer message) {
// TODO Auto-generated method stub

}

}
Expand Up @@ -24,86 +24,109 @@
* OTHER DEALINGS IN THE SOFTWARE.
*/

import java.awt.image.BufferedImage;
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintStream;
import java.io.UnsupportedEncodingException;
import java.net.InetSocketAddress;
import java.net.URI;
import java.net.UnknownHostException;
import java.nio.ByteBuffer;
import java.nio.charset.Charset;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

import javax.imageio.ImageIO;

import org.java_websocket.WebSocket;
import org.java_websocket.drafts.Draft;
import org.java_websocket.drafts.Draft_6455;
import org.java_websocket.handshake.ClientHandshake;
import org.java_websocket.server.WebSocketServer;

import msi.gama.headless.core.GamaHeadlessException;
import msi.gama.headless.job.ExperimentJob;
import msi.gama.headless.runtime.LocalSimulationRuntime.ExperimentJobThread;

/**
* A simple WebSocketServer implementation. Keeps track of a "chatroom".
*/
interface Endpoint {
void onOpen(WebSocket socket);

void onMessage(GamaWebSocketServer server, WebSocket socket, String message);
// add other event handlers here

void onMessage(GamaWebSocketServer server, WebSocket conn, ByteBuffer message);
}

public class GamaWebSocketServer extends WebSocketServer {
Map<String, Endpoint> endpoints = Collections.synchronizedMap(new HashMap<>());
Map<WebSocket, Endpoint> saved_endpoints = Collections.synchronizedMap(new HashMap<>());

private Application app;
/** The instance. */
private static GamaWebSocketServer instance;
/** The simulations. */
final Map<String, ExperimentJob> simulations = new HashMap<>();
private static WebSocketPrintStream bufferStream;

public GamaWebSocketServer(int port, Application ap) throws UnknownHostException {
public GamaWebSocketServer(int port, Application a) {
super(new InetSocketAddress(port));
app = ap;
app = a;
}

public Application getDefaultApp() {
return app;
}
public GamaWebSocketServer(InetSocketAddress address) {
super(address);

/**
* Gets the single instance of GamaWebSocketServer.
*
* @return single instance of GamaWebSocketServer
*/
public static GamaWebSocketServer newInstance(final int p, final Application a) {
if (instance == null) {
createSocketServer(p, a);
}
return instance;
}

public GamaWebSocketServer(int port, Draft_6455 draft) {
super(new InetSocketAddress(port), Collections.<Draft>singletonList(draft));
/**
* Creates the socket server.
*
* @throws UnknownHostException the unknown host exception
*/
public static void createSocketServer(final int port, final Application a) {
instance = new GamaWebSocketServer(port, a);
instance.endpoints.put("/compile", new CompileEndPoint());
instance.endpoints.put("/launch", new LaunchEndPoint());
instance.endpoints.put("/output", new OutputEndPoint());
instance.start();
System.out.println("ChatServer started on port: " + instance.getPort());
bufferStream = new WebSocketPrintStream(System.out, instance);
// System.setOut(bufferStream);
BufferedReader sysin = new BufferedReader(new InputStreamReader(System.in));
try {

while (true) {
String in = sysin.readLine();
instance.broadcast(in);
if ("exit".equals(in)) {
instance.stop(1000);
break;
}
}
} catch (Exception ex) {
ex.printStackTrace();
}
}

@Override
public void onOpen(WebSocket conn, ClientHandshake handshake) {
conn.send("Welcome " + conn.getRemoteSocketAddress().getAddress().getHostAddress() + " to the server!"); // This
// method
// sends
// a
// message
// to
// the
// new
// client
conn.send("Welcome " + conn.getRemoteSocketAddress().getAddress().getHostAddress() + " to the server!");
broadcast("new connection: " + handshake.getResourceDescriptor()); // This method sends a message to all clients
// connected
System.out.println(conn.getRemoteSocketAddress().getAddress().getHostAddress() + " entered the room!");



String path = URI.create(handshake.getResourceDescriptor()).getPath();
Endpoint endpoint = endpoints.get(path);
if(endpoint != null) {
if (endpoint != null) {
saved_endpoints.put(conn, endpoint);
endpoint.onOpen(conn);
}
Expand All @@ -117,34 +140,14 @@ public void onClose(WebSocket conn, int code, String reason, boolean remote) {

@Override
public void onMessage(WebSocket conn, String message) {
saved_endpoints.get(conn).onMessage(this,conn,message);
saved_endpoints.get(conn).onMessage(this, conn, message);
}

@Override
public void onMessage(WebSocket conn, ByteBuffer message) {
broadcast(message.array());
System.out.println(conn + ": " + message);
}

public static void main(String[] args) throws InterruptedException, IOException {
int port = 8887; // 843 flash policy port
try {
port = Integer.parseInt(args[0]);
} catch (Exception ex) {
}
GamaWebSocketServer s = new GamaWebSocketServer(new InetSocketAddress(port));
s.start();
System.out.println("ChatServer started on port: " + s.getPort());

BufferedReader sysin = new BufferedReader(new InputStreamReader(System.in));
while (true) {
String in = sysin.readLine();
s.broadcast(in);
if (in.equals("exit")) {
s.stop(1000);
break;
}
}
saved_endpoints.get(conn).onMessage(this, conn, message);
// broadcast(message.array());
// System.out.println(conn + ": " + message);
}

@Override
Expand All @@ -159,8 +162,8 @@ public void onError(WebSocket conn, Exception ex) {
@Override
public void onStart() {
System.out.println("Server started!");
setConnectionLostTimeout(0);
setConnectionLostTimeout(100);
// setConnectionLostTimeout(0);
// setConnectionLostTimeout(100);
}

}

0 comments on commit 0f781fb

Please sign in to comment.