Skip to content

Commit 29e218a

Browse files
authored
Merge pull request #1 from java-sheets/dev
Cleanup and dockerize
2 parents d2a4c51 + 1570345 commit 29e218a

File tree

4 files changed

+24
-8
lines changed

4 files changed

+24
-8
lines changed

Dockerfile

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
FROM openjdk:16
2+
3+
ADD ./src src
4+
ADD ./run.sh run.sh
5+
6+
ENTRYPOINT ["run.sh"]

run.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#!/usr/bin/env sh
2+
13
javac ./src/Box.java ./src/Controller.java
24
jar --create --file ./src/box.jar --main-class Box -C src Box.class
35
java -cp src Controller "$@"

src/Box.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
import java.net.InetSocketAddress;
33
import java.net.Socket;
44
import java.nio.ByteBuffer;
5-
import java.nio.file.Files;
6-
import java.nio.file.Path;
75
import java.time.Duration;
86
import java.util.UUID;
97

@@ -59,10 +57,9 @@ public void accept() throws IOException {
5957
}
6058
}
6159

62-
public static void main(String[] arguments) throws IOException {
60+
public static void main(String[] arguments) {
6361
validateArguments(arguments);
6462
var box = createBoxFromArguments(arguments);
65-
Files.writeString(Path.of(box.id.toString() + ".box.txt"), box.id.toString());
6663
try {
6764
box.start();
6865
} catch (IOException failure) {

src/Controller.java

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -150,16 +150,27 @@ private static void redirect(
150150
}
151151
}
152152

153+
private static final int internalPort = 3_1337 ;
154+
153155
// All logging is done to System.err since output is redirected to System.out
154156
public static void main(String[] arguments) throws InterruptedException {
155157
validateArguments(arguments);
156158
var executor = Executors.newCachedThreadPool();
157-
var controller = new Controller(3_1337, arguments[0], executor);
159+
var binaryPath = arguments[0];
160+
var controller = new Controller(internalPort, binaryPath, executor);
158161
runController(executor, controller);
159-
int boxCount = arguments.length > 1 ? Integer.parseInt(arguments[1]) : 3;
160-
startSomeBoxes(controller, boxCount);
162+
startSomeBoxes(controller, readBoxCount(arguments));
161163
Thread.sleep(10_000);
162-
executor.shutdown();
164+
executor.shutdownNow();
165+
}
166+
167+
private static final int boxCountIndex = 1;
168+
private static final int defaultBoxCount = 3;
169+
170+
private static int readBoxCount(String[] arguments) {
171+
return arguments.length > boxCountIndex
172+
? Integer.parseInt(arguments[boxCountIndex])
173+
: defaultBoxCount;
163174
}
164175

165176
private static void runController(Executor executor, Controller controller) {

0 commit comments

Comments
 (0)