From aaa553fc4aa19ee460d8e87b9f168202347bbb69 Mon Sep 17 00:00:00 2001 From: ajuvercr Date: Tue, 5 Nov 2019 17:43:37 +0100 Subject: [PATCH 01/13] Let's go, just pull the streams guys --- src/main/java/telraam/beacon/Beacon.java | 17 +++++ .../java/telraam/beacon/BeaconAggregator.java | 21 ++++++ .../java/telraam/beacon/BeaconMessage.java | 12 ++++ src/main/java/telraam/beacon/Callback.java | 5 ++ src/main/java/telraam/beacon/Event.java | 46 +++++++++++++ .../java/telraam/beacon/EventGenerator.java | 19 ++++++ src/main/java/telraam/beacon/Generator.java | 13 ++++ src/main/java/telraam/beacon/README.md | 14 ++++ src/main/java/telraam/beacon/TCPFactory.java | 65 +++++++++++++++++++ src/test/java/telraam/beacon/BeaconTest.java | 0 10 files changed, 212 insertions(+) create mode 100644 src/main/java/telraam/beacon/Beacon.java create mode 100644 src/main/java/telraam/beacon/BeaconAggregator.java create mode 100644 src/main/java/telraam/beacon/BeaconMessage.java create mode 100644 src/main/java/telraam/beacon/Callback.java create mode 100644 src/main/java/telraam/beacon/Event.java create mode 100644 src/main/java/telraam/beacon/EventGenerator.java create mode 100644 src/main/java/telraam/beacon/Generator.java create mode 100644 src/main/java/telraam/beacon/README.md create mode 100644 src/main/java/telraam/beacon/TCPFactory.java create mode 100644 src/test/java/telraam/beacon/BeaconTest.java diff --git a/src/main/java/telraam/beacon/Beacon.java b/src/main/java/telraam/beacon/Beacon.java new file mode 100644 index 0000000..a860e94 --- /dev/null +++ b/src/main/java/telraam/beacon/Beacon.java @@ -0,0 +1,17 @@ +package telraam.beacon; + +import java.net.Socket; + +public class Beacon extends EventGenerator { + public Beacon(Callback> h) { + super(h); + } + + public Beacon(Socket socket, Callback> h) { + this(h); + + // socket.getInputStream().readNBytes(10); + + // TODO: Setup socket, generating B things somehow + } +} diff --git a/src/main/java/telraam/beacon/BeaconAggregator.java b/src/main/java/telraam/beacon/BeaconAggregator.java new file mode 100644 index 0000000..07f1c36 --- /dev/null +++ b/src/main/java/telraam/beacon/BeaconAggregator.java @@ -0,0 +1,21 @@ +package telraam.beacon; + +import java.io.IOException; + +public class BeaconAggregator extends TCPFactory implements Callback> { + + public BeaconAggregator(int port) throws IOException { + // Does not work, java can't handle cool code + // super((s) -> new Beacon(s, this), port); + super(port); + super.creator = (s) -> { + new Beacon(s, this); + return null; + }; + } + + public Void handle(Event event) { + event.handle(this); + return null; + } +} diff --git a/src/main/java/telraam/beacon/BeaconMessage.java b/src/main/java/telraam/beacon/BeaconMessage.java new file mode 100644 index 0000000..ec63d17 --- /dev/null +++ b/src/main/java/telraam/beacon/BeaconMessage.java @@ -0,0 +1,12 @@ +package telraam.beacon; + +public class BeaconMessage { + public byte[] data; + + public BeaconMessage() { + } + + public BeaconMessage(byte[] data) { + this.data = data; + } +} diff --git a/src/main/java/telraam/beacon/Callback.java b/src/main/java/telraam/beacon/Callback.java new file mode 100644 index 0000000..ef12728 --- /dev/null +++ b/src/main/java/telraam/beacon/Callback.java @@ -0,0 +1,5 @@ +package telraam.beacon; + +public interface Callback { + public Output handle(Input value); +} diff --git a/src/main/java/telraam/beacon/Event.java b/src/main/java/telraam/beacon/Event.java new file mode 100644 index 0000000..48fa7c3 --- /dev/null +++ b/src/main/java/telraam/beacon/Event.java @@ -0,0 +1,46 @@ +package telraam.beacon; + +public abstract class Event { + abstract void handle(EventHandler h); + + public static class Data extends Event { + public B inner; + + public Data(B data) { + inner = data; + } + + void handle(EventHandler h) { + h.data(inner); + } + } + + public static class Error extends Event { + public Exception inner; + + public Error(Exception e) { + inner = e; + } + + void handle(EventHandler h) { + h.error(inner); + } + } + + public static class Exit extends Event { + public Exit() { + } + + void handle(EventHandler h) { + h.exit(); + } + } + + public interface EventHandler { + void exit(); + + void error(Exception e); + + void data(B b); + } +} diff --git a/src/main/java/telraam/beacon/EventGenerator.java b/src/main/java/telraam/beacon/EventGenerator.java new file mode 100644 index 0000000..96d44bb --- /dev/null +++ b/src/main/java/telraam/beacon/EventGenerator.java @@ -0,0 +1,19 @@ +package telraam.beacon; + +public abstract class EventGenerator extends Generator> { + public EventGenerator(Callback> handler) { + super(handler); + } + + protected void data(B data) { + super.handle(new Event.Data<>(data)); + } + + protected void error(Exception e) { + super.handle(new Event.Error<>(e)); + } + + protected void exit() { + super.handle(new Event.Exit<>()); + } +} diff --git a/src/main/java/telraam/beacon/Generator.java b/src/main/java/telraam/beacon/Generator.java new file mode 100644 index 0000000..d395cb9 --- /dev/null +++ b/src/main/java/telraam/beacon/Generator.java @@ -0,0 +1,13 @@ +package telraam.beacon; + +public abstract class Generator { + protected Callback handler; + + public Generator(Callback handler) { + this.handler = handler; + } + + protected void handle(T element) { + handler.handle(element); + } +} diff --git a/src/main/java/telraam/beacon/README.md b/src/main/java/telraam/beacon/README.md new file mode 100644 index 0000000..e47f691 --- /dev/null +++ b/src/main/java/telraam/beacon/README.md @@ -0,0 +1,14 @@ +# BEACONS + +Yes I really need a README to explain my shitty code. +So, generics are a thing in Java. But they suck. + +The main class in this package is the BeaconAggregator, which is basically an Event generator, B being the thing built from the data. + +BeaconAggregator extends TCPFactory that spawns all the connecting Beacon that actually do the generating. On TCPFactories you can subscribe handlers for basic events, like Errors (with `onError`), data (with `onData`) and disconnects (with `onDisconnect`). + +So beacons generate events, which is a nice way to hide IO exceptions etc that is wrapped in Event. These get handled by `Event.EventHandlers` like the TCPFactory. + +TODO: Function scoping, eg the functions `exit` `error` and `data` should not be public, but interfaces bla bla bla. + +TODO: How does one create B's from the plain sockets in Beacon. Remove B generic, I sure hope not? diff --git a/src/main/java/telraam/beacon/TCPFactory.java b/src/main/java/telraam/beacon/TCPFactory.java new file mode 100644 index 0000000..7f34781 --- /dev/null +++ b/src/main/java/telraam/beacon/TCPFactory.java @@ -0,0 +1,65 @@ +package telraam.beacon; + +import java.io.IOException; +import java.net.ServerSocket; +import java.net.Socket; +import java.util.List; +import java.util.ArrayList; + +public class TCPFactory implements Event.EventHandler, Runnable { + protected Callback creator; + private ServerSocket socket; + List> handlers = new ArrayList<>(); + List> errorHandlers = new ArrayList<>(); + List> exitHandlers = new ArrayList<>(); + + public TCPFactory(Callback creator, int port) throws IOException { + this(port); + this.creator = creator; + + new Thread(this).run(); + } + + protected TCPFactory(int port) throws IOException { + this.socket = new ServerSocket(port); + } + + public void run() { + while (true) { + try { + Socket s = socket.accept(); + this.creator.handle(s); + } catch (IOException e) { + throw new RuntimeException(e); + } + } + } + + public TCPFactory onError(Callback handler) { + this.errorHandlers.add(handler); + return this; + } + + public TCPFactory onData(Callback handler) { + this.handlers.add(handler); + return this; + } + + public TCPFactory onDisconnect(Callback handler) { + this.exitHandlers.add(handler); + return this; + } + + public void exit() { + this.exitHandlers.forEach((eh) -> eh.handle(null)); + } + + public void error(Exception e) { + // Hiding types with lambda's + this.errorHandlers.forEach((eh) -> eh.handle(e)); + } + + public void data(B t) { + this.handlers.forEach((th) -> th.handle(t)); + } +} diff --git a/src/test/java/telraam/beacon/BeaconTest.java b/src/test/java/telraam/beacon/BeaconTest.java new file mode 100644 index 0000000..e69de29 From 170b89bfa6580caaa86072534ab40049d5d80706 Mon Sep 17 00:00:00 2001 From: Maxime <12089026+mcbloch@users.noreply.github.com> Date: Tue, 5 Nov 2019 18:07:11 +0100 Subject: [PATCH 02/13] Update README.md --- src/main/java/telraam/beacon/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/telraam/beacon/README.md b/src/main/java/telraam/beacon/README.md index e47f691..df2d2af 100644 --- a/src/main/java/telraam/beacon/README.md +++ b/src/main/java/telraam/beacon/README.md @@ -3,7 +3,7 @@ Yes I really need a README to explain my shitty code. So, generics are a thing in Java. But they suck. -The main class in this package is the BeaconAggregator, which is basically an Event generator, B being the thing built from the data. +The main class in this package is the BeaconAggregator, which is basically an `Event` generator, B being the thing built from the data. BeaconAggregator extends TCPFactory that spawns all the connecting Beacon that actually do the generating. On TCPFactories you can subscribe handlers for basic events, like Errors (with `onError`), data (with `onData`) and disconnects (with `onDisconnect`). From 0133fb61cc295654e3b4925403dcff08b764957b Mon Sep 17 00:00:00 2001 From: ajuvercr Date: Wed, 6 Nov 2019 11:17:39 +0100 Subject: [PATCH 03/13] make beacons actually read the tcp connection --- src/main/java/telraam/beacon/Beacon.java | 41 ++++++++++++++++--- .../java/telraam/beacon/BeaconAggregator.java | 2 + .../java/telraam/beacon/BeaconMessage.java | 1 + src/main/java/telraam/beacon/Event.java | 10 +++++ .../java/telraam/beacon/EventGenerator.java | 4 ++ src/main/java/telraam/beacon/TCPFactory.java | 20 +++++++-- 6 files changed, 69 insertions(+), 9 deletions(-) diff --git a/src/main/java/telraam/beacon/Beacon.java b/src/main/java/telraam/beacon/Beacon.java index a860e94..93cafef 100644 --- a/src/main/java/telraam/beacon/Beacon.java +++ b/src/main/java/telraam/beacon/Beacon.java @@ -1,17 +1,46 @@ package telraam.beacon; +import java.io.IOException; +import java.io.InputStream; +import java.io.EOFException; import java.net.Socket; -public class Beacon extends EventGenerator { - public Beacon(Callback> h) { +public class Beacon extends EventGenerator implements Runnable { + private Socket s; + private int messageSize = 10; + + public Beacon(Socket socket, Callback> h) { super(h); + + this.s = socket; + + new Thread(this).run(); } - public Beacon(Socket socket, Callback> h) { - this(h); + public void run() { + this.connect(); + + byte[] buf = new byte[messageSize]; + int at = 0; + + try { + InputStream is = s.getInputStream(); - // socket.getInputStream().readNBytes(10); + while (true) { + int c = is.read(buf, at, messageSize - at); + if (c < 0) throw new EOFException(); + at += c; + if (at == messageSize) { + this.data(new BeaconMessage(buf)); + at = 0; + } + } - // TODO: Setup socket, generating B things somehow + } catch (EOFException e) { + exit(); + } catch (IOException e) { + error(e); + exit(); + } } } diff --git a/src/main/java/telraam/beacon/BeaconAggregator.java b/src/main/java/telraam/beacon/BeaconAggregator.java index 07f1c36..8502648 100644 --- a/src/main/java/telraam/beacon/BeaconAggregator.java +++ b/src/main/java/telraam/beacon/BeaconAggregator.java @@ -15,6 +15,8 @@ public BeaconAggregator(int port) throws IOException { } public Void handle(Event event) { + // this is the handler for event. + // Sending the data to the correct handlers set by TCPFactory event.handle(this); return null; } diff --git a/src/main/java/telraam/beacon/BeaconMessage.java b/src/main/java/telraam/beacon/BeaconMessage.java index ec63d17..675bb2d 100644 --- a/src/main/java/telraam/beacon/BeaconMessage.java +++ b/src/main/java/telraam/beacon/BeaconMessage.java @@ -6,6 +6,7 @@ public class BeaconMessage { public BeaconMessage() { } + // DO NOT STORE THIS DATA, IT WILL BE OVERWRITTEN public BeaconMessage(byte[] data) { this.data = data; } diff --git a/src/main/java/telraam/beacon/Event.java b/src/main/java/telraam/beacon/Event.java index 48fa7c3..f5182f9 100644 --- a/src/main/java/telraam/beacon/Event.java +++ b/src/main/java/telraam/beacon/Event.java @@ -27,6 +27,15 @@ void handle(EventHandler h) { } } + public static class Connect extends Event { + public Connect() { + } + + void handle(EventHandler h) { + h.connect(); + } + } + public static class Exit extends Event { public Exit() { } @@ -38,6 +47,7 @@ void handle(EventHandler h) { public interface EventHandler { void exit(); + void connect(); void error(Exception e); diff --git a/src/main/java/telraam/beacon/EventGenerator.java b/src/main/java/telraam/beacon/EventGenerator.java index 96d44bb..ca7a1b3 100644 --- a/src/main/java/telraam/beacon/EventGenerator.java +++ b/src/main/java/telraam/beacon/EventGenerator.java @@ -5,6 +5,10 @@ public EventGenerator(Callback> handler) { super(handler); } + protected void connect() { + super.handle(new Event.Connect<>()); + } + protected void data(B data) { super.handle(new Event.Data<>(data)); } diff --git a/src/main/java/telraam/beacon/TCPFactory.java b/src/main/java/telraam/beacon/TCPFactory.java index 7f34781..7f0f6eb 100644 --- a/src/main/java/telraam/beacon/TCPFactory.java +++ b/src/main/java/telraam/beacon/TCPFactory.java @@ -6,25 +6,31 @@ import java.util.List; import java.util.ArrayList; +import java.util.logging.Level; +import java.util.logging.Logger; + public class TCPFactory implements Event.EventHandler, Runnable { + private static Logger logger = Logger.getLogger(TCPFactory.class.getName()); + protected Callback creator; private ServerSocket socket; List> handlers = new ArrayList<>(); List> errorHandlers = new ArrayList<>(); List> exitHandlers = new ArrayList<>(); + List> connectHandlers = new ArrayList<>(); public TCPFactory(Callback creator, int port) throws IOException { this(port); this.creator = creator; - - new Thread(this).run(); } protected TCPFactory(int port) throws IOException { this.socket = new ServerSocket(port); + logger.log(Level.INFO, "Starting tcp on port "+port); } public void run() { + logger.log(Level.INFO, "Accepting actual connections"); while (true) { try { Socket s = socket.accept(); @@ -50,12 +56,20 @@ public TCPFactory onDisconnect(Callback handler) { return this; } + public TCPFactory onConnect(Callback handler) { + this.connectHandlers.add(handler); + return this; + } + public void exit() { this.exitHandlers.forEach((eh) -> eh.handle(null)); } + public void connect() { + this.connectHandlers.forEach((th) -> th.handle(null)); + } + public void error(Exception e) { - // Hiding types with lambda's this.errorHandlers.forEach((eh) -> eh.handle(e)); } From 1675e8a8bfc80c10f303ec80ca7993b4e305526d Mon Sep 17 00:00:00 2001 From: ajuvercr Date: Wed, 6 Nov 2019 13:11:47 +0100 Subject: [PATCH 04/13] add beacon tests --- src/main/java/telraam/App.java | 27 +++- src/main/java/telraam/beacon/Beacon.java | 4 +- .../java/telraam/beacon/BeaconMessage.java | 2 + src/main/java/telraam/beacon/TCPFactory.java | 5 +- src/test/java/telraam/beacon/BeaconTest.java | 153 ++++++++++++++++++ 5 files changed, 184 insertions(+), 7 deletions(-) diff --git a/src/main/java/telraam/App.java b/src/main/java/telraam/App.java index ee5d75e..9b878a7 100644 --- a/src/main/java/telraam/App.java +++ b/src/main/java/telraam/App.java @@ -1,7 +1,9 @@ package telraam; +import telraam.beacon.BeaconAggregator; import telraam.database.Database; +import java.io.IOException; import java.sql.Connection; import java.util.logging.Level; import java.util.logging.Logger; @@ -9,16 +11,35 @@ public class App { private static Logger logger = Logger.getLogger(App.class.getName()); - public static void main(String[] args) { + public static void main(String[] args) throws IOException { logger.log(Level.INFO, "Main method"); - Connection conn = Database.getInstance().getDataAccessContext().getConnection(); + // Connection conn = Database.getInstance().getDataAccessContext().getConnection(); + BeaconAggregator ba = new BeaconAggregator(5678); + ba.onData((b) -> { + logger.log(Level.INFO, new String(b.data)); + return null; + }); + ba.onError((b) -> { + logger.log(Level.WARNING, b.getMessage()); + return null; + }); + ba.onDisconnect((_e) -> { + logger.log(Level.INFO, "Beacon disconnected"); + return null; + }); + + ba.onConnect((_e) -> { + logger.log(Level.INFO, "Beacon connected"); + return null; + }); + + ba.run(); } /** * Temporary test method */ public String greeting() { - return "test"; } } diff --git a/src/main/java/telraam/beacon/Beacon.java b/src/main/java/telraam/beacon/Beacon.java index 93cafef..86bf17c 100644 --- a/src/main/java/telraam/beacon/Beacon.java +++ b/src/main/java/telraam/beacon/Beacon.java @@ -7,14 +7,14 @@ public class Beacon extends EventGenerator implements Runnable { private Socket s; - private int messageSize = 10; + private int messageSize = BeaconMessage.MESSAGESIZE; public Beacon(Socket socket, Callback> h) { super(h); this.s = socket; - new Thread(this).run(); + new Thread(this).start(); } public void run() { diff --git a/src/main/java/telraam/beacon/BeaconMessage.java b/src/main/java/telraam/beacon/BeaconMessage.java index 675bb2d..78070ea 100644 --- a/src/main/java/telraam/beacon/BeaconMessage.java +++ b/src/main/java/telraam/beacon/BeaconMessage.java @@ -1,6 +1,8 @@ package telraam.beacon; public class BeaconMessage { + public static final int MESSAGESIZE = 10; + public byte[] data; public BeaconMessage() { diff --git a/src/main/java/telraam/beacon/TCPFactory.java b/src/main/java/telraam/beacon/TCPFactory.java index 7f0f6eb..bf0ea6e 100644 --- a/src/main/java/telraam/beacon/TCPFactory.java +++ b/src/main/java/telraam/beacon/TCPFactory.java @@ -25,12 +25,13 @@ public TCPFactory(Callback creator, int port) throws IOException { } protected TCPFactory(int port) throws IOException { - this.socket = new ServerSocket(port); + if (port > 0) + this.socket = new ServerSocket(port); logger.log(Level.INFO, "Starting tcp on port "+port); } public void run() { - logger.log(Level.INFO, "Accepting actual connections"); + logger.log(Level.INFO, "Actually accepting connections"); while (true) { try { Socket s = socket.accept(); diff --git a/src/test/java/telraam/beacon/BeaconTest.java b/src/test/java/telraam/beacon/BeaconTest.java index e69de29..8d978be 100644 --- a/src/test/java/telraam/beacon/BeaconTest.java +++ b/src/test/java/telraam/beacon/BeaconTest.java @@ -0,0 +1,153 @@ +package telraam.beacon; + +import org.junit.jupiter.api.Test; +import static org.junit.jupiter.api.Assertions.*; + +import java.io.IOException; +import java.io.InputStream; +import java.io.PipedInputStream; +import java.io.PipedOutputStream; +import java.lang.reflect.Field; +import java.net.ServerSocket; +import java.net.Socket; +import java.util.ArrayList; +import java.util.List; + +import org.junit.jupiter.api.BeforeAll; + +import java.util.logging.Logger; + +public class BeaconTest { + private static Logger logger = Logger.getLogger(BeaconTest.class.getName()); + + static List connectedSockets = new ArrayList<>(); + + public static class OurSocket extends Socket { + private PipedInputStream pis; + private PipedOutputStream pos; + + public OurSocket() throws IOException { + super(); + + pis = new PipedInputStream(); + pos = new PipedOutputStream(pis); + } + + public InputStream getInputStream() throws IOException { + return this.pis; + } + + public void write(byte[] buf) throws IOException { + pos.write(buf); + pos.flush(); + } + + public void close() throws IOException { + pos.close(); + pis.close(); + super.close(); + } + } + + public static class OurServerSocket extends ServerSocket { + private int connections; + + public OurServerSocket(int connections) throws IOException { + super(); + this.connections = connections; + } + + @Override + public Socket accept() throws IOException { + // Only spawn connections amount of sockets + if (connections < 1) { + while (true) { + try { + Thread.sleep(1000); + } catch (Exception e) { + } + } + } + connections--; + OurSocket s = new OurSocket(); + // super.implAccept(s); // This fails, and should not be called + connectedSockets.add(s); + logger.info("Created new socket"); + return s; + } + } + + static BeaconAggregator ba; + static int data, connects, errors, exits; + + @BeforeAll + public static void init() throws Exception { + ba = new BeaconAggregator(-1); + + Field socketField = ba.getClass().getSuperclass().getDeclaredField("socket"); + + socketField.setAccessible(true); + socketField.set(ba, new OurServerSocket(5)); + } + + @Test + public void testEverythingBeacon() throws Exception { + + ba.onConnect((_e) -> { + connects += 1; + return null; + }); + + ba.onData((_e) -> { + data += 1; + return null; + }); + + ba.onDisconnect((_e) -> { + exits += 1; + return null; + }); + + ba.onError((_e) -> { + errors += 1; + return null; + }); + + new Thread(ba).start(); + Thread.sleep(100); + + // Check if all beacons are connected + assertEquals(connects, 5); + + // Check if they can disconnect at will + connectedSockets.remove(0).close(); + Thread.sleep(100); + assertEquals(exits, 1); + + // Check if no beacon messages are sent with incomplete data + // Aka do they buffer correctly? + for (OurSocket s: connectedSockets) { + s.write("hadeksfd".getBytes()); + } + Thread.sleep(100); + assertEquals(data, 0); + + // But not too much either + for (OurSocket s: connectedSockets) { + s.write("dsa".getBytes()); + } + + Thread.sleep(100); + assertEquals(data, connectedSockets.size()); + + // Do they all close correctly + for (OurSocket s: connectedSockets) { + s.close(); + } + Thread.sleep(100); + assertEquals(exits, 5); + + // No errors received + assertEquals(errors, 0); + } +} From ceea7c9904cf7431c49d8d355719a7799bbe01b2 Mon Sep 17 00:00:00 2001 From: ajuvercr Date: Wed, 6 Nov 2019 13:32:26 +0100 Subject: [PATCH 05/13] add docs --- src/main/java/telraam/beacon/Beacon.java | 10 +++++++- .../java/telraam/beacon/BeaconAggregator.java | 8 +++++++ .../java/telraam/beacon/BeaconMessage.java | 9 ++++--- src/main/java/telraam/beacon/Callback.java | 5 ++++ src/main/java/telraam/beacon/Event.java | 7 ++++++ .../java/telraam/beacon/EventGenerator.java | 24 ++++++++++++++----- src/main/java/telraam/beacon/Generator.java | 13 ---------- src/main/java/telraam/beacon/README.md | 9 +++---- src/main/java/telraam/beacon/TCPFactory.java | 17 +++++++++---- src/test/java/telraam/beacon/BeaconTest.java | 11 +++++---- 10 files changed, 75 insertions(+), 38 deletions(-) delete mode 100644 src/main/java/telraam/beacon/Generator.java diff --git a/src/main/java/telraam/beacon/Beacon.java b/src/main/java/telraam/beacon/Beacon.java index 86bf17c..77c7075 100644 --- a/src/main/java/telraam/beacon/Beacon.java +++ b/src/main/java/telraam/beacon/Beacon.java @@ -5,6 +5,15 @@ import java.io.EOFException; import java.net.Socket; +/** +* Beacon is socket wrapper that listens to the sockets +* and emits BeaconMessages when enough bytes are read. +* +* Beacons are closed at the first Exception encountered. +* This could be changed if need be. +* +* @author Arthur Vercruysse +*/ public class Beacon extends EventGenerator implements Runnable { private Socket s; private int messageSize = BeaconMessage.MESSAGESIZE; @@ -35,7 +44,6 @@ public void run() { at = 0; } } - } catch (EOFException e) { exit(); } catch (IOException e) { diff --git a/src/main/java/telraam/beacon/BeaconAggregator.java b/src/main/java/telraam/beacon/BeaconAggregator.java index 8502648..224d73a 100644 --- a/src/main/java/telraam/beacon/BeaconAggregator.java +++ b/src/main/java/telraam/beacon/BeaconAggregator.java @@ -2,6 +2,14 @@ import java.io.IOException; +/** +* BeaconAggregator is the main class to handle aggregate BeaconMessages. +* Register listeners to data, errors, connects and disconnects. +* +* If port is negative no server is started (should only be used in tests). +* +* @author Arthur Vercruysse +*/ public class BeaconAggregator extends TCPFactory implements Callback> { public BeaconAggregator(int port) throws IOException { diff --git a/src/main/java/telraam/beacon/BeaconMessage.java b/src/main/java/telraam/beacon/BeaconMessage.java index 78070ea..558edc2 100644 --- a/src/main/java/telraam/beacon/BeaconMessage.java +++ b/src/main/java/telraam/beacon/BeaconMessage.java @@ -1,13 +1,16 @@ package telraam.beacon; +/** +* BeaconMessage is the representation of what is received from a beacon. +* This should parse the incoming byte[]. +* +* @author Arthur Vercruysse +*/ public class BeaconMessage { public static final int MESSAGESIZE = 10; public byte[] data; - public BeaconMessage() { - } - // DO NOT STORE THIS DATA, IT WILL BE OVERWRITTEN public BeaconMessage(byte[] data) { this.data = data; diff --git a/src/main/java/telraam/beacon/Callback.java b/src/main/java/telraam/beacon/Callback.java index ef12728..d6e9a53 100644 --- a/src/main/java/telraam/beacon/Callback.java +++ b/src/main/java/telraam/beacon/Callback.java @@ -1,5 +1,10 @@ package telraam.beacon; +/** +* Stupid interface for callbacks. You mind if I request Callback? +* +* @author Arthur Vercruysse +*/ public interface Callback { public Output handle(Input value); } diff --git a/src/main/java/telraam/beacon/Event.java b/src/main/java/telraam/beacon/Event.java index f5182f9..887089c 100644 --- a/src/main/java/telraam/beacon/Event.java +++ b/src/main/java/telraam/beacon/Event.java @@ -1,5 +1,12 @@ package telraam.beacon; +/** +* Event is an 'enum' class with data attached. +* An events can be handled with an EventHandler like a TCPFactory. +* `event.handle(this);` +* +* @author Arthur Vercruysse +*/ public abstract class Event { abstract void handle(EventHandler h); diff --git a/src/main/java/telraam/beacon/EventGenerator.java b/src/main/java/telraam/beacon/EventGenerator.java index ca7a1b3..5db062a 100644 --- a/src/main/java/telraam/beacon/EventGenerator.java +++ b/src/main/java/telraam/beacon/EventGenerator.java @@ -1,23 +1,35 @@ package telraam.beacon; -public abstract class EventGenerator extends Generator> { +/** +* Callback> wrapper in disguise. +* Exposing simpler methods to wrap in the right Event. +* +* @author Arthur Vercruysse +*/ +public abstract class EventGenerator { + protected Callback> handler; + public EventGenerator(Callback> handler) { - super(handler); + this.handler = handler; } protected void connect() { - super.handle(new Event.Connect<>()); + handle(new Event.Connect<>()); } protected void data(B data) { - super.handle(new Event.Data<>(data)); + handle(new Event.Data<>(data)); } protected void error(Exception e) { - super.handle(new Event.Error<>(e)); + handle(new Event.Error<>(e)); } protected void exit() { - super.handle(new Event.Exit<>()); + handle(new Event.Exit<>()); + } + + protected void handle(Event event) { + handler.handle(event); } } diff --git a/src/main/java/telraam/beacon/Generator.java b/src/main/java/telraam/beacon/Generator.java deleted file mode 100644 index d395cb9..0000000 --- a/src/main/java/telraam/beacon/Generator.java +++ /dev/null @@ -1,13 +0,0 @@ -package telraam.beacon; - -public abstract class Generator { - protected Callback handler; - - public Generator(Callback handler) { - this.handler = handler; - } - - protected void handle(T element) { - handler.handle(element); - } -} diff --git a/src/main/java/telraam/beacon/README.md b/src/main/java/telraam/beacon/README.md index df2d2af..a3651f6 100644 --- a/src/main/java/telraam/beacon/README.md +++ b/src/main/java/telraam/beacon/README.md @@ -1,14 +1,11 @@ # BEACONS Yes I really need a README to explain my shitty code. -So, generics are a thing in Java. But they suck. -The main class in this package is the BeaconAggregator, which is basically an `Event` generator, B being the thing built from the data. +The main class in this package is the BeaconAggregator, which is basically an `Event` generator, B being the thing built from the data, here `BeaconMessages`. -BeaconAggregator extends TCPFactory that spawns all the connecting Beacon that actually do the generating. On TCPFactories you can subscribe handlers for basic events, like Errors (with `onError`), data (with `onData`) and disconnects (with `onDisconnect`). +`BeaconAggregator` extends `TCPFactory` that spawns `Beacon`'s that actually do the generating. On TCPFactories you can subscribe handlers for basic events, like Data (with `onData`), errors (with `onError`), connects (with `onConnect`) and disconnects (with `onDisconnect`). -So beacons generate events, which is a nice way to hide IO exceptions etc that is wrapped in Event. These get handled by `Event.EventHandlers` like the TCPFactory. +So beacons generate events, which is a nice way to hide IO exceptions etc that is wrapped in `Event`. These get handled by `Event.EventHandlers` like the `TCPFactory`. TODO: Function scoping, eg the functions `exit` `error` and `data` should not be public, but interfaces bla bla bla. - -TODO: How does one create B's from the plain sockets in Beacon. Remove B generic, I sure hope not? diff --git a/src/main/java/telraam/beacon/TCPFactory.java b/src/main/java/telraam/beacon/TCPFactory.java index bf0ea6e..bc74ddc 100644 --- a/src/main/java/telraam/beacon/TCPFactory.java +++ b/src/main/java/telraam/beacon/TCPFactory.java @@ -9,15 +9,22 @@ import java.util.logging.Level; import java.util.logging.Logger; +/** +* The meat and potato's, but actually just spawning new connections with a creator, +* and exposing subscriber pattern. +* +* @author Arthur Vercruysse +*/ public class TCPFactory implements Event.EventHandler, Runnable { private static Logger logger = Logger.getLogger(TCPFactory.class.getName()); - protected Callback creator; private ServerSocket socket; - List> handlers = new ArrayList<>(); - List> errorHandlers = new ArrayList<>(); - List> exitHandlers = new ArrayList<>(); - List> connectHandlers = new ArrayList<>(); + protected Callback creator; + + protected List> handlers = new ArrayList<>(); + protected List> errorHandlers = new ArrayList<>(); + protected List> exitHandlers = new ArrayList<>(); + protected List> connectHandlers = new ArrayList<>(); public TCPFactory(Callback creator, int port) throws IOException { this(port); diff --git a/src/test/java/telraam/beacon/BeaconTest.java b/src/test/java/telraam/beacon/BeaconTest.java index 8d978be..0a3a1e7 100644 --- a/src/test/java/telraam/beacon/BeaconTest.java +++ b/src/test/java/telraam/beacon/BeaconTest.java @@ -15,10 +15,14 @@ import org.junit.jupiter.api.BeforeAll; -import java.util.logging.Logger; - +/** +* Beacon integration test. +* Spoofing ServerSocket and Socket so you can write to it at will. +* TODO: Test socket exception, but I don't really know what could fail. +* +* @author Arthur Vercruysse +*/ public class BeaconTest { - private static Logger logger = Logger.getLogger(BeaconTest.class.getName()); static List connectedSockets = new ArrayList<>(); @@ -72,7 +76,6 @@ public Socket accept() throws IOException { OurSocket s = new OurSocket(); // super.implAccept(s); // This fails, and should not be called connectedSockets.add(s); - logger.info("Created new socket"); return s; } } From 04e72a85b9216ab49abace6d916b34ec161b02dc Mon Sep 17 00:00:00 2001 From: ajuvercr Date: Wed, 6 Nov 2019 13:41:22 +0100 Subject: [PATCH 06/13] make tests easier to pass --- src/main/java/telraam/App.java | 27 +++----------------- src/test/java/telraam/beacon/BeaconTest.java | 12 ++++----- 2 files changed, 9 insertions(+), 30 deletions(-) diff --git a/src/main/java/telraam/App.java b/src/main/java/telraam/App.java index 9b878a7..ee5d75e 100644 --- a/src/main/java/telraam/App.java +++ b/src/main/java/telraam/App.java @@ -1,9 +1,7 @@ package telraam; -import telraam.beacon.BeaconAggregator; import telraam.database.Database; -import java.io.IOException; import java.sql.Connection; import java.util.logging.Level; import java.util.logging.Logger; @@ -11,35 +9,16 @@ public class App { private static Logger logger = Logger.getLogger(App.class.getName()); - public static void main(String[] args) throws IOException { + public static void main(String[] args) { logger.log(Level.INFO, "Main method"); - // Connection conn = Database.getInstance().getDataAccessContext().getConnection(); - BeaconAggregator ba = new BeaconAggregator(5678); - ba.onData((b) -> { - logger.log(Level.INFO, new String(b.data)); - return null; - }); - ba.onError((b) -> { - logger.log(Level.WARNING, b.getMessage()); - return null; - }); - ba.onDisconnect((_e) -> { - logger.log(Level.INFO, "Beacon disconnected"); - return null; - }); - - ba.onConnect((_e) -> { - logger.log(Level.INFO, "Beacon connected"); - return null; - }); - - ba.run(); + Connection conn = Database.getInstance().getDataAccessContext().getConnection(); } /** * Temporary test method */ public String greeting() { + return "test"; } } diff --git a/src/test/java/telraam/beacon/BeaconTest.java b/src/test/java/telraam/beacon/BeaconTest.java index 0a3a1e7..1037023 100644 --- a/src/test/java/telraam/beacon/BeaconTest.java +++ b/src/test/java/telraam/beacon/BeaconTest.java @@ -67,7 +67,7 @@ public Socket accept() throws IOException { if (connections < 1) { while (true) { try { - Thread.sleep(1000); + Thread.sleep(2000); } catch (Exception e) { } } @@ -117,14 +117,14 @@ public void testEverythingBeacon() throws Exception { }); new Thread(ba).start(); - Thread.sleep(100); + Thread.sleep(500); // Check if all beacons are connected assertEquals(connects, 5); // Check if they can disconnect at will connectedSockets.remove(0).close(); - Thread.sleep(100); + Thread.sleep(200); assertEquals(exits, 1); // Check if no beacon messages are sent with incomplete data @@ -132,7 +132,7 @@ public void testEverythingBeacon() throws Exception { for (OurSocket s: connectedSockets) { s.write("hadeksfd".getBytes()); } - Thread.sleep(100); + Thread.sleep(200); assertEquals(data, 0); // But not too much either @@ -140,14 +140,14 @@ public void testEverythingBeacon() throws Exception { s.write("dsa".getBytes()); } - Thread.sleep(100); + Thread.sleep(200); assertEquals(data, connectedSockets.size()); // Do they all close correctly for (OurSocket s: connectedSockets) { s.close(); } - Thread.sleep(100); + Thread.sleep(200); assertEquals(exits, 5); // No errors received From f80d0bcd0f1486fda30b086bf7958eb31014612e Mon Sep 17 00:00:00 2001 From: ajuvercr Date: Wed, 6 Nov 2019 13:44:23 +0100 Subject: [PATCH 07/13] make tests easier to pass --- src/test/java/telraam/beacon/BeaconTest.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/test/java/telraam/beacon/BeaconTest.java b/src/test/java/telraam/beacon/BeaconTest.java index 1037023..8d822a4 100644 --- a/src/test/java/telraam/beacon/BeaconTest.java +++ b/src/test/java/telraam/beacon/BeaconTest.java @@ -124,7 +124,7 @@ public void testEverythingBeacon() throws Exception { // Check if they can disconnect at will connectedSockets.remove(0).close(); - Thread.sleep(200); + Thread.sleep(500); assertEquals(exits, 1); // Check if no beacon messages are sent with incomplete data @@ -132,7 +132,7 @@ public void testEverythingBeacon() throws Exception { for (OurSocket s: connectedSockets) { s.write("hadeksfd".getBytes()); } - Thread.sleep(200); + Thread.sleep(500); assertEquals(data, 0); // But not too much either @@ -140,14 +140,14 @@ public void testEverythingBeacon() throws Exception { s.write("dsa".getBytes()); } - Thread.sleep(200); + Thread.sleep(500); assertEquals(data, connectedSockets.size()); // Do they all close correctly for (OurSocket s: connectedSockets) { s.close(); } - Thread.sleep(200); + Thread.sleep(500); assertEquals(exits, 5); // No errors received From be828fb9985c9a29004a4ad7084b41b47d375a3d Mon Sep 17 00:00:00 2001 From: ajuvercr Date: Wed, 6 Nov 2019 14:39:16 +0100 Subject: [PATCH 08/13] semaphores in my tests, hell yeah --- src/test/java/telraam/beacon/BeaconTest.java | 69 ++++++++++++++------ 1 file changed, 50 insertions(+), 19 deletions(-) diff --git a/src/test/java/telraam/beacon/BeaconTest.java b/src/test/java/telraam/beacon/BeaconTest.java index 8d822a4..d4cc02b 100644 --- a/src/test/java/telraam/beacon/BeaconTest.java +++ b/src/test/java/telraam/beacon/BeaconTest.java @@ -12,6 +12,8 @@ import java.net.Socket; import java.util.ArrayList; import java.util.List; +import java.util.concurrent.Semaphore; +import java.util.concurrent.atomic.AtomicInteger; import org.junit.jupiter.api.BeforeAll; @@ -24,6 +26,8 @@ */ public class BeaconTest { + private static final Semaphore barrier = new Semaphore(8); + static List connectedSockets = new ArrayList<>(); public static class OurSocket extends Socket { @@ -32,6 +36,7 @@ public static class OurSocket extends Socket { public OurSocket() throws IOException { super(); + barrier.acquireUninterruptibly(); pis = new PipedInputStream(); pos = new PipedOutputStream(pis); @@ -41,12 +46,17 @@ public InputStream getInputStream() throws IOException { return this.pis; } - public void write(byte[] buf) throws IOException { + public void write(byte[] buf, boolean acq) throws IOException { + if (acq) { + barrier.acquireUninterruptibly(); + } + pos.write(buf); pos.flush(); } public void close() throws IOException { + barrier.acquireUninterruptibly(); pos.close(); pis.close(); super.close(); @@ -65,6 +75,7 @@ public OurServerSocket(int connections) throws IOException { public Socket accept() throws IOException { // Only spawn connections amount of sockets if (connections < 1) { + barrier.release(); while (true) { try { Thread.sleep(2000); @@ -81,7 +92,10 @@ public Socket accept() throws IOException { } static BeaconAggregator ba; - static int data, connects, errors, exits; + static AtomicInteger data = new AtomicInteger(); + static AtomicInteger connects = new AtomicInteger(); + static AtomicInteger errors = new AtomicInteger(); + static AtomicInteger exits = new AtomicInteger(); @BeforeAll public static void init() throws Exception { @@ -97,60 +111,77 @@ public static void init() throws Exception { public void testEverythingBeacon() throws Exception { ba.onConnect((_e) -> { - connects += 1; + connects.incrementAndGet(); + barrier.release(); return null; }); ba.onData((_e) -> { - data += 1; + data.incrementAndGet(); + barrier.release(); return null; }); ba.onDisconnect((_e) -> { - exits += 1; + exits.incrementAndGet(); + barrier.release(); return null; }); ba.onError((_e) -> { - errors += 1; + errors.incrementAndGet(); return null; }); + barrier.acquire(); new Thread(ba).start(); - Thread.sleep(500); + + barrier.acquire(8); + barrier.release(8); // Check if all beacons are connected - assertEquals(connects, 5); + assertEquals(5, connects.get()); // Check if they can disconnect at will connectedSockets.remove(0).close(); - Thread.sleep(500); - assertEquals(exits, 1); + + barrier.acquire(8); + barrier.release(8); + + assertEquals(exits.get(), 1); // Check if no beacon messages are sent with incomplete data // Aka do they buffer correctly? for (OurSocket s: connectedSockets) { - s.write("hadeksfd".getBytes()); + s.write("hadeksfd".getBytes(), false); } - Thread.sleep(500); - assertEquals(data, 0); + + barrier.acquire(8); + barrier.release(8); + + assertEquals(data.get(), 0); // But not too much either for (OurSocket s: connectedSockets) { - s.write("dsa".getBytes()); + s.write("dsa".getBytes(), true); } - Thread.sleep(500); - assertEquals(data, connectedSockets.size()); + barrier.acquire(8); + barrier.release(8); + + assertEquals(data.get(), connectedSockets.size()); // Do they all close correctly for (OurSocket s: connectedSockets) { s.close(); } - Thread.sleep(500); - assertEquals(exits, 5); + + barrier.acquire(8); + barrier.release(8); + + assertEquals(exits.get(), 5); // No errors received - assertEquals(errors, 0); + assertEquals(errors.get(), 0); } } From 0701d375ba781a07b3f1df449e45accda6b3cb41 Mon Sep 17 00:00:00 2001 From: ajuvercr Date: Wed, 6 Nov 2019 14:46:52 +0100 Subject: [PATCH 09/13] I would like to print my error please --- src/test/java/telraam/beacon/BeaconTest.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/test/java/telraam/beacon/BeaconTest.java b/src/test/java/telraam/beacon/BeaconTest.java index d4cc02b..87a9695 100644 --- a/src/test/java/telraam/beacon/BeaconTest.java +++ b/src/test/java/telraam/beacon/BeaconTest.java @@ -128,7 +128,8 @@ public void testEverythingBeacon() throws Exception { return null; }); - ba.onError((_e) -> { + ba.onError((e) -> { + System.out.println(e.getLocalizedMessage()); errors.incrementAndGet(); return null; }); From 373ae336025c8af90ec132d9253482eecc6ea5c5 Mon Sep 17 00:00:00 2001 From: ajuvercr Date: Wed, 6 Nov 2019 14:53:24 +0100 Subject: [PATCH 10/13] more checks for error count --- src/test/java/telraam/beacon/BeaconTest.java | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/test/java/telraam/beacon/BeaconTest.java b/src/test/java/telraam/beacon/BeaconTest.java index 87a9695..94f88e0 100644 --- a/src/test/java/telraam/beacon/BeaconTest.java +++ b/src/test/java/telraam/beacon/BeaconTest.java @@ -142,6 +142,7 @@ public void testEverythingBeacon() throws Exception { // Check if all beacons are connected assertEquals(5, connects.get()); + assertEquals(errors.get(), 0); // Check if they can disconnect at will connectedSockets.remove(0).close(); @@ -150,6 +151,7 @@ public void testEverythingBeacon() throws Exception { barrier.release(8); assertEquals(exits.get(), 1); + assertEquals(errors.get(), 0); // Check if no beacon messages are sent with incomplete data // Aka do they buffer correctly? @@ -161,6 +163,7 @@ public void testEverythingBeacon() throws Exception { barrier.release(8); assertEquals(data.get(), 0); + assertEquals(errors.get(), 0); // But not too much either for (OurSocket s: connectedSockets) { @@ -171,6 +174,7 @@ public void testEverythingBeacon() throws Exception { barrier.release(8); assertEquals(data.get(), connectedSockets.size()); + assertEquals(errors.get(), 0); // Do they all close correctly for (OurSocket s: connectedSockets) { From e4e2b59752a1ebde2ea9e0be397f0ff8cdfd3038 Mon Sep 17 00:00:00 2001 From: ajuvercr Date: Wed, 6 Nov 2019 14:56:10 +0100 Subject: [PATCH 11/13] actually log the error --- src/test/java/telraam/beacon/BeaconTest.java | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/test/java/telraam/beacon/BeaconTest.java b/src/test/java/telraam/beacon/BeaconTest.java index 94f88e0..de54488 100644 --- a/src/test/java/telraam/beacon/BeaconTest.java +++ b/src/test/java/telraam/beacon/BeaconTest.java @@ -16,6 +16,8 @@ import java.util.concurrent.atomic.AtomicInteger; import org.junit.jupiter.api.BeforeAll; +import java.util.logging.Logger; +import java.util.logging.Level; /** * Beacon integration test. @@ -25,6 +27,7 @@ * @author Arthur Vercruysse */ public class BeaconTest { + private static Logger logger = Logger.getLogger(BeaconTest.class.getName()); private static final Semaphore barrier = new Semaphore(8); @@ -129,7 +132,7 @@ public void testEverythingBeacon() throws Exception { }); ba.onError((e) -> { - System.out.println(e.getLocalizedMessage()); + logger.log(Level.SEVERE, "error", e); errors.incrementAndGet(); return null; }); From c021c300dcbb1c486a68ed8e5bcce0fa70aaf41f Mon Sep 17 00:00:00 2001 From: ajuvercr Date: Wed, 6 Nov 2019 15:22:34 +0100 Subject: [PATCH 12/13] set log level to info by default --- build.gradle | 2 + gradle.properties | 1 + gradle/wrapper/gradle-wrapper.properties | 5 +- gradlew.bat | 200 +++++++++---------- src/test/java/telraam/beacon/BeaconTest.java | 1 - 5 files changed, 105 insertions(+), 104 deletions(-) create mode 100644 gradle.properties diff --git a/build.gradle b/build.gradle index 8de22cb..ea87c59 100644 --- a/build.gradle +++ b/build.gradle @@ -107,3 +107,5 @@ task migrateTestingDatabase(type: FlywayMigrateTask) { url = testProps.getProperty("DB_URL") baselineOnMigrate = true } + +// org.gradle.logging.level=info diff --git a/gradle.properties b/gradle.properties new file mode 100644 index 0000000..6c7b331 --- /dev/null +++ b/gradle.properties @@ -0,0 +1 @@ +org.gradle.logging.level=info diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index 1ecd3a1..5028f28 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -1,6 +1,5 @@ -#Sun Nov 03 23:02:12 CET 2019 -distributionUrl=https\://services.gradle.org/distributions/gradle-5.6.4-all.zip distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists -zipStorePath=wrapper/dists +distributionUrl=https\://services.gradle.org/distributions/gradle-5.6.4-bin.zip zipStoreBase=GRADLE_USER_HOME +zipStorePath=wrapper/dists diff --git a/gradlew.bat b/gradlew.bat index 9618d8d..24467a1 100644 --- a/gradlew.bat +++ b/gradlew.bat @@ -1,100 +1,100 @@ -@rem -@rem Copyright 2015 the original author or authors. -@rem -@rem Licensed under the Apache License, Version 2.0 (the "License"); -@rem you may not use this file except in compliance with the License. -@rem You may obtain a copy of the License at -@rem -@rem https://www.apache.org/licenses/LICENSE-2.0 -@rem -@rem Unless required by applicable law or agreed to in writing, software -@rem distributed under the License is distributed on an "AS IS" BASIS, -@rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -@rem See the License for the specific language governing permissions and -@rem limitations under the License. -@rem - -@if "%DEBUG%" == "" @echo off -@rem ########################################################################## -@rem -@rem Gradle startup script for Windows -@rem -@rem ########################################################################## - -@rem Set local scope for the variables with windows NT shell -if "%OS%"=="Windows_NT" setlocal - -set DIRNAME=%~dp0 -if "%DIRNAME%" == "" set DIRNAME=. -set APP_BASE_NAME=%~n0 -set APP_HOME=%DIRNAME% - -@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. -set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m" - -@rem Find java.exe -if defined JAVA_HOME goto findJavaFromJavaHome - -set JAVA_EXE=java.exe -%JAVA_EXE% -version >NUL 2>&1 -if "%ERRORLEVEL%" == "0" goto init - -echo. -echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. -echo. -echo Please set the JAVA_HOME variable in your environment to match the -echo location of your Java installation. - -goto fail - -:findJavaFromJavaHome -set JAVA_HOME=%JAVA_HOME:"=% -set JAVA_EXE=%JAVA_HOME%/bin/java.exe - -if exist "%JAVA_EXE%" goto init - -echo. -echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% -echo. -echo Please set the JAVA_HOME variable in your environment to match the -echo location of your Java installation. - -goto fail - -:init -@rem Get command-line arguments, handling Windows variants - -if not "%OS%" == "Windows_NT" goto win9xME_args - -:win9xME_args -@rem Slurp the command line arguments. -set CMD_LINE_ARGS= -set _SKIP=2 - -:win9xME_args_slurp -if "x%~1" == "x" goto execute - -set CMD_LINE_ARGS=%* - -:execute -@rem Setup the command line - -set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar - -@rem Execute Gradle -"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% - -:end -@rem End local scope for the variables with windows NT shell -if "%ERRORLEVEL%"=="0" goto mainEnd - -:fail -rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of -rem the _cmd.exe /c_ return code! -if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 -exit /b 1 - -:mainEnd -if "%OS%"=="Windows_NT" endlocal - -:omega +@rem +@rem Copyright 2015 the original author or authors. +@rem +@rem Licensed under the Apache License, Version 2.0 (the "License"); +@rem you may not use this file except in compliance with the License. +@rem You may obtain a copy of the License at +@rem +@rem https://www.apache.org/licenses/LICENSE-2.0 +@rem +@rem Unless required by applicable law or agreed to in writing, software +@rem distributed under the License is distributed on an "AS IS" BASIS, +@rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +@rem See the License for the specific language governing permissions and +@rem limitations under the License. +@rem + +@if "%DEBUG%" == "" @echo off +@rem ########################################################################## +@rem +@rem Gradle startup script for Windows +@rem +@rem ########################################################################## + +@rem Set local scope for the variables with windows NT shell +if "%OS%"=="Windows_NT" setlocal + +set DIRNAME=%~dp0 +if "%DIRNAME%" == "" set DIRNAME=. +set APP_BASE_NAME=%~n0 +set APP_HOME=%DIRNAME% + +@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m" + +@rem Find java.exe +if defined JAVA_HOME goto findJavaFromJavaHome + +set JAVA_EXE=java.exe +%JAVA_EXE% -version >NUL 2>&1 +if "%ERRORLEVEL%" == "0" goto init + +echo. +echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. +echo. +echo Please set the JAVA_HOME variable in your environment to match the +echo location of your Java installation. + +goto fail + +:findJavaFromJavaHome +set JAVA_HOME=%JAVA_HOME:"=% +set JAVA_EXE=%JAVA_HOME%/bin/java.exe + +if exist "%JAVA_EXE%" goto init + +echo. +echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% +echo. +echo Please set the JAVA_HOME variable in your environment to match the +echo location of your Java installation. + +goto fail + +:init +@rem Get command-line arguments, handling Windows variants + +if not "%OS%" == "Windows_NT" goto win9xME_args + +:win9xME_args +@rem Slurp the command line arguments. +set CMD_LINE_ARGS= +set _SKIP=2 + +:win9xME_args_slurp +if "x%~1" == "x" goto execute + +set CMD_LINE_ARGS=%* + +:execute +@rem Setup the command line + +set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar + +@rem Execute Gradle +"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% + +:end +@rem End local scope for the variables with windows NT shell +if "%ERRORLEVEL%"=="0" goto mainEnd + +:fail +rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of +rem the _cmd.exe /c_ return code! +if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 +exit /b 1 + +:mainEnd +if "%OS%"=="Windows_NT" endlocal + +:omega diff --git a/src/test/java/telraam/beacon/BeaconTest.java b/src/test/java/telraam/beacon/BeaconTest.java index de54488..18dfcc7 100644 --- a/src/test/java/telraam/beacon/BeaconTest.java +++ b/src/test/java/telraam/beacon/BeaconTest.java @@ -62,7 +62,6 @@ public void close() throws IOException { barrier.acquireUninterruptibly(); pos.close(); pis.close(); - super.close(); } } From 9712ca5685fb3df844948f8428d39c679e64b260 Mon Sep 17 00:00:00 2001 From: ajuvercr Date: Wed, 6 Nov 2019 15:34:05 +0100 Subject: [PATCH 13/13] Catch weaker exceptions, but it shouldn't matter --- src/main/java/telraam/beacon/Beacon.java | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/main/java/telraam/beacon/Beacon.java b/src/main/java/telraam/beacon/Beacon.java index 77c7075..faac2bc 100644 --- a/src/main/java/telraam/beacon/Beacon.java +++ b/src/main/java/telraam/beacon/Beacon.java @@ -31,10 +31,16 @@ public void run() { byte[] buf = new byte[messageSize]; int at = 0; + InputStream is; try { - InputStream is = s.getInputStream(); + is = s.getInputStream(); + } catch (IOException e) { + error(e); + return; + } + try { while (true) { int c = is.read(buf, at, messageSize - at); if (c < 0) throw new EOFException(); @@ -44,10 +50,7 @@ public void run() { at = 0; } } - } catch (EOFException e) { - exit(); } catch (IOException e) { - error(e); exit(); } }