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 58147b339..1d1258920 100644 --- a/xwords4/android/XWords4/src/org/eehouse/android/xw4/RelayService.java +++ b/xwords4/android/XWords4/src/org/eehouse/android/xw4/RelayService.java @@ -43,6 +43,10 @@ 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; @@ -55,7 +59,8 @@ public class RelayService extends Service { Thread thread = new Thread( null, new Runnable() { public void run() { - ArrayListids = collectIDs(); + int[] nBytes = new int[1]; + ArrayListids = collectIDs( nBytes ); if ( null != ids && 0 < ids.size() ) { try { SocketFactory factory = SocketFactory.getDefault(); @@ -65,16 +70,26 @@ public class RelayService extends Service { socket.setSoTimeout( 3000 ); DataOutputStream outStream = 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() ); 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.writeShort( id.length ); + // Utils.logf( "wrote length %d to proxy socket", + // id.length ); outStream.write( id, 0, id.length ); + outStream.write( '\n' ); } outStream.flush(); @@ -82,8 +97,31 @@ public class RelayService extends Service { new DataInputStream(socket.getInputStream()); Utils.logf( "reading from proxy socket" ); 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(); - 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 ) { Utils.logf( uhe.toString() ); } catch( java.io.IOException ioe ) { @@ -136,19 +174,24 @@ public class RelayService extends Service { // m_nm.notify( R.string.running_notification, notification ); // } - private ArrayList collectIDs() + private ArrayList collectIDs( int[] nBytes ) { + nBytes[0] = 0; ArrayList ids = new ArrayList(); 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 ) { + if ( null != summary && null != summary.relayID ) { + Utils.logf( "adding id %s with length %d", summary.relayID, + summary.relayID.length ); 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; }