-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClient.java
More file actions
145 lines (122 loc) · 4.55 KB
/
Client.java
File metadata and controls
145 lines (122 loc) · 4.55 KB
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
/*
<Applet Code="Client.class" fps=10 width=600 height=800> </Applet>
*/
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.net.*;
import java.io.*;
public class Client extends Applet {
static final long serialVersionUID = 1L;//give it a version so compiler won't complain
TriggerType triggerType = TriggerType.TIMER_TRIGGER;
// TriggerType triggerType = TriggerType.EVENT_TRIGGER;
// threshold for event based sensor (in degrees)
double threshold = 5;
// The speed of simulation
// (How many simulation second elapses when 1 second real time elapses)
double simSpeed = 0.1;
// Sensor sampling rate (per simulation second)
double sensorSamplingRate = 100;
// advance of simulation time (in second) per step
double tau_sim = 0.01;
private static ObjectOutputStream out;
private static ObjectInputStream in;
Physics physics;
Socket requestSocket;
Thread physicsThread;
Thread sensorThread, actuatorThread;
Thread updatingUIThread;
UpdatingUIThread animator;
// frames per second for updating UI
int fps = 10;
// simulation time between two samples (in seconds)
double sensorSamplingPeriod_sim = 1.0 / sensorSamplingRate;
double sensorSamplingPeriod_phy = sensorSamplingPeriod_sim / simSpeed;
String[] configInfo;
final int APPLET_WIDTH = 800;
final int APPLET_HEIGHT = 400;
/**
* This method initializes the pole state and sets up animation timing.
*/
public void init() {
this.setSize(new Dimension(APPLET_WIDTH, APPLET_HEIGHT));
String str;
// Build configuration info string
configInfo = new String[2];
StringBuilder sb = new StringBuilder();
sb.append("Sim. Speed: ").append(String.format("%.3f ", simSpeed));
sb.append(" Sim. Step: ").append(String.format("%.3f sec ",tau_sim));
configInfo[0] = sb.toString();
sb = new StringBuilder();
if(triggerType == TriggerType.EVENT_TRIGGER){
sb.append("Event Based Sensor ");
}else{
sb.append("Time Based Sensor ");
}
sb.append(String.format("%.2f Hz", sensorSamplingRate));
if(triggerType == TriggerType.EVENT_TRIGGER){
sb.append(" Threshold: ").append(String.format("%.02f", threshold));
}
configInfo[1] = sb.toString();
// -------------------------------------
physics = new Physics(tau_sim, tau_sim / simSpeed);
try {
requestSocket = new Socket("localhost", 25533);
out = new ObjectOutputStream(requestSocket.getOutputStream());
out.flush();
in = new ObjectInputStream(requestSocket.getInputStream());
} catch (IOException e) {
System.out.println("Not able to bind to server");
}
}
/**
* This method starts animating by creating a new Thread.
*/
public void start() {
//Start animating!
if (physicsThread == null) {
physicsThread = new Thread(physics);
}
physicsThread.start();
if (sensorThread == null) {
sensorThread = new Thread(new Sensor(physics, out, triggerType, threshold, sensorSamplingPeriod_sim, sensorSamplingPeriod_phy));
}
sensorThread.start();
if (actuatorThread == null) {
actuatorThread = new Thread(new Actuator(physics, in));
}
actuatorThread.start();
animator = new UpdatingUIThread(this, physics, (1000 / fps), configInfo);
if (updatingUIThread == null) {
updatingUIThread = new Thread(animator);
}
updatingUIThread.start();
}
/**
* This method stops the animating thread and gets rid of the objects necessary for double buffering.
*/
public void stop() {
//Stop the animating thread.
physicsThread = null;
sensorThread = null;
actuatorThread = null;
updatingUIThread.stop();
try {
out.writeObject("bye"); // signal to close the sever
out.flush();
in.readObject();
in.close();
out.close();
requestSocket.close();
} catch (Exception ex) {
ex.printStackTrace();
}
}
/**
* This method paints the graphics by calling the update method.
*/
public void paint(Graphics gr) {
animator.update(gr);
}
}