wire in enough comms that java's send proc gets called when device is

a guest.  Networking's next....
This commit is contained in:
eehouse 2010-01-30 14:38:44 +00:00
parent b915319178
commit 7b0f100f27
6 changed files with 83 additions and 20 deletions

View file

@ -27,6 +27,7 @@ local_SRC_FILES += \
xwjni.c \
utilwrapper.c \
drawwrapper.c \
xportwrapper.c \
anddict.c \
andutils.c \

View file

@ -10,6 +10,7 @@ typedef struct _AndGlobals {
CurGameInfo* gi;
DrawCtx* dctx;
XW_UtilCtxt* util;
TransportProcs* xportProcs;
} AndGlobals;
#endif

View file

@ -1,4 +1,4 @@
/* -*-mode: C; compile-command: "cd XWords4; ../scripts/ndkbuild.sh"; -*- */
/* -*-mode: C; compile-command: "../../scripts/ndkbuild.sh"; -*- */
#include <string.h>
#include <sys/time.h>
@ -13,6 +13,7 @@
#include "utilwrapper.h"
#include "drawwrapper.h"
#include "xportwrapper.h"
#include "anddict.h"
#include "andutils.h"
@ -196,10 +197,10 @@ loadCommonPrefs( JNIEnv* env, CommonPrefs* cp, jobject j_cp )
}
static XWStreamCtxt*
and_empty_stream( MPFORMAL AndGlobals* globals )
and_empty_stream( MPFORMAL AndGlobals* globals, void* closure )
{
XWStreamCtxt* stream = mem_stream_make( MPPARM(mpool) globals->vtMgr,
NULL, 0, NULL );
closure, 0, NULL );
return stream;
}
@ -328,12 +329,13 @@ Java_org_eehouse_android_xw4_jni_XwJNI_game_1makeNewGame
globals->util = util;
DrawCtx* dctx = makeDraw( MPPARM(mpool) &state->env, j_draw );
globals->dctx = dctx;
globals->xportProcs = makeXportProcs( MPPARM(mpool) &state->env, j_procs );
CommonPrefs cp;
(void)loadCommonPrefs( env, &cp, j_cp );
XP_LOGF( "calling game_makeNewGame" );
game_makeNewGame( MPPARM(mpool) &state->game, gi, util, dctx, gameID,
&cp, NULL );
&cp, globals->xportProcs );
DictionaryCtxt* dict = makeDict( MPPARM(mpool) env, util, jDictBytes );
#ifdef STUBBED_DICT
@ -364,6 +366,7 @@ JNIEXPORT void JNICALL Java_org_eehouse_android_xw4_jni_XwJNI_game_1dispose
game_dispose( &state->game );
destroyDraw( globals->dctx );
destroyXportProcs( globals->xportProcs );
destroyUtil( globals->util );
vtmgr_destroy( MPPARM(mpool) globals->vtMgr );
@ -386,6 +389,7 @@ Java_org_eehouse_android_xw4_jni_XwJNI_game_1makeFromStream
globals->util = makeUtil( MPPARM(mpool) &state->env, jutil, globals->gi, globals );
DictionaryCtxt* dict = makeDict( MPPARM(mpool) env, globals->util, jdict );
globals->dctx = makeDraw( MPPARM(mpool) &state->env, jdraw );
globals->xportProcs = makeXportProcs( MPPARM(mpool) &state->env, jprocs );
jbyte* jelems = (*env)->GetByteArrayElements( env, jstream, NULL );
XWStreamCtxt* stream = mem_stream_make( MPPARM(mpool) globals->vtMgr,
@ -399,13 +403,14 @@ Java_org_eehouse_android_xw4_jni_XwJNI_game_1makeFromStream
result = game_makeFromStream( MPPARM(mpool) stream, &state->game,
globals->gi, dict,
globals->util, globals->dctx, &cp,
NULL );
globals->xportProcs );
stream_destroy( stream );
if ( result ) {
setJGI( env, jgi, globals->gi );
} else {
destroyDraw( globals->dctx );
destroyXportProcs( globals->xportProcs );
dict_destroy( dict );
destroyUtil( globals->util );
destroyGI( MPPARM(mpool) globals->gi );
@ -717,7 +722,7 @@ Java_org_eehouse_android_xw4_jni_XwJNI_server_1formatDictCounts
{
jstring result;
XWJNI_START();
XWStreamCtxt* stream = and_empty_stream( MPPARM(mpool) globals );
XWStreamCtxt* stream = and_empty_stream( MPPARM(mpool) globals, state );
server_formatDictCounts( state->game.server, stream, nCols );
result = streamToJString( MPPARM(mpool) env, stream );
stream_destroy( stream );
@ -743,7 +748,7 @@ Java_org_eehouse_android_xw4_jni_XwJNI_model_1writeGameHistory
{
jstring result;
XWJNI_START();
XWStreamCtxt* stream = and_empty_stream( MPPARM(mpool) globals );
XWStreamCtxt* stream = and_empty_stream( MPPARM(mpool) globals, state );
model_writeGameHistory( state->game.model, stream, state->game.server,
gameOver );
result = streamToJString( MPPARM(mpool) env, stream );
@ -759,7 +764,7 @@ Java_org_eehouse_android_xw4_jni_XwJNI_server_1writeFinalScores
{
jstring result;
XWJNI_START();
XWStreamCtxt* stream = and_empty_stream( MPPARM(mpool) globals );
XWStreamCtxt* stream = and_empty_stream( MPPARM(mpool) globals, state );
server_writeFinalScores( state->game.server, stream );
result = streamToJString( MPPARM(mpool) env, stream );
(*env)->DeleteLocalRef( env, result );
@ -767,3 +772,37 @@ Java_org_eehouse_android_xw4_jni_XwJNI_server_1writeFinalScores
XWJNI_END();
return result;
}
static void
and_send_on_close( XWStreamCtxt* stream, void* closure )
{
JNIState* state = (JNIState*)closure;
XP_ASSERT( !!state->game.comms );
comms_send( state->game.comms, stream );
}
JNIEXPORT void JNICALL
Java_org_eehouse_android_xw4_jni_XwJNI_server_1initClientConnection
( JNIEnv* env, jclass C, jint gamePtr )
{
LOG_FUNC();
XWJNI_START();
XWStreamCtxt* stream = and_empty_stream( MPPARM(mpool) globals, state );
stream_setOnCloseProc( stream, and_send_on_close );
server_initClientConnection( state->game.server, stream );
XWJNI_END();
LOG_RETURN_VOID();
}
JNIEXPORT void JNICALL
Java_org_eehouse_android_xw4_jni_XwJNI_comms_1start
( JNIEnv* env, jclass C, jint gamePtr )
{
XWJNI_START();
if ( !!state->game.comms ) {
comms_start( state->game.comms );
}
XWJNI_END();
}

View file

@ -10,11 +10,14 @@ import java.util.concurrent.LinkedBlockingQueue;
import android.os.Handler;
import android.os.Message;
import org.eehouse.android.xw4.jni.CurGameInfo.DeviceRole;
public class JNIThread extends Thread {
public enum JNICmd { CMD_NONE,
CMD_DRAW,
CMD_LAYOUT,
CMD_START,
CMD_DO,
CMD_PEN_DOWN,
CMD_PEN_MOVE,
@ -42,6 +45,7 @@ public class JNIThread extends Thread {
private boolean m_stopped = false;
private int m_jniGamePtr;
private CurGameInfo m_gi;
private Handler m_handler;
LinkedBlockingQueue<QueueElem> m_queue;
@ -54,9 +58,10 @@ public class JNIThread extends Thread {
Object[] m_args;
}
public JNIThread( int gamePtr, Handler handler ) {
public JNIThread( int gamePtr, CurGameInfo gi, Handler handler ) {
Utils.logf( "in JNIThread()" );
m_jniGamePtr = gamePtr;
m_gi = gi;
m_handler = handler;
m_queue = new LinkedBlockingQueue<QueueElem>();
@ -135,7 +140,6 @@ public class JNIThread extends Thread {
XwJNI.board_invalAll( m_jniGamePtr );
}
public void run()
{
boolean[] barr = new boolean[1]; // scratch boolean
@ -163,6 +167,14 @@ public class JNIThread extends Thread {
draw = true;
break;
case CMD_START:
XwJNI.comms_start( m_jniGamePtr );
if ( m_gi.serverRole == DeviceRole.SERVER_ISCLIENT ) {
XwJNI.server_initClientConnection( m_jniGamePtr );
}
/* FALLTHRU; works in java? */
draw = XwJNI.server_do( m_jniGamePtr );
break;
case CMD_DO:
draw = XwJNI.server_do( m_jniGamePtr );
break;

View file

@ -2,7 +2,7 @@
package org.eehouse.android.xw4.jni;
public interface TransportProcs {
int transportSend( byte[] buf, int len, final CommsAddrRec addr );
int transportSend( byte[] buf, final CommsAddrRec addr );
public static final int COMMS_RELAYSTATE_UNCONNECTED = 0;
public static final int COMMS_RELAYSTATE_DENIED = 1;

View file

@ -45,7 +45,6 @@ public class XwJNI {
public static native void game_dispose( int gamePtr );
// Board methods
public static native void board_invalAll( int gamePtr );
public static native boolean board_draw( int gamePtr );
public static native void board_setPos( int gamePtr, int left, int top,
@ -58,10 +57,13 @@ public class XwJNI {
public static native void board_setTrayLoc( int gamePtr, int left,
int top, int width,
int height, int minDividerWidth );
public static native boolean board_handlePenDown( int gamePtr, int xx, int yy,
boolean[] handled );
public static native boolean board_handlePenMove( int gamePtr, int xx, int yy );
public static native boolean board_handlePenUp( int gamePtr, int xx, int yy );
public static native boolean board_handlePenDown( int gamePtr,
int xx, int yy,
boolean[] handled );
public static native boolean board_handlePenMove( int gamePtr,
int xx, int yy );
public static native boolean board_handlePenUp( int gamePtr,
int xx, int yy );
public static native boolean board_juggleTray( int gamePtr );
public static native int board_getTrayVisState( int gamePtr );
@ -72,18 +74,26 @@ public class XwJNI {
public static native boolean board_flip( int gamePtr );
public static native boolean board_replaceTiles( int gamePtr );
public static native void board_resetEngine( int gamePtr );
public static native boolean board_requestHint( int gamePtr, boolean useTileLimits,
public static native boolean board_requestHint( int gamePtr,
boolean useTileLimits,
boolean[] workRemains );
public static native boolean board_beginTrade( int gamePtr );
public static native String board_formatRemainingTiles( int gamePtr );
// Model
public static native String model_writeGameHistory( int gamePtr,
boolean gameOver );
// Server
public static native void server_handleUndo( int gamePtr );
public static native boolean server_do( int gamePtr );
public static native String server_formatDictCounts( int gamePtr, int nCols );
public static native boolean server_getGameIsOver( int gamePtr );
public static native String model_writeGameHistory( int gamePtr, boolean gameOver );
public static native String server_writeFinalScores( int gamePtr );
public static native void server_initClientConnection( int gamePtr );
// Comms
public static native void comms_start( int gamePtr );
}