From 741f9073488c9963036309ed7b5367ca2ab30c3e Mon Sep 17 00:00:00 2001 From: Yannik Bretschneider Date: Wed, 26 May 2021 13:57:47 +0200 Subject: [PATCH] feat: changed pipeline return type to optional for better clarity, and added comments --- Gamelib | 2 +- .../server/Lobby/pipelining/Pipeline.java | 30 +++++++++++++------ 2 files changed, 22 insertions(+), 10 deletions(-) diff --git a/Gamelib b/Gamelib index a4c5102..65c33d8 160000 --- a/Gamelib +++ b/Gamelib @@ -1 +1 @@ -Subproject commit a4c510279a35bfd10da62b3026eb2ed4c3c369ad +Subproject commit 65c33d886c9c8f2d4a06356f32ad6d772b60e7ee diff --git a/Server/src/main/java/uulm/teamname/marvelous/server/Lobby/pipelining/Pipeline.java b/Server/src/main/java/uulm/teamname/marvelous/server/Lobby/pipelining/Pipeline.java index 8f1cee2..bcfe4ff 100644 --- a/Server/src/main/java/uulm/teamname/marvelous/server/Lobby/pipelining/Pipeline.java +++ b/Server/src/main/java/uulm/teamname/marvelous/server/Lobby/pipelining/Pipeline.java @@ -7,6 +7,7 @@ import uulm.teamname.marvelous.gamelibrary.requests.Request; import java.util.ArrayList; import java.util.Arrays; import java.util.List; +import java.util.Optional; import java.util.concurrent.atomic.AtomicBoolean; /** @@ -32,22 +33,33 @@ public class Pipeline { * {@link Event events} relevant to the {@link Segment segment}. The {@link Event Events} are returned at the * end of the pipeline. * @param requests are the requests that are being pipelined through the pipeline - * @return a {@link Tuple}<{@link Boolean}, {@link Event}[]>, whereby the {@link Boolean} declares whether the - * execution of the {@link Pipeline} is successful. If that is the case, the events can be ignored, - * and an error message can be sent to the client. + * @return a {@link Optional}<{@link Event}[]>, whereby the state of the {@link Optional} declares whether the + * execution of the {@link Pipeline} was successful. If the optional is empty, the input requests were + * invalid, and an error message can be sent to the client. To get the {@link Event Events} + * out of the {@link Optional}, first check whether the {@link Optional} is empty by doing + * {@link Optional#isEmpty()} or {@link Optional#isPresent()}, and act accordingly. */ - public Tuple processRequests(Request[] requests) { + public Optional processRequests(Request[] requests) { + // The packet carries the requests, and gets smaller per segment List packet = Arrays.asList(requests); + // The packet is filled by the requests resulting from events per segment List carrier = new ArrayList<>(); - AtomicBoolean abort = new AtomicBoolean(); - abort.set(false); + // The abort boolean describes whether an abort happened in a segment + AtomicBoolean abort = new AtomicBoolean(false); + + // Loop through all segments for (Segment segment: segments) { + // Give the segment the packet, carrier and abort, and let it process requests segment.processRequests(packet, carrier, abort); - if (packet.size() == 0 || abort.get()) { - break; + if (packet.size() == 0 || abort.get()) { // if packet is empty (all requests processed) or abort initiated + break; // (abort boolean true), break out of the loop } } - return new Tuple (abort.get(), carrier.toArray(new Event[0])); + if (abort.get()) { // if abort is true, return empty optional + return Optional.empty(); + } else { // else return an optional of the array + return Optional.of(carrier.toArray(new Event[0])); + } } /**