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