[Tux4kids-commits] r1537 - in tuxmath/trunk: . src
David Bruce
dbruce-guest at alioth.debian.org
Sun Sep 13 20:19:08 UTC 2009
Author: dbruce-guest
Date: 2009-09-13 20:19:08 +0000 (Sun, 13 Sep 2009)
New Revision: 1537
Modified:
tuxmath/trunk/configure.ac
tuxmath/trunk/src/menu.c
tuxmath/trunk/src/server.c
tuxmath/trunk/src/server.h
Log:
Implementation of pthreads-based launcher for tuxmathserver from within tuxmath
Modified: tuxmath/trunk/configure.ac
===================================================================
--- tuxmath/trunk/configure.ac 2009-09-13 16:54:45 UTC (rev 1536)
+++ tuxmath/trunk/configure.ac 2009-09-13 20:19:08 UTC (rev 1537)
@@ -187,7 +187,7 @@
AC_FUNC_ALLOCA
AC_HEADER_DIRENT
AC_HEADER_STDC
-AC_CHECK_HEADERS([argz.h error.h errno.h fcntl.h float.h iconv.h inttypes.h langinfo.h libgen.h libintl.h limits.h locale.h malloc.h math.h stddef.h stdint.h stdio_ext.h stdlib.h string.h strings.h sys/param.h unistd.h wchar.h])
+AC_CHECK_HEADERS([argz.h error.h errno.h fcntl.h float.h iconv.h inttypes.h langinfo.h libgen.h libintl.h limits.h locale.h malloc.h math.h pthread.h stddef.h stdint.h stdio_ext.h stdlib.h string.h strings.h sys/param.h unistd.h wchar.h])
# --------------------------------------------------------------------------------------------
Modified: tuxmath/trunk/src/menu.c
===================================================================
--- tuxmath/trunk/src/menu.c 2009-09-13 16:54:45 UTC (rev 1536)
+++ tuxmath/trunk/src/menu.c 2009-09-13 20:19:08 UTC (rev 1537)
@@ -582,18 +582,21 @@
char* argv[3];
NameEntry(server_name, _("Enter Server Name:"), _("(limit 50 characters)"));
-printf("About to do argv assignments:\n");
argv[0] = "tuxmathserver";
-printf("argv[0] d0ne\n");
argv[1] = "--name";
-// argv[2] = server_name;
snprintf(buf, 256, "\"%s\"", server_name);
-printf("snprintf done\n");
argv[2] = buf;
+
+#ifdef HAVE_PTHREAD_H
+ /* If we have POSIX threads available (Linux), we launch server in a thread within */
+ /* our same process. The server will use the currently selected Mathcards settings */
+ RunServer_pthread(3, argv);
+#else
+ /* Without pthreads, we just launch standalone server, which for now will have */
+ /* hardcoded default settings. */
RunServer_prog(3, argv);
+#endif
-// snprintf(buf, 256, "tuxmathserver --name \"%s\" &", server_name);
-// system(buf);
#endif
return 0;
}
Modified: tuxmath/trunk/src/server.c
===================================================================
--- tuxmath/trunk/src/server.c 2009-09-13 16:54:45 UTC (rev 1536)
+++ tuxmath/trunk/src/server.c 2009-09-13 20:19:08 UTC (rev 1537)
@@ -17,6 +17,10 @@
*/
#include "globals.h"
+#include "server.h"
+#include "transtruct.h"
+#include "mathcards.h"
+#include "throttle.h"
#include <stdio.h>
#include <stdlib.h>
@@ -25,22 +29,22 @@
#include <sys/types.h>
#include <unistd.h>
-#include "server.h"
-#include "transtruct.h"
-#include "mathcards.h"
-#include "throttle.h"
+#ifdef HAVE_PTHREAD_H
+#include <pthread.h>
+#endif
#define MAX_CLIENTS 16
+#define MAX_ARGS 16
-
/* ----------- Local function prototypes: ------------ */
// setup and cleanup:
int setup_server(void);
void cleanup_server(void);
void server_handle_command_args(int argc, char* argv[]);
+void* run_server_local_args(void);
// top level functions in main loop:
void check_UDP(void);
@@ -95,6 +99,8 @@
static int game_in_progress = 0;
static int quit = 0;
MC_FlashCard flash;
+int local_argc;
+char* local_argv[MAX_ARGS];
@@ -149,30 +155,56 @@
}
-int RunServer_prog(int argc, char** argv)
+int RunServer_prog(int argc, char* argv[])
{
char buf[256];
int i;
-printf("Enter RunServer_prog\n");
+
/* Construct command-line argument string from argc and argv: */
/* NOTE this is not safe from buffer overflow - do */
/* not use with user-supplied arguments. */
snprintf(buf, 256, "tuxmathserver ");
for(i = 1; i < argc; i++)
{
-printf("argv[%d] is: %s\n", i, argv[i]);
strncat(buf, argv[i], 256);
strncat(buf, " ", 256);
}
/* Add '&' to make it non-blocking: */
strncat(buf, "&", 256);
-printf("About to call system(%s)\n", buf);
return system(buf);
}
+int RunServer_pthread(int argc, char* argv[])
+{
+ pthread_t server_thread;
+ int i;
+ /* We can only pass a single arg into the new thread, but it shares */
+ /* the same address space, so we save argc and argv locally instead: */
+ local_argc = argc;
+ for(i = 0; i < argc && i < MAX_ARGS; i++)
+ {
+ local_argv[i] = argv[i];
+ }
+
+ if(pthread_create(&server_thread, NULL, run_server_local_args, NULL))
+ {
+ printf("Error creating thread\n");
+ return -1;
+ }
+ return 0;
+}
+
+
+void* run_server_local_args(void)
+{
+ RunServer(local_argc, local_argv);
+ pthread_exit(NULL);
+ return NULL;
+}
+
/*********************************************************************/
/* "Private" (to server.c) functions */
/*********************************************************************/
Modified: tuxmath/trunk/src/server.h
===================================================================
--- tuxmath/trunk/src/server.h 2009-09-13 16:54:45 UTC (rev 1536)
+++ tuxmath/trunk/src/server.h 2009-09-13 20:19:08 UTC (rev 1537)
@@ -50,10 +50,15 @@
/* Ways to run the server - all accept command-line style arguments: */
-/* 1. Type "tuxmathserver" at command line tp rim as standalone program. */
+/* 1. Type "tuxmathserver" at command line to run as standalone program. */
+/* From within Tuxmath: */
+
+#ifdef HAVE_PTHREAD_H
/* 2. Using POSIX threads library (RECOMMENDED if phtreads available on your system): */
-int RunServer_pthreads(int argc, char* argv[]);
+int RunServer_pthread(int argc, char* argv[]);
+#endif
+
/* 3. As a standalone program using system() - same as "tuxmathserver" at console: */
int RunServer_prog(int argc, char* argv[]);
/* 4. Using old-school Unix fork() call: */
More information about the Tux4kids-commits
mailing list