-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSensor.java
More file actions
85 lines (74 loc) · 2.24 KB
/
Copy pathSensor.java
File metadata and controls
85 lines (74 loc) · 2.24 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
package Backend;
import java.io.*;
import java.net.*;
import java.lang.*;
//Sensor of 1 parking spot associated with an areaserver
public class Sensor extends Thread implements Serializable{
private SensorPackage pack;
private String ip;
private int port;
Sensor(String ip, int port, String name, int spot) {
this.ip = ip;
this.port = port;
this.pack = new SensorPackage(name,spot,true);
}
public boolean isAvailable(){
return this.pack.isAvailable();
}
public String getStreetname(){
return this.pack.getStreetName();
}
public int getSpot(){
return this.pack.getSpot();
}
public int getPort(){
return this.port;
}
public String getIp(){
return this.ip;
}
public SensorPackage getPack() {
return pack;
}
public void setPack(SensorPackage sp){
this.pack = sp;
}
public void run() {
Socket s = null;
ObjectOutputStream out;
ObjectInputStream in;
try {
//Connection to corresponding areaserver
java.util.Random rand = new java.util.Random();
while (true) {
double r = rand.nextDouble();
//Simulate coming and going of cars
if (r < 0.05) {
s = new Socket(ip, port);
in = new ObjectInputStream(s.getInputStream());
out = new ObjectOutputStream(s.getOutputStream());
out.flush();
if(this.pack.isAvailable()==true) {
this.pack.setAvailable(false);
} else {
this.pack.setAvailable(true);
}
out.writeObject(pack);
out.reset();
s.close();
in.close();
out.close();
}
synchronized (this){
this.wait(1000);
}
}
} catch (UnknownHostException e) {
System.err.println(e);
} catch (IOException e) {
System.err.println(e);
} catch (InterruptedException e){
System.err.println(e);
}
}
}