-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMessageHandlerTest.java
373 lines (319 loc) · 12.8 KB
/
MessageHandlerTest.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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package websocket;
import coreCode.Model;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.websocket.MessageHandler;
import javax.websocket.Session;
import fitnessFunction.*;
import learningJavaSwing.*;
import models.*;
import coreCode.*;
import java.util.InputMismatchException;
import static java.lang.Thread.sleep;
import java.util.ArrayList;
import java.util.List;
/**
*
* @author Dai
*/
public class MessageHandlerTest implements MessageHandler.Whole<String>, Runnable{
private Session clientsession = null;
private Model boidsModel;
private int num_agents;
private int configuration;
private List <Integer> parameter_list;
private ArrayList <Circle> circle_list;
private ArrayList <Rectangle> rectangle_list;
private int max_x;
private int max_y;
private int num_frames;
private boolean has_agents=false;
private String replyMessage;
private Thread tempthread=null;
//private int timestep;
private volatile boolean keeprunning=true;
private volatile boolean paused=false;
public MessageHandlerTest (Session inputsession)
{
this.clientsession = inputsession;
//timestep=0;
}
@Override
public void onMessage(String message)
{
try
{
switch (message)
{
case "resume"://animation should continue
System.out.println("resume");
keeprunning=true;
paused=false;
stopAnimationThread();
startAnimationThread();
break;
case "pause"://animation should be paused
System.out.println("pause");
keeprunning=false;
paused=true;
stopAnimationThread();
break;
case "stop"://animation should be stopped;
System.out.println("stop");
keeprunning=false;
paused=false;
stopAnimationThread();
//must have thread quit, but with option to restart animation
//timestep=0;
has_agents=false;
replyMessage="";
break;
default:
try
{
System.out.println("start");
parameter_list = new ArrayList<>();
circle_list = new ArrayList<>();
rectangle_list = new ArrayList<>();
//get values sent from the client
String [] values = (""+message).split(" ");
this.configuration=Integer.parseInt(values[0]);
this.num_agents = Integer.parseInt(values[1]);
this.max_x=Integer.parseInt(values[2]);
this.max_y=Integer.parseInt(values[3]);
this.num_frames=Integer.parseInt(values[4]);
Driver.numAgents = num_agents;
Driver.widthOfWorld = max_x;
Driver.heightOfWorld = max_y;
BoidsNoObstacles referenceModel;
//check configuration possibilities
switch(configuration)
{
case 1: //configuration id = 1
int distance_between_agents = Integer.parseInt(values[5]);
parameter_list.add(distance_between_agents);
//set up model
referenceModel = new BoidsNoObstacles(0, num_agents,
Driver.numberOfTimesteps,1,distance_between_agents,circle_list,rectangle_list);
//public BoidsNoObstacles( 0, num_agents, Driver.numberOfTimesteps,circle_list,rectangle_list,configuration, parameter_list);
boidsModel = referenceModel.model;
//last value taken was 5. any obstacles start at 6.
getObstacles(values,6);
break;
case 2: //configuration id =2
int radius_of_circle=Integer.parseInt(values[5]); //width of the circle
parameter_list.add(radius_of_circle);
//set up model
referenceModel = new BoidsNoObstacles(0, num_agents,
Driver.numberOfTimesteps,2,radius_of_circle,circle_list,rectangle_list);
//public BoidsNoObstacles( 0, num_agents, Driver.numberOfTimesteps,circle_list,rectangle_list,configuration, parameter_list);
boidsModel = referenceModel.model;
//last value taken was 5. any obstacles start at 6.
getObstacles(values,6);
break;
/*case 3:
int sin_amplitude=Integer.parseInt(values[5]); //amplitude of the sin wave
int sin_frequency=Integer.parseInt(values[6]); //frequency of the sin wave
//last value taken was 6. any obstacles start at 7.
getObstacles(values,7);
break;*/
}
keeprunning=true;
paused=false;
startAnimationThread();
}
catch(InputMismatchException ie) //if someone tries to break server by sending false message
{
sendStop();
}
break;
}
}
catch(ThreadDeath td)
{
}
}
/*public void setUpModel(String [] input,int index)
{
index = getObstacles(input,index);
//set up model
referenceModel = new BoidsNoObstacles(0, num_agents, Driver.numberOfTimesteps);
//public BoidsNoObstacles( 0, num_agents, Driver.numberOfTimesteps,circle_list,rectangle_list,configuration, parameter_list);
boidsModel = referenceModel.model;
if(!has_agents){
boidsModel.randomIntialConditions();
}
else
{
getAgents(input,index);
}
}*/
public void getObstacles(String [] input, int index)
{
for(int i=index;i<input.length;i++)
{
if(input[i].compareTo("Circle")==0)
{
//circles come in order: x,y,radius
int x_origin = Integer.parseInt(input[++i]);
int y_origin = Integer.parseInt(input[++i]);
Point center = new Point(x_origin,y_origin);
int radius = Integer.parseInt(input[++i]);
Circle circle = new Circle(center,radius);
circle_list.add(circle);
}
else if(input[i].compareTo("Rectangle")==0)
{
//rectangles come in order: origin x, origin y, ending x, ending y
int x_origin = Integer.parseInt(input[++i]);
int y_origin = Integer.parseInt(input[++i]);
Point startPoint = new Point(x_origin,y_origin);
int x_final = Integer.parseInt(input[++i]);
int y_final = Integer.parseInt(input[++i]);
Point endPoint = new Point(x_final,y_final);
Rectangle rectangle = new Rectangle(startPoint,endPoint);
rectangle_list.add(rectangle);
}
//do stuff with the values found
else if(input[i].compareTo("Agents")==0)
{
has_agents=true;
getAgents(input,i);
}
}
if(!has_agents){
boidsModel.randomIntialConditions();
}
}
//if any agents were passed from the canvas, get them and create the simulation based off those
public void getAgents(String [] input, int index)
{
Position [] initialpositions = new Position [input.length];
double [] initialheading = new double [input.length];
Speed [] initialspeeds = new Speed [input.length];
int count=0;
for(int i=index;i<input.length-1;i++)
{
//each should be in form x,y,heading
int x=Integer.parseInt(input[++i]);
int y=Integer.parseInt(input[++i]);
initialpositions[count] = new Position(x, y);
initialheading[count] = Double.parseDouble(input[++i]);
initialspeeds[count] = new Speed(Speed.randomSpeedWithinIncrement());
i--;
count++;
}
boidsModel.nextIteration(initialheading, initialpositions, initialspeeds, true);//true on first call
}
public void sendMessage()
{
/*
timestep++;
//make message immediately after sending previous one to try to improve performance
if(timestep<=Driver.numberOfTimesteps)
replyMessage = getValues();
else
replyMessage="0";
*/
replyMessage=getValues();
try
{
clientsession.getBasicRemote().sendText(replyMessage);
} catch (IOException ex)
{
Logger.getLogger(MessageHandlerTest.class.getName()).log(Level.SEVERE, null, ex);
}
if(replyMessage.equals("0"))
stopAnimationThread();
//System.out.println(replyMessage);
}
public void sendStop()
{
stopAnimationThread();
replyMessage="0";
try
{
clientsession.getBasicRemote().sendText(replyMessage);
} catch (IOException ex)
{
Logger.getLogger(MessageHandlerTest.class.getName()).log(Level.SEVERE, null, ex);
}
}
public String getValues()
{
boidsModel.nextIteration(new double[0], new Position[0], new Speed[0], false);
String output="[";
//for all position markers, get x value and y value
for(int i=0;i<num_agents;i++)
{
int x_value;
int y_value;
double angle;
try
{
x_value = (int) boidsModel.agents.get(i).position.getX();
y_value = (int) boidsModel.agents.get(i).position.getY();
angle = boidsModel.agents.get(i).heading.getValue();
//not for unlimited run
//x_value = (int) boidsModel.positionHistory[timestep][i].getX();
//y_value = (int) boidsModel.positionHistory[timestep][i].getY();
//angle = (int) boidsModel.headingHistory[timestep][i].getAngle();
}
catch(ArrayIndexOutOfBoundsException e)
{
return "0";
}
//put coordinate pairs into JSON format
output += "{\"x_value\":"+x_value+",\"y_value\":"+y_value+",\"heading\":"+angle+"}";
//add comma after every coordinate pair
if(i != num_agents-1)
output+=",";
}
output +="]";
return output;
}
@Override
public void run()
{
Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
try
{
sendMessage();
sleep((int)(1000/this.num_frames));
if(keeprunning)
run();
if(paused)
while(paused)
{
if(!paused)
run();
}
if(!paused)
run();
}
catch (InterruptedException | IllegalStateException ex)
{
}
}
public void startAnimationThread()
{
tempthread = new Thread(this);
tempthread.start();
}
public void stopAnimationThread()
{
try
{
tempthread.stop();
}
catch(Exception e)
{
}
}
}