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,
88 _ttt_client_execute_invite (ttt_client_t *client,
93 _ttt_client_execute_accept (ttt_client_t *client,
97 typedef struct _ttt_command_description {
101 ttt_command_func_t execute;
103 const char *description;
104 } ttt_command_description_t;
106 ttt_command_description_t command_descriptions[] = {
107 {"HELO", 1, 1, _ttt_client_execute_helo,
108 "HELO <username> ", "Register."},
110 {"HELP", 0, 1, _ttt_client_execute_help,
111 "HELP <command> ", "Display help for a command."},
113 {"INVITE", 1, 1, _ttt_client_execute_invite,
114 "INVITE <username> ", "Invite a player to play a game."},
116 {"ACCEPT", 1, 1, _ttt_client_execute_accept,
117 "ACCEPT <username> ", "Accept a game invitation."},
119 {"MESSAGE", 1, 1, _ttt_client_execute_message,
120 "MESSAGE <message> ", "Send a message to everyone."},
122 {"STATISTICS", 1, 1, _ttt_client_execute_statistics,
123 "STATISTICS <username> ", "Lists the statistics for the specified user."},
125 {"QUIT", 0, 0, _ttt_client_execute_quit,
126 "QUIT ", "Quit session."},
128 {"VERSION", 1, 1, _ttt_client_execute_version,
129 "VERSION <client-version>", "Negotiate protocol version."},
131 {"WHO", 0, 0, _ttt_client_execute_who,
132 "WHO ", "List registered users."}
135 #define ARRAY_SIZE(arr) (sizeof(arr)/sizeof(arr[0]))
138 _ttt_client_execute_helo (ttt_client_t *client,
146 assert (num_args == 1);
148 ttt_client_set_username (client, args[0]);
150 error = ttt_server_register_client (client->server, client);
153 client->registered = TRUE;
155 xasprintf (&response, "HELO %s %s %s\r\n",
157 ttt_server_get_host (client->server),
158 ttt_server_get_port (client->server));
159 ttt_client_send (client, response);
161 xasprintf (¬ice, "NOTICE USER %s\r\n",
163 ttt_server_broadcast (client->server, notice);
168 return TTT_ERROR_NONE;
172 _ttt_client_execute_who (ttt_client_t *client,
178 assert (num_args == 0);
180 if (!client->registered)
181 return TTT_ERROR_NO_NAME_SET;
183 response = xstrdup (ttt_server_who (client->server));
184 ttt_client_send (client, response);
188 return TTT_ERROR_NONE;
192 _ttt_client_execute_statistics (ttt_client_t *client,
200 assert (num_args == 1);
204 if (!client->registered)
205 return TTT_ERROR_NO_NAME_SET;
207 error = ttt_server_statistics (client->server, username, &response);
211 ttt_client_send (client, response);
215 return TTT_ERROR_NONE;
219 _ttt_client_execute_message (ttt_client_t *client,
226 assert (num_args == 1);
228 if (!client->registered)
229 return TTT_ERROR_NO_NAME_SET;
231 xasprintf (&response, "MESSAGE\r\n");
232 ttt_client_send (client, response);
234 xasprintf (¬ice, "NOTICE MESSAGE %s \"%s\"\r\n",
237 ttt_server_broadcast (client->server, notice);
242 return TTT_ERROR_NONE;
246 _ttt_client_execute_help (ttt_client_t *client,
252 ttt_command_description_t *desc;
254 ttt_bool_t is_command = FALSE;
257 xasprintf (&response, "HELP \"\r\n"
258 "Available Commands:\r\n");
259 for (i = 0; i < ARRAY_SIZE(command_descriptions); i++) {
260 desc = &command_descriptions[i];
261 xasprintf (&response, "%s\r\n %s - %s\r\n",
266 xasprintf (&response, "%s\"\r\n", response);
269 for (i = 0; i < strlen (command); i++)
270 command[i] = toupper (command[i]);
271 for (i = 0; i < ARRAY_SIZE(command_descriptions); i++) {
272 desc = &command_descriptions[i];
273 if (strcmp (desc->command, command) == 0) {
275 xasprintf (&response, "HELP %s \"\r\n"
284 /* XXX: Add detailed help. */
289 if ((num_args == 1) && (!is_command))
290 return TTT_ERROR_SYNTAX;
292 ttt_client_send (client, response);
295 return TTT_ERROR_NONE;
299 _ttt_client_execute_version (ttt_client_t *client,
308 assert (num_args == 1);
310 clientversion = args[0];
312 /* Verify that provided version arg is a positive integer */
313 for (i = 0; i < strlen(clientversion); i++)
314 if (!isdigit (clientversion[i]))
315 return TTT_ERROR_SYNTAX;
317 version = atoi (clientversion);
320 return TTT_ERROR_SYNTAX;
322 if (version > TTT_SERVER_PROTOCOL_VERSION)
323 version = TTT_SERVER_PROTOCOL_VERSION;
325 xasprintf (&response, "VERSION %d\r\n", version);
326 ttt_client_send (client, response);
329 return TTT_ERROR_NONE;
333 _ttt_client_execute_quit (ttt_client_t *client,
339 assert (num_args == 0);
341 if (!client->registered)
342 return TTT_ERROR_QUIT_REQUESTED;
344 xasprintf (¬ice, "NOTICE QUIT %s\r\n",
346 ttt_server_broadcast (client->server, notice);
350 return TTT_ERROR_QUIT_REQUESTED;
354 _ttt_client_execute_invite (ttt_client_t *client,
358 const char *username;
363 assert (num_args == 1);
367 if (!client->registered)
368 return TTT_ERROR_NO_NAME_SET;
370 error = ttt_server_verify_username (client->server, username);
374 xasprintf (&response, "INVITE\r\n");
375 ttt_client_send (client, response);
377 xasprintf (¬ice, "NOTICE INVITE %s %s\r\n",
380 ttt_server_broadcast (client->server, notice);
385 return TTT_ERROR_NONE;
389 _ttt_client_execute_accept (ttt_client_t *client,
393 const char *username;
398 assert (num_args == 1);
402 if (!client->registered)
403 return TTT_ERROR_NO_NAME_SET;
405 error = ttt_server_verify_username (client->server, username);
409 xasprintf (&response, "ACCEPT\r\n");
410 ttt_client_send (client, response);
412 xasprintf (¬ice, "NOTICE ACCEPT %s %s\r\n",
415 ttt_server_broadcast (client->server, notice);
417 /* XXX: Start a new game */
422 return TTT_ERROR_NONE;
426 _free_request (ttt_client_t *client);
429 _ttt_client_init (ttt_client_t *client,
430 ttt_server_t *server,
435 pthread_mutex_init (&client->mutex, NULL);
437 client->server = server;
438 client->socket = socket;
440 file = xfdopen (socket, "r");
441 yylex_init (&client->scanner);
442 yyset_in (file, client->scanner);
444 client->request_strings = NULL;
445 client->num_request_strings = 0;
447 client->username = NULL;
448 client->registered = FALSE;
449 client->num_wins = 0;
453 _ttt_client_fini (ttt_client_t *client)
455 pthread_mutex_lock (&client->mutex);
457 if (client->registered)
458 ttt_server_unregister_client (client->server, client);
460 free (client->username);
461 client->username = NULL;
463 yylex_destroy (client->scanner);
464 shutdown (client->socket, SHUT_RDWR);
466 _free_request (client);
468 pthread_mutex_unlock (&client->mutex);
470 pthread_mutex_destroy (&client->mutex);
473 /* XXX: The memory management for the request strings is pretty cheesy. */
475 _append_to_request (ttt_client_t *client,
478 client->num_request_strings++;
479 client->request_strings =
480 xrealloc (client->request_strings,
481 client->num_request_strings * sizeof (char *));
483 client->request_strings[client->num_request_strings - 1] = xstrdup (string);
487 _free_request (ttt_client_t *client)
491 for (i = 0; i < client->num_request_strings; i++)
492 free (client->request_strings[i]);
494 free (client->request_strings);
496 client->request_strings = NULL;
497 client->num_request_strings = 0;
501 _read_request (ttt_client_t *client)
504 ttt_token_type_t token_type;
506 _free_request (client);
509 token_type = yylex (client->scanner, &token);
510 /* Yes, EOF in two different enums is pretty ugly. */
511 if (token_type == TTT_TOKEN_TYPE_EOF)
512 return TTT_STATUS_EOF;
514 if (token_type == TTT_TOKEN_TYPE_NEWLINE) {
515 if (client->num_request_strings)
516 return TTT_STATUS_SUCCESS;
521 assert (token_type == TTT_TOKEN_TYPE_STRING);
523 _append_to_request (client, token.u.string);
525 free (token.u.string);
530 _execute_request (ttt_client_t *client)
534 char *command = client->request_strings[0];
535 int num_args = client->num_request_strings-1;
536 ttt_command_description_t *desc;
538 for (i = 0; i < strlen (command); i++)
539 command[i] = toupper (command[i]);
541 for (i = 0; i < ARRAY_SIZE(command_descriptions); i++) {
542 desc = &command_descriptions[i];
543 if (strcmp (command, desc->command) == 0) {
544 if ((num_args < desc->args_min) || (num_args > desc->args_max))
545 return TTT_ERROR_SYNTAX;
546 return (desc->execute) (client,
547 &client->request_strings[1],
552 return TTT_ERROR_COMMAND;
556 _handle_requests_thread (void *closure)
560 ttt_client_t *client = closure;
564 status = _read_request (client);
565 if (status == TTT_STATUS_EOF)
570 error = _execute_request (client);
571 if (error == TTT_ERROR_QUIT_REQUESTED)
574 ttt_client_send (client, ttt_error_string (error));
577 _ttt_client_fini (client);
583 /* Exported: See ttt-client.h for documentation. */
585 ttt_client_new (void *closure, int client_socket)
587 ttt_server_t *server = closure;
588 ttt_client_t *client;
591 client = xmalloc (sizeof (ttt_client_t));
593 _ttt_client_init (client, server, client_socket);
595 err = pthread_create (&client->thread, NULL,
596 _handle_requests_thread, client);
598 fprintf (stderr, "Error: pthread_create failed: %s. Aborting.\r\n",
604 /* Exported: See ttt-client.h for documentation. */
606 ttt_client_send (ttt_client_t *client, const char *message)
608 pthread_mutex_lock (&client->mutex);
610 ttt_socket_write (client->socket, message, strlen (message));
612 pthread_mutex_unlock (&client->mutex);
615 /* Exported: See ttt-client.h for documentation. */
617 ttt_client_get_username (ttt_client_t *client)
619 return client->username;
622 /* Exported: See ttt-client.h for documentation. */
624 ttt_client_set_username (ttt_client_t *client, const char *username)
626 free (client->username);
627 client->username = xstrdup (username);
630 /* Exported: See ttt-client.h for documentation. */
632 ttt_client_get_num_wins (ttt_client_t *client)
634 return client->num_wins;