-
Notifications
You must be signed in to change notification settings - Fork 4
/
LaunchEndPoint.java
270 lines (246 loc) · 9.44 KB
/
LaunchEndPoint.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
/*******************************************************************************************************
*
* LaunchEndPoint.java, in msi.gama.headless, is part of the source code of the GAMA modeling and simulation platform
* (v.1.8.2).
*
* (c) 2007-2022 UMI 209 UMMISCO IRD/SU & Partners (IRIT, MIAT, TLU, CTU)
*
* Visit https://github.com/gama-platform/gama for license information and contacts.
*
********************************************************************************************************/
package msi.gama.headless.runtime;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.nio.ByteBuffer;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;
import org.java_websocket.WebSocket;
import msi.gama.common.GamlFileExtension;
import msi.gama.headless.common.Globals;
import msi.gama.headless.core.GamaHeadlessException;
import msi.gama.headless.job.ExperimentJob;
import msi.gama.headless.job.IExperimentJob;
import msi.gama.headless.job.ManualExperimentJob;
import msi.gama.headless.script.ExperimentationPlanFactory;
import msi.gama.metamodel.agent.IAgent;
import msi.gama.metamodel.shape.IShape;
import msi.gaml.operators.Spatial;
/**
* The Class LaunchEndPoint.
*/
public class LaunchEndPoint implements Endpoint {
@Override
public void onOpen(final WebSocket socket) {
socket.send("You have connected to chat");
}
public void compileGamlSimulation(final WebSocket socket, final List<String> args)
throws IOException, GamaHeadlessException {
final String pathToModel = args.get(args.size() - 1);
if (!GamlFileExtension.isGaml(pathToModel)) {
System.exit(-1);
}
final String argExperimentName = args.get(args.size() - 2);
final String argGamlFile = args.get(args.size() - 1);
final List<IExperimentJob> jb = ExperimentationPlanFactory.buildExperiment(argGamlFile);
ExperimentJob selectedJob = null;
for (final IExperimentJob j : jb) {
if (j.getExperimentName().equals(argExperimentName)) {
selectedJob = (ExperimentJob) j;
break;
}
}
if (selectedJob == null)
return;
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutputStream out = null;
try {
out = new ObjectOutputStream(bos);
out.writeObject(selectedJob);
out.flush();
byte[] yourBytes = bos.toByteArray();
socket.send(yourBytes);
} finally {
try {
bos.close();
} catch (IOException ex) {
// ignore close exception
}
}
}
public void runModelSimulation(final GamaWebSocketServer server, final ByteBuffer compiledModel)
throws IOException, GamaHeadlessException {
ByteArrayInputStream bis = new ByteArrayInputStream(compiledModel.array());
ObjectInput in = null;
ExperimentJob selectedJob = null;
try {
in = new ObjectInputStream(bis);
Object o = in.readObject();
selectedJob = (ExperimentJob) o;
} catch (ClassNotFoundException ex) {
ex.printStackTrace();
} finally {
try {
if (in != null) {
in.close();
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
server.getDefaultApp().processorQueue.pushSimulation(selectedJob);
}
/**
* Run gaml simulation.
*
* @param server the server
* @param message the args
* @throws IOException Signals that an I/O exception has occurred.
* @throws GamaHeadlessException the gama headless exception
*/
public void launchGamlSimulation(final GamaWebSocketServer server, WebSocket socket, final List<String> args)
throws IOException, GamaHeadlessException {
final String pathToModel = args.get(args.size() - 1);
File ff = new File(pathToModel);
System.out.println(ff.getAbsoluteFile().toString());
if (!GamlFileExtension.isGaml(ff.getAbsoluteFile().toString())) {
System.exit(-1);
}
final String argExperimentName = args.get(args.size() - 2);
// final String argGamlFile = args.get(args.size() - 1);
if (!ff.exists())
return;
final List<IExperimentJob> jb = ExperimentationPlanFactory.buildExperiment(ff.getAbsoluteFile().toString());
ManualExperimentJob selectedJob = null;
for (final IExperimentJob j : jb) {
if (j.getExperimentName().equals(argExperimentName)) {
selectedJob = new ManualExperimentJob((ExperimentJob) j, server, socket);
break;
}
}
if (selectedJob == null)
return;
Globals.OUTPUT_PATH = args.get(args.size() - 3);
try {
selectedJob.loadAndBuild();
} catch (InstantiationException | IllegalAccessException | ClassNotFoundException | IOException
| GamaHeadlessException e) {
e.printStackTrace();
}
if (server.getExperimentsOf(""+socket.hashCode()) == null) {
final ConcurrentHashMap<String, ExperimentJob> exps = new ConcurrentHashMap<String, ExperimentJob>();
server.getAllExperiments().put(""+socket.hashCode(), exps);
}
server.getExperimentsOf(""+socket.hashCode()).put(selectedJob.getExperimentID(), selectedJob);
final int size = selectedJob.getListenedVariables().length;
// String lst_out = "";
// if (size != 0) {
// for (int i = 0; i < size; i++) {
// final ListenedVariable v = selectedJob.getListenedVariables()[i];
// lst_out += "@" + v.getName();
// }
// }
IAgent agt = selectedJob.getSimulation().getSimulation();
IShape geom = Spatial.Projections.transform_CRS(agt.getScope(), agt.getGeometry(), "EPSG:4326");
System.out.println("exp@" + ""+socket.hashCode() + "@" + selectedJob.getExperimentID() + "@" + size + "@"
+ geom.getLocation().x + "@" + geom.getLocation().y);
socket.send("exp@" + ""+socket.hashCode() + "@" + selectedJob.getExperimentID() + "@" + size + "@"
+ geom.getLocation().x + "@" + geom.getLocation().y);
((ManualExperimentJob) selectedJob).exportVariables();
// server.getDefaultApp().processorQueue.pushSimulation(selectedJob);
}
@Override
public void onMessage(final GamaWebSocketServer server, final WebSocket socket, final String message) {
// server.broadcast(message);
final String socket_id = ""+socket.hashCode();
System.out.println(socket + ": " + message);
String[] args = message.split("@");
if ("launch".equals(args[0])) {
try {
Globals.IMAGES_PATH = Globals.TEMP_PATH + "\\snapshot";
launchGamlSimulation(server, socket, Arrays.asList("-gaml", ".", args[2], args[1]));
} catch (IOException | GamaHeadlessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if ("play".equals(args[0])) {
String id_exp = args[1];
System.out.println("play " + id_exp);
if (server.getExperiment(socket_id, id_exp) != null
&& server.getExperiment(socket_id, id_exp).getSimulation() != null) {
((ManualExperimentJob) server.getExperiment(socket_id, id_exp)).paused = false;
((ManualExperimentJob) server.getExperiment(socket_id, id_exp)).stepping = false;
// DEBUG.TIMER("Simulation duration", () -> {
if (((ManualExperimentJob) server.getExperiment(socket_id, id_exp)).internalThread == null) {
((ManualExperimentJob) server.getExperiment(socket_id, id_exp)).internalThread = new Thread() {
@Override
public void run() {
while (!server.getExperiment(socket_id, id_exp).getSimulation().isInterrupted()) {
if (((ManualExperimentJob) server.getExperiment(socket_id, id_exp)).stepping) {
((ManualExperimentJob) server.getExperiment(socket_id, id_exp)).stepping = false;
((ManualExperimentJob) server.getExperiment(socket_id, id_exp)).paused = true;
server.getExperiment(socket_id, id_exp).doStep();
}
if (!((ManualExperimentJob) server.getExperiment(socket_id, id_exp)).paused)
server.getExperiment(socket_id, id_exp).doStep();
}
}
};
((ManualExperimentJob) server.getExperiment(socket_id, id_exp)).internalThread.start();
}
}
}
if ("step".equals(args[0])) {
String id_exp = args[1];
System.out.println("step " + id_exp);
if (server.getExperiment(socket_id, id_exp) != null
&& server.getExperiment(socket_id, id_exp).getSimulation() != null) {
((ManualExperimentJob) server.getExperiment(socket_id, id_exp)).stepping = true;
}
}
if ("pause".equals(args[0])) {
String id_exp = args[1];
System.out.println("pause " + id_exp);
if (server.getExperiment(socket_id, id_exp) != null
&& server.getExperiment(socket_id, id_exp).getSimulation() != null) {
((ManualExperimentJob) server.getExperiment(socket_id, id_exp)).paused = true;
}
}
if ("stop".equals(args[0])) {
String id_exp = args[1];
System.out.println("stop " + id_exp);
if (server.getExperiment(socket_id, id_exp) != null
&& server.getExperiment(socket_id, id_exp).getSimulation() != null) {
((ManualExperimentJob) server.getExperiment(socket_id, id_exp)).paused = true;
((ManualExperimentJob) server.getExperiment(socket_id, id_exp)).dispose();
}
}
if ("exit".equals(args[0])) {
System.exit(0);
}
if ("compile".equals(args[0])) {
try {
Globals.IMAGES_PATH = "C:\\GAMA\\headless\\samples\\toto\\snapshot";
compileGamlSimulation(socket, Arrays.asList("-gaml", ".", args[2], args[1]));
} catch (IOException | GamaHeadlessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
@Override
public void onMessage(GamaWebSocketServer server, WebSocket conn, ByteBuffer compiledModel) {
try {
runModelSimulation(server, compiledModel);
} catch (IOException | GamaHeadlessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}