send the list of saved deleted relayID/seed pairs off to the relay and

remove them from db when relay reports success.  TODO: relay always
reports success even if some are malformed and it's aborted parsing.
This commit is contained in:
Andy2 2010-11-23 17:56:17 -08:00
parent 1720fbbd50
commit 7c8622392d
3 changed files with 107 additions and 28 deletions

View file

@ -36,6 +36,14 @@ public class DBUtils {
private static SQLiteOpenHelper s_dbHelper = null;
public static class Obit {
public Obit( String relayID, int seed ) {
m_relayID = relayID; m_seed = seed;
}
String m_relayID;
int m_seed;
}
public static GameSummary getSummary( Context context, String file )
{
initDB( context );
@ -294,6 +302,60 @@ public class DBUtils {
}
}
public static Obit[] listObits( Context context )
{
Obit[] result = null;
ArrayList<Obit> al = new ArrayList<Obit>();
initDB( context );
synchronized( s_dbHelper ) {
SQLiteDatabase db = s_dbHelper.getReadableDatabase();
String[] columns = { DBHelper.RELAYID, DBHelper.SEED };
Cursor cursor = db.query( DBHelper.TABLE_NAME_OBITS, columns,
null, null, null, null, null );
if ( 0 < cursor.getCount() ) {
cursor.moveToFirst();
for ( ; ; ) {
int index = cursor.getColumnIndex( DBHelper.RELAYID );
String relayID = cursor.getString( index );
index = cursor.getColumnIndex( DBHelper.SEED );
int seed = cursor.getInt( index );
al.add( new Obit( relayID, seed ) );
if ( cursor.isLast() ) {
break;
}
cursor.moveToNext();
}
}
cursor.close();
db.close();
}
int siz = al.size();
if ( siz > 0 ) {
result = al.toArray( new Obit[siz] );
}
return result;
}
public static void clearObits( Context context, Obit[] obits )
{
String fmt = DBHelper.RELAYID + "= \"%s\" AND + "
+ DBHelper.SEED + " = %d";
initDB( context );
synchronized( s_dbHelper ) {
SQLiteDatabase db = s_dbHelper.getWritableDatabase();
for ( Obit obit: obits ) {
String selection = String.format( fmt, obit.m_relayID,
obit.m_seed );
db.delete( DBHelper.TABLE_NAME_OBITS, selection, null );
}
db.close();
}
}
private static void initDB( Context context )
{
if ( null == s_dbHelper ) {

View file

@ -476,7 +476,7 @@ public class GameUtils {
GameSummary summary = DBUtils.getSummary( context, path );
if ( null != summary.relayID ) {
DBUtils.addDeceased( context, summary.relayID, summary.seed );
// NetUtils.informOfDeath( context, summary.relayID, summary.seed );
NetUtils.informOfDeaths( context );
}
}

View file

@ -65,44 +65,61 @@ public class NetUtils {
}
private static class InformThread extends Thread {
Context m_context;
private Socket m_socket;
private String m_relayID;
private int m_seed;
public InformThread( Socket socket, String relayID, int seed )
public InformThread( Context context, Socket socket )
{
m_context = context;
m_socket = socket;
m_relayID = relayID;
m_seed = seed;
}
public void run() {
try {
DataOutputStream outStream =
new DataOutputStream( m_socket.getOutputStream() );
outStream.writeShort( 2 + 2 + 2 + m_relayID.length() + 1 );
outStream.writeByte( NetUtils.PROTOCOL_VERSION );
outStream.writeByte( NetUtils.PRX_DEVICE_GONE );
outStream.writeShort( 1 ); // only one id for now
outStream.writeShort( m_seed );
outStream.writeBytes( m_relayID );
outStream.write( '\n' );
outStream.flush();
DataInputStream dis =
new DataInputStream( m_socket.getInputStream() );
short resLen = dis.readShort();
m_socket.close();
} catch ( java.io.IOException ioe ) {
Utils.logf( ioe.toString() );
public void run() {
DBUtils.Obit[] obits = DBUtils.listObits( m_context );
if ( null != obits && 0 < obits.length ) {
int strLens = 0;
for ( int ii = 0; ii < obits.length; ++ii ) {
strLens += obits[ii].m_relayID.length() + 1; // 1 for /n
}
try {
DataOutputStream outStream =
new DataOutputStream( m_socket.getOutputStream() );
outStream.writeShort( 2 + 2 + (2*obits.length) + strLens );
outStream.writeByte( NetUtils.PROTOCOL_VERSION );
outStream.writeByte( NetUtils.PRX_DEVICE_GONE );
outStream.writeShort( obits.length );
for ( int ii = 0; ii < obits.length; ++ii ) {
outStream.writeShort( obits[ii].m_seed );
outStream.writeBytes( obits[ii].m_relayID );
outStream.write( '\n' );
}
outStream.flush();
DataInputStream dis =
new DataInputStream( m_socket.getInputStream() );
short resLen = dis.readShort();
m_socket.close();
if ( resLen == 0 ) {
DBUtils.clearObits( m_context, obits );
}
} catch ( java.io.IOException ioe ) {
Utils.logf( ioe.toString() );
}
}
}
}
public static void informOfDeath( Context context, String relayID,
int seed )
public static void informOfDeaths( Context context )
{
Socket socket = MakeProxySocket( context, 10000 );
InformThread thread = new InformThread( socket, relayID, seed );
thread.start();
if ( null != socket ) {
InformThread thread = new InformThread( context, socket );
thread.start();
}
}
public static String[] QueryRelay( Context context )