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