device now passes list of "relayID"s, consisting of connName plus

device id, to relay, one for each stored game that's communicating via
the relay.  Relay parses out each relayID.  Next relay can use these
to look up whether messages are available and reply with that, and
device can put up a notification.
This commit is contained in:
Andy2 2010-08-22 12:16:57 -07:00
parent 56b6a425e2
commit a34ccb424f
8 changed files with 150 additions and 25 deletions

View file

@ -972,6 +972,13 @@ Java_org_eehouse_android_xw4_jni_XwJNI_game_1summarize
"org/eehouse/android/xw4/jni/" "org/eehouse/android/xw4/jni/"
"CommsAddrRec$CommsConnType" ); "CommsAddrRec$CommsConnType" );
if ( COMMS_CONN_RELAY == addr.conType ) { if ( COMMS_CONN_RELAY == addr.conType ) {
XP_U8 buf[128];
XP_U16 len = VSIZE(buf);
if ( comms_getRelayID( state->game.comms, buf, &len ) ) {
jbyteArray barr = makeByteArray( env, len, (jbyte*)buf );
setObject( env, jsummary, "relayID", "[B", barr );
(*env)->DeleteLocalRef( env, barr );
}
setString( env, jsummary, "roomName", addr.u.ip_relay.invite ); setString( env, jsummary, "roomName", addr.u.ip_relay.invite );
} else if ( COMMS_CONN_SMS == addr.conType ) { } else if ( COMMS_CONN_SMS == addr.conType ) {
setString( env, jsummary, "smsPhone", addr.u.sms.phone ); setString( env, jsummary, "smsPhone", addr.u.sms.phone );

View file

@ -41,6 +41,7 @@ public class DBHelper extends SQLiteOpenHelper {
public static final String SNAPSHOT = "SNAPSHOT"; public static final String SNAPSHOT = "SNAPSHOT";
public static final String CONTYPE = "CONTYPE"; public static final String CONTYPE = "CONTYPE";
public static final String ROOMNAME = "ROOMNAME"; public static final String ROOMNAME = "ROOMNAME";
public static final String RELAYID = "RELAYID";
public static final String SMSPHONE = "SMSPHONE"; public static final String SMSPHONE = "SMSPHONE";
// not used yet // not used yet
public static final String CREATE_TIME = "CREATE_TIME"; public static final String CREATE_TIME = "CREATE_TIME";
@ -63,6 +64,7 @@ public class DBHelper extends SQLiteOpenHelper {
+ CONTYPE + " INTEGER," + CONTYPE + " INTEGER,"
+ ROOMNAME + " TEXT," + ROOMNAME + " TEXT,"
+ RELAYID + " BLOB,"
+ SMSPHONE + " TEXT," + SMSPHONE + " TEXT,"
+ SCORES + " TEXT," + SCORES + " TEXT,"
+ GAMEID + " INTEGER," + GAMEID + " INTEGER,"

View file

@ -44,7 +44,8 @@ public class DBUtils {
SQLiteDatabase db = s_dbHelper.getReadableDatabase(); SQLiteDatabase db = s_dbHelper.getReadableDatabase();
String[] columns = { DBHelper.NUM_MOVES, DBHelper.GAME_OVER, String[] columns = { DBHelper.NUM_MOVES, DBHelper.GAME_OVER,
DBHelper.CONTYPE, DBHelper.ROOMNAME, DBHelper.CONTYPE, DBHelper.ROOMNAME,
DBHelper.SMSPHONE, DBHelper.SCORES DBHelper.RELAYID, DBHelper.SMSPHONE,
DBHelper.SCORES
}; };
String selection = DBHelper.FILE_NAME + "=\"" + file + "\""; String selection = DBHelper.FILE_NAME + "=\"" + file + "\"";
@ -79,6 +80,10 @@ public class DBUtils {
if ( col >= 0 ) { if ( col >= 0 ) {
summary.roomName = cursor.getString( col ); summary.roomName = cursor.getString( col );
} }
col = cursor.getColumnIndex( DBHelper.RELAYID );
if ( col >= 0 ) {
summary.relayID = cursor.getBlob( col );
}
col = cursor.getColumnIndex( DBHelper.SMSPHONE ); col = cursor.getColumnIndex( DBHelper.SMSPHONE );
if ( col >= 0 ) { if ( col >= 0 ) {
summary.smsPhone = cursor.getString( col ); summary.smsPhone = cursor.getString( col );
@ -116,6 +121,7 @@ public class DBUtils {
if ( null != summary.conType ) { if ( null != summary.conType ) {
values.put( DBHelper.CONTYPE, summary.conType.ordinal() ); values.put( DBHelper.CONTYPE, summary.conType.ordinal() );
values.put( DBHelper.ROOMNAME, summary.roomName ); values.put( DBHelper.ROOMNAME, summary.roomName );
values.put( DBHelper.RELAYID, summary.relayID );
values.put( DBHelper.SMSPHONE, summary.smsPhone ); values.put( DBHelper.SMSPHONE, summary.smsPhone );
} }

View file

@ -31,12 +31,18 @@ import javax.net.SocketFactory;
import java.net.InetAddress; import java.net.InetAddress;
import java.net.Socket; import java.net.Socket;
import java.io.InputStream; import java.io.InputStream;
import java.io.DataInputStream;
import java.io.OutputStream; import java.io.OutputStream;
import java.io.DataOutputStream;
import java.util.ArrayList;
import org.eehouse.android.xw4.jni.GameSummary;
public class RelayService extends Service { public class RelayService extends Service {
private static final String proxy_addr = "10.0.2.2"; private static final String proxy_addr = "10.0.2.2";
private static final int proxy_port = 10998; private static final int proxy_port = 10998;
private static final byte PROTOCOL_VERSION = 0;
private NotificationManager m_nm; private NotificationManager m_nm;
@ -48,16 +54,34 @@ public class RelayService extends Service {
Thread thread = new Thread( null, new Runnable() { Thread thread = new Thread( null, new Runnable() {
public void run() { public void run() {
ArrayList<byte[]>ids = collectIDs();
if ( null != ids ) {
try { try {
SocketFactory factory = SocketFactory.getDefault(); SocketFactory factory = SocketFactory.getDefault();
InetAddress addr = InetAddress.getByName( proxy_addr ); InetAddress addr = InetAddress.getByName( proxy_addr );
Socket socket = factory.createSocket( addr, proxy_port ); Socket socket = factory.createSocket( addr,
proxy_port );
socket.setSoTimeout( 3000 ); socket.setSoTimeout( 3000 );
Utils.logf( "writing to proxy socket" ); DataOutputStream outStream =
OutputStream outStream = socket.getOutputStream(); new DataOutputStream( socket.getOutputStream() );
outStream.write( 0 ); outStream.writeByte( PROTOCOL_VERSION );
InputStream inStream = socket.getInputStream();
int result = inStream.read(); outStream.writeShort( ids.size() );
Utils.logf( "wrote count %d to proxy socket",
ids.size() );
for ( byte[] id : ids ) {
outStream.writeShort( id.length );
Utils.logf( "wrote length %d to proxy socket",
id.length );
outStream.write( id, 0, id.length );
}
outStream.flush();
DataInputStream dis =
new DataInputStream(socket.getInputStream());
Utils.logf( "reading from proxy socket" );
short result = dis.readShort();
socket.close(); socket.close();
Utils.logf( "read %d and closed proxy socket", result ); Utils.logf( "read %d and closed proxy socket", result );
} catch( java.net.UnknownHostException uhe ) { } catch( java.net.UnknownHostException uhe ) {
@ -65,7 +89,7 @@ public class RelayService extends Service {
} catch( java.io.IOException ioe ) { } catch( java.io.IOException ioe ) {
Utils.logf( ioe.toString() ); Utils.logf( ioe.toString() );
} }
}
RelayService.this.stopSelf(); RelayService.this.stopSelf();
} }
}, getClass().getName() ); }, getClass().getName() );
@ -112,5 +136,21 @@ public class RelayService extends Service {
// m_nm.notify( R.string.running_notification, notification ); // m_nm.notify( R.string.running_notification, notification );
// } // }
private ArrayList<byte[]> collectIDs()
{
ArrayList<byte[]> ids = new ArrayList<byte[]>();
String[] games = GameUtils.gamesList( this );
for ( String path : games ) {
Utils.logf( "looking at %s", path );
GameSummary summary = DBUtils.getSummary( this, path );
if ( null != summary.relayID ) {
ids.add( summary.relayID );
Utils.logf( "adding id with length %d", summary.relayID.length );
}
}
return ids;
}
} }

View file

@ -29,6 +29,8 @@ public class GameSummary {
public int[] scores; public int[] scores;
public boolean gameOver; public boolean gameOver;
public CommsAddrRec.CommsConnType conType; public CommsAddrRec.CommsConnType conType;
public String roomName;
public String smsPhone; public String smsPhone;
// relay-related fields
public String roomName;
public byte[] relayID;
} }

View file

@ -726,6 +726,29 @@ comms_setAddr( CommsCtxt* comms, const CommsAddrRec* addr )
} /* comms_setAddr */ } /* comms_setAddr */
#ifdef XWFEATURE_RELAY
XP_Bool
comms_getRelayID( const CommsCtxt* comms, XP_U8* buf, XP_U16* lenp )
{
XP_Bool success = comms->r.connName[0] != 0;
if ( success ) {
XP_U16 len = sizeof( comms->r.connName ) + 16;
XP_UCHAR local[len];
XP_SNPRINTF( local, sizeof(local), "%s/%d",
comms->r.connName, comms->r.myHostID );
XP_U16 strln = XP_STRLEN(local);
success = *lenp >= strln;
if ( success ) {
*lenp = strln;
XP_MEMCPY( buf, local, strln );
XP_LOGF( "%s: keysize=%d", __func__, strln );
}
}
LOG_RETURNF( "%d", success );
return success;
}
#endif
void void
comms_getInitialAddr( CommsAddrRec* addr comms_getInitialAddr( CommsAddrRec* addr
#ifdef XWFEATURE_RELAY #ifdef XWFEATURE_RELAY

View file

@ -162,6 +162,10 @@ XP_Bool comms_checkAddr( DeviceRole role, const CommsAddrRec* addr,
void comms_getAddr( const CommsCtxt* comms, CommsAddrRec* addr ); void comms_getAddr( const CommsCtxt* comms, CommsAddrRec* addr );
void comms_setAddr( CommsCtxt* comms, const CommsAddrRec* addr ); void comms_setAddr( CommsCtxt* comms, const CommsAddrRec* addr );
#ifdef XWFEATURE_RELAY
XP_Bool comms_getRelayID( const CommsCtxt* comms, XP_U8* buf, XP_U16* len );
#endif
CommsConnType comms_getConType( const CommsCtxt* comms ); CommsConnType comms_getConType( const CommsCtxt* comms );
XP_Bool comms_getIsServer( const CommsCtxt* comms ); XP_Bool comms_getIsServer( const CommsCtxt* comms );

View file

@ -76,6 +76,8 @@
#include "lstnrmgr.h" #include "lstnrmgr.h"
static int s_nSpawns = 0; static int s_nSpawns = 0;
#define MAX_PROXY_LEN 64
#define MAX_PROXY_COUNT 48
void void
logf( XW_LogLevel level, const char* format, ... ) logf( XW_LogLevel level, const char* format, ... )
@ -931,14 +933,53 @@ main( int argc, char** argv )
tPool->AddSocket( newSock ); tPool->AddSocket( newSock );
} else { } else {
unsigned char one; unsigned char protocol;
read( newSock, &one, 1 ); ssize_t nread;
logf( XW_LOGERROR, "new socket connected; read %d", one );
one = 1; nread = recv( newSock, &protocol, sizeof(protocol),
write( newSock, &one, 1 ); MSG_WAITALL );
close( newSock ); if ( nread == sizeof(protocol) && (0 == protocol) ) {
logf( XW_LOGERROR, "proxy socket connected; "
"protocol=%d", protocol );
unsigned short count;
nread = recv( newSock, &count, sizeof(count),
MSG_WAITALL );
if ( nread == sizeof( count ) ) {
count = ntohs( count );
logf( XW_LOGERROR, "count=%d", count );
if ( count < MAX_PROXY_COUNT ) {
for ( int ii = 0; ii < count; ++ii ) {
unsigned short len;
nread = recv( newSock, &len, sizeof(len),
MSG_WAITALL );
if ( nread != sizeof(len) ) {
break;
}
len = ntohs( len );
if ( len > MAX_PROXY_LEN ) {
break;
}
logf( XW_LOGERROR, "len=%d", len );
unsigned char buf[MAX_PROXY_LEN+1];
nread = recv( newSock, buf, len,
MSG_WAITALL );
if ( nread == len ) {
buf[len] = '\0';
logf( XW_LOGINFO, "read %s", buf );
} }
/* fake, random result */
unsigned char result = ii % 2 == 0? 1 : 0;
write( newSock, &result, sizeof(result) );
}
}
} else {
logf( XW_LOGERROR, "Read %d bytes instead of %d",
nread, sizeof(count) );
}
}
close( newSock );
}
--retval; --retval;
} }
} }