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

David Bruce dbruce-guest at alioth.debian.org
Mon Jun 29 00:54:51 UTC 2009


Author: dbruce-guest
Date: 2009-06-29 00:54:51 +0000 (Mon, 29 Jun 2009)
New Revision: 1106

Modified:
   tuxmath/branches/lan/server/server.c
Log:
commenting and whitespace/indenting cleanup



Modified: tuxmath/branches/lan/server/server.c
===================================================================
--- tuxmath/branches/lan/server/server.c	2009-06-28 23:49:58 UTC (rev 1105)
+++ tuxmath/branches/lan/server/server.c	2009-06-29 00:54:51 UTC (rev 1106)
@@ -113,10 +113,10 @@
   int quit, quit2;
   char buffer[NET_BUF_LEN];
   char buf[NET_BUF_LEN];
-  int command_type = -1,numready,j;
+  int command_type = -1, numready, j;
   static int sockets_used=0;
-  static int i=0;
-  static int num_clients=0;
+  static int i = 0;
+  static int num_clients = 0;
   MC_FlashCard flash;
   static int initialize = 0;
   int id,x;
@@ -177,83 +177,93 @@
       }
     }
 
-    /* This check the sd if there is a pending connection.
+
+
+    /* Now we check to see if anyone is trying to connect.
      * If there is one, accept that, and open a new socket for communicating */
+    /* FIXME how do we know slot i is available??? */
     client[i].sock = SDLNet_TCP_Accept(server_sock);
     if (client[i].sock !=NULL)
     {
-      IPaddress* client_ip = NULL;
 
-     if( SDLNet_TCP_Recv(client[i].sock, buffer, NET_BUF_LEN) > 0)
-     {
-      strncpy(client[i].name, buffer, NAME_SIZE);
-      printf(" JOINED  :::   %s",client[i].name);
-     }
-      printf("this is the value of i = %d\n",i);
-      num_clients++;
-      /* Now we can communicate with the client using client[i].sock socket
-      /* serv_sock will remain opened waiting other connections */
-      /* Get the remote address */
-      client_ip = SDLNet_TCP_GetPeerAddress(client[i].sock);
-      if (client_ip != NULL)
-        /* Print the address, converting in the host format */
-      {
-        printf("Client connected\n>\n");
 #ifdef LAN_DEBUG
-        printf("Client: IP = %x, Port = %d\n",
-	       SDLNet_Read32(&client_ip->host),
-	       SDLNet_Read16(&client_ip->port));
+      printf("creating connection for client[%d].sock:\n", i);
 #endif
-      }
-      else
-        fprintf(stderr, "SDLNet_TCP_GetPeerAddress: %s\n", SDLNet_GetError());
-      sockets_used = SDLNet_TCP_AddSocket(client_set,client[i].sock);
+      sockets_used = SDLNet_TCP_AddSocket(client_set, client[i].sock);
       if(sockets_used == -1) 
       {
         printf("SDLNet_AddSocket: %s\n", SDLNet_GetError());
         // perhaps you need to restart the set and make it bigger...
       }
-      i++;
+      else if( SDLNet_TCP_Recv(client[i].sock, buffer, NET_BUF_LEN) > 0)
+      {
+        strncpy(client[i].name, buffer, NAME_SIZE);
+        printf(" JOINED  :::   %s\n", client[i].name);
+        //FIXME AFAICT, num_clients and i are always the same
+        num_clients++;
+        i++;
+      }
+      /* Now we can communicate with the client using client[i].sock socket
+      /* serv_sock will remain opened waiting other connections */
     
+
+#ifdef LAN_DEBUG
+      /* Get the remote address */
+      {
+        IPaddress* client_ip = NULL;
+        client_ip = SDLNet_TCP_GetPeerAddress(client[i].sock);
+        if (client_ip != NULL)
+        /* Print the address, converting in the host format */
+        {
+          printf("Client connected\n>\n");
+          printf("Client: IP = %x, Port = %d\n",
+	       SDLNet_Read32(&client_ip->host),
+	       SDLNet_Read16(&client_ip->port));
+        }
+        else
+          fprintf(stderr, "SDLNet_TCP_GetPeerAddress: %s\n", SDLNet_GetError());
+      }
+#endif
+
     }//end of *if(client[i].sock = SDLNet_TCP_Accept(sd))*  
-    
   }
 
-num_clients=sockets_used;
+  num_clients = sockets_used;
 
-
-/*This loop sees that the game starts only when all the players are ready */
- for(j=0;j<num_clients;j++)
- {
-  if(client[j].connected!=1)
+  /*FIXME this will only work if the clients are in a contiguous series starting */
+  /* at client[0].                                                             */
+  /*This loop sees that the game starts only when all the players are ready */
+  for(j = 0; j < num_clients; j++)
   {
-   if (SDLNet_TCP_Recv(client[j].sock, buffer, NET_BUF_LEN) > 0)
-   {
-    if(strncmp(buffer,"start",5)==0)
+    if(client[j].connected != 1)
     {
-     client[j].connected=1;
-     snprintf(buf, NET_BUF_LEN, 
+      if (SDLNet_TCP_Recv(client[j].sock, buffer, NET_BUF_LEN) > 0)
+      {
+        if(strncmp(buffer,"start",5)==0)
+        {
+          client[j].connected=1;
+          snprintf(buf, NET_BUF_LEN, 
                 "%s\n",
                 "Success");
-     x = SDLNet_TCP_Send(client[j].sock, buf, sizeof(buf));
+          x = SDLNet_TCP_Send(client[j].sock, buf, sizeof(buf));
+        }
+      }
     }
-   }
   }
- }
 
-/* If no players join the game */
-if(num_clients==0)
-{
- printf("There were no players........=(\n");
- SDLNet_FreeSocketSet(client_set);              //releasing the memory of the client socket set
- client_set=NULL; //this helps us remember that this set is not allocated
- SDLNet_TCP_Close(server_sock);
- SDLNet_Quit();
- exit(1);
-}
+  /* If no players join the game (should not happen) */
+  if(num_clients == 0)
+  {
+    printf("There were no players........=(\n");
+    SDLNet_FreeSocketSet(client_set);              //releasing the memory of the client socket set
+    client_set = NULL; //this helps us remember that this set is not allocated
+    SDLNet_TCP_Close(server_sock);
+    SDLNet_Quit();
+    exit(1);
+  }
 
 #ifdef LAN_DEBUG
-printf("We have %d players.......\n",sockets_used);
+  printf("We have %d players.......\n",sockets_used);
 #endif
 
 
@@ -261,241 +271,239 @@
   if (!MC_StartGame())
   {
     fprintf(stderr, "\nMC_StartGame() failed!");
+    exit(1);
   }
 
 
- if (!MC_NextQuestion(&flash))
- { 
-   /* no more questions available */
-   printf("MC_NextQuestion() returned NULL - no questions available\n");
- }
- else
- {                                     
+  if (!MC_NextQuestion(&flash))
+  { 
+    /* no more questions available */
+    printf("MC_NextQuestion() returned NULL - no questions available\n");
+    exit(1);
+  }
+  else
+  {                                     
 #ifdef LAN_DEBUG
-   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);
+    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);
 #endif
- }
- for(j=0;j<num_clients;j++)
- {
-  if(!SendQuestion(flash,client[j].sock))
-  {
-    printf("Unable to send Question\n");
   }
- } 
 
- quit2 = 0;
- while (!quit2)
- {
-   while(1)       //keep on checking for all the clients in a round robin manner
-   {
-    for(j=0;j<num_clients;j++)                  // keep on looping across the num_clients in a round-robin manner
+  for(j = 0; j < num_clients; j++)
+  {
+    if(!SendQuestion(flash, client[j].sock))
     {
+      printf("Unable to send Question to %s\n", client[j].name);
+    }
+  } 
 
-
-      /* this is mainly to avoid joining of clients after the game has started*/
-      temp_sock = SDLNet_TCP_Accept(server_sock);
-      if (temp_sock !=NULL)
+  quit2 = 0;
+  while (!quit2)
+  {
+    while(1)       //keep on checking for all the clients in a round robin manner
+    {
+      for(j = 0; j < num_clients; j++)                  // keep on looping across the num_clients in a round-robin manner
       {
-
-  snprintf(buf, NET_BUF_LEN, 
+        /* this is only to avoid joining of clients after the game has started: */
+        temp_sock = SDLNet_TCP_Accept(server_sock);
+        if (temp_sock !=NULL)
+        {
+          snprintf(buf, NET_BUF_LEN, 
                 "%s\n",
                 "Sorry the game has started...... =(\n");
-     x = SDLNet_TCP_Send(temp_sock, buf, sizeof(buf));
+          x = SDLNet_TCP_Send(temp_sock, buf, sizeof(buf));
 #ifdef LAN_DEBUG
-  printf("buf sent:::: %d bytes\n", x);
-  printf("buf is: %s\n", buf);
+          printf("buf sent:::: %d bytes\n", x);
+          printf("buf is: %s\n", buf);
 #endif
-    
-      }        
-      /*This loop mainly checks if all the clients have disconnected*/
-      int c;
-      for(c=0;c<num_clients;c++)
-      {
-       if(client[c].connected==1)
-       break;     
-       if(c==(num_clients-1))
+        }        
+        /*This loop mainly checks if all the clients have disconnected*/
+        /* FIXME how do we know the client is really connected?  */
+        int c;
+        for(c = 0; c < num_clients; c++)
         {
-         printf("All the clients have disconnected..=( \n");
-         exit(2);
+          if(client[c].connected == 1)
+            break;     
+          if(c==(num_clients - 1))
+          {
+            printf("All the clients have disconnected..=( \n");
+            exit(2);
+          }
         }
-      }
 
-      /*Implies that this particular client has already quit itself , so move on to other clients*/         
-      if(client[j].connected==0)
-      continue;                                           
-      numready=SDLNet_CheckSockets(client_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) 
-      {
-        printf("There are %d sockets with activity!\n",numready);
-        // check all sockets with SDLNet_SocketReady and handle the active ones.
-        if(SDLNet_SocketReady(client[j].sock)) 
+        /*Implies that this particular client has already quit itself , so move on to other clients*/         
+        if(client[j].connected == 0)
+          continue;                                           
+
+        numready = SDLNet_CheckSockets(client_set, 0);
+
+        if(numready == -1) 
         {
-         if (SDLNet_TCP_Recv(client[j].sock, buffer, NET_BUF_LEN) > 0  )
-         {
-           char command[NET_BUF_LEN];
-           command_type = -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) 
+        {
+          printf("There are %d sockets with activity!\n", numready);
+          // check all sockets with SDLNet_SocketReady and handle the active ones.
+          if(SDLNet_SocketReady(client[j].sock)) 
+          {
+            if (SDLNet_TCP_Recv(client[j].sock, buffer, NET_BUF_LEN) > 0  )
+            {
+              char command[NET_BUF_LEN];
+              command_type = -1;
 #ifdef LAN_DEBUG  
-           printf("Buffer received from client: %s\n", buffer);
+              printf("Buffer received from client: %s\n", buffer);
 #endif
-           sscanf (buffer,"%s %d\n",
+              sscanf (buffer,"%s %d\n",
                          command,
                          &id);  
      
-           if(strncmp(command, "CORRECT_ANSWER",14) == 0)
-           {
-             command_type = CORRECT_ANSWER; 
-             printf("question id %d was answered correctly by %s",id,client[j].name);             
-               if (!MC_NextQuestion(&flash))
-               { 
-                 /* no more questions available */
-                 printf("MC_NextQuestion() returned NULL - no questions available\n");
-               }
-               else
-               {                                     
+              if(strncmp(command, "CORRECT_ANSWER", 14) == 0)
+              {
+                command_type = CORRECT_ANSWER; 
+                printf("question id %d was answered correctly by %s",id,client[j].name);             
+                if (!MC_NextQuestion(&flash))
+                { 
+                  /* no more questions available */
+                  printf("MC_NextQuestion() returned NULL - no questions available\n");
+                }
+                else
+                {                                     
 #ifdef LAN_DEBUG
-                 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);
+                  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);
 #endif
-               }
-               int n;
-               for(n=0;n<num_clients&&client[n].sock;n++)
-               {
-
-                 if(!SendQuestion(flash,client[n].sock))
-                 {
-                   printf("Unable to send Question\n");
-                 }
-               } 
-           }                             
-                                        
-           //'b' for asking for a question(flashcard)                    //Now it has no use , since the clients dont ask question.
-           if(strncmp(command, "next_question",13) == 0)
-           {
+                }
+                int n;
+                for(n = 0; n < num_clients && client[n].sock; n++)
+                {
+                  if(!SendQuestion(flash,client[n].sock))
+                  {
+                    printf("Unable to send Question\n");
+                  }
+                } 
+              }                            
+              //Now it has no use , since the clients dont ask question.
+              else if(strncmp(command, "next_question", 13) == 0)
+              {
 #ifdef LAN_DEBUG
-             printf("received request to send question\n");
+                printf("received request to send question\n");
 #endif
-               command_type = SEND_A_QUESTION;              
-           } 
+                command_type = SEND_A_QUESTION;              
+              } 
 
-           if(strncmp(command, "exit",4) == 0) /* Terminate this connection */
-           {
-             printf("LEFT the GAME : %s",client[j].name);
-             client[j].connected=0;
-             SDLNet_TCP_DelSocket(client_set,client[j].sock);
-             SDLNet_TCP_Close(client[j].sock);
-             printf("Terminating client connection\n");
-           }
+              else if(strncmp(command, "exit",4) == 0) /* Terminate this connection */
+              {
+                printf("LEFT the GAME : %s",client[j].name);
+                client[j].connected=0;
+                SDLNet_TCP_DelSocket(client_set,client[j].sock);
+                SDLNet_TCP_Close(client[j].sock);
+                printf("Terminating client connection\n");
+              }
 
-           if(strncmp(command, "quit",4) == 0) /* Quit the program */
-           {
-             printf("Server has been shut down by %s",client[j].name); 
-             client[j].connected=0;
-             SDLNet_TCP_DelSocket(client_set,client[j].sock);
-             SDLNet_TCP_Close(client[j].sock);
-             quit2 = 1;
-             printf("Quit program....Server is shutting down...\n");
-           }
+              else if(strncmp(command, "quit",4) == 0) /* Quit the program */
+              {
+                printf("Server has been shut down by %s",client[j].name); 
+                client[j].connected=0;
+                SDLNet_TCP_DelSocket(client_set,client[j].sock);
+                SDLNet_TCP_Close(client[j].sock);
+                quit2 = 1;
+                printf("Quit program....Server is shutting down...\n");
+              }
           
-           switch(command_type)
-           {
-//             case NEW_GAME:  //mainly to setup the question list
-//             {
-//               if (!MC_StartGame())
+              switch(command_type)
+              {
+//               case NEW_GAME:  //mainly to setup the question list
 //               {
-//                 fprintf(stderr, "\nMC_StartGame() failed!");
-//               }
-//               if(!SendMessage(LIST_SET_UP,0,client[j].sock))
-//              {
-//                 printf("Unable to communicate to the client\n");
-//               }
-//               break;                                           
-//             } 
+//                 if (!MC_StartGame())
+//                 {
+//                   fprintf(stderr, "\nMC_StartGame() failed!");
+//                 }
+//                 if(!SendMessage(LIST_SET_UP,0,client[j].sock))
+//                 {
+//                   printf("Unable to communicate to the client\n");
+//                 }
+//                 break;                                           
+//               } 
 
-             case CORRECT_ANSWER:
-             {
- //              if(!SendMessage(ANSWER_CORRECT,id,client[j].sock))
- //              {
- //                printf("Unable to communicate to the client\n");
- //              }
-               break;
-             }
+                case CORRECT_ANSWER:
+                {
+ //               if(!SendMessage(ANSWER_CORRECT,id,client[j].sock))
+ //               {
+ //                 printf("Unable to communicate to the client\n");
+ //               }
+                  break;
+                }
 
-             case LIST_NOT_SETUP:                    //to send any message to the client 
-             {              
-               if(!SendMessage(NO_QUESTION_LIST,id,client[j].sock))
-               {
-                 printf("Unable to communicate to the client\n");
-               }
-               break;
-             }
+                case LIST_NOT_SETUP:                    //to send any message to the client 
+                {              
+                  if(!SendMessage(NO_QUESTION_LIST,id,client[j].sock))
+                  {
+                    printf("Unable to communicate to the client\n");
+                  }
+                  break;
+                }
 
-             case SEND_A_QUESTION:
-             {
-               if (!MC_NextQuestion(&flash))
-               { 
-                 /* no more questions available */
-                 printf("MC_NextQuestion() returned NULL - no questions available\n");
-               }
-               else
-               {                                     
+                case SEND_A_QUESTION:
+                {
+                  if (!MC_NextQuestion(&flash))
+                  { 
+                    /* no more questions available */
+                    printf("MC_NextQuestion() returned NULL - no questions available\n");
+                  }
+                  else
+                  {                                     
 #ifdef LAN_DEBUG
-                 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);
+                    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);
 #endif
-                 if(!SendQuestion(flash,client[j].sock))
-                 {
-                   printf("Unable to send Question\n");
-                 }
-               } 
-
-               break;
-             } 
+                    if(!SendQuestion(flash,client[j].sock))
+                    {
+                      printf("Unable to send Question\n");
+                    }
+                  } 
+                  break;
+                } 
         
-             default:
-               break;                             //this *break* comes out of the switch statement
-           }
+                default:
+                  break;                             //this *break* comes out of the switch statement
+              }  // end of switch() statement
 
-         }//if loop
-       }
-    }
+            }//if loop
+          }
+        }
 
-         if(quit2==1)
-         break;   
-
-        }//end of for loop
-        if(quit2==1)
+        if(quit2 == 1)
+          break;   
+      }//end of for loop
+      if(quit2==1)
         break;   
-       }//  end of while(1) loop
-      }//while loop
+    }//  end of while(1) loop
+  }//while loop
 
-      /* Close the client socket */
+  /* Close the client socket */
   
-    for(j=0;j<num_clients;j++)
-    {
-     if(client[j].connected==1)                           //close only those clients that are still connected 
-     SDLNet_TCP_Close(client[j].sock);                //close all the client sockets one by one
-    }          
-    SDLNet_FreeSocketSet(client_set);              //releasing the memory of the client socket set
-    client_set=NULL; //this helps us remember that this set is not allocated
+  for(j = 0; j < num_clients; j++)
+  {
+    if(client[j].connected == 1)                           //close only those clients that are still connected 
+      SDLNet_TCP_Close(client[j].sock);                //close all the client sockets one by one
+  }          
+  SDLNet_FreeSocketSet(client_set);              //releasing the memory of the client socket set
+  client_set=NULL; //this helps us remember that this set is not allocated
       
   
  /* Clean up mathcards heap memory */




More information about the Tux4kids-commits mailing list