Browse Source

select, adding

Iman Anwarzai 1 year ago
parent
commit
1325220d28
10 changed files with 55 additions and 18 deletions
  1. 7 12
      Makefile
  2. BIN
      boggle
  3. 0 0
      demo_server
  4. BIN
      dictionary.o
  5. BIN
      game.o
  6. 47 5
      game_server.c
  7. 1 1
      game_server.h
  8. BIN
      user_data
  9. BIN
      users.o
  10. BIN
      words.o

+ 7 - 12
Makefile

@@ -3,17 +3,12 @@ CFLAGS = -Wall -std=gnu99
 
 default: boggle
 
-boggle: game.o words.o users.o dictionary.o
-	$(CC) $(CFLAGS) -o boggle game.o words.o users.o dictionary.o
-
-game.o: game.c utils.h
-	$(CC) $(CFLAGS) -c game.c
-words.o: words.c utils.h dictionary.h
-	$(CC) $(CFLAGS) -c words.c dictionary.c
-users.o: users.c utils.h
-	$(CC) $(CFLAGS) -c users.c
-dictionary.o: dictionary.c
-	$(CC) $(CFLAGS) -c dictionary.c
+boggle: game_server.o players.o 
+	$(CC) $(CFLAGS) -o boggle game_server.o players.o 
 
+game_server.o: game_server.c game_server.h
+	$(CC) $(CFLAGS) -c game_server.c
+players.o: players.c game_server.h
+	$(CC) $(CFLAGS) -c players.c
 clean:
-	rm game.o words.o users.o
+	rm players.o game_server.o 

BIN
boggle


+ 0 - 0
demo_server


BIN
dictionary.o


BIN
game.o


+ 47 - 5
game_server.c

@@ -18,13 +18,51 @@ int main(int argc, char* argv[]) {
     int listenfd = setup();
     
     //TODO install timer signal handler
-   
+	struct sigaction signal_handler = timer_handler;
+	sigemptyset(&signal_handler.sa_mask);
+	signal_handler.sa_flags = 0;
+	sigaction(SIGALRM, &signal_handler, NULL);
+	
+	//timeval for timeout
+	struct timeval tv;
+    	tv.tv_sec = TIMER_TICK - 1; 
+    	//struct timeval t
+	tv.tv_usec = 0;
+	   
 	//start timer
     alarm(TIMER_TICK);
 
 	//TODO: implement select()
     while (1) {
-        
+        fd_set fdlist; 
+ 	//maxfd = serv_fd;
+	int maxfd = listenfd+ 1;
+	FD_ZERO(&fdlist);
+	FD_SET(listenfd, &fdlist);
+
+	Client * curr = client_list;
+	while (curr != NULL){
+		FD_SET(curr->fd, &fdlist);
+		if (curr->fd + 1 > maxfd)
+			maxfd = curr->fd;
+	}
+
+	if (select(maxfd + 1, &fdlist, NULL, NULL, &tv) < 0) {
+		perror("select");
+	} 
+	else {
+		curr = client_list;
+		while(curr)
+			if (FD_ISSET(curr->fd, &fdlist))
+				break;
+		//if (p)
+		//	handle(p->fd); 
+		if (FD_ISSET(listenfd, &fdlist))
+			new_connection(maxfd);
+	}
+	tv.tv_sec = TIMER_TICK - 1;
+        //struct timeval t
+        tv.tv_usec = 0;
     }
     return 0;
 }
@@ -77,7 +115,7 @@ void timer_handler(int sig) {
     }
 	
 	//TODO - generate new game board
-	
+	generate_board();	
 	//reset the timer so we get called again in 120 seconds
 	alarm(TIMER_TICK);
 }
@@ -111,7 +149,10 @@ void add_client(int fd, struct in_addr addr){
     p->state = NAME; //needs yet to identify new client by name
     p->inbuf = 0;
     //TODO - add it to the list of clients
-
+	Client * temp = client_list;
+	while(client_list->next)
+		temp = client_list->next;
+	temp->next = p;
     
 	sendclient(p, "What is your player name?\n");
 }
@@ -201,7 +242,8 @@ int do_command(struct client * p, int cmd_argc, char **cmd_argv) {
         return -1;
     } else if (strcmp(cmd_argv[0], "all_players") == 0 && cmd_argc == 1) {
 		//TODO produce list of all players and their stats in nice text format and sendclient
-        
+        	Player * 
+		sendclient(p, 
     } else if (strcmp(cmd_argv[0], "top_3") == 0 && cmd_argc == 1) {
 		//TODO produce list of top 3 players with their total score and sentclient as a text
        

+ 1 - 1
game_server.h

@@ -107,4 +107,4 @@ int do_command(struct client * p, int cmd_argc, char **cmd_argv);
 
 int find_network_newline(char *buf, int inbuf);
 
-#endif
+#endif

BIN
user_data


BIN
users.o


BIN
words.o