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

David Bruce dbruce-guest at alioth.debian.org
Mon Jul 13 04:29:00 UTC 2009


Author: dbruce-guest
Date: 2009-07-13 04:28:58 +0000 (Mon, 13 Jul 2009)
New Revision: 1194

Modified:
   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:
code cleanup and reorganization



Modified: tuxmath/branches/lan/server/server.c
===================================================================
--- tuxmath/branches/lan/server/server.c	2009-07-13 02:54:28 UTC (rev 1193)
+++ tuxmath/branches/lan/server/server.c	2009-07-13 04:28:58 UTC (rev 1194)
@@ -363,48 +363,42 @@
   }
 #endif
 
-  return;
-}
-
-
-
-// check_messages() is where we look at the client socket set to see which 
-// have sent us messages. This function is used in each server loop whether
-// or not a math game is in progress (although we expect different messages
-// during a game from those encountered outside of a game)
-
-int check_messages(void)
-{
-  int i = 0, c = 0;
-  int actives = 0;
-  int ready_found = 0;
-  char buffer[NET_BUF_LEN];
-
-
-  //NOTE Does this belong here? Seems to have more to do wth client connections.
+  //If everyone is disconnected, game no longer in progress:
   if(game_in_progress == 1)
   {
+    int i = 0, playing = 0;
     for(i = 0; i < MAX_CLIENTS; i++)
     {
       if(client[i].sock != NULL)
       {
-        c = 1;
+        playing = 1;
         break;
       }
     }
-    if(c == 0)
+    if(!playing)
     {
       printf("All the clients have been disconnected....\n");
-      //We just need to clean up to start a new math game
-      cleanup_server();
-      setup_server();
       game_in_progress = 0;
-      return 0;
     }
   }
 
+  return;
+}
 
 
+
+// check_messages() is where we look at the client socket set to see which 
+// have sent us messages. This function is used in each server loop whether
+// or not a math game is in progress (although we expect different messages
+// during a game from those encountered outside of a game)
+
+int check_messages(void)
+{
+  int actives = 0, i = 0;
+  int ready_found = 0;
+  char buffer[NET_BUF_LEN];
+
+
   /* Check the client socket set for activity: */
   actives = SDLNet_CheckSockets(client_set, 0);
 //  printf("in check_messages(), actives = %d\n", actives);
@@ -495,6 +489,7 @@
   if(strncmp(command, "CORRECT_ANSWER", strlen("CORRECT_ANSWER")) == 0)
   {
     game_msg_correct_answer(i,id);
+    game_msg_next_question();
   }                            
 
   else if(strncmp(command, "NEXT_QUESTION",strlen("NEXT_QUESTION")) == 0) /* Send Next Question */

Modified: tuxmath/branches/lan/server/testclient.c
===================================================================
--- tuxmath/branches/lan/server/testclient.c	2009-07-13 02:54:28 UTC (rev 1193)
+++ tuxmath/branches/lan/server/testclient.c	2009-07-13 04:28:58 UTC (rev 1194)
@@ -38,12 +38,12 @@
 int sockets_used = 0;
 int quit = 0;
 MC_FlashCard flash;    //current question
+int have_question = 0;
 
 /* Local function prototypes: */
 int setup_client(int argc, char **argv);
-
 int playgame(void);
-
+int game_check_msgs(void);
 int read_stdin_nonblock(char* buf, size_t max_length);
 
 
@@ -191,6 +191,55 @@
 
 
 
+int game_check_msgs(void)
+{
+  char buf[NET_BUF_LEN];
+  int status = 1;
+  while(1)
+  {
+    buf[0] = '\0';
+    status = get_next_msg(buf);
+    if (status == -1)  //Fatal error
+    {
+      printf("Error - get_next_msg() returned -1\n");
+      return -1;
+    }
+
+    if (status == 0)  //Fatal error
+    {
+      //No messages
+      return 0;
+    }
+
+    /* Now we process the buffer according to the command: */
+    if(strncmp(buf, "SEND_QUESTION", strlen("SEND_QUESTION")) == 0)
+    {
+      /* function call to parse buffer and receive question */
+      if(Make_Flashcard(buf, &flash))
+      {
+        have_question = 1; 
+        printf("The question is: %s\n>\n", flash.formula_string);
+      }
+      else
+        printf("Unable to parse buffer into FlashCard\n");
+    }
+    else if(strncmp(buf, "SEND_MESSAGE", strlen("SEND_MESSAGE")) == 0)
+    {
+      printf("%s\n", buf);
+    }
+    else if(strncmp(buf, "PLAYER_MSG", strlen("PLAYER_MSG")) == 0)
+    {
+      player_msg_recvd(buf);
+    }
+    else 
+    {
+      printf("game_check_msgs() - unrecognized message: %s\n", buf);
+    }
+  }
+
+  return 1;
+}
+
 int playgame(void)
 {
   int numready;
@@ -198,7 +247,6 @@
   int ans = 0;
   int x=0, i = 0;
   int end = 0;
-  int have_question = 0;
   int len = 0;
   char buf[NET_BUF_LEN];
   char buffer[NET_BUF_LEN];
@@ -230,87 +278,11 @@
   //Begin game loop:
   while (!end)
   {
-    //First we check for any responses from server:
-    //NOTE keep looping until SDLNet_CheckSockets() detects no activity.
-    numready = 1;
-    while(numready > 0)
-    {
-     
-      char command[NET_BUF_LEN];
-      int i = 0;
 
-      //This is supposed to check to see if there is activity:
-      numready = SDLNet_CheckSockets(set, 0);
-      if(numready == -1)
-      {
-        printf("SDLNet_CheckSockets: %s\n", SDLNet_GetError());
-        //most of the time this is a system error, where perror might help you.
-        perror("SDLNet_CheckSockets");
-      }
-      else if(numready > 0)
-      {
-#ifdef LAN_DEBUG
-//        printf("There are %d sockets with activity!\n", numready);
-#endif
-        // check all sockets with SDLNet_SocketReady and handle the active ones.
-        if(SDLNet_SocketReady(sd))
-        {
-          buf[0] = '\0';
-          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);
-          }
-#ifdef LAN_DEBUG
-//          printf("%d bytes received\n", x);
-#endif
-          /* Copy the command name out of the tab-delimited buffer: */
-          for (i = 0;
-               buf[i] != '\0' && buf[i] != '\t' && i < NET_BUF_LEN;
-               i++)
-          {
-            command[i] = buf[i];
-          }
-
-          command[i] = '\0';
-#ifdef LAN_DEBUG
-          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)
-          {
-            /* function call to parse buffer into MC_FlashCard */
-            if(Make_Flashcard(buf, &flash))
-            {
-              have_question = 1; 
-              printf("The question is: %s\n>\n", flash.formula_string);
-            }
-            else
-              printf("Unable to parse buffer into FlashCard\n");
-          }
-          else if(strncmp(command,"SEND_MESSAGE", strlen("SEND_MESSAGE")) == 0)
-          {
-            // Presumably we want to print the message to stdout
-            printf("%s\n", buf);
-          }
-          else if(strncmp(command,"PLAYER_MSG", strlen("PLAYER_MSG")) == 0)
-          {
-            player_msg_recvd(buf);
-          }
-	  else if(strncmp(command,"PING", strlen("PING")) == 0)
-          {
-            server_pinged();
-          }
-        }
-      }
-    } // End of loop for checking server activity
-
-#ifdef LAN_DEBUG
-//    printf(".\n");
-#endif
-
+    //Check our network messages, bailing out for fatal errors:
+    if (game_check_msgs() == -1)
+      return -1;
+    
     //Now we check for any user responses
 
     //This function returns 1 and updates buf with input from

Modified: tuxmath/branches/lan/src/network.c
===================================================================
--- tuxmath/branches/lan/src/network.c	2009-07-13 02:54:28 UTC (rev 1193)
+++ tuxmath/branches/lan/src/network.c	2009-07-13 04:28:58 UTC (rev 1194)
@@ -161,6 +161,69 @@
   return 1;
 }
 
+
+/* Here we get the next message from the server if one is available. */
+/* We return 1 if a message received, 0 if no activity, -1 on errors */
+/* or if connection is lost:                                         */
+int get_next_msg(char* buf)
+{ 
+  int x = 0, numready = 0;
+
+  /* Make sure we have place to put message: */
+  if(buf == NULL)
+  {
+    printf("get_next_msg() passed NULL buffer\n");
+    return -1;
+  }
+  
+  //Check to see if there is socket activity:
+  numready = SDLNet_CheckSockets(set, 0);
+  if(numready == -1)
+  {
+    printf("SDLNet_CheckSockets: %s\n", SDLNet_GetError());
+    //most of the time this is a system error, where perror might help you.
+    perror("SDLNet_CheckSockets");
+    return -1;
+  }
+  else if(numready > 0)
+  {
+   // check with SDLNet_SocketReady():
+    if(SDLNet_SocketReady(sd))
+    {
+      buf[0] = '\0';
+      
+      if(SDLNet_TCP_Recv(sd, buf, NET_BUF_LEN) > 0)
+      {
+        //Success - message is now in buffer
+        return 1;
+      }
+      else
+      {
+        fprintf(stderr, "In get_next_msg(), SDLNet_TCP_Recv() failed!\n");
+        SDLNet_TCP_DelSocket(set, sd);
+        if(sd != NULL)
+          SDLNet_TCP_Close(sd);
+        sd = NULL;
+        return -1;
+      }
+    }
+    else
+    {
+      fprintf(stderr, "In get_next_msg(), socket set reported active but no activity found\n");
+      SDLNet_TCP_DelSocket(set, sd);
+      if(sd != NULL)
+        SDLNet_TCP_Close(sd);
+      sd = NULL;
+      return -1;
+    }
+  }
+  // No socket activity - just return 0:
+  return 0;
+}
+
+
+
+
 int Make_Flashcard(char* buf, MC_FlashCard* fc)
 {
   int i = 0,tab = 0, s = 0;

Modified: tuxmath/branches/lan/src/network.h
===================================================================
--- tuxmath/branches/lan/src/network.h	2009-07-13 02:54:28 UTC (rev 1193)
+++ tuxmath/branches/lan/src/network.h	2009-07-13 04:28:58 UTC (rev 1194)
@@ -18,6 +18,7 @@
 #define NETWORK_H
 
 int setup_net(char *host, int port);
+int get_next_msg(char* buf);
 int say_to_server(char *statement);
 int evaluate(char *statement);
 int LAN_AnsweredCorrectly(MC_FlashCard* fc);




More information about the Tux4kids-commits mailing list