Skip to content

Commit d89ba65

Browse files
authored
Merge pull request #1791 from tadscottsmith/openmhz-reconnect-error
#1791 Resolves OpenMHz Broadcaster Auto-Reconnection Issue
2 parents d2cf2ee + 7c5ef38 commit d89ba65

File tree

1 file changed

+28
-12
lines changed

1 file changed

+28
-12
lines changed

src/main/java/io/github/dsheirer/audio/broadcast/openmhz/OpenMHzBroadcaster.java

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
import java.io.BufferedReader;
5555
import java.io.InputStreamReader;
5656
import java.io.OutputStreamWriter;
57+
import java.net.ConnectException;
5758
import java.net.URI;
5859
import java.net.URL;
5960
import java.net.URLConnection;
@@ -69,6 +70,7 @@
6970
import java.util.concurrent.TimeUnit;
7071

7172

73+
7274
/**
7375
* Audio broadcaster to push completed audio recordings to the OpenMHz call upload API.
7476
*
@@ -115,14 +117,15 @@ public void start()
115117
String response = testConnection(getBroadcastConfiguration());
116118
mLastConnectionAttempt = System.currentTimeMillis();
117119

118-
/**
119-
* OpenMHz API does not currently expose a test method.
120-
* TODO: FIX THIS
121-
*/
122-
if(response == "OK")// && response.toLowerCase().startsWith("<head><title>502 bad gateway</title></head>"))
120+
if(response == "OK")
123121
{
124122
setBroadcastState(BroadcastState.CONNECTED);
125123
}
124+
else if (response == "No Response")
125+
{
126+
setBroadcastState(BroadcastState.NO_SERVER);
127+
mLog.error("Error connecting to OpenMHz server [Server not found or not reachable]");
128+
}
126129
else
127130
{
128131
mLog.error("Error connecting to OpenMHz server on startup [" + response + "]");
@@ -170,8 +173,6 @@ public void dispose()
170173
* Indicates if this broadcaster continues to have successful connections to and transactions with the remote
171174
* server. If there is a connectivity or other issue, the broadcast state is set to temporary error and
172175
* the audio processor thread will persistently invoke this method to attempt a reconnect.
173-
*
174-
* OpenMHz does not have a test API endpoint, so we look for the incomplete call response.
175176
*/
176177
private boolean connected()
177178
{
@@ -183,14 +184,21 @@ private boolean connected()
183184
String response = testConnection(getBroadcastConfiguration());
184185
mLastConnectionAttempt = System.currentTimeMillis();
185186

186-
if(response != null && response == "200")
187+
if(response == "OK")
187188
{
188189
setBroadcastState(BroadcastState.CONNECTED);
189190
}
191+
else if (response == "No Response")
192+
{
193+
setBroadcastState(BroadcastState.NO_SERVER);
194+
mLog.error("Error reconnecting to OpenMHz server [Server not found or not reachable]");
195+
}
190196
else
191197
{
192198
setBroadcastState(BroadcastState.ERROR);
199+
mLog.error("Error reconnecting to OpenMHz server [" + response + "]");
193200
}
201+
194202
}
195203

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

566574
try
567575
{
568-
HttpResponse<String> response = httpClient.send(request, responseHandler);
569-
String responseBody = response.body();
576+
HttpResponse<String> response = httpClient.send(request, responseHandler);
577+
570578
if (response.statusCode() == 200)
571579
{
572580
return "OK";
@@ -583,8 +591,16 @@ else if(response.statusCode() == 500)
583591
return "No Response";
584592
}
585593
catch(Exception e)
586-
{
587-
return e.getLocalizedMessage();
594+
{
595+
Throwable throwableCause = e.getCause();
596+
597+
if(throwableCause instanceof ConnectException)
598+
{
599+
return "No Response";
600+
}
601+
602+
mLog.error("Exception connecting to OpenMHz server [" + e.toString() + "]");
603+
return "Uknown Exception";
588604
}
589605
}
590606

0 commit comments

Comments
 (0)