54
54
import java .io .BufferedReader ;
55
55
import java .io .InputStreamReader ;
56
56
import java .io .OutputStreamWriter ;
57
+ import java .net .ConnectException ;
57
58
import java .net .URI ;
58
59
import java .net .URL ;
59
60
import java .net .URLConnection ;
69
70
import java .util .concurrent .TimeUnit ;
70
71
71
72
73
+
72
74
/**
73
75
* Audio broadcaster to push completed audio recordings to the OpenMHz call upload API.
74
76
*
@@ -115,14 +117,15 @@ public void start()
115
117
String response = testConnection (getBroadcastConfiguration ());
116
118
mLastConnectionAttempt = System .currentTimeMillis ();
117
119
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" )
123
121
{
124
122
setBroadcastState (BroadcastState .CONNECTED );
125
123
}
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
+ }
126
129
else
127
130
{
128
131
mLog .error ("Error connecting to OpenMHz server on startup [" + response + "]" );
@@ -170,8 +173,6 @@ public void dispose()
170
173
* Indicates if this broadcaster continues to have successful connections to and transactions with the remote
171
174
* server. If there is a connectivity or other issue, the broadcast state is set to temporary error and
172
175
* 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.
175
176
*/
176
177
private boolean connected ()
177
178
{
@@ -183,14 +184,21 @@ private boolean connected()
183
184
String response = testConnection (getBroadcastConfiguration ());
184
185
mLastConnectionAttempt = System .currentTimeMillis ();
185
186
186
- if (response != null && response == "200 " )
187
+ if (response == "OK " )
187
188
{
188
189
setBroadcastState (BroadcastState .CONNECTED );
189
190
}
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
+ }
190
196
else
191
197
{
192
198
setBroadcastState (BroadcastState .ERROR );
199
+ mLog .error ("Error reconnecting to OpenMHz server [" + response + "]" );
193
200
}
201
+
194
202
}
195
203
196
204
return getBroadcastState () == BroadcastState .CONNECTED ;
@@ -565,8 +573,8 @@ public static String testConnection(OpenMHzConfiguration configuration)
565
573
566
574
try
567
575
{
568
- HttpResponse <String > response = httpClient .send (request , responseHandler );
569
- String responseBody = response . body ();
576
+ HttpResponse <String > response = httpClient .send (request , responseHandler );
577
+
570
578
if (response .statusCode () == 200 )
571
579
{
572
580
return "OK" ;
@@ -583,8 +591,16 @@ else if(response.statusCode() == 500)
583
591
return "No Response" ;
584
592
}
585
593
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" ;
588
604
}
589
605
}
590
606
0 commit comments