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