1 /* ttt-client.c - client handling code for tic-tac-toe game server
3 * Copyright © 2005 Carl Worth
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)
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.
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.
19 * Author: Carl Worth <cworth@cworth.org>
22 #include "ttt-client.h"
24 #define YY_DECL int yylex (yyscan_t yyscanner, ttt_token_t *token)
26 #include "ttt-error.h"
28 #include "ttt-server.h"
29 #include "ttt-socket.h"
30 #include "ttt-token.h"
33 pthread_mutex_t mutex;
40 char **request_strings;
41 int num_request_strings;
44 ttt_bool_t registered;
48 typedef ttt_error_t (*ttt_command_func_t) (ttt_client_t *client,
53 _ttt_client_execute_helo (ttt_client_t *client,
58 _ttt_client_execute_who (ttt_client_t *client,
63 _ttt_client_execute_message (ttt_client_t *client,
68 _ttt_client_execute_help (ttt_client_t *client,
73 _ttt_client_execute_version (ttt_client_t *client,
78 _ttt_client_execute_quit (ttt_client_t *client,
82 typedef struct _ttt_command_description {
86 ttt_command_func_t execute;
87 } ttt_command_description_t;
89 ttt_command_description_t command_descriptions[] = {
90 {"HELO", 1, 1, _ttt_client_execute_helo },
91 {"WHO", 0, 0, _ttt_client_execute_who },
92 {"MESSAGE", 1, 1, _ttt_client_execute_message},
93 {"HELP", 0, 1, _ttt_client_execute_help },
94 {"VERSION", 1, 1, _ttt_client_execute_version},
95 {"QUIT", 0, 0, _ttt_client_execute_quit }
98 #define ARRAY_SIZE(arr) (sizeof(arr)/sizeof(arr[0]))
101 _ttt_client_execute_helo (ttt_client_t *client,
109 assert (num_args == 1);
111 ttt_client_set_name (client, args[0]);
113 error = ttt_server_register_client (client->server, client);
116 client->registered = TRUE;
118 xasprintf (&response, "HELO %s %s %s\r\n",
120 ttt_server_get_host (client->server),
121 ttt_server_get_port (client->server));
122 ttt_client_send (client, response);
124 xasprintf (¬ice, "NOTICE USER %s\r\n",
126 ttt_server_broadcast (client->server, notice);
131 return TTT_ERROR_NONE;
135 _ttt_client_execute_who (ttt_client_t *client,
141 assert (num_args == 0);
143 if (!client->registered)
144 return TTT_ERROR_NO_NAME_SET;
146 response = xstrdup (ttt_server_who (client->server));
147 ttt_client_send (client, response);
151 return TTT_ERROR_NONE;
155 _ttt_client_execute_message (ttt_client_t *client,
162 assert (num_args == 1);
164 if (!client->registered)
165 return TTT_ERROR_NO_NAME_SET;
167 xasprintf(&response, "MESSAGE\r\n");
168 ttt_client_send (client, response);
170 xasprintf(¬ice, "NOTICE MESSAGE %s \"%s\"\r\n",
173 ttt_server_broadcast (client->server, notice);
178 return TTT_ERROR_NONE;
182 _ttt_client_execute_help (ttt_client_t *client,
190 xasprintf(&response, "HELP - NYI\r\n");
191 ttt_client_send (client, response);
194 return TTT_ERROR_NONE;
198 _ttt_client_execute_version (ttt_client_t *client,
204 assert (num_args == 1);
206 /* XXX: Argument is being ignored.
207 This is not completely implemented. */
209 xasprintf (&response, "VERSION 1\r\n");
210 ttt_client_send (client, response);
213 return TTT_ERROR_NONE;
217 _ttt_client_execute_quit (ttt_client_t *client,
223 assert (num_args == 0);
225 if (!client->registered)
226 return TTT_ERROR_QUIT_REQUESTED;
228 xasprintf (¬ice, "NOTICE QUIT %s\r\n",
230 ttt_server_broadcast (client->server, notice);
234 return TTT_ERROR_QUIT_REQUESTED;
238 _free_request (ttt_client_t *client);
241 _ttt_client_init (ttt_client_t *client,
242 ttt_server_t *server,
247 pthread_mutex_init (&client->mutex, NULL);
249 client->server = server;
250 client->socket = socket;
252 file = xfdopen (socket, "r");
253 yylex_init (&client->scanner);
254 yyset_in (file, client->scanner);
256 client->request_strings = NULL;
257 client->num_request_strings = 0;
260 client->registered = FALSE;
261 client->num_wins = 0;
265 _ttt_client_fini (ttt_client_t *client)
267 pthread_mutex_lock (&client->mutex);
269 if (client->registered)
270 ttt_server_unregister_client (client->server, client);
275 yylex_destroy (client->scanner);
276 shutdown (client->socket, SHUT_RDWR);
278 _free_request (client);
280 pthread_mutex_unlock (&client->mutex);
282 pthread_mutex_destroy (&client->mutex);
285 /* XXX: The memory management for the request strings is pretty cheesy. */
287 _append_to_request (ttt_client_t *client,
290 client->num_request_strings++;
291 client->request_strings =
292 xrealloc (client->request_strings,
293 client->num_request_strings * sizeof (char *));
295 client->request_strings[client->num_request_strings - 1] = xstrdup (string);
299 _free_request (ttt_client_t *client)
303 for (i = 0; i < client->num_request_strings; i++)
304 free (client->request_strings[i]);
306 free (client->request_strings);
308 client->request_strings = NULL;
309 client->num_request_strings = 0;
313 _read_request (ttt_client_t *client)
316 ttt_token_type_t token_type;
318 _free_request (client);
321 token_type = yylex (client->scanner, &token);
322 /* Yes, EOF in two different enums is pretty ugly. */
323 if (token_type == TTT_TOKEN_TYPE_EOF)
324 return TTT_STATUS_EOF;
326 if (token_type == TTT_TOKEN_TYPE_NEWLINE) {
327 if (client->num_request_strings)
328 return TTT_STATUS_SUCCESS;
333 assert (token_type == TTT_TOKEN_TYPE_STRING);
335 _append_to_request (client, token.u.string);
337 free (token.u.string);
342 _execute_request (ttt_client_t *client)
346 char *command = client->request_strings[0];
347 int num_args = client->num_request_strings-1;
348 ttt_command_description_t *desc;
350 for (i=0; i < strlen (command); i++)
351 command[i] = toupper (command[i]);
353 for (i=0; i < ARRAY_SIZE(command_descriptions); i++) {
354 desc = &command_descriptions[i];
355 if (strcmp(command, desc->command) == 0) {
356 if ((num_args < desc->args_min) || (num_args > desc->args_max))
357 return TTT_ERROR_SYNTAX;
358 return (desc->execute) (client, &client->request_strings[1], num_args);
362 return TTT_ERROR_COMMAND;
366 _handle_requests_thread (void *closure)
370 ttt_client_t *client = closure;
374 status = _read_request (client);
375 if (status == TTT_STATUS_EOF)
380 error = _execute_request (client);
381 if (error == TTT_ERROR_QUIT_REQUESTED)
384 ttt_client_send (client, ttt_error_string (error));
387 _ttt_client_fini (client);
393 /* Exported: See ttt-client.h for documentation. */
395 ttt_client_new (void *closure, int client_socket)
397 ttt_server_t *server = closure;
398 ttt_client_t *client;
401 client = xmalloc (sizeof (ttt_client_t));
403 _ttt_client_init (client, server, client_socket);
405 err = pthread_create (&client->thread, NULL,
406 _handle_requests_thread, client);
408 fprintf (stderr, "Error: pthread_create failed: %s. Aborting.\r\n",
414 /* Exported: See ttt-client.h for documentation. */
416 ttt_client_send (ttt_client_t *client, const char *message)
418 pthread_mutex_lock (&client->mutex);
420 ttt_socket_write (client->socket, message, strlen (message));
422 pthread_mutex_unlock (&client->mutex);
425 /* Exported: See ttt-client.h for documentation. */
427 ttt_client_get_name (ttt_client_t *client)
432 /* Exported: See ttt-client.h for documentation. */
434 ttt_client_set_name (ttt_client_t *client, const char *name)
437 client->name = xstrdup (name);
440 /* Exported: See ttt-client.h for documentation. */
442 ttt_client_get_num_wins (ttt_client_t *client)
444 return client->num_wins;