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
|
package derms.replica.replica1;
import java.io.IOException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.UnknownHostException;
import java.util.concurrent.Executors;
import java.util.concurrent.ExecutorService;
import java.util.logging.Logger;
public class StationServer implements Runnable {
public static final String usage = "Usage: java StationServer <city> <local address>";
public static final InetSocketAddress announceGroup = new InetSocketAddress("225.5.5.5", 5555);
public City city;
public InetAddress localAddr;
public Resources resources;
public Servers servers;
private Logger log;
private ResponderServer responderServer;
private CoordinatorServer coordinatorServer;
public StationServer(City city, InetAddress localAddr) throws IOException {
this.city = city;
this.localAddr = localAddr;
this.resources = new Resources();
this.servers = new Servers();
this.log = DermsLogger.getLogger(getClass());
try {
this.responderServer = new ResponderServer(city, resources, servers);
} catch (IOException e) {
throw new IOException("Failed to create ResponderServer: "+e.getMessage());
}
log.info("Created ResponderServer");
try {
this.coordinatorServer = new CoordinatorServer(city, resources, servers);
} catch (IOException e) {
throw new IOException("Failed to create CoordinatorServer: "+e.getMessage());
}
log.info("Created CoordinatorServer");
}
public static void main(String cmdlineArgs[]) {
Args args = null;
try {
args = new Args(cmdlineArgs);
} catch (IllegalArgumentException e) {
System.err.println(e.getMessage());
System.out.println(usage);
System.exit(1);
}
try {
(new StationServer(args.city, args.localAddr)).run();
} catch (Exception e) {
System.err.println(e.getMessage());
System.exit(1);
}
}
@Override
public void run() {
log.info("Running");
log.config("Local address is "+localAddr.toString());
ExecutorService pool = Executors.newCachedThreadPool();
try {
pool.execute(new ResourceAvailability.Server(localAddr, resources));
} catch (IOException e) {
String msg = "Failed to start ResourceAvailability Server: "+e.getMessage();
log.severe(msg);
return;
}
try {
pool.execute(new RequestResource.Server(localAddr, resources));
} catch (IOException e) {
log.severe("Failed to start RequestResource Server: "+e.getMessage());
return;
}
try {
pool.execute(new FindResource.Server(localAddr, resources));
} catch (IOException e) {
log.severe("Failed to start FindResource Server: "+e.getMessage());
return;
}
try {
pool.execute(new ReturnResource.Server(localAddr, resources));
} catch (IOException e) {
log.severe("Failed to start ReturnResource Server: "+e.getMessage());
return;
}
try {
pool.execute(new SwapResource.Server(localAddr, resources, servers));
} catch (IOException e) {
log.severe("Failed to start SwapResource Server: "+e.getMessage());
return;
}
try {
pool.execute(new Announcer(announceGroup, localAddr, city));
} catch (IOException e) {
log.severe("Failed to start Announcer: "+e.getMessage());
return;
}
try {
pool.execute(new AnnounceListener(announceGroup, localAddr, servers));
} catch (IOException e) {
log.severe("Failed to start AnnounceListener: "+e.getMessage());
return;
}
}
private static class Args {
private City city;
private InetAddress localAddr;
private Args(String[] args) throws IllegalArgumentException {
if (args.length < 1) {
throw new IllegalArgumentException("Missing argument 'city'");
}
city = new City(args[0]);
if (args.length < 2) {
throw new IllegalArgumentException("Missing argument 'local host'");
}
try {
localAddr = InetAddress.getByName(args[1]);
} catch (UnknownHostException | SecurityException e) {
throw new IllegalArgumentException("Bad value of 'local host': "+e.getMessage());
}
}
}
}
|