]> git.cworth.org Git - ttt/blob - src/ttt-server.c
2005-11-11 Carl Worth <cworth@cworth.org>
[ttt] / src / ttt-server.c
1 /* ttt-server.c - tic-tac-toe game server
2  *
3  * Copyright © 2005 Carl Worth
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2, or (at your option)
8  * any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software Foundation,
17  * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18  *
19  * Author: Carl Worth <cworth@cworth.org>
20  */
21
22 #include "ttt.h"
23 #include "ttt-client.h"
24 #include "ttt-socket.h"
25
26 struct _ttt_server {
27     pthread_mutex_t mutex;
28
29     ttt_client_t **clients;
30     int num_clients;
31 };
32
33 static void
34 ttt_server_init (ttt_server_t *server)
35 {
36     pthread_mutex_init (&server->mutex, NULL);
37
38     server->clients = NULL;
39     server->num_clients = 0;
40 }
41
42 static void
43 ttt_server_add_client (ttt_server_t *server, ttt_client_t *client)
44 {
45     pthread_mutex_lock (&server->mutex);
46
47     server->num_clients++;
48     server->clients = xrealloc (server->clients,
49                                 server->num_clients * sizeof (ttt_client_t *));
50
51     server->clients [server->num_clients - 1] = client;
52
53     pthread_mutex_unlock (&server->mutex);
54 }
55
56 static void
57 _accept_client (void *closure, int client_socket)
58 {
59     ttt_server_t *server = closure;
60     ttt_client_t *client;
61     
62     client = ttt_client_create (server, client_socket);
63
64     ttt_server_add_client (server, client);
65
66     ttt_client_handle_requests (client);
67 }
68
69 static const char *WELCOME_MESSAGE = 
70 "Welcome to ttt-server. So far, this program is simply a demonstration\n"
71 "of a TCP/IP server capable of handling multiple simultaneous clients.\n"
72 "The server is currently listening on:\n"
73 "\n     %s:%s\n"
74 "\nTo test this program, simply connect a client to that host and port.\n"
75 "For example:\n"
76 "\n     telnet %s %s\n"
77 "\nOnce you have connected a client, the server will echo any characters\n"
78 "it receives back to the client as well as to stdout.\n"
79 "\nNote that to terminate the telnet client you type Control-], then\n"
80 "<Enter>, then \"close\" (and <Enter>) at the \"telnet> \" prompt.\n"
81 "\nHave fun!\n"
82 "-Carl\n"
83 "\nPS. The server does support multiple clients, but there is not yet any\n"
84 "interaction between the clients. The next step is probably to turn the\n"
85 "server into a simple chat server. After that, we should have the necessary\n"
86 "structure in place to start implementing the real tic-tac-toe protocol.\n\n";
87
88 int 
89 main (int argc, char **argv)
90 {
91     ttt_args_t args;
92     ttt_server_t server;
93     int socket;
94
95     ttt_args_parse (&args, argc, argv);
96
97     if (args.log_file)
98         xfreopen (args.log_file, "a", stderr);
99
100     socket = ttt_socket_create_server (args.host, args.port);
101
102     printf (WELCOME_MESSAGE, args.host, args.port, args.host, args.port);
103
104     ttt_server_init (&server);
105
106     while (1)
107         ttt_socket_accept (socket, _accept_client, &server);
108
109     /* We only reach here if something bad happened. */
110     return 1;
111 }