mirror of
git://xwords.git.sourceforge.net/gitroot/xwords/xwords
synced 2025-01-16 15:41:16 +01:00
generalize relay-msg-check code so can be used from two places.
This commit is contained in:
parent
d1b1e6db14
commit
94b8aedda7
1 changed files with 100 additions and 0 deletions
|
@ -25,6 +25,12 @@ import java.net.InetAddress;
|
|||
import java.net.Socket;
|
||||
import android.content.Context;
|
||||
|
||||
import java.io.InputStream;
|
||||
import java.io.DataInputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.io.DataOutputStream;
|
||||
import java.util.ArrayList;
|
||||
|
||||
import org.eehouse.android.xw4.jni.CommonPrefs;
|
||||
|
||||
public class NetUtils {
|
||||
|
@ -57,4 +63,98 @@ public class NetUtils {
|
|||
return socket;
|
||||
}
|
||||
|
||||
public static String[] QueryRelay( Context context )
|
||||
{
|
||||
String[] result = null;
|
||||
int[] nBytes = new int[1];
|
||||
String[] ids = collectIDs( context, nBytes );
|
||||
if ( null != ids && 0 < ids.length ) {
|
||||
try {
|
||||
Socket socket = MakeProxySocket( context, 8000 );
|
||||
DataOutputStream outStream =
|
||||
new DataOutputStream( socket.getOutputStream() );
|
||||
|
||||
// total packet size
|
||||
outStream.writeShort( 2 + nBytes[0] + ids.length + 1 );
|
||||
Utils.logf( "total packet size: %d",
|
||||
2 + nBytes[0] + ids.length );
|
||||
|
||||
outStream.writeByte( NetUtils.PROTOCOL_VERSION );
|
||||
outStream.writeByte( NetUtils.PRX_HAS_MSGS );
|
||||
|
||||
// number of ids
|
||||
outStream.writeShort( ids.length );
|
||||
Utils.logf( "wrote count %d to proxy socket",
|
||||
ids.length );
|
||||
|
||||
for ( String id : ids ) {
|
||||
outStream.writeBytes( id );
|
||||
outStream.write( '\n' );
|
||||
}
|
||||
outStream.flush();
|
||||
|
||||
DataInputStream dis =
|
||||
new DataInputStream(socket.getInputStream());
|
||||
Utils.logf( "reading from proxy socket" );
|
||||
short resLen = dis.readShort();
|
||||
short nameCount = dis.readShort();
|
||||
short[] msgCounts = null;
|
||||
if ( nameCount == ids.length ) {
|
||||
msgCounts = new short[nameCount];
|
||||
for ( int ii = 0; ii < nameCount; ++ii ) {
|
||||
msgCounts[ii] = dis.readShort();
|
||||
Utils.logf( "msgCounts[%d]=%d", ii,
|
||||
msgCounts[ii] );
|
||||
}
|
||||
}
|
||||
socket.close();
|
||||
Utils.logf( "closed proxy socket" );
|
||||
|
||||
if ( null == msgCounts ) {
|
||||
Utils.logf( "relay has no messages" );
|
||||
} else {
|
||||
ArrayList<String> idsWMsgs =
|
||||
new ArrayList<String>( nameCount );
|
||||
for ( int ii = 0; ii < nameCount; ++ii ) {
|
||||
if ( msgCounts[ii] > 0 ) {
|
||||
String msg =
|
||||
String.format("%d messages for %s",
|
||||
msgCounts[ii],
|
||||
ids[ii] );
|
||||
Utils.logf( msg );
|
||||
DBUtils.setHasMsgs( ids[ii] );
|
||||
idsWMsgs.add( ids[ii] );
|
||||
}
|
||||
}
|
||||
if ( 0 < idsWMsgs.size() ) {
|
||||
ids = new String[idsWMsgs.size()];
|
||||
result = idsWMsgs.toArray( ids );
|
||||
}
|
||||
}
|
||||
|
||||
} catch( java.net.UnknownHostException uhe ) {
|
||||
Utils.logf( uhe.toString() );
|
||||
} catch( java.io.IOException ioe ) {
|
||||
Utils.logf( ioe.toString() );
|
||||
} catch( NullPointerException npe ) {
|
||||
Utils.logf( npe.toString() );
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
private static String[] collectIDs( Context context, int[] nBytes )
|
||||
{
|
||||
String[] ids = DBUtils.getRelayIDNoMsgs( context );
|
||||
int len = 0;
|
||||
if ( null != ids ) {
|
||||
for ( String id : ids ) {
|
||||
Utils.logf( "got relayID: %s", id );
|
||||
len += id.length();
|
||||
}
|
||||
}
|
||||
nBytes[0] = len;
|
||||
return ids;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue