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