-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathServer.java
More file actions
100 lines (80 loc) · 3.12 KB
/
Server.java
File metadata and controls
100 lines (80 loc) · 3.12 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
import fr.ybonnel.simpleweb4j.SimpleWeb4j;
import fr.ybonnel.simpleweb4j.exception.HttpErrorException;
import fr.ybonnel.simpleweb4j.handlers.ContentType;
import fr.ybonnel.simpleweb4j.handlers.Response;
import fr.ybonnel.simpleweb4j.handlers.Route;
import fr.ybonnel.simpleweb4j.handlers.RouteParameters;
import static fr.ybonnel.simpleweb4j.SimpleWeb4j.get;
import static fr.ybonnel.simpleweb4j.SimpleWeb4j.setPort;
public class Server {
private final Elevator elevator;
public Server() {
elevator = new Omnibus();
}
public void start() {
setPort(getPort());
get(new Route<Void, Command>("/nextCommand", Void.class, ContentType.PLAIN_TEXT) {
@Override
public Response<Command> handle(Void o, RouteParameters parameters) throws HttpErrorException {
return new Response<>(elevator.nextCommand());
}
});
get(new Route<Void, Void>("/reset", Void.class) {
@Override
public Response<Void> handle(Void param, RouteParameters routeParams) throws HttpErrorException {
elevator.reset(routeParams.getParam("cause"));
return new Response<>(null);
}
});
get(new Route<Void, Void>("/call", Void.class) {
@Override
public Response<Void> handle(Void param, RouteParameters routeParams) throws HttpErrorException {
elevator.call(Integer.parseInt(routeParams.getParam("atFloor")),
routeParams.getParam("to"));
return new Response<>(null);
}
});
get(new Route<Void, Void>("/go", Void.class) {
@Override
public Response<Void> handle(Void param, RouteParameters routeParams) throws HttpErrorException {
elevator.go(Integer.parseInt(routeParams.getParam("floorToGo")));
return new Response<>(null);
}
});
get( new Route<Void, Void>("/userHasEntered", Void.class) {
@Override
public Response<Void> handle(Void param, RouteParameters routeParams) throws HttpErrorException {
elevator.userHasEntered();
return new Response<>(null);
}
});
get(new Route<Void, Void>("/userHasExited", Void.class) {
@Override
public Response<Void> handle(Void param, RouteParameters routeParams) throws HttpErrorException {
elevator.userHasExited();
return new Response<>(null);
}
});
SimpleWeb4j.start();
}
/**
* @return port to use
*/
private static int getPort() {
// Heroku
String herokuPort = System.getenv("PORT");
if (herokuPort != null) {
return Integer.parseInt(herokuPort);
}
// Cloudbees
String cloudbeesPort = System.getProperty("app.port");
if (cloudbeesPort != null) {
return Integer.parseInt(cloudbeesPort);
}
// Default port;
return 9999;
}
public static void main(String[] args) {
new Server().start();
}
}