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\n",
107 ttt_server_get_host (client->server),
108 ttt_server_get_port (client->server));
110 ttt_client_send (client, response);
114 return TTT_ERROR_NONE;
118 _ttt_client_execute_who (ttt_client_t *client,
124 assert (num_args == 0);
126 response = ttt_server_who (client->server);
128 ttt_client_send (client, response);
132 return TTT_ERROR_NONE;
136 _ttt_client_execute_quit (ttt_client_t *client,
140 assert (num_args == 0);
142 return TTT_ERROR_QUIT_REQUESTED;
146 _ttt_client_execute_message (ttt_client_t *client,
153 assert (num_args > 0);
155 xasprintf(&response, "MESSAGE\n");
156 ttt_client_send (client, response);
158 xasprintf(&response, "NOTICE MESSAGE %s", client->name);
159 for (i=0; i<num_args; i++){
160 xasprintf(&response, "%s %s",response,args[i]);
163 xasprintf(&response, "%s\n",response);
164 ttt_server_broadcast (client->server,response);
167 return TTT_ERROR_NONE;
173 _free_request (ttt_client_t *client);
176 _ttt_client_init (ttt_client_t *client,
177 ttt_server_t *server,
182 pthread_mutex_init (&client->mutex, NULL);
184 client->server = server;
185 client->socket = socket;
187 file = xfdopen (socket, "r");
188 yylex_init(&client->scanner);
189 yyset_in (file, client->scanner);
191 client->request_strings = NULL;
192 client->num_request_strings = 0;
195 client->registered = FALSE;
196 client->num_wins = 0;
200 _ttt_client_fini (ttt_client_t *client)
202 pthread_mutex_lock (&client->mutex);
204 if (client->registered)
205 ttt_server_unregister_client (client->server, client);
210 yylex_destroy (client->scanner);
211 shutdown (client->socket, SHUT_RDWR);
213 _free_request (client);
215 pthread_mutex_unlock (&client->mutex);
217 pthread_mutex_destroy (&client->mutex);
220 /* XXX: The memory management for the request strings is pretty cheesy. */
222 _append_to_request (ttt_client_t *client,
225 client->num_request_strings++;
226 client->request_strings =
227 xrealloc (client->request_strings,
228 client->num_request_strings * sizeof (char *));
230 client->request_strings[client->num_request_strings - 1] = xstrdup (string);
234 _free_request (ttt_client_t *client)
238 for (i = 0; i < client->num_request_strings; i++)
239 free (client->request_strings[i]);
241 free (client->request_strings);
243 client->request_strings = NULL;
244 client->num_request_strings = 0;
248 _read_request (ttt_client_t *client)
252 _free_request (client);
255 token = yylex (client->scanner);
256 /* Yes, EOF in two different enums is pretty ugly. */
257 if (token == TTT_TOKEN_EOF)
258 return TTT_STATUS_EOF;
260 if (token == TTT_TOKEN_NEWLINE) {
261 if (client->num_request_strings)
262 return TTT_STATUS_SUCCESS;
267 assert (token == TTT_TOKEN_STRING);
269 _append_to_request (client, yyget_text (client->scanner));
274 _execute_request (ttt_client_t *client)
278 char *command = client->request_strings[0];
279 int num_args = client->num_request_strings-1;
280 ttt_command_description_t *desc;
282 for (i=0; i < strlen (command); i++)
283 command[i] = toupper (command[i]);
285 for (i=0; i < ARRAY_SIZE(command_descriptions); i++) {
286 desc = &command_descriptions[i];
287 if (strcmp(command, desc->command) == 0) {
288 if (num_args < desc->args_required)
289 return TTT_ERROR_SYNTAX;
290 return (desc->execute) (client, &client->request_strings[1], num_args);
294 return TTT_ERROR_COMMAND;
298 _handle_requests_thread (void *closure)
302 ttt_client_t *client = closure;
306 status = _read_request (client);
307 if (status == TTT_STATUS_EOF)
312 error = _execute_request (client);
313 if (error == TTT_ERROR_QUIT_REQUESTED)
316 ttt_client_send (client, ttt_error_string (error));
319 _ttt_client_fini (client);
325 /* Exported: See ttt-client.h for documentation. */
327 ttt_client_new (void *closure, int client_socket)
329 ttt_server_t *server = closure;
330 ttt_client_t *client;
333 client = xmalloc (sizeof (ttt_client_t));
335 _ttt_client_init (client, server, client_socket);
337 err = pthread_create (&client->thread, NULL,
338 _handle_requests_thread, client);
340 fprintf (stderr, "Error: pthread_create failed: %s. Aborting.\n",
346 /* Exported: See ttt-client.h for documentation. */
348 ttt_client_send (ttt_client_t *client, const char *message)
350 pthread_mutex_lock (&client->mutex);
352 ttt_socket_write (client->socket, message, strlen (message));
354 pthread_mutex_unlock (&client->mutex);
357 /* Exported: See ttt-client.h for documentation. */
359 ttt_client_get_name (ttt_client_t *client)
364 /* Exported: See ttt-client.h for documentation. */
366 ttt_client_set_name (ttt_client_t *client, const char *name)
369 client->name = xstrdup (name);
372 /* Exported: See ttt-client.h for documentation. */
374 ttt_client_get_num_wins (ttt_client_t *client)
376 return client->num_wins;