]> git.cworth.org Git - ttt/blob - src/ttt-client.c
24738649e2f52990dd3631e48e1eee9183c28d40
[ttt] / src / ttt-client.c
1 /* ttt-client.c - client handling code for 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-client.h"
23
24 #include "ttt-error.h"
25 #include "ttt-lex.h"
26 #include "ttt-server.h"
27 #include "ttt-socket.h"
28 #include "ttt-token.h"
29
30 struct _ttt_client {
31     pthread_mutex_t mutex;
32     pthread_t       thread;
33
34     ttt_server_t    *server;
35     int             socket;
36     yyscan_t        scanner;
37
38     char            **request_strings;
39     int             num_request_strings;
40
41     char            *name;
42     ttt_bool_t      registered;
43     int             num_wins;
44 };
45
46 typedef ttt_error_t (*ttt_command_func_t) (ttt_client_t *client,
47                                            char **args,
48                                            int num_args);
49
50 static ttt_error_t
51 _ttt_client_execute_helo (ttt_client_t *client,
52                           char         **args,
53                           int          num_args);
54
55 static ttt_error_t
56 _ttt_client_execute_who (ttt_client_t *client,
57                          char         **args,
58                          int          num_args);
59
60 static ttt_error_t
61 _ttt_client_execute_quit (ttt_client_t *client,
62                           char         **args,
63                           int          num_args);
64
65 typedef struct _ttt_command_description {
66     const char         *command;
67     int                args_required;
68     ttt_command_func_t execute;
69 } ttt_command_description_t;
70
71 ttt_command_description_t command_descriptions[] = {
72     {"HELO", 1, _ttt_client_execute_helo},
73     {"WHO",  0, _ttt_client_execute_who },
74     {"QUIT", 0, _ttt_client_execute_quit}
75 };
76 #define ARRAY_SIZE(arr) (sizeof(arr)/sizeof(arr[0]))
77
78 static ttt_error_t
79 _ttt_client_execute_helo (ttt_client_t *client,
80                           char         **args,
81                           int          num_args)
82 {
83     ttt_error_t error;
84     char *response;
85
86     assert (num_args == 1);
87
88     ttt_client_set_name (client, args[0]);
89
90     error = ttt_server_register_client (client->server, client);
91     if (error)
92         return error;
93
94     client->registered = TRUE;
95
96     xasprintf (&response, "HELO %s %s %s\n",
97                client->name,
98                ttt_server_get_host (client->server),
99                ttt_server_get_port (client->server));
100
101     ttt_client_send (client, response);
102
103     free (response);
104
105     return TTT_ERROR_NONE;
106 }
107
108 static ttt_error_t
109 _ttt_client_execute_who (ttt_client_t *client,
110                          char         **args,
111                          int          num_args)
112 {
113     const char *response;
114
115     assert (num_args == 0);
116
117     response = ttt_server_who (client->server);
118
119     ttt_client_send (client, response);
120
121     free (response);
122
123     return TTT_ERROR_NONE;
124 }
125
126 static ttt_error_t
127 _ttt_client_execute_quit (ttt_client_t   *client,
128                           char          **args,
129                           int             num_args)
130 {
131     assert (num_args == 0);
132
133     return TTT_ERROR_QUIT_REQUESTED;
134 }
135
136 static void
137 _free_request (ttt_client_t *client);
138
139 static void
140 _ttt_client_init (ttt_client_t  *client,
141                   ttt_server_t  *server,
142                   int            socket)
143 {
144     FILE *file;
145
146     pthread_mutex_init (&client->mutex, NULL);
147
148     client->server = server;
149     client->socket = socket;
150
151     file = xfdopen (socket, "r");
152     yylex_init(&client->scanner);
153     yyset_in (file, client->scanner);
154
155     client->request_strings = NULL;
156     client->num_request_strings = 0;
157
158     client->name = NULL;
159     client->registered = FALSE;
160     client->num_wins = 0;
161 }
162
163 static void
164 _ttt_client_fini (ttt_client_t *client)
165 {
166     pthread_mutex_lock (&client->mutex);
167
168     if (client->registered)
169         ttt_server_unregister_client (client->server, client);
170
171     free (client->name);
172     client->name = NULL;
173
174     yylex_destroy (client->scanner);
175     shutdown (client->socket, SHUT_RDWR);
176
177     _free_request (client);
178
179     pthread_mutex_unlock (&client->mutex);
180
181     pthread_mutex_destroy (&client->mutex);
182 }
183
184 /* XXX: The memory management for the request strings is pretty cheesy. */
185 static void
186 _append_to_request (ttt_client_t        *client,
187                     const char          *string)
188 {
189     client->num_request_strings++;
190     client->request_strings =
191         xrealloc (client->request_strings,
192                   client->num_request_strings * sizeof (char *));
193
194     client->request_strings[client->num_request_strings - 1] = xstrdup (string);
195 }
196
197 static void
198 _free_request (ttt_client_t *client)
199 {
200     int i;
201
202     for (i = 0; i < client->num_request_strings; i++)
203         free (client->request_strings[i]);
204
205     free (client->request_strings);
206
207     client->request_strings = NULL;
208     client->num_request_strings = 0;
209 }
210
211 static ttt_status_t
212 _read_request (ttt_client_t *client)
213 {
214     ttt_token_t token;
215
216     _free_request (client);
217
218     while (1) {
219         token = yylex (client->scanner);
220         /* Yes, EOF in two different enums is pretty ugly. */
221         if (token == TTT_TOKEN_EOF)
222             return TTT_STATUS_EOF;
223
224         if (token == TTT_TOKEN_NEWLINE) {
225             if (client->num_request_strings)
226                 return TTT_STATUS_SUCCESS;
227             else
228                 continue;
229         }
230
231         assert (token == TTT_TOKEN_STRING);
232
233         _append_to_request (client, yyget_text (client->scanner));
234     }
235 }
236
237 static ttt_error_t
238 _execute_request (ttt_client_t *client)
239 {
240     int i;
241
242     char *command = client->request_strings[0];
243     int num_args = client->num_request_strings-1;
244     ttt_command_description_t *desc;
245
246     for (i=0; i < strlen (command); i++)
247         command[i] = toupper (command[i]);
248
249     for (i=0; i < ARRAY_SIZE(command_descriptions); i++) {
250         desc = &command_descriptions[i];
251         if (strcmp(command, desc->command) == 0) {
252             if (num_args != desc->args_required)
253                 return TTT_ERROR_SYNTAX;
254             return (desc->execute) (client, &client->request_strings[1], num_args);
255             }
256         }
257
258     return TTT_ERROR_COMMAND;
259 }
260
261 static void *
262 _handle_requests_thread (void *closure)
263 {
264     ttt_status_t status;
265     ttt_error_t error;
266     ttt_client_t *client = closure;
267
268     while (1) {
269
270         status = _read_request (client);
271         if (status == TTT_STATUS_EOF)
272             break;
273         if (status)
274             ASSERT_NOT_REACHED;
275
276         error = _execute_request (client);
277         if (error == TTT_ERROR_QUIT_REQUESTED)
278             break;
279         if (error)
280             ttt_client_send (client, ttt_error_string (error));
281     }
282
283     _ttt_client_fini (client);
284     free (client);
285
286     return (void *) 0;
287 }
288
289 /* Exported: See ttt-client.h for documentation. */
290 void
291 ttt_client_new (void *closure, int client_socket)
292 {
293     ttt_server_t *server = closure;
294     ttt_client_t *client;
295     int err;
296
297     client = xmalloc (sizeof (ttt_client_t));
298     
299     _ttt_client_init (client, server, client_socket);
300
301     err = pthread_create (&client->thread, NULL,
302                           _handle_requests_thread, client);
303     if (err != 0) {
304         fprintf (stderr, "Error: pthread_create failed: %s. Aborting.\n",
305                  strerror (err));
306         exit (1);
307     }
308 }
309
310 /* Exported: See ttt-client.h for documentation. */
311 void
312 ttt_client_send (ttt_client_t *client, const char *message)
313 {
314     pthread_mutex_lock (&client->mutex);
315
316     ttt_socket_write (client->socket, message, strlen (message));
317
318     pthread_mutex_unlock (&client->mutex);
319 }
320
321 /* Exported: See ttt-client.h for documentation. */
322 const char*
323 ttt_client_get_name (ttt_client_t *client)
324 {
325     return client->name;
326 }
327
328 /* Exported: See ttt-client.h for documentation. */
329 void
330 ttt_client_set_name (ttt_client_t *client, const char *name)
331 {
332     free (client->name);
333     client->name = xstrdup (name);
334 }
335
336 /* Exported: See ttt-client.h for documentation. */
337 int
338 ttt_client_get_num_wins (ttt_client_t *client)
339 {
340     return client->num_wins;
341 }