Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#1791 Resolves OpenMHz Broadcaster Auto-Reconnection Issue #1791

Merged
merged 2 commits into from
Jan 12, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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