Preliminary sketch of how cmd callbacks work

This commit is contained in:
Eric House 2023-08-14 08:10:29 -07:00
parent 7f1eec654c
commit cae91d20cb
8 changed files with 123 additions and 8 deletions

View file

@ -243,6 +243,7 @@ OBJ = \
$(BUILD_PLAT_DIR)/relaycon.o \
$(BUILD_PLAT_DIR)/mqttcon.o \
$(BUILD_PLAT_DIR)/lindutil.o \
$(BUILD_PLAT_DIR)/cmdspipe.o \
$(CURSES_OBJS) $(GTK_OBJS) $(MAIN_OBJS)
LIBS = -lm -lpthread -luuid -lcurl -ljson-c $(GPROFFLAG)

42
xwords4/linux/cmdspipe.c Normal file
View file

@ -0,0 +1,42 @@
/* -*- compile-command: "make MEMDEBUG=TRUE -j3"; -*- */
/*
* Copyright 2023 by Eric House (xwords@eehouse.org). All rights reserved.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include "cmdspipe.h"
XP_Bool
cmds_readCmd( CmdBuf* cb, const char* buf )
{
XP_Bool success = XP_TRUE;
XP_MEMSET( cb, 0, sizeof(*cb) );
XP_LOGFF( "got buf: %s", buf );
gchar** strs = g_strsplit ( buf, " ", 100 );
if ( 0 == strcmp( strs[0], "null" ) ) {
cb->cmd = CMD_NONE;
} else if ( 0 == strcmp( strs[0], "quit" ) ) {
cb->cmd = CMD_QUIT;
} else {
XP_ASSERT(0);
success = XP_FALSE;
}
g_strfreev( strs );
return success;
}

35
xwords4/linux/cmdspipe.h Normal file
View file

@ -0,0 +1,35 @@
/* -*- compile-command: "make MEMDEBUG=TRUE -j5"; -*- */
/*
* Copyright 2023 by Eric House (xwords@eehouse.org). All rights reserved.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#ifndef _CMDSPIPE_H_
# define _CMDSPIPE_H_
#include "xptypes.h"
typedef enum { CMD_NONE,
CMD_QUIT,
} Cmd;
typedef struct _CmdBuf {
Cmd cmd;
} CmdBuf;
XP_Bool cmds_readCmd( CmdBuf* cb, const char* buf );
#endif

View file

@ -245,6 +245,14 @@ handleQuit( void* closure, int XP_UNUSED(key) )
return XP_TRUE;
} /* handleQuit */
static void
invokeQuit( void* data )
{
LaunchParams* params = (LaunchParams*)data;
CursesAppGlobals* globals = (CursesAppGlobals*)params->appGlobals;
handleQuit( globals, 0 );
}
static void
figureDims( CursesAppGlobals* aGlobals, cb_dims* dims )
{
@ -1443,6 +1451,8 @@ cursesmain( XP_Bool XP_UNUSED(isServer), LaunchParams* params )
g_globals.cag.params = params;
params->appGlobals = &g_globals;
params->cmdProcs.quit = invokeQuit;
initCurses( &g_globals );
if ( !params->closeStdin ) {
g_globals.menuState = cmenu_init( g_globals.mainWin );

View file

@ -75,6 +75,7 @@
#include "dbgutil.h"
#include "dictiter.h"
#include "gsrcwrap.h"
#include "cmdspipe.h"
/* #include "commgr.h" */
/* #include "compipe.h" */
#include "memstream.h"
@ -2556,7 +2557,7 @@ writeStatus( const char* statusSocket, const char* dbName )
static gboolean
handle_gotcmd( GIOChannel* source, GIOCondition condition,
gpointer XP_UNUSED(data) )
gpointer data )
{
// XP_LOGFF( "got something!!" );
gboolean keep = TRUE;
@ -2567,6 +2568,20 @@ handle_gotcmd( GIOChannel* source, GIOCondition condition,
ssize_t nread = read( sock, buf, sizeof(buf) );
buf[nread] = '\0';
XP_LOGFF( "read: %s", buf );
LaunchParams* params = (LaunchParams*)data;
CmdBuf cb;
cmds_readCmd( &cb, buf );
switch( cb.cmd ) {
case CMD_NONE:
break;
case CMD_QUIT:
(*params->cmdProcs.quit)( params );
break;
default:
XP_ASSERT( 0 );
}
}
if ( 0 != ((G_IO_HUP) & condition) ) {

View file

@ -156,6 +156,10 @@ typedef struct _LaunchParams {
const XP_UCHAR* iterTestPatStr;
#endif
struct {
void (*quit)(void* params);
} cmdProcs;
XP_U16 conTypes;
struct {
XP_U16 inviteeCounts[MAX_NUM_PLAYERS];

View file

@ -121,7 +121,7 @@ enqueue( MQTTConStorage* storage, const char* topic,
} else {
QElem* elem = g_malloc0( sizeof(*elem) );
elem->topic = g_strdup( topic );
elem->buf = g_memdup2( buf, len );
elem->buf = g_memdup( buf, len );
elem->len = len;
storage->queue = g_slist_append( storage->queue, elem );
XP_LOGFF( "added elem; len now %d", g_slist_length(storage->queue) );

View file

@ -1,24 +1,32 @@
#!/usr/bin/python3
import os, subprocess, time
import os, subprocess, threading, time
gFIFO_NAME = '/tmp/fifo'
def launch_thread():
print('launch_thread() called')
args = ['./obj_linux_memdbg/xwords', '--curses', '--cmd-socket-name', gFIFO_NAME ]
prcss = subprocess.Popen(args, stdout = subprocess.DEVNULL)
print('launch_thread() calling communicate()')
prcss.communicate()
print('launch_thread() DONE')
def main():
os.unlink(gFIFO_NAME)
os.mkfifo(gFIFO_NAME)
# mkfifo
# launch app in background
args = ['./obj_linux_memdbg/xwords', '--curses', '--cmd-socket-name', gFIFO_NAME ]
subprocess.Popen(args, stdout = subprocess.DEVNULL)
thrd = threading.Thread( target=launch_thread)
thrd.start()
# Loop writing to fifo
for ii in range(5):
for cmd in ['null', 'null', 'null', 'quit']:
time.sleep(2)
print('calling open')
fifo_out = open( gFIFO_NAME, 'w' )
print('open DONE')
fifo_out.write( 'message {}'.format(ii) )
fifo_out.write( '{}'.format(cmd) )
fifo_out.close()
# Kill app