[Tux4kids-commits] r1190 - in tuxmath/branches/lan: server src

David Bruce dbruce-guest at alioth.debian.org
Sun Jul 12 22:28:45 UTC 2009


Author: dbruce-guest
Date: 2009-07-12 22:28:45 +0000 (Sun, 12 Jul 2009)
New Revision: 1190

Modified:
   tuxmath/branches/lan/server/Makefile.am
   tuxmath/branches/lan/server/server.c
   tuxmath/branches/lan/server/testclient.c
   tuxmath/branches/lan/src/network.c
   tuxmath/branches/lan/src/network.h
Log:
testclient.c now uses src/network.c for equivalent functions, redundant code removed.
fixed several crashes in these files.



Modified: tuxmath/branches/lan/server/Makefile.am
===================================================================
--- tuxmath/branches/lan/server/Makefile.am	2009-07-12 19:39:51 UTC (rev 1189)
+++ tuxmath/branches/lan/server/Makefile.am	2009-07-12 22:28:45 UTC (rev 1190)
@@ -3,8 +3,10 @@
 
 bin_PROGRAMS = tuxmathserver tuxmathtestclient
 
-tuxmathserver_SOURCES = server.c mathcards.c 
-tuxmathtestclient_SOURCES = testclient.c
+tuxmathserver_SOURCES = server.c mathcards.c ../src/throttle.c
+tuxmathtestclient_SOURCES = testclient.c \
+                            ../src/throttle.c \
+                            ../src/network.c
 
 EXTRA_DIST = mathcards.h server.h testclient.h
  

Modified: tuxmath/branches/lan/server/server.c
===================================================================
--- tuxmath/branches/lan/server/server.c	2009-07-12 19:39:51 UTC (rev 1189)
+++ tuxmath/branches/lan/server/server.c	2009-07-12 22:28:45 UTC (rev 1190)
@@ -87,41 +87,17 @@
   /*    ------------- Main server loop:  ------------------   */
   while (!quit)
   {
-//    frame++;
-//    /* See if our existing clients are really still there. For */
-//    /* performance reasons, we don't do this on every loop:    */
-//    if(frame%1000 == 0)
-//      test_connections();
 
     /* Now we check to see if anyone is trying to connect. */
     update_clients();
     /* Check for any pending messages from clients already connected: */
     check_messages();
 
-    /*checking for two mathcards dependent exit conditions*/ 
-    if(num_clients)
-    {
-      if (!MC_TotalQuestionsLeft())
-      {
-        if(no_questions_left())
-        {
-          printf("function no_questions_left() failed \n");
-        }
-      }
-    
-      if (MC_MissionAccomplished())
-      {
-        if(mission_accomplished())
-        {
-          printf("function mission_accomplished failed \n");
-        }
-      }
-    }
     /* Limit frame rate to keep from eating all CPU: */
     /* NOTE almost certainly could make this longer wtihout noticably */
     /* affecting performance, but even throttling to 1 msec/loop cuts */
     /* CPU from 100% to ~2% on my desktop - DSB                       */
-    throttle(1);  //min loop time 1 msec
+    Throttle(5);  //min loop time 5 msec
   }
    
   /*   -----  Free resources before exiting: -------    */
@@ -131,6 +107,7 @@
 }
 
 
+
 /*********************************************************************/
 /*  "Private" (to server.c) functions                                */
 /*********************************************************************/
@@ -258,9 +235,9 @@
     server_sock = NULL;
   }
 
-  
+  SDLNet_Quit();
 
- /* Clean up mathcards heap memory */
+  /* Clean up mathcards heap memory */
   MC_EndGame();
 }
 
@@ -558,37 +535,8 @@
 }
 
 
-/* FIXME these are just sending integers to be sent for the client */
-/* to display to the player - printf() or however we decide to do it */
-/* in the real game. If they are supposed to be messages for the   */
-/* client _program_, they need to have their own message type to   */
-/* tell the client what to do with it.  We want it to get e.g      */
-/*	TOTAL_QUESTIONS 22                                         */
-/* rather than:                                                    */
-/*	PLAYER_MSG 22                                              */
 
-/*
-void game_msg_total_questions_left(int i)
-{
- int total_questions_left;
- char ch[10];
- total_questions_left = MC_TotalQuestionsLeft();
- snprintf(ch, 10, "%d",total_questions_left); 
- player_msg(i,ch);
-}
 
-
-void game_msg_mission_accomplished(int i)
-{
-
- int total_questions_left;
- char ch[10];
- total_questions_left=MC_MissionAccomplished();
- snprintf(ch,10,"%d",total_questions_left); 
- player_msg(i,ch);
-}
-*/
-
 void game_msg_correct_answer(int i, int id)
 {
   int n;
@@ -638,57 +586,10 @@
   } 
 }
 
-// Go through and test all the current connections, removing
-// any clients that fail to respond:
-void test_connections(void)
-{
-  int i = 0;
 
-  for (i = 0; i < MAX_CLIENTS; i++)
-    ping_client(i);
-}
 
 
-// This is supposed to be a way to test and see if each client
-// is really connected.
-// FIXME I think we need to put in a SDLNet_TCP_Recv() to see
-// if we get a reply, now that the client is modified to send back
-// PING_BACK.  I am worried, however, that we could have a problem
-// with intercepting messages not related to the ping testing - DSB
 
-void ping_client(int i)
-{
-  char buf[NET_BUF_LEN];
-  char msg[NET_BUF_LEN];
-  int x;
-
-  if(i < 0 || i > MAX_CLIENTS)
-  {
-    printf("ping_client() - invalid index argument\n");
-    return;
-  }
-  
-  if(client[i].sock == NULL)
-  {
-    return;
-  }
-  
-//  sprintf(msg,"%s", "PING\n");
-//  snprintf(buf, NET_BUF_LEN, "%s\t%s\n", "SEND_MESSAGE", msg);
-  snprintf(buf, NET_BUF_LEN, "%s\n", "PING");
-  x = SDLNet_TCP_Send(client[i].sock, buf, NET_BUF_LEN);
-  if(x < NET_BUF_LEN)
-  {
-   printf("The client %s is disconnected\n",client[i].name);
-   remove_client(i);
-  }
-//#ifdef LAN_DEBUG
-  printf("buf is: %s\n", buf);
-  printf("SendMessage() - buf sent:::: %d bytes\n", x);
-//#endif
-}
-
-
 void game_msg_exit(int i)
 {
   printf("LEFT the GAME : %s",client[i].name);
@@ -900,24 +801,22 @@
 #ifdef LAN_DEBUG
     printf("player_msg() - invalid index argument\n");
 #endif
-  return 0;
+    return 0;
   }
-  
-  if(!client[i].sock)
+
+  if(!msg)
   {
 #ifdef LAN_DEBUG
-    printf("player_msg() - client socket is NULL\n");
+    printf("player_msg() - msg argument is NULL\n");
 #endif
-  return 0;
+    return 0;
   }
   
-  if(!msg)
+  if(!client[i].sock)
   {
-#ifdef LAN_DEBUG
-    printf("player_msg() - msg argument is NULL\n");
-#endif
-  return 0;
+    return 0;
   }
+  
 
   //transmit:
   snprintf(buf, NET_BUF_LEN, "%s\t%s\n", "PLAYER_MSG", msg);
@@ -942,32 +841,66 @@
 }
 
 
-/* Simple function that returns a minimum of 'loop_msec' */
-/* milliseconds after it returned the previous time it   */
-/* was called.                                           */
-void throttle(int loop_msec)
+
+
+
+
+
+
+
+
+/* Code related to "pinging system" for pollng all clients to */
+/* see if they are still connected - we may not need this.    */
+/* (kept out of way here at bottom until we decide)           */
+
+
+
+// Go through and test all the current connections, removing
+// any clients that fail to respond:
+void test_connections(void)
 {
-  static Uint32 now_t, last_t; //These will be zero first time through
-  int wait_t;
+  int i = 0;
 
-  //Target loop time must be between 0 and 100 msec:
-  if(loop_msec < 0)
-    loop_msec = 0;
-  if(loop_msec > 100)
-    loop_msec = 100;
+  for (i = 0; i < MAX_CLIENTS; i++)
+    ping_client(i);
+}
 
-  if (now_t == 0)  //For sane behavior first time through:
-    last_t = SDL_GetTicks();
-  else
-    last_t = now_t;
-  now_t = SDL_GetTicks();
-  wait_t = (last_t + loop_msec) - now_t;
 
-  //Avoid problem if we somehow wrap past uint32 size
-  if(wait_t < 0)
-    wait_t = 0;
-  if(wait_t > loop_msec)
-    wait_t = loop_msec;
+// This is supposed to be a way to test and see if each client
+// is really connected.
+// FIXME I think we need to put in a SDLNet_TCP_Recv() to see
+// if we get a reply, now that the client is modified to send back
+// PING_BACK.  I am worried, however, that we could have a problem
+// with intercepting messages not related to the ping testing - DSB
 
-  SDL_Delay(wait_t);
+void ping_client(int i)
+{
+  char buf[NET_BUF_LEN];
+  char msg[NET_BUF_LEN];
+  int x;
+
+  if(i < 0 || i > MAX_CLIENTS)
+  {
+    printf("ping_client() - invalid index argument\n");
+    return;
+  }
+  
+  if(client[i].sock == NULL)
+  {
+    return;
+  }
+  
+//  sprintf(msg,"%s", "PING\n");
+//  snprintf(buf, NET_BUF_LEN, "%s\t%s\n", "SEND_MESSAGE", msg);
+  snprintf(buf, NET_BUF_LEN, "%s\n", "PING");
+  x = SDLNet_TCP_Send(client[i].sock, buf, NET_BUF_LEN);
+  if(x < NET_BUF_LEN)
+  {
+   printf("The client %s is disconnected\n",client[i].name);
+   remove_client(i);
+  }
+//#ifdef LAN_DEBUG
+  printf("buf is: %s\n", buf);
+  printf("SendMessage() - buf sent:::: %d bytes\n", x);
+//#endif
 }

Modified: tuxmath/branches/lan/server/testclient.c
===================================================================
--- tuxmath/branches/lan/server/testclient.c	2009-07-12 19:39:51 UTC (rev 1189)
+++ tuxmath/branches/lan/server/testclient.c	2009-07-12 22:28:45 UTC (rev 1190)
@@ -27,6 +27,8 @@
 #include "transtruct.h"
 #include "mathcards.h"
 #include "testclient.h"
+#include "../src/throttle.h"
+#include "../src/network.h"
 
 /* Local (to testclient.c) "globals": */
 TCPsocket sd;           /* Server socket descriptor */
@@ -39,15 +41,10 @@
 
 /* Local function prototypes: */
 int setup_client(int argc, char **argv);
-void cleanup_client(void);
-int Make_Flashcard(char *buf, MC_FlashCard* fc);
-int LAN_AnsweredCorrectly(MC_FlashCard* fc);
+
 int playgame(void);
-void server_pinged(void);
 
-int player_msg_recvd(char *command,char* buf);
 int read_stdin_nonblock(char* buf, size_t max_length);
-void throttle(int loop_msec);
 
 
 
@@ -70,7 +67,7 @@
 
   /* Send messages */
   quit = 0;
-  do
+  while(!quit)
   { 
     //Get user input from command line and send it to server: 
     /*now display the options*/
@@ -106,14 +103,10 @@
              "'quit' to end both client and server\n\n>\n");
     }
     //Limit loop to once per 10 msec so we don't eat all CPU
-    throttle(10);
-  }while(!quit);
+    Throttle(10);
+  }
  
-  SDLNet_TCP_Close(sd);
-  SDLNet_FreeSocketSet(set);
-  set=NULL; //this helps us remember that this set is not allocated
-
-  SDLNet_Quit();
+  cleanup_client();
  
   return EXIT_SUCCESS;
 }
@@ -198,132 +191,6 @@
 
 
 
-
-int LAN_AnsweredCorrectly(MC_FlashCard* fc)
-{
-  int len;
-  char buffer[NET_BUF_LEN];
-
-  snprintf(buffer, NET_BUF_LEN, 
-                  "%s %d\n",
-                  "CORRECT_ANSWER",
-                  fc->question_id);
-  len = strlen(buffer) + 1;
-  if (SDLNet_TCP_Send(sd, (void *)buffer, NET_BUF_LEN) < NET_BUF_LEN)
-  {
-    fprintf(stderr, "SDLNet_TCP_Send: %s\n", SDLNet_GetError());
-    exit(EXIT_FAILURE);
-  }
-
-  snprintf(buffer, NET_BUF_LEN, 
-                  "%s\n",
-                  "NEXT_QUESTION");
-  len = strlen(buffer) + 1;
-  if (SDLNet_TCP_Send(sd, (void *)buffer, NET_BUF_LEN) < NET_BUF_LEN)
-  {
-    fprintf(stderr, "SDLNet_TCP_Send: %s\n", SDLNet_GetError());
-    exit(EXIT_FAILURE);
-  }
-
-
-  return 1;
-}
-                
-
-void server_pinged(void)
-{ 
-  int len;
-  char buffer[NET_BUF_LEN];
-
-  snprintf(buffer, NET_BUF_LEN, 
-                  "%s \n",
-                  "PING_BACK");
-  len = strlen(buffer) + 1;
-  if (SDLNet_TCP_Send(sd, (void *)buffer, NET_BUF_LEN) < NET_BUF_LEN)
-  {
-    fprintf(stderr, "SDLNet_TCP_Send: %s\n", SDLNet_GetError());
-    exit(EXIT_FAILURE);
-  }
- 
-#ifdef LAN_DEBUG
-//   printf("Buffer sent is %s\n",buffer);
-#endif
- 
-}
-
-
-int Make_Flashcard(char* buf, MC_FlashCard* fc)
-{
-  int i = 0, j, tab = 0, s = 0;
-  char formula[MC_FORMULA_LEN];
-  sscanf (buf,"%*s%d%d%d%s",
-              &fc->question_id,
-              &fc->difficulty,
-              &fc->answer,
-              fc->answer_string); /* can't formula_string in sscanf in here cause it includes spaces*/
- 
-  /*doing all this cause sscanf will break on encountering space in formula_string*/
-  /* NOTE changed to index notation so we keep within NET_BUF_LEN */
-  while(buf[i]!='\n' && i < NET_BUF_LEN)
-  {
-    if(buf[i]=='\t')
-      tab++; 
-    i++;
-    if(tab == 5)
-      break;
-  }
-
-  while((buf[i] != '\n') 
-    && (s < MC_FORMULA_LEN - 1)) //Must leave room for terminating null
-  {
-    formula[s] = buf[i] ;
-    i++;
-    s++;
-  }
-  formula[s]='\0';
-  strcpy(fc->formula_string, formula); 
-
-#ifdef LAN_DEBUG
-  printf ("card is:\n");
-  printf("QUESTION_ID       :      %d\n",fc->question_id);
-  printf("FORMULA_STRING    :      %s\n",fc->formula_string);
-  printf("ANSWER STRING     :      %s\n",fc->answer_string);
-  printf("ANSWER            :      %d\n",fc->answer);
-  printf("DIFFICULTY        :      %d\n",fc->difficulty);  
-#endif
-
-return 1;
-} 
-int evaluate(char statement[20])
-{
-  int ans,x;
-  char command[NET_BUF_LEN];
-  int len;
-  char buffer[NET_BUF_LEN];
-  char buf[NET_BUF_LEN];
-
-  snprintf(buffer, NET_BUF_LEN, 
-                  "%s\n",
-                  statement);
-   len = strlen(buffer) + 1;
-   if (SDLNet_TCP_Send(sd, (void *)buffer, NET_BUF_LEN) < NET_BUF_LEN)
-   {
-     fprintf(stderr, "SDLNet_TCP_Send: %s\n", SDLNet_GetError());
-     exit(EXIT_FAILURE);
-   }
-        x = SDLNet_TCP_Recv(sd, buf, NET_BUF_LEN);
-          if( x <= 0)
-          {
-            fprintf(stderr, "In play_game(), SDLNet_TCP_Recv() failed!\n");
-            exit(EXIT_FAILURE);
-          }
-  player_msg_recvd(buf,command);
-  ans=atoi(command);
-
-  return ans;
-}
-
-
 int playgame(void)
 {
   int numready;
@@ -407,11 +274,9 @@
           }
 
           command[i] = '\0';
-printf("this is the value of mission accomplished... %d ...\n",evaluate("TOTAL_QUESTIONS_LEFT"));
-evaluate("TOTAL_QUESTIONS_LEFT");
 #ifdef LAN_DEBUG
-//          printf("buf is %s\n", buf);
-//          printf("command is %s\n", command);
+          printf("buf is %s\n", buf);
+          printf("command is %s\n", command);
 #endif
           /* Now we process the buffer according to the command: */
           if(strncmp(command, "SEND_QUESTION", 13) == 0)
@@ -432,7 +297,7 @@
           }
           else if(strncmp(command,"PLAYER_MSG", strlen("PLAYER_MSG")) == 0)
           {
-            player_msg_recvd(NULL,buf);
+            player_msg_recvd(buf);
           }
 	  else if(strncmp(command,"PING", strlen("PING")) == 0)
           {
@@ -462,7 +327,7 @@
       }
       else if(strncmp(buf,"PLAYER_MSG", strlen("PLAYER_MSG")) == 0)
       {
-        player_msg_recvd(NULL,buf);
+        player_msg_recvd(buf);
       } 
       else
       {
@@ -487,7 +352,7 @@
       }  //input wasn't any of our keywords
     } // Input was received 
 
-    throttle(10);  //so don't eat all CPU
+    Throttle(10);  //so don't eat all CPU
   } //End of game loop 
 #ifdef LAN_DEBUG
   printf("Leaving playgame()\n");
@@ -495,21 +360,8 @@
 }
 
 
-//Goes past the title field in the tab-delimited buffer
-//and prints the rest to stdout:
-int player_msg_recvd(char *command,char* buf)
-{
-  command = strchr(buf, '\t');
-  if(command)
-  { 
-    command++;
-    printf("%s\n", command);
-    return 1;
-  }
-  else
-    return 0;
-}
 
+
 //Here we read up to max_length bytes from stdin into the buffer.
 //The first '\n' in the buffer, if present, is replaced with a
 //null terminator.
@@ -534,32 +386,3 @@
   return bytes_read;
 }
 
-
-
-void throttle(int loop_msec)
-{
-  static Uint32 now_t, last_t; //These will be zero first time through
-  int wait_t;
-
-  //Target loop time must be between 0 and 100 msec:
-  if(loop_msec < 0)
-    loop_msec = 0;
-  if(loop_msec > 100)
-    loop_msec = 100;
-
-  if (now_t == 0)  //For sane behavior first time through:
-    last_t = SDL_GetTicks();
-  else
-    last_t = now_t;
-  now_t = SDL_GetTicks();
-  wait_t = (last_t + loop_msec) - now_t;
-
-  //Avoid problem if we somehow wrap past uint32 size
-  if(wait_t < 0)
-    wait_t = 0;
-  if(wait_t > loop_msec)
-    wait_t = loop_msec;
-
-  SDL_Delay(wait_t);
-}
-

Modified: tuxmath/branches/lan/src/network.c
===================================================================
--- tuxmath/branches/lan/src/network.c	2009-07-12 19:39:51 UTC (rev 1189)
+++ tuxmath/branches/lan/src/network.c	2009-07-12 22:28:45 UTC (rev 1190)
@@ -91,8 +91,13 @@
 
 }
 
-int player_msg_recvd(char* buf,char* p)
+/* This function prints the 'msg' part of the buffer (i.e. everything */
+/* after the first '\t') to stdout.                                   */
+int player_msg_recvd(char* buf)
 {
+  char* p;
+  if(buf == NULL)
+    return 0;
   p = strchr(buf, '\t');
   if(p)
   { 
@@ -222,18 +227,18 @@
 
 void cleanup_client(void)
 {
-  
-  if (set != NULL)
+  if(sd)
   {
-    SDLNet_FreeSocketSet(set);    //releasing the memory of the client socket set
-    set = NULL;                   //this helps us remember that this set is not allocated
-  } 
-
-  if(sd != NULL)
-  {
     SDLNet_TCP_Close(sd);
     sd = NULL;
   }
+
+  if(set)
+  {
+    SDLNet_FreeSocketSet(set);
+    set = NULL;
+  }
+  SDLNet_Quit();
 }
 
 

Modified: tuxmath/branches/lan/src/network.h
===================================================================
--- tuxmath/branches/lan/src/network.h	2009-07-12 19:39:51 UTC (rev 1189)
+++ tuxmath/branches/lan/src/network.h	2009-07-12 22:28:45 UTC (rev 1190)
@@ -23,7 +23,7 @@
 int LAN_AnsweredCorrectly(MC_FlashCard* fc);
 void cleanup_client(void);
 int check_messages(char *);
-int player_msg_recvd(char* buf,char* p);
+int player_msg_recvd(char* buf);
 int Make_Flashcard(char* buf, MC_FlashCard* fc);
 void server_pinged(void);        //The ping system is not yet used and so is this function.
 #endif // NETWORK_H




More information about the Tux4kids-commits mailing list