change text and names around native-vs-web choice

The plan's to use the native relay protocol first, then to fall back to
the slower but more reliable (esp. on paranoid/block-everything wifi
networks) webAPI. This is the name change without behavior
change (except that the native kill() to report deleted games is gone.)
This commit is contained in:
Eric House 2017-12-14 06:47:03 -08:00
parent bda545fc8e
commit c7a635285c
6 changed files with 15 additions and 75 deletions

View file

@ -90,16 +90,8 @@ public class NetUtils {
m_obits = obits;
}
@Override
public void run()
{
if ( XWPrefs.getPreferWebAPI( m_context ) ) {
runViaWeb();
} else {
runWithProxySocket();
}
}
private void runViaWeb()
{
try {
JSONArray params = new JSONArray();
@ -110,69 +102,19 @@ public class NetUtils {
params.put( one );
}
HttpURLConnection conn = makeHttpRelayConn( m_context, "kill" );
Log.d( TAG, "runViaWeb(): kill params: %s", params.toString() );
String resStr = runConn( conn, params );
Log.d( TAG, "runViaWeb(): kill => %s", resStr );
Log.d( TAG, "runViaWeb(): kill(%s) => %s", params, resStr );
if ( null != resStr ) {
JSONObject result = new JSONObject( resStr );
if ( 0 == result.optInt( "err", -1 ) ) {
DBUtils.clearObits( m_context, m_obits );
}
} else {
Log.e( TAG, "runViaWeb(): KILL => null" );
}
} catch ( JSONException ex ) {
Assert.assertFalse( BuildConfig.DEBUG );
}
}
private void runWithProxySocket()
{
Socket socket = makeProxySocket( m_context, 10000 );
if ( null != socket ) {
int strLens = 0;
int nObits = 0;
for ( int ii = 0; ii < m_obits.length; ++ii ) {
String relayID = m_obits[ii].m_relayID;
if ( null != relayID ) {
++nObits;
strLens += relayID.length() + 1; // 1 for /n
}
}
try {
DataOutputStream outStream =
new DataOutputStream( socket.getOutputStream() );
outStream.writeShort( 2 + 2 + (2*nObits) + strLens );
outStream.writeByte( NetUtils.PROTOCOL_VERSION );
outStream.writeByte( NetUtils.PRX_DEVICE_GONE );
outStream.writeShort( m_obits.length );
for ( int ii = 0; ii < m_obits.length; ++ii ) {
String relayID = m_obits[ii].m_relayID;
if ( null != relayID ) {
outStream.writeShort( m_obits[ii].m_seed );
outStream.writeBytes( relayID );
outStream.write( '\n' );
}
}
outStream.flush();
DataInputStream dis =
new DataInputStream( socket.getInputStream() );
short resLen = dis.readShort();
socket.close();
if ( resLen == 0 ) {
DBUtils.clearObits( m_context, m_obits );
}
} catch ( java.io.IOException ioe ) {
Log.ex( TAG, ioe );
}
}
}
}
public static void informOfDeaths( Context context )

View file

@ -640,7 +640,7 @@ public class RelayService extends XWService
}
int sentLen;
if ( XWPrefs.getPreferWebAPI( RelayService.this ) ) {
if ( XWPrefs.getSkipToWebAPI( RelayService.this ) ) {
sentLen = sendViaWeb( dataList );
} else {
sentLen = sendViaUDP( dataList );
@ -715,9 +715,7 @@ public class RelayService extends XWService
try {
DatagramPacket udpPacket = new DatagramPacket( data, data.length );
m_UDPSocket.send( udpPacket );
if ( BuildConfig.DEBUG ) {
Assert.assertFalse( XWPrefs.getPreferWebAPI( this ) );
}
sentLen += udpPacket.getLength();
noteSent( packet );
getOut = false;
@ -746,7 +744,7 @@ public class RelayService extends XWService
{
int pid = packet.m_packetID;
Log.d( TAG, "Sent [udp?] packet: cmd=%s, id=%d",
packet.m_cmd.toString(), pid);
packet.m_cmd.toString(), pid );
if ( packet.m_cmd != XWRelayReg.XWPDEV_ACK ) {
synchronized( s_packetsSent ) {
s_packetsSent.put( pid, packet );
@ -1188,7 +1186,7 @@ public class RelayService extends XWService
@Override
protected Void doInBackground( Void... ignored )
{
Assert.assertFalse( XWPrefs.getPreferWebAPI( m_context ) );
Assert.assertFalse( XWPrefs.getSkipToWebAPI( m_context ) );
// format: total msg lenth: 2
// number-of-relayIDs: 2
// for-each-relayid: relayid + '\n': varies
@ -1237,7 +1235,7 @@ public class RelayService extends XWService
// Now open a real socket, write size and proto, and
// copy in the formatted buffer
Assert.assertFalse( XWPrefs.getPreferWebAPI( m_context ) );
Assert.assertFalse( XWPrefs.getSkipToWebAPI( m_context ) );
Socket socket = NetUtils.makeProxySocket( m_context, 8000 );
if ( null != socket ) {
DataOutputStream outStream =

View file

@ -120,9 +120,9 @@ public class XWPrefs {
return getPrefsString( context, R.string.key_relay_url );
}
public static boolean getPreferWebAPI( Context context )
public static boolean getSkipToWebAPI( Context context )
{
return getPrefsBoolean( context, R.string.key_relay_via_http, false );
return getPrefsBoolean( context, R.string.key_relay_via_http_first, false );
}
public static int getDefaultProxyPort( Context context )

View file

@ -36,7 +36,7 @@
<string name="key_relay_host">key_relay_host</string>
<string name="key_relay_port">key_relay_port2</string>
<string name="key_relay_via_http">key_relay_via_http</string>
<string name="key_relay_via_http_first">key_relay_via_http_first</string>
<string name="key_update_url">key_update_url</string>
<string name="key_relay_url">key_relay_url</string>
<string name="key_update_prerel">key_update_prerel</string>

View file

@ -2486,8 +2486,8 @@
<string name="advanced">For debugging</string>
<string name="advanced_summary">You should never need these...</string>
<string name="relay_host">Relay host</string>
<string name="relay_via_http">Use Web APIs</string>
<string name="relay_via_http_summary">(instead of custom protocol to ports below)</string>
<string name="relay_via_http_first">Use Web APIs first</string>
<string name="relay_via_http_first_summary">(instead of as fallback for custom protocol)</string>
<string name="dict_host">Wordlist download URL</string>
<string name="logging_on">Enable logging</string>
<string name="logging_on_summary">(release builds only)</string>

View file

@ -426,9 +426,9 @@
android:defaultValue="@string/default_host"
/>
<CheckBoxPreference android:key="@string/key_relay_via_http"
android:title="@string/relay_via_http"
android:summary="@string/relay_via_http_summary"
<CheckBoxPreference android:key="@string/key_relay_via_http_first"
android:title="@string/relay_via_http_first"
android:summary="@string/relay_via_http_first_summary"
android:defaultValue="false"
/>