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_statistics (ttt_client_t *client,
68 _ttt_client_execute_message (ttt_client_t *client,
73 _ttt_client_execute_help (ttt_client_t *client,
78 _ttt_client_execute_version (ttt_client_t *client,
83 _ttt_client_execute_quit (ttt_client_t *client,
87 typedef struct _ttt_command_description {
91 ttt_command_func_t execute;
93 const char *description;
94 } ttt_command_description_t;
96 ttt_command_description_t command_descriptions[] = {
97 {"HELO", 1, 1, _ttt_client_execute_helo,
98 "HELO <username> ", "Register."},
100 {"HELP", 0, 1, _ttt_client_execute_help,
101 "HELP <command> ", "Display help for a command."},
103 {"MESSAGE", 1, 1, _ttt_client_execute_message,
104 "MESSAGE <message> ", "Send a message to everyone."},
106 {"STATISTICS", 1, 1, _ttt_client_execute_statistics,
107 "STATISTICS <username> ", "Lists the statistics for the specified user."},
109 {"QUIT", 0, 0, _ttt_client_execute_quit,
110 "QUIT ", "Quit session."},
112 {"VERSION", 1, 1, _ttt_client_execute_version,
113 "VERSION <client-version>", "Negotiate protocol version."},
115 {"WHO", 0, 0, _ttt_client_execute_who,
116 "WHO ", "List registered users."}
119 #define ARRAY_SIZE(arr) (sizeof(arr)/sizeof(arr[0]))
122 _ttt_client_execute_helo (ttt_client_t *client,
130 assert (num_args == 1);
132 ttt_client_set_username (client, args[0]);
134 error = ttt_server_register_client (client->server, client);
137 client->registered = TRUE;
139 xasprintf (&response, "HELO %s %s %s\r\n",
141 ttt_server_get_host (client->server),
142 ttt_server_get_port (client->server));
143 ttt_client_send (client, response);
145 xasprintf (¬ice, "NOTICE USER %s\r\n",
147 ttt_server_broadcast (client->server, notice);
152 return TTT_ERROR_NONE;
156 _ttt_client_execute_who (ttt_client_t *client,
162 assert (num_args == 0);
164 if (!client->registered)
165 return TTT_ERROR_NO_NAME_SET;
167 response = xstrdup (ttt_server_who (client->server));
168 ttt_client_send (client, response);
172 return TTT_ERROR_NONE;
176 _ttt_client_execute_statistics (ttt_client_t *client,
184 assert (num_args == 1);
188 if (!client->registered)
189 return TTT_ERROR_NO_NAME_SET;
191 error = ttt_server_statistics (client->server, username, &response);
195 ttt_client_send (client, response);
199 return TTT_ERROR_NONE;
203 _ttt_client_execute_message (ttt_client_t *client,
210 assert (num_args == 1);
212 if (!client->registered)
213 return TTT_ERROR_NO_NAME_SET;
215 xasprintf (&response, "MESSAGE\r\n");
216 ttt_client_send (client, response);
218 xasprintf (¬ice, "NOTICE MESSAGE %s \"%s\"\r\n",
221 ttt_server_broadcast (client->server, notice);
226 return TTT_ERROR_NONE;
230 _ttt_client_execute_help (ttt_client_t *client,
236 ttt_command_description_t *desc;
238 ttt_bool_t is_command = FALSE;
241 xasprintf (&response, "HELP \"\r\n"
242 "Available Commands:\r\n");
243 for (i = 0; i < ARRAY_SIZE(command_descriptions); i++) {
244 desc = &command_descriptions[i];
245 xasprintf (&response, "%s\r\n %s - %s\r\n",
250 xasprintf (&response, "%s\"\r\n", response);
253 for (i = 0; i < strlen (command); i++)
254 command[i] = toupper (command[i]);
255 for (i = 0; i < ARRAY_SIZE(command_descriptions); i++) {
256 desc = &command_descriptions[i];
257 if (strcmp (desc->command, command) == 0) {
259 xasprintf (&response, "HELP %s \"\r\n"
268 /* XXX: Add detailed help. */
273 if ((num_args == 1) && (!is_command))
274 return TTT_ERROR_SYNTAX;
276 ttt_client_send (client, response);
279 return TTT_ERROR_NONE;
283 _ttt_client_execute_version (ttt_client_t *client,
292 assert (num_args == 1);
294 clientversion = args[0];
296 /* Verify that provided version arg is a positive integer */
297 for (i = 0; i < strlen(clientversion); i++)
298 if (!isdigit (clientversion[i]))
299 return TTT_ERROR_SYNTAX;
301 version = atoi (clientversion);
304 return TTT_ERROR_SYNTAX;
306 if (version > TTT_SERVER_PROTOCOL_VERSION)
307 version = TTT_SERVER_PROTOCOL_VERSION;
309 xasprintf (&response, "VERSION %d\r\n", version);
310 ttt_client_send (client, response);
313 return TTT_ERROR_NONE;
317 _ttt_client_execute_quit (ttt_client_t *client,
323 assert (num_args == 0);
325 if (!client->registered)
326 return TTT_ERROR_QUIT_REQUESTED;
328 xasprintf (¬ice, "NOTICE QUIT %s\r\n",
330 ttt_server_broadcast (client->server, notice);
334 return TTT_ERROR_QUIT_REQUESTED;
338 _free_request (ttt_client_t *client);
341 _ttt_client_init (ttt_client_t *client,
342 ttt_server_t *server,
347 pthread_mutex_init (&client->mutex, NULL);
349 client->server = server;
350 client->socket = socket;
352 file = xfdopen (socket, "r");
353 yylex_init (&client->scanner);
354 yyset_in (file, client->scanner);
356 client->request_strings = NULL;
357 client->num_request_strings = 0;
359 client->username = NULL;
360 client->registered = FALSE;
361 client->num_wins = 0;
365 _ttt_client_fini (ttt_client_t *client)
367 pthread_mutex_lock (&client->mutex);
369 if (client->registered)
370 ttt_server_unregister_client (client->server, client);
372 free (client->username);
373 client->username = NULL;
375 yylex_destroy (client->scanner);
376 shutdown (client->socket, SHUT_RDWR);
378 _free_request (client);
380 pthread_mutex_unlock (&client->mutex);
382 pthread_mutex_destroy (&client->mutex);
385 /* XXX: The memory management for the request strings is pretty cheesy. */
387 _append_to_request (ttt_client_t *client,
390 client->num_request_strings++;
391 client->request_strings =
392 xrealloc (client->request_strings,
393 client->num_request_strings * sizeof (char *));
395 client->request_strings[client->num_request_strings - 1] = xstrdup (string);
399 _free_request (ttt_client_t *client)
403 for (i = 0; i < client->num_request_strings; i++)
404 free (client->request_strings[i]);
406 free (client->request_strings);
408 client->request_strings = NULL;
409 client->num_request_strings = 0;
413 _read_request (ttt_client_t *client)
416 ttt_token_type_t token_type;
418 _free_request (client);
421 token_type = yylex (client->scanner, &token);
422 /* Yes, EOF in two different enums is pretty ugly. */
423 if (token_type == TTT_TOKEN_TYPE_EOF)
424 return TTT_STATUS_EOF;
426 if (token_type == TTT_TOKEN_TYPE_NEWLINE) {
427 if (client->num_request_strings)
428 return TTT_STATUS_SUCCESS;
433 assert (token_type == TTT_TOKEN_TYPE_STRING);
435 _append_to_request (client, token.u.string);
437 free (token.u.string);
442 _execute_request (ttt_client_t *client)
446 char *command = client->request_strings[0];
447 int num_args = client->num_request_strings-1;
448 ttt_command_description_t *desc;
450 for (i = 0; i < strlen (command); i++)
451 command[i] = toupper (command[i]);
453 for (i = 0; i < ARRAY_SIZE(command_descriptions); i++) {
454 desc = &command_descriptions[i];
455 if (strcmp (command, desc->command) == 0) {
456 if ((num_args < desc->args_min) || (num_args > desc->args_max))
457 return TTT_ERROR_SYNTAX;
458 return (desc->execute) (client,
459 &client->request_strings[1],
464 return TTT_ERROR_COMMAND;
468 _handle_requests_thread (void *closure)
472 ttt_client_t *client = closure;
476 status = _read_request (client);
477 if (status == TTT_STATUS_EOF)
482 error = _execute_request (client);
483 if (error == TTT_ERROR_QUIT_REQUESTED)
486 ttt_client_send (client, ttt_error_string (error));
489 _ttt_client_fini (client);
495 /* Exported: See ttt-client.h for documentation. */
497 ttt_client_new (void *closure, int client_socket)
499 ttt_server_t *server = closure;
500 ttt_client_t *client;
503 client = xmalloc (sizeof (ttt_client_t));
505 _ttt_client_init (client, server, client_socket);
507 err = pthread_create (&client->thread, NULL,
508 _handle_requests_thread, client);
510 fprintf (stderr, "Error: pthread_create failed: %s. Aborting.\r\n",
516 /* Exported: See ttt-client.h for documentation. */
518 ttt_client_send (ttt_client_t *client, const char *message)
520 pthread_mutex_lock (&client->mutex);
522 ttt_socket_write (client->socket, message, strlen (message));
524 pthread_mutex_unlock (&client->mutex);
527 /* Exported: See ttt-client.h for documentation. */
529 ttt_client_get_username (ttt_client_t *client)
531 return client->username;
534 /* Exported: See ttt-client.h for documentation. */
536 ttt_client_set_username (ttt_client_t *client, const char *username)
538 free (client->username);
539 client->username = xstrdup (username);
542 /* Exported: See ttt-client.h for documentation. */
544 ttt_client_get_num_wins (ttt_client_t *client)
546 return client->num_wins;