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;
88 const char *description;
89 } ttt_command_description_t;
91 ttt_command_description_t command_descriptions[] = {
92 {"HELO", 1, 1, _ttt_client_execute_helo,
93 "HELO <username> ", "Register."},
95 {"HELP", 0, 1, _ttt_client_execute_help,
96 "HELP <command> ", "Display help for a command."},
98 {"MESSAGE", 1, 1, _ttt_client_execute_message,
99 "MESSAGE <message> ", "Send a message to everyone."},
101 {"QUIT", 0, 0, _ttt_client_execute_quit,
102 "QUIT ", "Quit session."},
104 {"VERSION", 1, 1, _ttt_client_execute_version,
105 "VERSION <client-version>", "Negotiate protocol version."},
107 {"WHO", 0, 0, _ttt_client_execute_who,
108 "WHO ", "List registered users."}
111 #define ARRAY_SIZE(arr) (sizeof(arr)/sizeof(arr[0]))
114 _ttt_client_execute_helo (ttt_client_t *client,
122 assert (num_args == 1);
124 ttt_client_set_name (client, args[0]);
126 error = ttt_server_register_client (client->server, client);
129 client->registered = TRUE;
131 xasprintf (&response, "HELO %s %s %s\r\n",
133 ttt_server_get_host (client->server),
134 ttt_server_get_port (client->server));
135 ttt_client_send (client, response);
137 xasprintf (¬ice, "NOTICE USER %s\r\n",
139 ttt_server_broadcast (client->server, notice);
144 return TTT_ERROR_NONE;
148 _ttt_client_execute_who (ttt_client_t *client,
154 assert (num_args == 0);
156 if (!client->registered)
157 return TTT_ERROR_NO_NAME_SET;
159 response = xstrdup (ttt_server_who (client->server));
160 ttt_client_send (client, response);
164 return TTT_ERROR_NONE;
168 _ttt_client_execute_message (ttt_client_t *client,
175 assert (num_args == 1);
177 if (!client->registered)
178 return TTT_ERROR_NO_NAME_SET;
180 xasprintf (&response, "MESSAGE\r\n");
181 ttt_client_send (client, response);
183 xasprintf (¬ice, "NOTICE MESSAGE %s \"%s\"\r\n",
186 ttt_server_broadcast (client->server, notice);
191 return TTT_ERROR_NONE;
195 _ttt_client_execute_help (ttt_client_t *client,
201 ttt_command_description_t *desc;
203 ttt_bool_t is_command = FALSE;
206 xasprintf (&response, "HELP \"\r\n"
207 "Available Commands:\r\n");
208 for (i = 0; i < ARRAY_SIZE(command_descriptions); i++) {
209 desc = &command_descriptions[i];
210 xasprintf (&response, "%s\r\n %s - %s\r\n",
215 xasprintf (&response, "%s\"\r\n", response);
218 for (i = 0; i < strlen (command); i++)
219 command[i] = toupper (command[i]);
220 for (i = 0; i < ARRAY_SIZE(command_descriptions); i++) {
221 desc = &command_descriptions[i];
222 if (strcmp (desc->command, command) == 0) {
224 xasprintf (&response, "HELP %s \"\r\n"
233 /* XXX: Add detailed help. */
238 if ((num_args == 1) && (!is_command))
239 return TTT_ERROR_SYNTAX;
241 ttt_client_send (client, response);
244 return TTT_ERROR_NONE;
248 _ttt_client_execute_version (ttt_client_t *client,
254 assert (num_args == 1);
256 /* XXX: Argument is being ignored.
257 This is not completely implemented. */
259 xasprintf (&response, "VERSION 1\r\n");
260 ttt_client_send (client, response);
263 return TTT_ERROR_NONE;
267 _ttt_client_execute_quit (ttt_client_t *client,
273 assert (num_args == 0);
275 if (!client->registered)
276 return TTT_ERROR_QUIT_REQUESTED;
278 xasprintf (¬ice, "NOTICE QUIT %s\r\n",
280 ttt_server_broadcast (client->server, notice);
284 return TTT_ERROR_QUIT_REQUESTED;
288 _free_request (ttt_client_t *client);
291 _ttt_client_init (ttt_client_t *client,
292 ttt_server_t *server,
297 pthread_mutex_init (&client->mutex, NULL);
299 client->server = server;
300 client->socket = socket;
302 file = xfdopen (socket, "r");
303 yylex_init (&client->scanner);
304 yyset_in (file, client->scanner);
306 client->request_strings = NULL;
307 client->num_request_strings = 0;
310 client->registered = FALSE;
311 client->num_wins = 0;
315 _ttt_client_fini (ttt_client_t *client)
317 pthread_mutex_lock (&client->mutex);
319 if (client->registered)
320 ttt_server_unregister_client (client->server, client);
325 yylex_destroy (client->scanner);
326 shutdown (client->socket, SHUT_RDWR);
328 _free_request (client);
330 pthread_mutex_unlock (&client->mutex);
332 pthread_mutex_destroy (&client->mutex);
335 /* XXX: The memory management for the request strings is pretty cheesy. */
337 _append_to_request (ttt_client_t *client,
340 client->num_request_strings++;
341 client->request_strings =
342 xrealloc (client->request_strings,
343 client->num_request_strings * sizeof (char *));
345 client->request_strings[client->num_request_strings - 1] = xstrdup (string);
349 _free_request (ttt_client_t *client)
353 for (i = 0; i < client->num_request_strings; i++)
354 free (client->request_strings[i]);
356 free (client->request_strings);
358 client->request_strings = NULL;
359 client->num_request_strings = 0;
363 _read_request (ttt_client_t *client)
366 ttt_token_type_t token_type;
368 _free_request (client);
371 token_type = yylex (client->scanner, &token);
372 /* Yes, EOF in two different enums is pretty ugly. */
373 if (token_type == TTT_TOKEN_TYPE_EOF)
374 return TTT_STATUS_EOF;
376 if (token_type == TTT_TOKEN_TYPE_NEWLINE) {
377 if (client->num_request_strings)
378 return TTT_STATUS_SUCCESS;
383 assert (token_type == TTT_TOKEN_TYPE_STRING);
385 _append_to_request (client, token.u.string);
387 free (token.u.string);
392 _execute_request (ttt_client_t *client)
396 char *command = client->request_strings[0];
397 int num_args = client->num_request_strings-1;
398 ttt_command_description_t *desc;
400 for (i = 0; i < strlen (command); i++)
401 command[i] = toupper (command[i]);
403 for (i = 0; i < ARRAY_SIZE(command_descriptions); i++) {
404 desc = &command_descriptions[i];
405 if (strcmp (command, desc->command) == 0) {
406 if ((num_args < desc->args_min) || (num_args > desc->args_max))
407 return TTT_ERROR_SYNTAX;
408 return (desc->execute) (client,
409 &client->request_strings[1],
414 return TTT_ERROR_COMMAND;
418 _handle_requests_thread (void *closure)
422 ttt_client_t *client = closure;
426 status = _read_request (client);
427 if (status == TTT_STATUS_EOF)
432 error = _execute_request (client);
433 if (error == TTT_ERROR_QUIT_REQUESTED)
436 ttt_client_send (client, ttt_error_string (error));
439 _ttt_client_fini (client);
445 /* Exported: See ttt-client.h for documentation. */
447 ttt_client_new (void *closure, int client_socket)
449 ttt_server_t *server = closure;
450 ttt_client_t *client;
453 client = xmalloc (sizeof (ttt_client_t));
455 _ttt_client_init (client, server, client_socket);
457 err = pthread_create (&client->thread, NULL,
458 _handle_requests_thread, client);
460 fprintf (stderr, "Error: pthread_create failed: %s. Aborting.\r\n",
466 /* Exported: See ttt-client.h for documentation. */
468 ttt_client_send (ttt_client_t *client, const char *message)
470 pthread_mutex_lock (&client->mutex);
472 ttt_socket_write (client->socket, message, strlen (message));
474 pthread_mutex_unlock (&client->mutex);
477 /* Exported: See ttt-client.h for documentation. */
479 ttt_client_get_name (ttt_client_t *client)
484 /* Exported: See ttt-client.h for documentation. */
486 ttt_client_set_name (ttt_client_t *client, const char *name)
489 client->name = xstrdup (name);
492 /* Exported: See ttt-client.h for documentation. */
494 ttt_client_get_num_wins (ttt_client_t *client)
496 return client->num_wins;