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

David Bruce dbruce-guest at alioth.debian.org
Tue Aug 4 15:05:16 UTC 2009


Author: dbruce-guest
Date: 2009-08-04 15:05:16 +0000 (Tue, 04 Aug 2009)
New Revision: 1355

Modified:
   tuxmath/branches/lan/server/server.c
   tuxmath/branches/lan/server/server.h
   tuxmath/branches/lan/server/testclient.c
Log:
- added timeout to server name entry after which default name is used
- added code so testclient listens for server messages in outer loop as well as during game loop

Modified: tuxmath/branches/lan/server/server.c
===================================================================
--- tuxmath/branches/lan/server/server.c	2009-08-04 11:18:30 UTC (rev 1354)
+++ tuxmath/branches/lan/server/server.c	2009-08-04 15:05:16 UTC (rev 1355)
@@ -22,6 +22,7 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
+#include <fcntl.h> 
  
 #include "server.h" 
 #include "../src/transtruct.h"
@@ -69,10 +70,10 @@
 int transmit(int i, char* msg);
 int transmit_all(char* msg);
 
-//Deprecated:
-// void test_connections(void);
-// void ping_client(int i);
+// For non-blocking input:
+int read_stdin_nonblock(char* buf, size_t max_length);
 
+
 // not really deprecated but not done in response to 
 // client message --needs better name:
 void game_msg_next_question(void);
@@ -109,15 +110,33 @@
   }
 
   /* Get server name: */
+  /* We use default name after 30 sec timeout if no name entered. */
   /* FIXME we should save this to disc so it doesn't */
   /* have to be entered every time.                  */
+
+  server_name[0] = '\0';
+  fcntl(0, F_SETFL, fcntl(0, F_GETFL, 0) | O_NONBLOCK);
+
   printf("Enter the SERVER's NAME: \n>");
   fflush(stdout);
-  fgets(server_name, NAME_SIZE, stdin);
+//  fgets(server_name, NAME_SIZE, stdin);
+  {
+    Uint32 timeout = SDL_GetTicks() + SERVER_NAME_TIMEOUT;
+    int name_recvd = 0;
 
+    while(!name_recvd && (SDL_GetTicks() < timeout))
+    {
+      if(read_stdin_nonblock(server_name, NAME_SIZE))
+        name_recvd = 1;
+    }
+    if(!name_recvd)
+      printf("No name entered within timeout, will use default: %s\n",
+             DEFAULT_SERVER_NAME);
+  }
+
   /* If no nickname received, use default: */
-  if(strlen(server_name) == 1)
-     strcpy(server_name, "TuxMath Server");
+  if(strlen(server_name) == 0)
+     strcpy(server_name, DEFAULT_SERVER_NAME);
   
   printf("Waiting for clients to connect:\n>");
   fflush(stdout);
@@ -1117,66 +1136,34 @@
 
 
 
+//Here we read up to max_length bytes from stdin into the buffer.
+//The first '\n' in the buffer, if present, is replaced with a
+//null terminator.
+//returns 0 if no data ready, 1 if at least one byte read.
+//NOTE for this to work we must first set stdin to O_NONBLOCK with:
+//  fcntl(0, F_SETFL, fcntl(0, F_GETFL, 0) | O_NONBLOCK);
 
+int read_stdin_nonblock(char* buf, size_t max_length)
+{
+  int bytes_read = 0;
+  char* term = NULL;
+  buf[0] = '\0';
 
+  bytes_read = fread (buf, 1, max_length, stdin);
+  term = strchr(buf, '\n');
+  if (term)
+    *term = '\0';
+     
+  if(bytes_read > 0)
+    bytes_read = 1;
+  else
+    bytes_read = 0;
+      
+  return bytes_read;
+}
 
 
 
 
 
 
-/* Code related to "pinging system" for pollng all clients to */
-/* see if they are still connected - we may not need this.    */
-/* (kept out of way here at bottom until we decide)           */
-
-
-
-// Go through and test all the current connections, removing
-// any clients that fail to respond:
-// void test_connections(void)
-// {
-//   int i = 0;
-// 
-//   for (i = 0; i < MAX_CLIENTS; i++)
-//     ping_client(i);
-// }
-
-
-// This is supposed to be a way to test and see if each client
-// is really connected.
-// FIXME I think we need to put in a SDLNet_TCP_Recv() to see
-// if we get a reply, now that the client is modified to send back
-// PING_BACK.  I am worried, however, that we could have a problem
-// with intercepting messages not related to the ping testing - DSB
-
-// void ping_client(int i)
-// {
-//   char buf[NET_BUF_LEN];
-//   char msg[NET_BUF_LEN];
-//   int x;
-// 
-//   if(i < 0 || i > MAX_CLIENTS)
-//   {
-//     printf("ping_client() - invalid index argument\n");
-//     return;
-//   }
-//   
-//   if(client[i].sock == NULL)
-//   {
-//     return;
-//   }
-//   
-// //  sprintf(msg,"%s", "PING\n");
-// //  snprintf(buf, NET_BUF_LEN, "%s\t%s\n", "SEND_MESSAGE", msg);
-//   snprintf(buf, NET_BUF_LEN, "%s\n", "PING");
-//   x = SDLNet_TCP_Send(client[i].sock, buf, NET_BUF_LEN);
-//   if(x < NET_BUF_LEN)
-//   {
-//    printf("The client %s is disconnected\n",client[i].name);
-//    remove_client(i);
-//   }
-// //#ifdef LAN_DEBUG
-//   printf("buf is: %s\n", buf);
-//   printf("SendMessage() - buf sent:::: %d bytes\n", x);
-// //#endif
-// }

Modified: tuxmath/branches/lan/server/server.h
===================================================================
--- tuxmath/branches/lan/server/server.h	2009-08-04 11:18:30 UTC (rev 1354)
+++ tuxmath/branches/lan/server/server.h	2009-08-04 15:05:16 UTC (rev 1355)
@@ -12,12 +12,14 @@
 */
 
 #ifndef SERVER_H
-#define SERVER_H
+#define SERVER_H == 1)
 
+#include "SDL_net.h"
 
-#include "SDL_net.h"
+
 #define NAME_SIZE 50
 #define DEFAULT_SERVER_NAME "TuxMath LAN Server"
+#define SERVER_NAME_TIMEOUT 30000
 
 typedef struct client_type {
   int game_ready;                 //game_ready = 1 , if client has said OK to start, and 0 otherwise

Modified: tuxmath/branches/lan/server/testclient.c
===================================================================
--- tuxmath/branches/lan/server/testclient.c	2009-08-04 11:18:30 UTC (rev 1354)
+++ tuxmath/branches/lan/server/testclient.c	2009-08-04 15:05:16 UTC (rev 1355)
@@ -110,13 +110,6 @@
     }
 
     printf("connected\n");
-
-//    /* Connect to server, create socket set, get player nickname, etc: */
-//    if(!LAN_Setup(server_ip, server_port))
-//    {
-//      printf("setup_client() failed - exiting.\n");
-//      exit(EXIT_FAILURE);
-//    }
   }
   
 
@@ -150,10 +143,12 @@
   fcntl(0, F_SETFL, fcntl(0, F_GETFL, 0) | O_NONBLOCK);
 
 
-  /* FIXME we're not listening for server messages in this loop */
   quit = 0;
   while(!quit)
   { 
+    // See if we have any messages from server:
+    game_check_msgs();
+
     //Get user input from command line and send it to server: 
     /*now display the options*/
     if(read_stdin_nonblock(buffer, NET_BUF_LEN))
@@ -161,17 +156,13 @@
       //Figure out if we are trying to quit:
       if( (strncmp(buffer, "exit", 4) == 0)
         ||(strncmp(buffer, "quit", 4) == 0))
-      //FIXME need a "LAN_Logout() function" so testclient doesn't need to know about SDL_Net
+      
       {
         quit = 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);
-        }*/
       }
       else if (strncmp(buffer, "game", 4) == 0)
       {
+        // Begin the actual math game
         playgame();
         printf("Math game finished.\n\n");
         printf("Type:\n"
@@ -456,6 +447,8 @@
 //The first '\n' in the buffer, if present, is replaced with a
 //null terminator.
 //returns 0 if no data ready, 1 if at least one byte read.
+//NOTE for this to work we must first set stdin to O_NONBLOCK with:
+//  fcntl(0, F_SETFL, fcntl(0, F_GETFL, 0) | O_NONBLOCK);
 
 int read_stdin_nonblock(char* buf, size_t max_length)
 {




More information about the Tux4kids-commits mailing list