[Tux4kids-commits] r1536 - tuxmath/trunk/src

David Bruce dbruce-guest at alioth.debian.org
Sun Sep 13 16:54:45 UTC 2009


Author: dbruce-guest
Date: 2009-09-13 16:54:45 +0000 (Sun, 13 Sep 2009)
New Revision: 1536

Modified:
   tuxmath/trunk/src/menu.c
   tuxmath/trunk/src/server.c
   tuxmath/trunk/src/server.h
Log:
Implementation of tuxmathserver launcher using C library's system() call.



Modified: tuxmath/trunk/src/menu.c
===================================================================
--- tuxmath/trunk/src/menu.c	2009-09-13 00:02:59 UTC (rev 1535)
+++ tuxmath/trunk/src/menu.c	2009-09-13 16:54:45 UTC (rev 1536)
@@ -579,10 +579,21 @@
 #ifdef HAVE_LIBSDL_NET
   char buf[256];
   char server_name[150];
+  char* argv[3];
 
   NameEntry(server_name, _("Enter Server Name:"), _("(limit 50 characters)"));
-  snprintf(buf, 256, "tuxmathserver --name \"%s\" &", server_name);
-  system(buf);
+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;
+  RunServer_prog(3, argv);
+
+//  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 00:02:59 UTC (rev 1535)
+++ tuxmath/trunk/src/server.c	2009-09-13 16:54:45 UTC (rev 1536)
@@ -92,10 +92,8 @@
 SDLNet_SocketSet client_set = NULL, temp_set = NULL;
 static client_type client[MAX_CLIENTS];
 static int num_clients = 0;
-static int numready = 0;
 static int game_in_progress = 0;
 static int quit = 0;
-static int frame = 0;
 MC_FlashCard flash;
 
 
@@ -109,7 +107,7 @@
 /* allow the server to be run as a function in a process or thread     */
 /* within another program.  main() is now in a separate file,          */
 /* servermain.c, that consists solely of a call to RunServer().        */
-int RunServer(int argc, char **argv)
+int RunServer(int argc, char* argv[])
 { 
   printf("Started tuxmathserver, waiting for client to connect:\n>\n");
 
@@ -151,7 +149,30 @@
 }
 
 
+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);
+}
+
+
+
 /*********************************************************************/
 /*  "Private" (to server.c) functions                                */
 /*********************************************************************/
@@ -352,7 +373,6 @@
 //network on this port, and the server sends a response.
 void check_UDP(void)
 {
-  char buf[NET_BUF_LEN];
   int recvd = 0;
   UDPpacket* in = SDLNet_AllocPacket(NET_BUF_LEN);
   recvd = SDLNet_UDP_Recv(udpsock, in);
@@ -361,7 +381,6 @@
   if(strncmp((char*)in->data, "TUXMATH_CLIENT", strlen("TUXMATH_CLIENT")) == 0)
   {
     UDPpacket* out;
-    IPaddress bcast_ip;
     int sent = 0;
     char buf[NET_BUF_LEN];
     // Send "I am here" reply so client knows where to connect socket,
@@ -571,6 +590,7 @@
       //test_connections();
     }
   } 
+  return 1;
 }
 
 
@@ -673,7 +693,6 @@
 void handle_client_nongame_msg(int i, char* buffer)
 {
   char buf[NET_BUF_LEN];
-  int x;
 
   if(strncmp(buffer, "START_GAME", strlen("START_GAME")) == 0)
   {
@@ -892,8 +911,7 @@
 void start_game(void)
 {
   char buf[NET_BUF_LEN];
-  char buffer[NET_BUF_LEN];
-  int x,j;
+  int j;
 
 
   /* NOTE this should no longer be needed - doing the same thing earlier    */
@@ -960,9 +978,6 @@
   /* Send enough questions to fill the initial comet slots (currently 10) */
   for(j = 0; j < QUEST_QUEUE_SIZE; j++)
   {
-  
-    int k = 0;
-
     if (!MC_NextQuestion(&flash))
     { 
       /* no more questions available */
@@ -994,7 +1009,7 @@
 //and so forth:
 int send_counter_updates(void)
 {
-  int i, total_questions;
+  int total_questions;
 
   //If game won, tell everyone:
   if(MC_MissionAccomplished())
@@ -1049,7 +1064,7 @@
   or anything the client is made to be informed */
 int SendMessage(int message, int ques_id, char *name, TCPsocket client_sock)         
 {
- int x, len;
+ int x;
  char buf[NET_BUF_LEN];
  char msg[100];  
 

Modified: tuxmath/trunk/src/server.h
===================================================================
--- tuxmath/trunk/src/server.h	2009-09-13 00:02:59 UTC (rev 1535)
+++ tuxmath/trunk/src/server.h	2009-09-13 16:54:45 UTC (rev 1536)
@@ -48,5 +48,19 @@
 };
 
 
+/* Ways to run the server - all accept command-line style arguments: */
+
+/* 1. Type "tuxmathserver" at command line tp rim as standalone program.              */
+
+/* 2. Using POSIX threads library (RECOMMENDED if phtreads available on your system): */
+int RunServer_pthreads(int argc, char* argv[]);
+/* 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:                                              */
+int RunServer_fork(int argc, char* argv[]);
+
+/* 2, 3, and 4 all return immediately, with the server running in a separate thread   */
+/* or process.  But if you don't mind waiting...                                      */
+/* 5. Plain "blocking" function call, leaving scheduling issues up to you:            */ 
 int RunServer(int argc, char **argv);
 #endif




More information about the Tux4kids-commits mailing list