[Tux4kids-commits] r1054 - tuxmath/branches/lan/server

David Bruce dbruce-guest at alioth.debian.org
Tue Jun 16 03:59:13 UTC 2009


Author: dbruce-guest
Date: 2009-06-16 03:59:12 +0000 (Tue, 16 Jun 2009)
New Revision: 1054

Modified:
   tuxmath/branches/lan/server/mathcards.c
   tuxmath/branches/lan/server/server.c
   tuxmath/branches/lan/server/testclient.c
   tuxmath/branches/lan/server/transtruct.h
Log:
setting up messaging system



Modified: tuxmath/branches/lan/server/mathcards.c
===================================================================
--- tuxmath/branches/lan/server/mathcards.c	2009-06-16 03:23:52 UTC (rev 1053)
+++ tuxmath/branches/lan/server/mathcards.c	2009-06-16 03:59:12 UTC (rev 1054)
@@ -201,7 +201,7 @@
 int length_time_per_question_list = 0;
 int length_alloc_time_per_question_list = 0;
 
-const MC_FlashCard DEFAULT_CARD = {NULL,NULL,0,0}; //empty card to signal error
+const MC_FlashCard DEFAULT_CARD = {0,0,0,0}; //empty card to signal error
 
 /* "private" function prototypes:                        */
 /*                                                       */
@@ -1870,36 +1870,41 @@
 MC_FlashCard MC_AllocateFlashcard(void)
 {
   MC_FlashCard ret;
-  mcdprintf("Allocating %d + %d bytes for flashcard\n",
-            max_formula_size + 1, max_answer_size + 1);
-  ret.formula_string = malloc( (max_formula_size + 1) * sizeof(char));
-  ret.answer_string = malloc( (max_answer_size + 1) * sizeof(char));
-  if (!ret.formula_string || !ret.answer_string)
-    {
-    free(ret.formula_string);
-    free(ret.answer_string);
-    printf("Couldn't allocate space for a new flashcard!\n");
-    ret = DEFAULT_CARD;
-    }
+
+//NOTE strings now simply hard-coded to MC_FORMULA_LEN (= 40) and
+//MC_ANSWER_LEN (= 5) instead of tailoring them to save a few bytes - DSB
+//  mcdprintf("Allocating %d + %d bytes for flashcard\n",
+//            max_formula_size + 1, max_answer_size + 1);
+//  ret.formula_string = malloc( (max_formula_size + 1) * sizeof(char));
+//  ret.answer_string = malloc( (max_answer_size + 1) * sizeof(char));
+//   if (!ret.formula_string || !ret.answer_string)
+//     {
+//     free(ret.formula_string);
+//     free(ret.answer_string);
+//     printf("Couldn't allocate space for a new flashcard!\n");
+//     ret = DEFAULT_CARD;
+//     }
   return ret;
 }
 
+//Now a no-op - MC_FlashCard no longer has dynamically allocated strings
 void MC_FreeFlashcard(MC_FlashCard* fc)
 {
-  if (!fc)
-    return;
-//  mcdprintf("Freeing formula_string\n");
-  if (fc->formula_string)
-  {
-    free(fc->formula_string);
-    fc->formula_string = NULL;
-  }
-//  mcdprintf("Freeing answer_string\n");
-  if (fc->answer_string)
-  {
-    free(fc->answer_string);
-    fc->answer_string = NULL;
-  }
+  return;
+//   if (!fc)
+//     return;
+// //  mcdprintf("Freeing formula_string\n");
+//   if (fc->formula_string)
+//   {
+//     free(fc->formula_string);
+//     fc->formula_string = NULL;
+//   }
+// //  mcdprintf("Freeing answer_string\n");
+//   if (fc->answer_string)
+//   {
+//     free(fc->answer_string);
+//     fc->answer_string = NULL;
+//   }
 }
 
 unsigned int MC_MapTextToIndex(const char* text)

Modified: tuxmath/branches/lan/server/server.c
===================================================================
--- tuxmath/branches/lan/server/server.c	2009-06-16 03:23:52 UTC (rev 1053)
+++ tuxmath/branches/lan/server/server.c	2009-06-16 03:59:12 UTC (rev 1054)
@@ -31,10 +31,10 @@
 { 
   IPaddress ip, *remoteIP;
   int quit, quit2;
-  char buffer[512];
+  char buffer[NET_BUF_LEN];
   int network_function = -1;
-  MC_FlashCard* fc;
   //     size_t length;
+  MC_FlashCard flash;
 
         
   if (SDLNet_Init() < 0)
@@ -44,7 +44,7 @@
   }
  
   /* Resolving the host using NULL make network interface to listen */
-  if (SDLNet_ResolveHost(&ip, NULL, 4778) < 0)
+  if (SDLNet_ResolveHost(&ip, NULL, DEFAULT_PORT) < 0)
   {
     fprintf(stderr, "SDLNet_ResolveHost: %s\n", SDLNet_GetError());
     exit(EXIT_FAILURE);
@@ -80,7 +80,7 @@
       quit2 = 0;
       while (!quit2)
       {
-        if (SDLNet_TCP_Recv(csd, buffer, 512) > 0)
+        if (SDLNet_TCP_Recv(csd, buffer, NET_BUF_LEN) > 0)
         {
           network_function = -1;
           printf("Client say: %s\n", buffer);
@@ -96,7 +96,20 @@
           {
             network_function = SEND_A_QUESTION;              
           } 
- 
+
+          if(strcmp(buffer, "exit") == 0) /* Terminate this connection */
+          {
+            quit2 = 1;
+            printf("Terminate connection\n");
+          }
+
+          if(strcmp(buffer, "quit") == 0) /* Quit the program */
+          {
+            quit2 = 1;
+            quit = 1;
+            printf("Quit program\n");
+          }
+
           switch(network_function)
           {
             case SETUP_QUESTION_LIST:  //mainly to setup the question list
@@ -112,63 +125,33 @@
 
             case SEND_A_QUESTION:
             {
-              fc = (MC_FlashCard *)malloc(sizeof(MC_FlashCard));
-              if (fc == NULL) 
-              {
-                printf("Allocation of comets failed");
-                return 0;
+              if (!MC_NextQuestion(&flash))
+              { 
+                /* no more questions available */
+                printf("MC_NextQuestion() returned NULL - no questions available\n");
               }
-              else 
-              {
-                *fc = MC_AllocateFlashcard();
-                if (!MC_FlashCardGood(fc) ) 
+              else
+              {                                     
+                printf("WILL SEND >>\n");  
+                printf("QUESTION_ID       :      %d\n", flash.question_id);
+                printf("FORMULA_STRING    :      %s\n", flash.formula_string);
+                printf("ANSWER STRING     :      %s\n", flash.answer_string);
+                printf("ANSWER            :      %d\n",flash.answer);
+                printf("DIFFICULTY        :      %d\n",flash.difficulty);
+
+                if(!SendQuestion(flash))
                 {
-                  //something's wrong
-                  printf("Allocation of flashcard failed\n");
-                  MC_FreeFlashcard(fc);
-                  return 0;
+                  printf("Unable to send Question\n");
                 }
-              }
-              //      fc->answer_string="";
-              //      fc->formula_string="";
- 
-              if (!MC_NextQuestion(fc))
-              { 
-                /* no more questions available - cannot create comet.  */
-                return 0;
-              }
-                                                   
-              printf("WILL SEND >>\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);
+              } 
 
-              if(!SendQuestion(fc))
-              {
-                printf("Unable to send Question\n");
-              }
-                                                 
               break;
-            }                             
+            } 
         
             default:
               break;
           }
 
-          if(strcmp(buffer, "exit") == 0) /* Terminate this connection */
-          {
-            quit2 = 1;
-            printf("Terminate connection\n");
-          }
-
-          if(strcmp(buffer, "quit") == 0) /* Quit the program */
-          {
-            quit2 = 1;
-            quit = 1;
-            printf("Quit program\n");
-          }
         }
       }
  
@@ -185,27 +168,44 @@
 
 
 //function to send a flashcard(question) from the server to the client
-int SendQuestion(MC_FlashCard* fc)
+int SendQuestion(MC_FlashCard flash)
 {
   char *ch;
   int x;
 
-  x = SDLNet_TCP_Send(csd, &(fc->question_id), sizeof(fc->question_id));
+  char buf[NET_BUF_LEN];
+  snprintf(buf, NET_BUF_LEN, 
+                "%s\t%d\t%d\t%d\t%s\t%s\n",
+                "SEND_QUESTION",
+                flash.question_id,
+                flash.difficulty,
+                flash.answer,
+                flash.answer_string,
+                flash.formula_string);
+  printf("buf is: %s\n", buf);
+  x = SDLNet_TCP_Send(csd, buf, sizeof(buf));
+  printf("SendQuestion() - buf sent:::: %d bytes\n", x);
+
+
+  //old code:
+  x = SDLNet_TCP_Send(csd, &(flash.question_id), sizeof(flash.question_id));
   printf("no:(1):::QUESTION_ID::::Sent %d bytes\n", x);
       
-  x = SDLNet_TCP_Send(csd,&(fc->difficulty),sizeof(fc->difficulty));
+  x = SDLNet_TCP_Send(csd,&(flash.difficulty),sizeof(flash.difficulty));
   printf("no:(2):::DIFFICULTY::::Sent %d bytes\n", x);
 
-  x = SDLNet_TCP_Send(csd, &(fc->answer), sizeof(fc->answer));
+  x = SDLNet_TCP_Send(csd, &(flash.answer), sizeof(flash.answer));
   printf("no:(3)::::ANSWER:::Sent %d bytes\n",x);
 
-  x = SDLNet_TCP_Send(csd, fc->answer_string, strlen(fc->answer_string) + 1);
+  x = SDLNet_TCP_Send(csd, flash.answer_string, strlen(flash.answer_string) + 1);
   printf("no:(4):::ANSWER_STRING::::Sent %d bytes\n", x);
 
-  x = SDLNet_TCP_Send(csd, fc->formula_string, strlen(fc->formula_string) + 1);
+  x = SDLNet_TCP_Send(csd, flash.formula_string, strlen(flash.formula_string) + 1);
   printf("no:(5):::FORMULA_STRING::::Sent %d bytes\n", x);
     
   return 1;
+
+
 }
 
 

Modified: tuxmath/branches/lan/server/testclient.c
===================================================================
--- tuxmath/branches/lan/server/testclient.c	2009-06-16 03:23:52 UTC (rev 1053)
+++ tuxmath/branches/lan/server/testclient.c	2009-06-16 03:59:12 UTC (rev 1054)
@@ -26,14 +26,17 @@
 #include "mathcards.c"
 
 TCPsocket sd;           /* Socket descriptor */
+MC_FlashCard flash;
 
 int main(int argc, char **argv)
 {
   IPaddress ip;           /* Server address */
   int quit, len;
-  char buffer[512];
+  char buffer[512];  // for command-line input
+  char buf[512];     // for network messages from server
   MC_FlashCard* fc;
-      
+  int x, i = 0;
+
   /* Simple parameter checking */
   if (argc < 3)
   {
@@ -65,9 +68,15 @@
   quit = 0;
   while (!quit)
   {
+    //Get user input from command line and send it to server: 
     printf("Write something:\n>");
     scanf("%s", buffer);
- 
+
+    if(strcmp(buffer, "exit") == 0)
+      quit = 1;
+    if(strcmp(buffer, "quit") == 0)
+      quit = 1;
+
     len = strlen(buffer) + 1;
     if (SDLNet_TCP_Send(sd, (void *)buffer, len) < len)
     {
@@ -75,35 +84,26 @@
       exit(EXIT_FAILURE);
     }
 
-    if(strcmp(buffer,"b") == 0)
-    { 
-      fc = (MC_FlashCard *)malloc(sizeof(MC_FlashCard));
-        
-      if (fc == NULL) 
-      {
-        printf("Allocation of comets failed");
-        return 0;
-      }
-      else 
-      {
-        *fc = MC_AllocateFlashcard();
-        if (!MC_FlashCardGood(fc) ) 
-        {
-          //something's wrong
-          printf("Allocation of flashcard failed\n");
-          MC_FreeFlashcard(fc);
-          return 0;
-        }
-      }
+    //Now we check for any responses from server:
+    //FIXME have to make this not block so we can keep checking until all the messages are gone
+    //do
+    {
+      char command[NET_BUF_LEN];
+      int i = 0;
+      x = SDLNet_TCP_Recv(sd, buf, sizeof(buf));
 
-      if(!RecvQuestion(fc))
-        printf("unable to recv question\n"); 
-    }  
+      /* Copy the command name out of the tab-delimited buffer: */
+      for (i = 0; buf[i] != '\t' && i < NET_BUF_LEN; i++)
+        command[i] = buf[i];
+      command[i] = '\0';
 
-    if(strcmp(buffer, "exit") == 0)
-      quit = 1;
-    if(strcmp(buffer, "quit") == 0)
-      quit = 1;
+      /* Now we process the buffer according to the command: */
+      if(strcmp(command, "") == 0)
+
+
+      
+    }// while (x > 0);
+
   }
  
   SDLNet_TCP_Close(sd);
@@ -114,18 +114,18 @@
 
 
 //function to receive a flashcard(question) by the client
-int RecvQuestion(MC_FlashCard* fc)
+int RecvQuestion(void)
 {
   char ch[5];
-  int x,i=0;
+  int x, i = 0;
 
-  x = SDLNet_TCP_Recv(sd, &(fc->question_id), sizeof(fc->question_id));
+  x = SDLNet_TCP_Recv(sd, &(flash.question_id), sizeof(flash.question_id));
   printf("no:(1):::QUESTION_ID::::Received %d bytes\n",x);
  
-  x = SDLNet_TCP_Recv(sd, &(fc->difficulty), sizeof(fc->difficulty));
+  x = SDLNet_TCP_Recv(sd, &(flash.difficulty), sizeof(flash.difficulty));
   printf("no:(2):::DIFFICULTY::::Received %d bytes\n",x);
  
-  x = SDLNet_TCP_Recv(sd, &(fc->answer), sizeof(fc->answer));
+  x = SDLNet_TCP_Recv(sd, &(flash.answer), sizeof(flash.answer));
   printf("no:(3):::ANSWER::::Received %d bytes\n",x);
 
   do{
@@ -134,17 +134,17 @@
     i++;
   }while(ch[i-1]!='\0');
 
-  strncpy(fc->answer_string, ch, i + 1);
+  strncpy(flash.answer_string, ch, i + 1);
 
-  x = SDLNet_TCP_Recv(sd, fc->formula_string, 13);
+  x = SDLNet_TCP_Recv(sd, flash.formula_string, 13);
 
   printf("no:(5):::FORMULA_STRING::::Received %d bytes\n",x);
   printf("RECEIVED >>\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);  
+  printf("QUESTION_ID    >>          %d\n",flash.question_id);  
+  printf("FORMULA_STRING >>          %s\n",flash.formula_string);  
+  printf("ANSWER_STRING  >>          %s\n",flash.answer_string);  
+  printf("ANSWER         >>          %d\n",flash.answer);  
+  printf("DIFFICULTY     >>          %d\n",flash.difficulty);  
        
   return 1;
 }

Modified: tuxmath/branches/lan/server/transtruct.h
===================================================================
--- tuxmath/branches/lan/server/transtruct.h	2009-06-16 03:23:52 UTC (rev 1053)
+++ tuxmath/branches/lan/server/transtruct.h	2009-06-16 03:59:12 UTC (rev 1054)
@@ -15,7 +15,11 @@
 #define TRANSTRUCT_H
 
 #define MC_USE_NEWARC
+#define MC_FORMULA_LEN 40
+#define MC_ANSWER_LEN 5
 
+#define NET_BUF_LEN 512
+#define DEFAULT_PORT 4778
 
 #ifndef MC_USE_NEWARC
 /* struct for individual "flashcard" */
@@ -31,8 +35,8 @@
 #else
 /* experimental struct for a more generalized flashcard */
 typedef struct _MC_FlashCard {
-  char* formula_string;
-  char* answer_string;
+  char formula_string[MC_FORMULA_LEN];
+  char answer_string[MC_ANSWER_LEN];
   int question_id;
   int answer;
   int difficulty;




More information about the Tux4kids-commits mailing list