parse reply to query for number of pending messages for games on

device.  Not doing anything with the info yet.
This commit is contained in:
Andy2 2010-09-26 20:25:00 -07:00
parent 14edbd7f18
commit 986ac4413f

View file

@ -43,6 +43,10 @@ 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 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; private NotificationManager m_nm;
@ -55,7 +59,8 @@ 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(); int[] nBytes = new int[1];
ArrayList<byte[]>ids = collectIDs( nBytes );
if ( null != ids && 0 < ids.size() ) { if ( null != ids && 0 < ids.size() ) {
try { try {
SocketFactory factory = SocketFactory.getDefault(); SocketFactory factory = SocketFactory.getDefault();
@ -65,16 +70,26 @@ public class RelayService extends Service {
socket.setSoTimeout( 3000 ); socket.setSoTimeout( 3000 );
DataOutputStream outStream = DataOutputStream outStream =
new DataOutputStream( socket.getOutputStream() ); new DataOutputStream( socket.getOutputStream() );
outStream.writeByte( PROTOCOL_VERSION );
// total packet size
outStream.writeShort( 2 + nBytes[0] + ids.size() + 1 );
Utils.logf( "total packet size: %d",
2 + nBytes[0] + ids.size() );
outStream.writeByte( PROTOCOL_VERSION );
outStream.writeByte( PRX_HAS_MSGS );
// number of ids
outStream.writeShort( ids.size() ); outStream.writeShort( ids.size() );
Utils.logf( "wrote count %d to proxy socket", Utils.logf( "wrote count %d to proxy socket",
ids.size() ); ids.size() );
for ( byte[] id : ids ) { for ( byte[] id : ids ) {
outStream.writeShort( id.length ); // outStream.writeShort( id.length );
Utils.logf( "wrote length %d to proxy socket", // Utils.logf( "wrote length %d to proxy socket",
id.length ); // id.length );
outStream.write( id, 0, id.length ); outStream.write( id, 0, id.length );
outStream.write( '\n' );
} }
outStream.flush(); outStream.flush();
@ -82,8 +97,31 @@ public class RelayService extends Service {
new DataInputStream(socket.getInputStream()); new DataInputStream(socket.getInputStream());
Utils.logf( "reading from proxy socket" ); Utils.logf( "reading from proxy socket" );
short result = dis.readShort(); short result = dis.readShort();
short nameCount = dis.readShort();
short[] msgCounts = null;
if ( nameCount == ids.size() ) {
msgCounts = new short[nameCount];
for ( int ii = 0; ii < nameCount; ++ii ) {
msgCounts[ii] = dis.readShort();
}
}
socket.close(); socket.close();
Utils.logf( "read %d and closed proxy socket", result ); Utils.logf( "closed proxy socket" );
if ( null != msgCounts ) {
for ( int ii = 0; ii < nameCount; ++ii ) {
if ( msgCounts[ii] > 0 ) {
String msg =
String.format("%d messages for %s",
msgCounts[ii],
ids.get(ii).toString() );
// Toast.makeText( RelayService.this, msg,
// Toast.LENGTH_SHORT).
// show();
}
}
}
} catch( java.net.UnknownHostException uhe ) { } catch( java.net.UnknownHostException uhe ) {
Utils.logf( uhe.toString() ); Utils.logf( uhe.toString() );
} catch( java.io.IOException ioe ) { } catch( java.io.IOException ioe ) {
@ -136,19 +174,24 @@ 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() private ArrayList<byte[]> collectIDs( int[] nBytes )
{ {
nBytes[0] = 0;
ArrayList<byte[]> ids = new ArrayList<byte[]>(); ArrayList<byte[]> ids = new ArrayList<byte[]>();
String[] games = GameUtils.gamesList( this ); String[] games = GameUtils.gamesList( this );
for ( String path : games ) { for ( String path : games ) {
Utils.logf( "looking at %s", path ); Utils.logf( "looking at %s", path );
GameSummary summary = DBUtils.getSummary( this, path ); GameSummary summary = DBUtils.getSummary( this, path );
if ( null != summary.relayID ) { if ( null != summary && null != summary.relayID ) {
Utils.logf( "adding id %s with length %d", summary.relayID,
summary.relayID.length );
ids.add( summary.relayID ); ids.add( summary.relayID );
Utils.logf( "adding id with length %d", summary.relayID.length ); nBytes[0] += summary.relayID.length;
} else {
Utils.logf( "no summary" );
} }
} }
return ids; return ids;
} }