don't crash when can't reach relay via http

This commit is contained in:
Eric House 2017-11-12 08:53:19 -08:00
parent ec075eff18
commit 8650795a81

View file

@ -166,7 +166,6 @@ public class RelayService extends XWService
{ {
boolean enabled = ! XWPrefs boolean enabled = ! XWPrefs
.getPrefsBoolean( context, R.string.key_disable_relay, false ); .getPrefsBoolean( context, R.string.key_disable_relay, false );
Log.d( TAG, "relayEnabled() => %b", enabled );
return enabled; return enabled;
} }
@ -656,40 +655,44 @@ public class RelayService extends XWService
{ {
Log.d( TAG, "sendViaWeb(): sending %d at once", packets.size() ); Log.d( TAG, "sendViaWeb(): sending %d at once", packets.size() );
int sentLen = 0; int sentLen = 0;
try { HttpURLConnection conn = NetUtils.makeHttpRelayConn( this, "post" );
JSONArray dataArray = new JSONArray(); if ( null == conn ) {
for ( PacketData packet : packets ) { Log.e( TAG, "sendViaWeb(): null conn for POST" );
byte[] datum = packet.assemble(); } else {
dataArray.put( Utils.base64Encode(datum) ); try {
sentLen += datum.length; JSONArray dataArray = new JSONArray();
} for ( PacketData packet : packets ) {
JSONObject params = new JSONObject(); byte[] datum = packet.assemble();
params.put( "data", dataArray ); dataArray.put( Utils.base64Encode(datum) );
sentLen += datum.length;
HttpURLConnection conn = NetUtils.makeHttpRelayConn( this, "post" );
String result = NetUtils.runConn(conn, params);
if ( null != result ) {
Log.d( TAG, "sendViaWeb(): POST => %s", result );
JSONObject resultObj = new JSONObject( result );
JSONArray resData = resultObj.getJSONArray( "data" );
int nReplies = resData.length();
Log.d( TAG, "sendViaWeb(): got %d replies", nReplies );
noteSent( packets ); // before we process the acks below :-)
if ( nReplies > 0 ) {
resetExitTimer();
} }
for ( int ii = 0; ii < nReplies; ++ii ) { JSONObject params = new JSONObject();
byte[] datum = Utils.base64Decode( resData.getString( ii ) ); params.put( "data", dataArray );
// PENDING: skip ack or not
gotPacket( datum, false ); String result = NetUtils.runConn(conn, params);
if ( null != result ) {
Log.d( TAG, "sendViaWeb(): POST => %s", result );
JSONObject resultObj = new JSONObject( result );
JSONArray resData = resultObj.getJSONArray( "data" );
int nReplies = resData.length();
Log.d( TAG, "sendViaWeb(): got %d replies", nReplies );
noteSent( packets ); // before we process the acks below :-)
if ( nReplies > 0 ) {
resetExitTimer();
}
for ( int ii = 0; ii < nReplies; ++ii ) {
byte[] datum = Utils.base64Decode( resData.getString( ii ) );
// PENDING: skip ack or not
gotPacket( datum, false );
}
} else {
Log.e( TAG, "sendViaWeb(): failed result for POST" );
} }
} else { } catch ( JSONException ex ) {
Log.e( TAG, "sendViaWeb(): failed result for POST" ); Assert.assertFalse( BuildConfig.DEBUG );
} }
} catch ( JSONException ex ) {
Assert.assertFalse( BuildConfig.DEBUG );
} }
return sentLen; return sentLen;
} }
@ -962,11 +965,15 @@ public class RelayService extends XWService
{ {
ByteArrayOutputStream bas = new ByteArrayOutputStream(); ByteArrayOutputStream bas = new ByteArrayOutputStream();
try { try {
String devid = getDevID( null ); DevIDType[] typp = new DevIDType[1];
String devid = getDevID( typp );
if ( null != devid ) { if ( null != devid ) {
DataOutputStream out = new DataOutputStream( bas ); DataOutputStream out = new DataOutputStream( bas );
writeVLIString( out, devid ); writeVLIString( out, devid );
Log.d(TAG, "requestMessagesImpl(): devid: %s; type: " + typp[0], devid );
postPacket( bas, reg ); postPacket( bas, reg );
} else {
Log.d(TAG, "requestMessagesImpl(): devid is null" );
} }
} catch ( java.io.IOException ioe ) { } catch ( java.io.IOException ioe ) {
Log.ex( TAG, ioe ); Log.ex( TAG, ioe );