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 #include "ttt-error.h"
26 #include "ttt-server.h"
27 #include "ttt-socket.h"
28 #include "ttt-token.h"
31 pthread_mutex_t mutex;
38 char **request_strings;
39 int num_request_strings;
42 ttt_bool_t registered;
46 typedef ttt_error_t (*ttt_command_func_t) (ttt_client_t *client,
51 _ttt_client_execute_helo (ttt_client_t *client,
56 _ttt_client_execute_who (ttt_client_t *client,
61 _ttt_client_execute_quit (ttt_client_t *client,
66 _ttt_client_execute_message (ttt_client_t *client,
72 typedef struct _ttt_command_description {
75 ttt_command_func_t execute;
76 } ttt_command_description_t;
78 ttt_command_description_t command_descriptions[] = {
79 {"HELO", 1, _ttt_client_execute_helo},
80 {"WHO", 0, _ttt_client_execute_who },
81 {"QUIT", 0, _ttt_client_execute_quit},
82 {"MESSAGE", 1, _ttt_client_execute_message}
85 #define ARRAY_SIZE(arr) (sizeof(arr)/sizeof(arr[0]))
88 _ttt_client_execute_helo (ttt_client_t *client,
95 assert (num_args == 1);
97 ttt_client_set_name (client, args[0]);
99 error = ttt_server_register_client (client->server, client);
103 client->registered = TRUE;
105 xasprintf (&response, "HELO %s %s %s\r\n",
107 ttt_server_get_host (client->server),
108 ttt_server_get_port (client->server));
110 ttt_client_send (client, response);
112 xasprintf (&response, "NOTICE USER %s\r\n",client->name);
113 ttt_server_broadcast(client->server,response);
117 return TTT_ERROR_NONE;
121 _ttt_client_execute_who (ttt_client_t *client,
127 assert (num_args == 0);
129 response = ttt_server_who (client->server);
131 ttt_client_send (client, response);
135 return TTT_ERROR_NONE;
139 _ttt_client_execute_quit (ttt_client_t *client,
145 assert (num_args == 0);
146 xasprintf (¬ice,"NOTICE QUIT %s\r\n",client->name);
147 ttt_server_broadcast(client->server,notice);
149 return TTT_ERROR_QUIT_REQUESTED;
153 _ttt_client_execute_message (ttt_client_t *client,
160 assert (num_args > 0);
162 xasprintf(&response, "MESSAGE\r\n");
163 ttt_client_send (client, response);
165 xasprintf(&response, "NOTICE MESSAGE %s", client->name);
166 for (i=0; i<num_args; i++){
167 xasprintf(&response, "%s %s",response,args[i]);
170 xasprintf(&response, "%s\r\n",response);
171 ttt_server_broadcast (client->server,response);
174 return TTT_ERROR_NONE;
180 _free_request (ttt_client_t *client);
183 _ttt_client_init (ttt_client_t *client,
184 ttt_server_t *server,
189 pthread_mutex_init (&client->mutex, NULL);
191 client->server = server;
192 client->socket = socket;
194 file = xfdopen (socket, "r");
195 yylex_init(&client->scanner);
196 yyset_in (file, client->scanner);
198 client->request_strings = NULL;
199 client->num_request_strings = 0;
202 client->registered = FALSE;
203 client->num_wins = 0;
207 _ttt_client_fini (ttt_client_t *client)
209 pthread_mutex_lock (&client->mutex);
211 if (client->registered)
212 ttt_server_unregister_client (client->server, client);
217 yylex_destroy (client->scanner);
218 shutdown (client->socket, SHUT_RDWR);
220 _free_request (client);
222 pthread_mutex_unlock (&client->mutex);
224 pthread_mutex_destroy (&client->mutex);
227 /* XXX: The memory management for the request strings is pretty cheesy. */
229 _append_to_request (ttt_client_t *client,
232 client->num_request_strings++;
233 client->request_strings =
234 xrealloc (client->request_strings,
235 client->num_request_strings * sizeof (char *));
237 client->request_strings[client->num_request_strings - 1] = xstrdup (string);
241 _free_request (ttt_client_t *client)
245 for (i = 0; i < client->num_request_strings; i++)
246 free (client->request_strings[i]);
248 free (client->request_strings);
250 client->request_strings = NULL;
251 client->num_request_strings = 0;
255 _read_request (ttt_client_t *client)
259 _free_request (client);
262 token = yylex (client->scanner);
263 /* Yes, EOF in two different enums is pretty ugly. */
264 if (token == TTT_TOKEN_EOF)
265 return TTT_STATUS_EOF;
267 if (token == TTT_TOKEN_NEWLINE) {
268 if (client->num_request_strings)
269 return TTT_STATUS_SUCCESS;
274 assert (token == TTT_TOKEN_STRING);
276 _append_to_request (client, yyget_text (client->scanner));
281 _execute_request (ttt_client_t *client)
285 char *command = client->request_strings[0];
286 int num_args = client->num_request_strings-1;
287 ttt_command_description_t *desc;
289 for (i=0; i < strlen (command); i++)
290 command[i] = toupper (command[i]);
292 for (i=0; i < ARRAY_SIZE(command_descriptions); i++) {
293 desc = &command_descriptions[i];
294 if (strcmp(command, desc->command) == 0) {
295 if (num_args < desc->args_required)
296 return TTT_ERROR_SYNTAX;
297 return (desc->execute) (client, &client->request_strings[1], num_args);
301 return TTT_ERROR_COMMAND;
305 _handle_requests_thread (void *closure)
309 ttt_client_t *client = closure;
313 status = _read_request (client);
314 if (status == TTT_STATUS_EOF)
319 error = _execute_request (client);
320 if (error == TTT_ERROR_QUIT_REQUESTED)
323 ttt_client_send (client, ttt_error_string (error));
326 _ttt_client_fini (client);
332 /* Exported: See ttt-client.h for documentation. */
334 ttt_client_new (void *closure, int client_socket)
336 ttt_server_t *server = closure;
337 ttt_client_t *client;
340 client = xmalloc (sizeof (ttt_client_t));
342 _ttt_client_init (client, server, client_socket);
344 err = pthread_create (&client->thread, NULL,
345 _handle_requests_thread, client);
347 fprintf (stderr, "Error: pthread_create failed: %s. Aborting.\r\n",
353 /* Exported: See ttt-client.h for documentation. */
355 ttt_client_send (ttt_client_t *client, const char *message)
357 pthread_mutex_lock (&client->mutex);
359 ttt_socket_write (client->socket, message, strlen (message));
361 pthread_mutex_unlock (&client->mutex);
364 /* Exported: See ttt-client.h for documentation. */
366 ttt_client_get_name (ttt_client_t *client)
371 /* Exported: See ttt-client.h for documentation. */
373 ttt_client_set_name (ttt_client_t *client, const char *name)
376 client->name = xstrdup (name);
379 /* Exported: See ttt-client.h for documentation. */
381 ttt_client_get_num_wins (ttt_client_t *client)
383 return client->num_wins;