-
Notifications
You must be signed in to change notification settings - Fork 0
/
PubLauncher.java
81 lines (60 loc) · 2.55 KB
/
PubLauncher.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
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Properties;
public class PubLauncher {
public VehiclePublisher vp;
//In the publauncher I initial a vehicle publisher as singleton publisher multiple threads will write data to the publisher.
//The properties are loaded from file.
//I create pubthread for each vehicle the start.
public PubLauncher() {
vp = new VehiclePublisher(0);
}
public ArrayList<PubThread> PubLancherMain() throws InterruptedException, ParseException {
Properties prop = new Properties();
InputStream input = null;
try {
input = new FileInputStream("pub.properties");
// load a properties file
prop.load(input);
ArrayList<PubThread> pubThreads = new ArrayList<>();
ArrayList<Route> routes = new ArrayList<>();
int numberOfRoutes = Integer.parseInt(prop.getProperty("numRoutes"));
for (int i = 1; i <= numberOfRoutes; i++) {
String routeName = prop.getProperty("route" + i);
int numOfStops = Integer.valueOf(prop.getProperty("route" + i + "numStops"));
double numOfSeconds = Double.valueOf(prop.getProperty("route" + i + "TimeBetweenStops"));
Route currentRoute = new Route(routeName, numOfStops, numOfSeconds);
routes.add(currentRoute);
int numberOfVehicles = Integer.parseInt(prop.getProperty("numVehicles"));
int numberOfBackupVehicles = Integer.parseInt(prop.getProperty("numInitialBackupVehicles"));
for (int j = numberOfVehicles + 1; j <= numberOfBackupVehicles + numberOfVehicles; j++) {
Vehicle currentVehicle = new Vehicle();
currentVehicle.vId = prop.getProperty("route" + i + "Vehicle" + j);
currentRoute.addBackupVehicle(currentVehicle);
System.out.println("Added " + "vehicle" + currentVehicle.vId + " for " + "Route " + routeName);
}
for (int j = 1; j <= numberOfVehicles; j++) {
Vehicle currentVehicle = new Vehicle();
currentVehicle.vId = prop.getProperty("route" + i + "Vehicle" + j);
currentRoute.addVehicle(currentVehicle);
pubThreads.add(new PubThread(currentRoute, currentVehicle, numOfSeconds, vp));
}
}
return pubThreads;
} catch (IOException ex) {
ex.printStackTrace();
} finally {
if (input != null) {
try {
input.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return null;
}
}