[Tux4kids-commits] r1141 - in tuxmath/branches/lan: doc server

David Bruce dbruce-guest at alioth.debian.org
Sat Jul 4 03:11:12 UTC 2009


Author: dbruce-guest
Date: 2009-07-04 03:11:11 +0000 (Sat, 04 Jul 2009)
New Revision: 1141

Modified:
   tuxmath/branches/lan/doc/changelog
   tuxmath/branches/lan/server/server.c
   tuxmath/branches/lan/server/testclient.c
   tuxmath/branches/lan/server/transtruct.h
Log:
fixes so that multiplayer communicates correctly when one player answers the question correctly.



Modified: tuxmath/branches/lan/doc/changelog
===================================================================
--- tuxmath/branches/lan/doc/changelog	2009-07-04 00:26:59 UTC (rev 1140)
+++ tuxmath/branches/lan/doc/changelog	2009-07-04 03:11:11 UTC (rev 1141)
@@ -1,3 +1,10 @@
+2009.Jul.03 (svn.debian.org/tux4kids - revision 1141)
+        LAN project - basic TCP/IP server and test client implemented in branches/lan.
+        The client and server provide multiplayer command-line tuxmath over tcp.
+
+        Akash Gangil <akashg1611 at gmail.com>
+	David Bruce <davidstuartbruce at gmail.com>
+
 2009.Mar.12 (svn.debian.org/tux4kids - revision 937)
 Version 1.7.2
         Build- some minor tweaks to tuxmath.desktop and specfiles.

Modified: tuxmath/branches/lan/server/server.c
===================================================================
--- tuxmath/branches/lan/server/server.c	2009-07-04 00:26:59 UTC (rev 1140)
+++ tuxmath/branches/lan/server/server.c	2009-07-04 03:11:11 UTC (rev 1141)
@@ -320,31 +320,32 @@
 
 int check_messages(void)
 {
-  int i = 0,c=0;
+  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(game_in_progress==1)
   {
-    for(i=0;i<MAX_CLIENTS;i++)
+    for(i = 0; i <MAX_CLIENTS; i++)
     {
-      if(client[i].sock!=NULL)
+      if(client[i].sock != NULL)
       {
-        c=1;
+        c = 1;
         break;
       }
     }
-    if(c==0)
+    if(c == 0)
     {
       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;
+      game_in_progress = 0;
       return 0;
-    }     
+    }
   }
 
 
@@ -475,9 +476,12 @@
 #endif
   }
                   
-  for(n = 0; n < num_clients && client[n].sock; n++)
+  for(n = 0; n < MAX_CLIENTS && client[n].sock; n++)
   {
-    if(!SendQuestion(flash,client[n].sock))
+#ifdef LAN_DEBUG
+    printf("About to send next question to client[%d]\n", n);
+#endif
+    if(!SendQuestion(flash, client[n].sock))
     {
       printf("Unable to send Question\n");
     }

Modified: tuxmath/branches/lan/server/testclient.c
===================================================================
--- tuxmath/branches/lan/server/testclient.c	2009-07-04 00:26:59 UTC (rev 1140)
+++ tuxmath/branches/lan/server/testclient.c	2009-07-04 03:11:11 UTC (rev 1141)
@@ -47,25 +47,20 @@
   int len, sockets_used;
   char buf[NET_BUF_LEN];     // for network messages from server
   char buffer[NET_BUF_LEN];  // for command-line input
+  char *check1;
+  char name[NAME_SIZE];
 
-      /* first just take in the name */
-      char *check1;
-      char name[NAME_SIZE];
-      printf("Enter your Name.\n");
-      check1 = fgets(buffer, NET_BUF_LEN, stdin);
-      strncpy(name, check1, strlen(check1));
-      snprintf(buffer, NET_BUF_LEN, 
-                       "%s",
-                       name);
-      
 
+
+
+
   /* Simple parameter checking */
   if (argc < 3)
   {
     fprintf(stderr, "Usage: %s host port\n", argv[0]);
     exit(EXIT_FAILURE);
   }
- 
+
   if (SDLNet_Init() < 0)
   {
     fprintf(stderr, "SDLNet_Init: %s\n", SDLNet_GetError());
@@ -99,22 +94,35 @@
     // perhaps you need to restart the set and make it bigger...
   }
 
+
+  /* Now we are connected. Take in nickname and send to server. */
+
+  /* first just take in the name */
+  printf("Enter your Name.\n");
+  check1 = fgets(buffer, NET_BUF_LEN, stdin);
+  strncpy(name, check1, strlen(check1));
+  snprintf(buffer, NET_BUF_LEN, 
+                       "%s",
+                       name);
+
   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("Sent the name of the player %s\n",check1);
 #endif
 
 
+
   /* Send messages */
   quit = 0;
   do
   { 
     //Get user input from command line and send it to server: 
-  /*now display the options*/
+    /*now display the options*/
     printf("Welcome to the Tux Math Test Client!\n");
     printf("Type:\n"
              "'game' to start math game;\n"
@@ -122,9 +130,10 @@
              "'quit' to end both client and server\n>\n"); 
     char *check;
     check = fgets(buffer, NET_BUF_LEN, stdin);
-   //Figure out if we are trying to quit:
-    if(  (strncmp(buffer, "exit",4) == 0)
-      || (strncmp(buffer, "quit",4) == 0))
+
+    //Figure out if we are trying to quit:
+    if(  (strncmp(buffer, "exit", 4) == 0)
+      || (strncmp(buffer, "quit", 4) == 0))
     {
       quit = 1;
       len = strlen(buffer) + 1;
@@ -136,7 +145,7 @@
     }
     else if (strncmp(buffer, "game",4) == 0)
     {
-      printf("Starting math game:\n");
+      printf("Starting Tux, of the Math Command Line ;-)\n");
       playgame();
       printf("Math game finished.\n");
     }
@@ -256,8 +265,12 @@
   char buf[NET_BUF_LEN];
   char buffer[NET_BUF_LEN];
   char ch;
+  char* term;
   size_t bytes_read = 0;
 
+  /* Set stdin to be non-blocking: */
+  fcntl(0, F_SETFL, fcntl(0, F_GETFL, 0) | O_NONBLOCK);
+
 #ifdef LAN_DEBUG
   printf("Entering playgame()\n");
 #endif
@@ -276,38 +289,7 @@
    printf("Sent the game notification %s\n",buffer);
  #endif
  
-//NOTE don't think we want to just stick this SDL_Net_TCP_Recv()
-// here because it could block.  Just wait for it in our message queue
-// below. If we receive "Sorry..." then we just have play_game() return.
-//
-//   if( SDLNet_TCP_Recv(sd, buf, sizeof(buf)) > 0)
-//   {
-//    if(strncmp(buf,"Sorry",5) == 0)
-//    {
-//      printf("Sorry , the game has already been started.......=(\n");
-//      SDLNet_TCP_Close(sd);
-//      SDLNet_FreeSocketSet(set);
-//      set=NULL; //this helps us remember that this set is not allocated
-//      SDLNet_Quit();
-//      exit(5);
-//    }
-//    else if(strncmp(buf,"Success",7) == 0)
-//    {
-//     printf("You Are In game.....Waiting for other players to be ready...\n");
-//    }
-//   }
-//   else
-//   {
-//     fprintf(stderr, "In play_game(), SDLNet_TCP_Recv() failed!\n");
-//     exit(EXIT_FAILURE);
-//   }
-// 
-// #ifdef LAN_DEBUG
-//   printf("Received %s\n",buf);
-// #endif
 
-
-
   //Begin game loop:
   while (!end)
   {
@@ -337,7 +319,7 @@
         if(SDLNet_SocketReady(sd))
         {
           buf[0] = '\0';
-          x = SDLNet_TCP_Recv(sd, buf, sizeof(buf));
+          x = SDLNet_TCP_Recv(sd, buf, NET_BUF_LEN);
           if( x <= 0)
           {
             fprintf(stderr, "In play_game(), SDLNet_TCP_Recv() failed!\n");
@@ -357,15 +339,18 @@
           command[i] = '\0';
 
 #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)
           {
             /* 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");
           }
@@ -382,16 +367,18 @@
     } // End of loop for checking server activity
 
 #ifdef LAN_DEBUG
-    printf(".\n");
+//    printf(".\n");
 #endif
 
     //Now we check for any user responses
-    while(have_question && !end)
+//    while(have_question && !end)
     { 
       buf[0] = '\0';
-      fcntl(0, F_SETFL, fcntl(0, F_GETFL, 0) | O_NONBLOCK);
       bytes_read = fread (buf, 1, NET_BUF_LEN, stdin);
-
+      term = strchr(buf, '\n');
+      if (term)
+        *term = '\0';
+      
 #ifdef LAN_DEBUG
 //      printf("\nbytes_read is %d\n", bytes_read);
 //      printf("buf is %s\n", buf);
@@ -418,22 +405,20 @@
           a valid int, not just '0' - should not be a big deal for
           our test program - DSB */
           ans = atoi(buf);
-          if(ans == flash.answer)
+          if(have_question && (ans == flash.answer))
           {  
             have_question = 0;
-#ifdef LAN_DEBUG
-            printf("requesting next question, buf: %s", buf);
-#endif
+            printf("%s is correct!\nRequesting next question...\n>\n", buf);
 
-           //Tell server we answered it right:
-           if(!LAN_AnsweredCorrectly(&flash))
+            //Tell server we answered it right:
+            if(!LAN_AnsweredCorrectly(&flash))
             {
               printf("Unable to communicate the same to server\n");
               exit(EXIT_FAILURE);
             }
           }
           else  //we got input, but not the correct answer:
-            printf("Sorry, incorrect. Try again!\n");
+            printf("Sorry, %s is incorrect. Try again!\n>\n", buf);
         }  //input wasn't any of our keywords
       } // Input was received 
     }  // End of while loop

Modified: tuxmath/branches/lan/server/transtruct.h
===================================================================
--- tuxmath/branches/lan/server/transtruct.h	2009-07-04 00:26:59 UTC (rev 1140)
+++ tuxmath/branches/lan/server/transtruct.h	2009-07-04 03:11:11 UTC (rev 1141)
@@ -14,7 +14,7 @@
 #ifndef TRANSTRUCT_H
 #define TRANSTRUCT_H
 
-#define LAN_DEBUG
+//#define LAN_DEBUG
 #define NET_BUF_LEN 512
 #define DEFAULT_PORT 4779
 #define NAME_SIZE 50




More information about the Tux4kids-commits mailing list