new class to create socket for talking to proxy

This commit is contained in:
Andy2 2010-09-28 06:47:21 -07:00
parent 39827ec894
commit 89d20578b8
2 changed files with 68 additions and 16 deletions

View file

@ -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;
}
}

View file

@ -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 {
ArrayList<byte[]>ids = 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();