Skip to content

Commit

Permalink
Merge pull request #1791 from tadscottsmith/openmhz-reconnect-error
Browse files Browse the repository at this point in the history
#1791 Resolves OpenMHz Broadcaster Auto-Reconnection Issue
  • Loading branch information
DSheirer authored Jan 12, 2024
2 parents d2cf2ee + 7c5ef38 commit d89ba65
Showing 1 changed file with 28 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.ConnectException;
import java.net.URI;
import java.net.URL;
import java.net.URLConnection;
Expand All @@ -69,6 +70,7 @@
import java.util.concurrent.TimeUnit;



/**
* Audio broadcaster to push completed audio recordings to the OpenMHz call upload API.
*
Expand Down Expand Up @@ -115,14 +117,15 @@ public void start()
String response = testConnection(getBroadcastConfiguration());
mLastConnectionAttempt = System.currentTimeMillis();

/**
* OpenMHz API does not currently expose a test method.
* TODO: FIX THIS
*/
if(response == "OK")// && response.toLowerCase().startsWith("<head><title>502 bad gateway</title></head>"))
if(response == "OK")
{
setBroadcastState(BroadcastState.CONNECTED);
}
else if (response == "No Response")
{
setBroadcastState(BroadcastState.NO_SERVER);
mLog.error("Error connecting to OpenMHz server [Server not found or not reachable]");
}
else
{
mLog.error("Error connecting to OpenMHz server on startup [" + response + "]");
Expand Down Expand Up @@ -170,8 +173,6 @@ public void dispose()
* Indicates if this broadcaster continues to have successful connections to and transactions with the remote
* server. If there is a connectivity or other issue, the broadcast state is set to temporary error and
* the audio processor thread will persistently invoke this method to attempt a reconnect.
*
* OpenMHz does not have a test API endpoint, so we look for the incomplete call response.
*/
private boolean connected()
{
Expand All @@ -183,14 +184,21 @@ private boolean connected()
String response = testConnection(getBroadcastConfiguration());
mLastConnectionAttempt = System.currentTimeMillis();

if(response != null && response == "200")
if(response == "OK")
{
setBroadcastState(BroadcastState.CONNECTED);
}
else if (response == "No Response")
{
setBroadcastState(BroadcastState.NO_SERVER);
mLog.error("Error reconnecting to OpenMHz server [Server not found or not reachable]");
}
else
{
setBroadcastState(BroadcastState.ERROR);
mLog.error("Error reconnecting to OpenMHz server [" + response + "]");
}

}

return getBroadcastState() == BroadcastState.CONNECTED;
Expand Down Expand Up @@ -565,8 +573,8 @@ public static String testConnection(OpenMHzConfiguration configuration)

try
{
HttpResponse<String> response = httpClient.send(request, responseHandler);
String responseBody = response.body();
HttpResponse<String> response = httpClient.send(request, responseHandler);

if (response.statusCode() == 200)
{
return "OK";
Expand All @@ -583,8 +591,16 @@ else if(response.statusCode() == 500)
return "No Response";
}
catch(Exception e)
{
return e.getLocalizedMessage();
{
Throwable throwableCause = e.getCause();

if(throwableCause instanceof ConnectException)
{
return "No Response";
}

mLog.error("Exception connecting to OpenMHz server [" + e.toString() + "]");
return "Uknown Exception";
}
}

Expand Down

0 comments on commit d89ba65

Please sign in to comment.