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

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


Author: dbruce-guest
Date: 2009-07-13 18:31:04 +0000 (Mon, 13 Jul 2009)
New Revision: 1195

Modified:
   tuxmath/branches/lan/server/testclient.c
   tuxmath/branches/lan/src/game.c
   tuxmath/branches/lan/src/network.c
   tuxmath/branches/lan/src/network.h
Log:
cleanup and organization of network files



Modified: tuxmath/branches/lan/server/testclient.c
===================================================================
--- tuxmath/branches/lan/server/testclient.c	2009-07-13 04:28:58 UTC (rev 1194)
+++ tuxmath/branches/lan/server/testclient.c	2009-07-13 18:31:04 UTC (rev 1195)
@@ -90,7 +90,7 @@
         exit(EXIT_FAILURE);
       }
     }
-    else if (strncmp(buffer, "game",4) == 0)
+    else if (strncmp(buffer, "game", 4) == 0)
     {
       playgame();
       printf("Math game finished.\n");
@@ -198,7 +198,7 @@
   while(1)
   {
     buf[0] = '\0';
-    status = get_next_msg(buf);
+    status = LAN_NextMsg(buf);
     if (status == -1)  //Fatal error
     {
       printf("Error - get_next_msg() returned -1\n");

Modified: tuxmath/branches/lan/src/game.c
===================================================================
--- tuxmath/branches/lan/src/game.c	2009-07-13 04:28:58 UTC (rev 1194)
+++ tuxmath/branches/lan/src/game.c	2009-07-13 18:31:04 UTC (rev 1195)
@@ -603,9 +603,13 @@
 //  }
 
    /*To function for the above 5 comments*/
-   say_to_server("START_GAME");
+//   say_to_server("START_GAME");
+  if (!LAN_StartGame())
+  {
+    fprintf(stderr, "\nLAN_StartGame() failed!");
+    return 0;
+  }
 
-
   /* Allocate memory */
   comets = NULL;  // set in case allocation fails partway through
   cities = NULL;
@@ -2676,18 +2680,14 @@
 //      return 0;
 //     }
 
-   /* FIXME there's no way this can work. We ask the server for another */
-   /* question, but we don't check the messages before trying to get    */
-   /* the next flash card out of the buffer. So the strncmp() is going  */
-   /* to get done on whatever was in the buffer before. If somehow there*/
-   /* was a leftover "SEND_QUESTION" in there, we will now make a       */
-   /* duplicate comet out of that question.  Otherwise (most likely)    */
-   /* we come back to here next time through the loop and ask for       */
-   /* _another_ question before we have received this one. Either way   */
-   /* it isn't what we want - DSB                                       */
-
- /*Server replacement for the above 5 comments*/
-   say_to_server("NEXT_QUESTION");
+   /* FIXME what we really need here is the capability within network.c to queue  */
+   /* any questions that have been received from the server in check_messages(),  */
+   /* and a function that gives us the next question in the local queue if there  */
+   /* is one. We can't assume that it will arrive from the server right at the    */
+   /* time we happen to need it to make a new comet. So I'm commenting out        */
+   /* the 'say_to_server()' call as well - DSB                                     */
+/*Server replacement for the above 5 comments*/
+//   say_to_server("NEXT_QUESTION");
 //   printf("buf is %s\n",buf);
 //  if(strncmp(command,"SEND_QUESTION",strlen("SEND_QUESTION"))==0) 
 //   {

Modified: tuxmath/branches/lan/src/network.c
===================================================================
--- tuxmath/branches/lan/src/network.c	2009-07-13 04:28:58 UTC (rev 1194)
+++ tuxmath/branches/lan/src/network.c	2009-07-13 18:31:04 UTC (rev 1195)
@@ -33,7 +33,11 @@
 int playgame(void);
 void server_pinged(void);*/
 
+/* Local function prototypes: */
+int say_to_server(char *statement);
+int evaluate(char *statement);
 
+
 int setup_net(char *host, int port)
 {
   IPaddress ip;           /* Server address */
@@ -112,13 +116,11 @@
 
 int say_to_server(char statement[20])
 {
-  int len;
   char buffer[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());
@@ -128,6 +130,7 @@
    return 1;
 }
 
+
 int check_messages(char buf[NET_BUF_LEN])
 { 
   int x = 0, numready;
@@ -165,9 +168,9 @@
 /* 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 LAN_NextMsg(char* buf)
 { 
-  int x = 0, numready = 0;
+  int numready = 0;
 
   /* Make sure we have place to put message: */
   if(buf == NULL)
@@ -268,17 +271,32 @@
 } 
 
 
+int LAN_StartGame(void)
+{
+  char buffer[NET_BUF_LEN];
+  snprintf(buffer, NET_BUF_LEN, 
+                  "%s\n",
+                  "START_GAME");
+  if (SDLNet_TCP_Send(sd, (void *)buffer, NET_BUF_LEN) < NET_BUF_LEN)
+  {
+    fprintf(stderr, "SDLNet_TCP_Send: %s\n", SDLNet_GetError());
+    return 0;
+  }
+#ifdef LAN_DEBUG
+  printf("Sent the game notification %s\n",buffer);
+#endif
+  return 1;
+}
 
+
 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());
@@ -307,6 +325,8 @@
 
 /*This mainly is a network version of all the MathCards Functions
   MC_* that have integer as their return value*/
+/* Looks to me like it just sends "statement".  Again, when we send a  */
+/* message, we can't assume when we are going to get a reply.          */
 int evaluate(char statement[20])
 {
   int ans,x;
@@ -339,27 +359,6 @@
 
 
 
-/*The Ping system is not yet used */
-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
- 
-}
 
 
-

Modified: tuxmath/branches/lan/src/network.h
===================================================================
--- tuxmath/branches/lan/src/network.h	2009-07-13 04:28:58 UTC (rev 1194)
+++ tuxmath/branches/lan/src/network.h	2009-07-13 18:31:04 UTC (rev 1195)
@@ -17,14 +17,28 @@
 #ifndef NETWORK_H
 #define NETWORK_H
 
+
+
+/* Networking setup and cleanup: */
 int setup_net(char *host, int port);
-int get_next_msg(char* buf);
-int say_to_server(char *statement);
-int evaluate(char *statement);
+void cleanup_client(void);
+
+/* Network replacement functions for mathcards "API": */
+/* These functions are how the client tells things to the server: */
+int LAN_StartGame(void);
 int LAN_AnsweredCorrectly(MC_FlashCard* fc);
-void cleanup_client(void);
+
+/* This is how the client receives messages from the server: */
+int LAN_NextMsg(char* buf);
+
+/* Functions to handle various messages from the server: */
+int player_msg_recvd(char* buf);
+
+
+
+/* FIXME appears this one is basically the same as LAN_NextMsg() */
 int check_messages(char *);
-int player_msg_recvd(char* buf);
+/* FIXME this should be local to network.c */
 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