[Tux4kids-commits] r1022 - tuxmath/branches/lan/src
akash gangil
gangil-guest at alioth.debian.org
Wed Jun 10 15:23:51 UTC 2009
Author: gangil-guest
Date: 2009-06-10 15:23:51 +0000 (Wed, 10 Jun 2009)
New Revision: 1022
Modified:
tuxmath/branches/lan/src/Makefile.am
tuxmath/branches/lan/src/game.c
tuxmath/branches/lan/src/mathcards.c
tuxmath/branches/lan/src/network.c
tuxmath/branches/lan/src/network.h
tuxmath/branches/lan/src/titlescreen.c
Log:
Added some more functions to network.c
Modified: tuxmath/branches/lan/src/Makefile.am
===================================================================
--- tuxmath/branches/lan/src/Makefile.am 2009-06-10 01:39:22 UTC (rev 1021)
+++ tuxmath/branches/lan/src/Makefile.am 2009-06-10 15:23:51 UTC (rev 1022)
@@ -73,10 +73,10 @@
linewrap.h \
loaders.h \
network.h \
- mathcards.h \
+ titlescreen.h \
options.h \
setup.h \
- titlescreen.h \
+ mathcards.h \
campaign.h \
multiplayer.h \
tuxmath.h \
Modified: tuxmath/branches/lan/src/game.c
===================================================================
--- tuxmath/branches/lan/src/game.c 2009-06-10 01:39:22 UTC (rev 1021)
+++ tuxmath/branches/lan/src/game.c 2009-06-10 15:23:51 UTC (rev 1022)
@@ -2501,7 +2501,7 @@
static int prev_city = -1;
int i, found;
float y_spacing;
- extern int n;
+ //extern int n;
/* Look for a free comet slot and see if all live comets are far */
/* enough down to avoid overlap and keep formulas legible: */
@@ -2534,25 +2534,25 @@
/* Get math question for new comet - the following function fills in */
/* the flashcard struct that is part of the comet struct: */
// if(n==1)
- // {
- if (!MC_NextQuestion(&(comets[found].flashcard)))
- {
+// {
+ if (!MC_NextQuestion(&(comets[found].flashcard)))
+ {
/* no more questions available - cannot create comet. */
return 0;
+ }
+/* if(!SendQuestion(&(comets[found].flashcard)))
+ {
+ printf("Unable to send Question\n");
+ }
}
-// if(!SendQuestion(&(comets[found].flashcard)))
-// {
-// printf("Unable to send Question\n");
-// }
-// }
-// if(n==0)
-// {
-// SDL_Delay(5000);
-// if(!ReceiveQuestion(&(comets[found].flashcard)))
-// printf("unable to recv question\n");
-// }
-
+ if(n==0)
+ {
+ SDL_Delay(5000);
+ if(!ReceiveQuestion(&(comets[found].flashcard)))
+ printf("unable to recv question\n");
+ }
+*/
/* If we make it to here, create a new comet! */
comets[found].answer = comets[found].flashcard.answer;
Modified: tuxmath/branches/lan/src/mathcards.c
===================================================================
--- tuxmath/branches/lan/src/mathcards.c 2009-06-10 01:39:22 UTC (rev 1021)
+++ tuxmath/branches/lan/src/mathcards.c 2009-06-10 15:23:51 UTC (rev 1022)
@@ -99,6 +99,8 @@
"END_OF_OPTS"
};
+
+
const int MC_DEFAULTS[] = {
1, //PLAY_THROUGH_LIST
1, //QUESTION_COPIES
@@ -177,6 +179,7 @@
#define NPRIMES 9
const int smallprimes[NPRIMES] = {2, 3, 5 ,7, 11, 13, 17, 19, 23};
const char operchars[4] = "+-*/";
+extern int n;
MC_Options* math_opts = 0;
MC_MathQuestion* question_list = 0;
@@ -320,6 +323,7 @@
/* successfully. */
int MC_StartGame(void)
{
+
mcdprintf("\nEntering MC_StartGame()");
/* if math_opts not set up yet, initialize it: */
@@ -364,11 +368,23 @@
mcdprintf("max answer, formula size: %d, %d\n",
max_answer_size, max_formula_size);
/* set up new list with pointer to top: */
- question_list = generate_list();
+ if(n==1) //if selects server , n==1 from titlescreen.c
+ {
+ question_list = generate_list();
- next_wrong_quest = 0;
- /* initialize counters for new game: */
- quest_list_length = list_length(question_list);
+ next_wrong_quest = 0;
+ /* initialize counters for new game: */
+ quest_list_length = list_length(question_list);
+
+ SendQuestionList(question_list,quest_list_length);
+ }
+
+if(n==0) //if selects client , n==0 from titlescreen.c
+{
+ next_wrong_quest = 0;
+ ReceiveQuestionList(question_list,quest_list_length);
+
+}
/* Note: the distinction between quest_list_length and */
/* unanswered is that the latter includes questions */
/* that are currently "in play" by the user interface - */
Modified: tuxmath/branches/lan/src/network.c
===================================================================
--- tuxmath/branches/lan/src/network.c 2009-06-10 01:39:22 UTC (rev 1021)
+++ tuxmath/branches/lan/src/network.c 2009-06-10 15:23:51 UTC (rev 1022)
@@ -20,7 +20,7 @@
extern char host[1024];
extern char port[1024];
-int SendQuestion(MC_FlashCard* fc)
+int SendQuestion(MC_FlashCard* fc) //function to send a flashcard from the server to the client
{
char *ch;
IPaddress ip; //int *remoteip;
@@ -28,8 +28,6 @@
//Uint32 ipaddr;
Uint16 portnum;
-//char message[1024];
-//char waiting[]="WAITING FOR OTHER PLAYER(minimum 2 players required)";
// initialize SDL
if(SDL_Init(0)==-1)
@@ -78,7 +76,7 @@
// read the buffer from client
SDLNet_TCP_Send(client,fc->formula_string,4);
- SDLNet_TCP_Recv(client,ch,1);
+ SDLNet_TCP_Recv(client,ch,1); //will send in the next item only when the first one is received
if(*ch=='1')
{
SDLNet_TCP_Send(client,fc->answer_string,4);
@@ -101,21 +99,6 @@
- //if(!len)
- //{
- // printf("SDLNet_TCP_Recv: %s\n",SDLNet_GetError());
- // continue;
- //}
-
- // print out the message
- //printf("Received: %.*s\n",len,message);
-
- //if(message[0]=='Q')
- //{
- // printf("Quitting on a Q received\n");
- // break;
- //}
- //break;
}
// SDLNet_TCP_Close(client);
@@ -128,7 +111,7 @@
}
-int ReceiveQuestion(MC_FlashCard* fc)
+int ReceiveQuestion(MC_FlashCard* fc) //function for the client to receive the flashcard "from" the server
{
IPaddress ip;
@@ -161,21 +144,25 @@
//connect to the "host" @ port "portnum"
sock=SDLNet_TCP_Open(&ip);
-if(!sock)
+
+while(!sock)
{
-printf("SDLNet_TCP_Open: %s\n",SDLNet_GetError());
-exit(4);
-}
-
+ if(!sock)
+ {
+ printf("SDLNet_TCP_Open: %s\n",SDLNet_GetError());
+ sock=SDLNet_TCP_Open(&ip);
- SDLNet_TCP_Recv(sock,fc->formula_string,4); // add 1 for the NULL
- SDLNet_TCP_Send(sock,"1",1);
+ continue;
+ }
+}
+
+ SDLNet_TCP_Recv(sock,fc->formula_string,4);
+ SDLNet_TCP_Send(sock,"1",1); // send a conformation that the 1st item has been received
SDLNet_TCP_Recv(sock,fc->answer_string,4);
SDLNet_TCP_Send(sock,"1",1);
SDLNet_TCP_Recv(sock,&(fc->answer),4);
SDLNet_TCP_Send(sock,"1",1);
SDLNet_TCP_Recv(sock,&(fc->difficulty),4);
-// SDLNet_TCP_Send(sock,"1",1);
@@ -191,10 +178,178 @@
+int SendInt(int x)
+{
+IPaddress ip;
+TCPsocket server,client;
+Uint16 portnum;
+char *ch;
+ch=(char*)x;
+// initialize SDL
+if(SDL_Init(0)==-1)
+{
+printf("SDL_Init: %s\n",SDL_GetError());
+exit(1);
+}
+// initialize SDL_net
+if(SDLNet_Init()==-1)
+{
+printf("SDLNet_Init: %s\n",SDLNet_GetError());
+exit(2);
+}
+
+portnum=(Uint16)strtol(port,NULL,0);
+
+// Resolve the argument into an IPaddress type
+if(SDLNet_ResolveHost(&ip,NULL,portnum)==-1)
+{
+printf("SDLNet_ResolveHost: %s\n",SDLNet_GetError());
+exit(3);
+}
+
+// open the server socket
+server=SDLNet_TCP_Open(&ip);
+if(!server)
+{
+printf("SDLNet_TCP_Open: %s\n",SDLNet_GetError());
+return 1;
+}
+
+while(1)
+{
+
+// try to accept a connection
+ client=SDLNet_TCP_Accept(server);
+ if(!client)
+ { // no connection accepted
+ //printf("SDLNet_TCP_Accept: %s\n",SDLNet_GetError());
+ SDL_Delay(100); //sleep 1/10th of a second
+ continue;
+ }
+
+
+ // read the buffer from client
+ SDLNet_TCP_Send(client,ch,1);
+
+
+
+}
+
+// SDLNet_TCP_Close(client);
+// SDLNet_TCP_Close(server);
+ // shutdown SDL_net
+// SDLNet_Quit();
+
+
+return 0;
+}
+
+int ReceiveInt(int x)
+{
+
+IPaddress ip;
+TCPsocket server,client;
+Uint16 portnum;
+char *ch;
+
+// initialize SDL
+if(SDL_Init(0)==-1)
+{
+printf("SDL_Init: %s\n",SDL_GetError());
+exit(1);
+}
+
+// initialize SDL_net
+if(SDLNet_Init()==-1)
+{
+printf("SDLNet_Init: %s\n",SDLNet_GetError());
+exit(2);
+}
+
+
+portnum=(Uint16)strtol(port,NULL,0);
+
+// Resolve the argument into an IPaddress type
+if(SDLNet_ResolveHost(&ip,NULL,portnum)==-1)
+{
+printf("SDLNet_ResolveHost: %s\n",SDLNet_GetError());
+exit(3);
+}
+
+// open the server socket
+server=SDLNet_TCP_Open(&ip);
+if(!server)
+{
+printf("SDLNet_TCP_Open: %s\n",SDLNet_GetError());
+return 1;
+}
+
+while(1)
+{
+
+// try to accept a connection
+ client=SDLNet_TCP_Accept(server);
+ if(!client)
+ { // no connection accepted
+ //printf("SDLNet_TCP_Accept: %s\n",SDLNet_GetError());
+ SDL_Delay(100); //sleep 1/10th of a second
+ continue;
+ }
+
+
+ // read the buffer from client
+ SDLNet_TCP_Recv(client,ch,1);
+ x=(int)(*ch); //typecasting so as to convert the char to int
+
+
+}
+
+// SDLNet_TCP_Close(client);
+// SDLNet_TCP_Close(server);
+ // shutdown SDL_net
+// SDLNet_Quit();
+
+
+return 0;
+}
+
+
+int SendQuestionList(MC_MathQuestion* ql,int list_length) // function to send in the MC_MathQuestion data structure
+{ // this helps in sending the question list from the server to the client
+ int randomizer_value;
+ randomizer_value=ql->randomizer;
+ SendInt(randomizer_value);
+ SendInt(list_length);
+ while(ql!=NULL)
+ {
+ SendQuestion(&(ql->card));
+ ql=ql->next;
+ }
+ return 0;
+}
+
+
+int ReceiveQuestionList(MC_MathQuestion* ql,int list_length) // function to receive in the MC_MathQuestion data structure
+{ // this helps in receiving the question list
+ int m;
+ ReceiveInt(ql->randomizer);
+ ReceiveInt(list_length);
+ for(m=list_length;m>0;m--)
+ {
+ ReceiveQuestion(&(ql->card));
+ ql->next=NULL;
+ ql=ql->next;
+ }
+ return 0;
+}
+
+
+
+
int lan_client_connect(char *host,char *port) //here "host" is either the hostname or the ipaddress(of the server) in string
{
char message[]="Client got connected";
@@ -233,7 +388,7 @@
while(!sock)
{
- while( SDL_PollEvent( &event ) )
+ while(SDL_PollEvent(&event))
{
if(event.type==SDL_KEYDOWN)
{
@@ -244,6 +399,8 @@
if(!sock)
{
printf("SDLNet_TCP_Open: %s\n",SDLNet_GetError());
+ sock=SDLNet_TCP_Open(&ip);
+
continue;
}
}
Modified: tuxmath/branches/lan/src/network.h
===================================================================
--- tuxmath/branches/lan/src/network.h 2009-06-10 01:39:22 UTC (rev 1021)
+++ tuxmath/branches/lan/src/network.h 2009-06-10 15:23:51 UTC (rev 1022)
@@ -15,5 +15,8 @@
int lan_client_connect(char *host,char *port);
int SendQuestion(MC_FlashCard* fc);
int ReceiveQuestion(MC_FlashCard* fc);
-
+int ReceiveInt(int x);
+int SendInt(int x);
+int SendQuestionList(MC_MathQuestion* ql,int list_length);
+int ReceiveQuestionList(MC_MathQuestion* ql,int list_length);
#endif // NETWORK_H
Modified: tuxmath/branches/lan/src/titlescreen.c
===================================================================
--- tuxmath/branches/lan/src/titlescreen.c 2009-06-10 01:39:22 UTC (rev 1021)
+++ tuxmath/branches/lan/src/titlescreen.c 2009-06-10 15:23:51 UTC (rev 1022)
@@ -2835,6 +2835,8 @@
}
else
{l=lan_client_connect(host,port);
+ if(l==7)
+ return 7;
printf("##############%d#################\n",l);
return 0;
}
More information about the Tux4kids-commits
mailing list