mirror of
git://xwords.git.sourceforge.net/gitroot/xwords/xwords
synced 2025-02-01 19:57:11 +01:00
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:
parent
bda545fc8e
commit
c7a635285c
6 changed files with 15 additions and 75 deletions
|
@ -90,16 +90,8 @@ public class NetUtils {
|
||||||
m_obits = obits;
|
m_obits = obits;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
public void run()
|
public void run()
|
||||||
{
|
|
||||||
if ( XWPrefs.getPreferWebAPI( m_context ) ) {
|
|
||||||
runViaWeb();
|
|
||||||
} else {
|
|
||||||
runWithProxySocket();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private void runViaWeb()
|
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
JSONArray params = new JSONArray();
|
JSONArray params = new JSONArray();
|
||||||
|
@ -110,69 +102,19 @@ public class NetUtils {
|
||||||
params.put( one );
|
params.put( one );
|
||||||
}
|
}
|
||||||
HttpURLConnection conn = makeHttpRelayConn( m_context, "kill" );
|
HttpURLConnection conn = makeHttpRelayConn( m_context, "kill" );
|
||||||
Log.d( TAG, "runViaWeb(): kill params: %s", params.toString() );
|
|
||||||
String resStr = runConn( conn, params );
|
String resStr = runConn( conn, params );
|
||||||
Log.d( TAG, "runViaWeb(): kill => %s", resStr );
|
Log.d( TAG, "runViaWeb(): kill(%s) => %s", params, resStr );
|
||||||
|
|
||||||
if ( null != resStr ) {
|
if ( null != resStr ) {
|
||||||
JSONObject result = new JSONObject( resStr );
|
JSONObject result = new JSONObject( resStr );
|
||||||
if ( 0 == result.optInt( "err", -1 ) ) {
|
if ( 0 == result.optInt( "err", -1 ) ) {
|
||||||
DBUtils.clearObits( m_context, m_obits );
|
DBUtils.clearObits( m_context, m_obits );
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
Log.e( TAG, "runViaWeb(): KILL => null" );
|
|
||||||
}
|
}
|
||||||
} catch ( JSONException ex ) {
|
} catch ( JSONException ex ) {
|
||||||
Assert.assertFalse( BuildConfig.DEBUG );
|
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 )
|
public static void informOfDeaths( Context context )
|
||||||
|
|
|
@ -640,7 +640,7 @@ public class RelayService extends XWService
|
||||||
}
|
}
|
||||||
|
|
||||||
int sentLen;
|
int sentLen;
|
||||||
if ( XWPrefs.getPreferWebAPI( RelayService.this ) ) {
|
if ( XWPrefs.getSkipToWebAPI( RelayService.this ) ) {
|
||||||
sentLen = sendViaWeb( dataList );
|
sentLen = sendViaWeb( dataList );
|
||||||
} else {
|
} else {
|
||||||
sentLen = sendViaUDP( dataList );
|
sentLen = sendViaUDP( dataList );
|
||||||
|
@ -715,9 +715,7 @@ public class RelayService extends XWService
|
||||||
try {
|
try {
|
||||||
DatagramPacket udpPacket = new DatagramPacket( data, data.length );
|
DatagramPacket udpPacket = new DatagramPacket( data, data.length );
|
||||||
m_UDPSocket.send( udpPacket );
|
m_UDPSocket.send( udpPacket );
|
||||||
if ( BuildConfig.DEBUG ) {
|
|
||||||
Assert.assertFalse( XWPrefs.getPreferWebAPI( this ) );
|
|
||||||
}
|
|
||||||
sentLen += udpPacket.getLength();
|
sentLen += udpPacket.getLength();
|
||||||
noteSent( packet );
|
noteSent( packet );
|
||||||
getOut = false;
|
getOut = false;
|
||||||
|
@ -746,7 +744,7 @@ public class RelayService extends XWService
|
||||||
{
|
{
|
||||||
int pid = packet.m_packetID;
|
int pid = packet.m_packetID;
|
||||||
Log.d( TAG, "Sent [udp?] packet: cmd=%s, id=%d",
|
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 ) {
|
if ( packet.m_cmd != XWRelayReg.XWPDEV_ACK ) {
|
||||||
synchronized( s_packetsSent ) {
|
synchronized( s_packetsSent ) {
|
||||||
s_packetsSent.put( pid, packet );
|
s_packetsSent.put( pid, packet );
|
||||||
|
@ -1188,7 +1186,7 @@ public class RelayService extends XWService
|
||||||
@Override
|
@Override
|
||||||
protected Void doInBackground( Void... ignored )
|
protected Void doInBackground( Void... ignored )
|
||||||
{
|
{
|
||||||
Assert.assertFalse( XWPrefs.getPreferWebAPI( m_context ) );
|
Assert.assertFalse( XWPrefs.getSkipToWebAPI( m_context ) );
|
||||||
// format: total msg lenth: 2
|
// format: total msg lenth: 2
|
||||||
// number-of-relayIDs: 2
|
// number-of-relayIDs: 2
|
||||||
// for-each-relayid: relayid + '\n': varies
|
// 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
|
// Now open a real socket, write size and proto, and
|
||||||
// copy in the formatted buffer
|
// copy in the formatted buffer
|
||||||
|
|
||||||
Assert.assertFalse( XWPrefs.getPreferWebAPI( m_context ) );
|
Assert.assertFalse( XWPrefs.getSkipToWebAPI( m_context ) );
|
||||||
Socket socket = NetUtils.makeProxySocket( m_context, 8000 );
|
Socket socket = NetUtils.makeProxySocket( m_context, 8000 );
|
||||||
if ( null != socket ) {
|
if ( null != socket ) {
|
||||||
DataOutputStream outStream =
|
DataOutputStream outStream =
|
||||||
|
|
|
@ -120,9 +120,9 @@ public class XWPrefs {
|
||||||
return getPrefsString( context, R.string.key_relay_url );
|
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 )
|
public static int getDefaultProxyPort( Context context )
|
||||||
|
|
|
@ -36,7 +36,7 @@
|
||||||
|
|
||||||
<string name="key_relay_host">key_relay_host</string>
|
<string name="key_relay_host">key_relay_host</string>
|
||||||
<string name="key_relay_port">key_relay_port2</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_update_url">key_update_url</string>
|
||||||
<string name="key_relay_url">key_relay_url</string>
|
<string name="key_relay_url">key_relay_url</string>
|
||||||
<string name="key_update_prerel">key_update_prerel</string>
|
<string name="key_update_prerel">key_update_prerel</string>
|
||||||
|
|
|
@ -2486,8 +2486,8 @@
|
||||||
<string name="advanced">For debugging</string>
|
<string name="advanced">For debugging</string>
|
||||||
<string name="advanced_summary">You should never need these...</string>
|
<string name="advanced_summary">You should never need these...</string>
|
||||||
<string name="relay_host">Relay host</string>
|
<string name="relay_host">Relay host</string>
|
||||||
<string name="relay_via_http">Use Web APIs</string>
|
<string name="relay_via_http_first">Use Web APIs first</string>
|
||||||
<string name="relay_via_http_summary">(instead of custom protocol to ports below)</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="dict_host">Wordlist download URL</string>
|
||||||
<string name="logging_on">Enable logging</string>
|
<string name="logging_on">Enable logging</string>
|
||||||
<string name="logging_on_summary">(release builds only)</string>
|
<string name="logging_on_summary">(release builds only)</string>
|
||||||
|
|
|
@ -426,9 +426,9 @@
|
||||||
android:defaultValue="@string/default_host"
|
android:defaultValue="@string/default_host"
|
||||||
/>
|
/>
|
||||||
|
|
||||||
<CheckBoxPreference android:key="@string/key_relay_via_http"
|
<CheckBoxPreference android:key="@string/key_relay_via_http_first"
|
||||||
android:title="@string/relay_via_http"
|
android:title="@string/relay_via_http_first"
|
||||||
android:summary="@string/relay_via_http_summary"
|
android:summary="@string/relay_via_http_first_summary"
|
||||||
android:defaultValue="false"
|
android:defaultValue="false"
|
||||||
/>
|
/>
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue