Skip to content

Commit

Permalink
Simplified TzevaAdomNotifier request logic
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidTheExplorer committed Oct 28, 2024
1 parent 8cdfae9 commit f95e07b
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 44 deletions.
16 changes: 4 additions & 12 deletions src/main/java/dte/tzevaadomapi/alertsource/AlertSource.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
public interface AlertSource
{
/**
* Returns the most recent {@link Alert} that happened in Israel.
* Returns the most recent {@link Alert} that happened in Israel
*
* @return Either the alert, or {@link #NO_RESULT} if one wasn't found.
* @return The alert, or null if no info was found.
* @throws Exception If an exception happened while gathering the alert.
*/
Alert getMostRecentAlert() throws Exception;
Expand All @@ -21,16 +21,8 @@ public interface AlertSource
* Returns all the alerts that happened since the provided {@code alert}.
*
* @param alert The 'minimum' alert.
* @return The alerts list, or an empty result if there was no Tzeva Adom afterwards.
* @throws Exception iF an exception happened while gathering the alerts.
* @return The alerts list, or an empty result if there was no Tzeva Adom afterward.
* @throws Exception If an exception happened while gathering the alerts.
*/
Deque<Alert> getSince(Alert alert) throws Exception;


/**
* This object signals when {@link #getMostRecentAlert()} couldn't find an alert to return, and no exceptions occurred.
* <p>
* This is addressed because <i>Pikud Ha'oref</i> returns an empty JSON if no Tzeva Adom happened in the last 24 hours.
*/
Alert NO_RESULT = null;
}
14 changes: 11 additions & 3 deletions src/main/java/dte/tzevaadomapi/alertsource/PHOAlertSource.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package dte.tzevaadomapi.alertsource;

import java.io.IOException;
import java.lang.reflect.Type;
import java.net.Proxy;
import java.net.URL;
Expand Down Expand Up @@ -79,12 +80,19 @@ public Deque<Alert> getSince(Alert alert) throws Exception
});
}

//starts reading the JSON list posted by Pikud Ha'oref, and applies the function on it
private <T> T beginReadingArray(CheckedFunction<JsonReader, T> resultParser)
//starts reading the JSON array posted by Pikud Ha'oref, and applies the function on it
private <T> T beginReadingArray(CheckedFunction<JsonReader, T> resultParser)
{
try(JsonReader reader = new JsonReader(newInputStreamReader()))
{
reader.beginArray();
try
{
reader.beginArray();
}
catch(IOException exception)
{
return null;
}

return resultParser.apply(reader);
}
Expand Down
35 changes: 13 additions & 22 deletions src/main/java/dte/tzevaadomapi/notifier/TzevaAdomNotifier.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
import dte.tzevaadomapi.alertsource.PHOAlertSource;
import dte.tzevaadomapi.exceptionhandler.LimitedExceptionHandler;
import dte.tzevaadomapi.listener.TzevaAdomListener;
import dte.tzevaadomapi.utils.CheckedSupplier;
import dte.tzevaadomapi.utils.CheckedFunction;

/**
* Notifies registered listeners immediately upon a <b>Tzeva Adom</b>.
Expand Down Expand Up @@ -49,13 +49,13 @@ public CompletableFuture<Void> listenAsync()
return CompletableFuture.runAsync(() ->
{
//start with the most recent alert
this.mostRecentAlert = requestMostRecentAlert();
this.mostRecentAlert = requestFromSource(AlertSource::getMostRecentAlert);

while(true)
{
Deque<Alert> newAlerts = requestFromSource(() -> this.alertSource.getSince(this.mostRecentAlert));
//if there are no new alerts - it's not Tzeva Adom
Deque<Alert> newAlerts = requestFromSource(source -> source.getSince(this.mostRecentAlert));

//wait until a terror organization decides to launch rockets
if(newAlerts.isEmpty())
continue;

Expand Down Expand Up @@ -88,31 +88,22 @@ public TzevaAdomHistory getHistory()
{
return this.history;
}

private Alert requestMostRecentAlert()
{
Alert alert;

//wait until a terror organization decides to launch rockets
do
{
alert = requestFromSource(this.alertSource::getMostRecentAlert);
}
while(alert == AlertSource.NO_RESULT);

return alert;
}

private <T> T requestFromSource(CheckedSupplier<T> resultFactory)
private <T> T requestFromSource(CheckedFunction<AlertSource, T> request)
{
while(true)
{
try
{
//sleep the defined delay
TimeUnit.MILLISECONDS.sleep(this.requestDelay.toMillis());

return resultFactory.get();

T result = request.apply(this.alertSource);

if(result == null)
continue;

return result;
}
catch(Exception exception)
{
Expand Down
7 changes: 0 additions & 7 deletions src/main/java/dte/tzevaadomapi/utils/CheckedSupplier.java

This file was deleted.

0 comments on commit f95e07b

Please sign in to comment.