diff --git a/xwords4/android/XWords4/src/org/eehouse/android/xw4/NetUtils.java b/xwords4/android/XWords4/src/org/eehouse/android/xw4/NetUtils.java new file mode 100644 index 000000000..393e8aea8 --- /dev/null +++ b/xwords4/android/XWords4/src/org/eehouse/android/xw4/NetUtils.java @@ -0,0 +1,60 @@ +/* -*- compile-command: "cd ../../../../../; ant install"; -*- */ +/* + * Copyright 2009-2010 by Eric House (xwords@eehouse.org). All + * rights reserved. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License as + * published by the Free Software Foundation; either version 2 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + */ + +package org.eehouse.android.xw4; + +import javax.net.SocketFactory; +import java.net.InetAddress; +import java.net.Socket; +import android.content.Context; + +import org.eehouse.android.xw4.jni.CommonPrefs; + +public class NetUtils { + + public static final byte PROTOCOL_VERSION = 0; + // from xwrelay.h + public static byte PRX_PUB_ROOMS = 1; + public static byte PRX_HAS_MSGS = 2; + + public static Socket MakeProxySocket( Context context, + int timeoutMillis ) + { + Socket socket = null; + try { + int port = CommonPrefs.getDefaultProxyPort( context ); + String host = CommonPrefs.getDefaultRelayHost( context ); + + SocketFactory factory = SocketFactory.getDefault(); + InetAddress addr = InetAddress.getByName( host ); + socket = factory.createSocket( addr, port ); + socket.setSoTimeout( timeoutMillis ); + + } catch ( java.net.UnknownHostException uhe ) { + Utils.logf( uhe.toString() ); + } catch( java.io.IOException ioe ) { + Utils.logf( ioe.toString() ); + } + Utils.logf( "MakeProxySocket=>%s", null != socket + ? socket.toString():"null" ); + return socket; + } + +} diff --git a/xwords4/android/XWords4/src/org/eehouse/android/xw4/RelayService.java b/xwords4/android/XWords4/src/org/eehouse/android/xw4/RelayService.java index 1d1258920..6d56c180f 100644 --- a/xwords4/android/XWords4/src/org/eehouse/android/xw4/RelayService.java +++ b/xwords4/android/XWords4/src/org/eehouse/android/xw4/RelayService.java @@ -39,14 +39,6 @@ import java.util.ArrayList; import org.eehouse.android.xw4.jni.GameSummary; public class RelayService extends Service { - - private static final String proxy_addr = "10.0.2.2"; - private static final int proxy_port = 10998; - private static final byte PROTOCOL_VERSION = 0; - - // from xwrelay.h - private static byte PRX_PUB_ROOMS = 1; - private static byte PRX_HAS_MSGS = 2; private NotificationManager m_nm; @@ -63,11 +55,9 @@ public class RelayService extends Service { ArrayListids = collectIDs( nBytes ); if ( null != ids && 0 < ids.size() ) { try { - SocketFactory factory = SocketFactory.getDefault(); - InetAddress addr = InetAddress.getByName( proxy_addr ); - Socket socket = factory.createSocket( addr, - proxy_port ); - socket.setSoTimeout( 3000 ); + Socket socket = + NetUtils.MakeProxySocket( RelayService.this, + 3000 ); DataOutputStream outStream = new DataOutputStream( socket.getOutputStream() ); @@ -76,8 +66,8 @@ public class RelayService extends Service { Utils.logf( "total packet size: %d", 2 + nBytes[0] + ids.size() ); - outStream.writeByte( PROTOCOL_VERSION ); - outStream.writeByte( PRX_HAS_MSGS ); + outStream.writeByte( NetUtils.PROTOCOL_VERSION ); + outStream.writeByte( NetUtils.PRX_HAS_MSGS ); // number of ids outStream.writeShort( ids.size() ); @@ -117,7 +107,7 @@ public class RelayService extends Service { ids.get(ii).toString() ); // Toast.makeText( RelayService.this, msg, // Toast.LENGTH_SHORT). - // show(); + // show(); } } } @@ -126,6 +116,8 @@ public class RelayService extends Service { Utils.logf( uhe.toString() ); } catch( java.io.IOException ioe ) { Utils.logf( ioe.toString() ); + } catch( NullPointerException npe ) { + Utils.logf( npe.toString() ); } } RelayService.this.stopSelf();