diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..f83e8cf
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,3 @@
+.idea
+target
+*.iml
diff --git a/pom.xml b/pom.xml
index 8fa1d2c..215e986 100644
--- a/pom.xml
+++ b/pom.xml
@@ -13,10 +13,15 @@
- com.sparkjava
- spark-core
- 1.1
+ fr.ybonnel
+ simpleweb4j
+ 0.0.6
+
+ org.slf4j
+ slf4j-simple
+ 1.7.5
+
diff --git a/src/main/java/Command.java b/src/main/java/Command.java
new file mode 100644
index 0000000..fcc64b4
--- /dev/null
+++ b/src/main/java/Command.java
@@ -0,0 +1,24 @@
+/*
+ * Copyright 2013- Yan Bonnel
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+public enum Command {
+ UP,
+ DOWN,
+ OPEN,
+ CLOSE,
+ NOTHING
+}
diff --git a/src/main/java/Elevator.java b/src/main/java/Elevator.java
index d51cfc8..3b8f36c 100644
--- a/src/main/java/Elevator.java
+++ b/src/main/java/Elevator.java
@@ -1,21 +1,26 @@
-public class Elevator {
+/*
+ * Copyright 2013- Yan Bonnel
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
- private String commands[] = {
- "OPEN","CLOSE","UP",
- "OPEN","CLOSE","UP",
- "OPEN","CLOSE","UP",
- "OPEN","CLOSE","UP",
- "OPEN","CLOSE","UP",
- "OPEN","CLOSE","DOWN",
- "OPEN","CLOSE","DOWN",
- "OPEN","CLOSE","DOWN",
- "OPEN","CLOSE","DOWN",
- "OPEN","CLOSE","DOWN"
- };
+public interface Elevator {
- private int count = 0;
-
- public String nextCommand() {
- return commands[(count++)%commands.length];
- }
+ Command nextCommand();
+ void call(int floor, String to);
+ void go(int floorToGo);
+ void userHasEntered();
+ void userHasExited();
+ void reset(String cause);
}
diff --git a/src/main/java/Omnibus.java b/src/main/java/Omnibus.java
new file mode 100644
index 0000000..9be4370
--- /dev/null
+++ b/src/main/java/Omnibus.java
@@ -0,0 +1,61 @@
+/*
+ * Copyright 2013- Yan Bonnel
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+public class Omnibus implements Elevator {
+
+ Command steps[] = {
+ Command.OPEN, Command.CLOSE, Command.UP,
+ Command.OPEN, Command.CLOSE, Command.UP,
+ Command.OPEN, Command.CLOSE, Command.UP,
+ Command.OPEN, Command.CLOSE, Command.UP,
+ Command.OPEN, Command.CLOSE, Command.UP,
+ Command.OPEN, Command.CLOSE, Command.DOWN,
+ Command.OPEN, Command.CLOSE, Command.DOWN,
+ Command.OPEN, Command.CLOSE, Command.DOWN,
+ Command.OPEN, Command.CLOSE, Command.DOWN,
+ Command.OPEN, Command.CLOSE, Command.DOWN
+ };
+
+ int currentIndex = 0;
+
+
+ @Override
+ public Command nextCommand() {
+ return steps[currentIndex++ % steps.length];
+ }
+
+ @Override
+ public void call(int floor, String to) {
+ }
+
+ @Override
+ public void go(int floorToGo) {
+ }
+
+ @Override
+ public void userHasEntered() {
+ }
+
+ @Override
+ public void userHasExited() {
+ }
+
+ @Override
+ public void reset(String cause) {
+ currentIndex = 0;
+ }
+}
diff --git a/src/main/java/Server.java b/src/main/java/Server.java
index 6160608..ead1838 100644
--- a/src/main/java/Server.java
+++ b/src/main/java/Server.java
@@ -1,31 +1,98 @@
-import spark.Request;
-import spark.Response;
-import spark.Route;
+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;
-import static spark.Spark.get;
public class Server {
private final Elevator elevator;
public Server() {
- elevator = new Elevator();
+ elevator = new Omnibus();
}
public void start() {
- get(new Route("/:path") {
+ setPort(getPort());
+
+ get(new Route("/nextCommand", Void.class, ContentType.PLAIN_TEXT) {
+ @Override
+ public Response handle(Void o, RouteParameters parameters) throws HttpErrorException {
+ return new Response<>(elevator.nextCommand());
+ }
+ });
+
+ get(new Route("/reset", Void.class) {
@Override
- public Object handle(Request request, Response response) {
- String path = request.params(":path");
- if (path.equals("nextCommand")) {
- return elevator.nextCommand();
- } else {
- return "";
- }
+ public Response handle(Void param, RouteParameters routeParams) throws HttpErrorException {
+ elevator.reset(routeParams.getParam("cause"));
+ return new Response<>(null);
}
});
+ get(new Route("/call", Void.class) {
+ @Override
+ public Response handle(Void param, RouteParameters routeParams) throws HttpErrorException {
+ elevator.call(Integer.parseInt(routeParams.getParam("atFloor")),
+ routeParams.getParam("to"));
+ return new Response<>(null);
+ }
+ });
+
+ get(new Route("/go", Void.class) {
+ @Override
+ public Response handle(Void param, RouteParameters routeParams) throws HttpErrorException {
+ elevator.go(Integer.parseInt(routeParams.getParam("floorToGo")));
+ return new Response<>(null);
+ }
+ });
+
+ get( new Route("/userHasEntered", Void.class) {
+ @Override
+ public Response handle(Void param, RouteParameters routeParams) throws HttpErrorException {
+ elevator.userHasEntered();
+ return new Response<>(null);
+ }
+ });
+
+ get(new Route("/userHasExited", Void.class) {
+ @Override
+ public Response 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();
}