[Tux4kids-commits] r1114 - tuxmath/branches/lan/server
akash gangil
gangil-guest at alioth.debian.org
Mon Jun 29 20:50:09 UTC 2009
Author: gangil-guest
Date: 2009-06-29 20:50:09 +0000 (Mon, 29 Jun 2009)
New Revision: 1114
Modified:
tuxmath/branches/lan/server/server.c
tuxmath/branches/lan/server/server.h
Log:
server_cleanup() added , everything functional
Modified: tuxmath/branches/lan/server/server.c
===================================================================
--- tuxmath/branches/lan/server/server.c 2009-06-29 20:22:56 UTC (rev 1113)
+++ tuxmath/branches/lan/server/server.c 2009-06-29 20:50:09 UTC (rev 1114)
@@ -110,8 +110,8 @@
int find_vacant_client(void);
int SendQuestion(MC_FlashCard flash, TCPsocket client_sock);
int SendMessage(int message, int z, TCPsocket client_sock);
+int server_cleanup(void);
-
int main(int argc, char **argv)
{
int h;
@@ -333,78 +333,27 @@
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);
-#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");
- }
- }
}
else if(strncmp(command, "exit",4) == 0) /* Terminate this connection */
{
- printf("LEFT the GAME : %s",client[j].name);
- client[j].game_ready=0;
- SDLNet_TCP_DelSocket(client_set,client[j].sock);
- SDLNet_TCP_Close(client[j].sock);
- printf("Terminating client connection\n");
+ command_type = EXIT;
}
else if(strncmp(command, "quit",4) == 0) /* Quit the program */
{
- printf("Server has been shut down by %s",client[j].name);
- client[j].game_ready=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");
+ command_type = QUIT;
}
switch(command_type)
{
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 SEND_A_QUESTION:
- {
+ 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");
+ /* no more questions available */
+ printf("MC_NextQuestion() returned NULL - no questions available\n");
}
else
{
@@ -416,14 +365,42 @@
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");
- }
+ }
+ int n;
+ for(n = 0; n < num_clients && client[n].sock; n++)
+ {
+ if(!SendQuestion(flash,client[n].sock))
+ {
+ printf("Unable to send Question\n");
+ }
}
+
break;
- }
-
+ }
+
+ case EXIT:
+ {
+ printf("LEFT the GAME : %s",client[j].name);
+ client[j].game_ready=0;
+ SDLNet_TCP_DelSocket(client_set,client[j].sock);
+ SDLNet_TCP_Close(client[j].sock);
+ printf("Terminating client connection\n");
+
+ break;
+ }
+
+ case QUIT:
+ {
+ printf("Server has been shut down by %s",client[j].name);
+ client[j].game_ready=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");
+
+ break;
+ }
+
default:
break; //this *break* comes out of the switch statement
} // end of switch() statement
@@ -439,22 +416,6 @@
break;
}// end of while(1) loop
}//while loop
-
- /* Close the client socket */
-
- for(j = 0; j < num_clients; j++)
- {
- if(client[j].game_ready == 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 */
- MC_EndGame();
- SDLNet_TCP_Close(server_sock);
- SDLNet_Quit();
return EXIT_SUCCESS;
}
@@ -625,6 +586,28 @@
}
+//Free resources, closing sockets, call MC_EndGame(), and so forth:
+int server_cleanup(void)
+{
+
+ int j;
+ /* Close the client socket(s) */
+
+ for(j = 0; j < num_clients; j++)
+ {
+ if(client[j].game_ready == 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 */
+ MC_EndGame();
+ SDLNet_TCP_Close(server_sock);
+ SDLNet_Quit();
+}
+
//Returns the index of the first vacant client, or -1 if all clients full
int find_vacant_client(void)
{
Modified: tuxmath/branches/lan/server/server.h
===================================================================
--- tuxmath/branches/lan/server/server.h 2009-06-29 20:22:56 UTC (rev 1113)
+++ tuxmath/branches/lan/server/server.h 2009-06-29 20:50:09 UTC (rev 1114)
@@ -27,9 +27,8 @@
/*enum for commands coming from the client side*/
enum {
- NEW_GAME,
- SEND_A_QUESTION,
- LIST_NOT_SETUP,
+ EXIT,
+ QUIT,
CORRECT_ANSWER
};
More information about the Tux4kids-commits
mailing list