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,
65 typedef struct _ttt_command_description {
68 ttt_command_func_t execute;
69 } ttt_command_description_t;
71 ttt_command_description_t command_descriptions[] = {
72 {"HELO", 1, _ttt_client_execute_helo},
73 {"WHO", 0, _ttt_client_execute_who },
74 {"QUIT", 0, _ttt_client_execute_quit}
76 #define ARRAY_SIZE(arr) (sizeof(arr)/sizeof(arr[0]))
79 _ttt_client_execute_helo (ttt_client_t *client,
86 assert (num_args == 1);
88 ttt_client_set_name (client, args[0]);
90 error = ttt_server_register_client (client->server, client);
94 client->registered = TRUE;
96 xasprintf (&response, "HELO %s %s %s\n",
98 ttt_server_get_host (client->server),
99 ttt_server_get_port (client->server));
101 ttt_client_send (client, response);
105 return TTT_ERROR_NONE;
109 _ttt_client_execute_who (ttt_client_t *client,
113 const char *response;
115 assert (num_args == 0);
117 response = ttt_server_who (client->server);
119 ttt_client_send (client, response);
123 return TTT_ERROR_NONE;
127 _ttt_client_execute_quit (ttt_client_t *client,
131 assert (num_args == 0);
133 return TTT_ERROR_QUIT_REQUESTED;
137 _free_request (ttt_client_t *client);
140 _ttt_client_init (ttt_client_t *client,
141 ttt_server_t *server,
146 pthread_mutex_init (&client->mutex, NULL);
148 client->server = server;
149 client->socket = socket;
151 file = xfdopen (socket, "r");
152 yylex_init(&client->scanner);
153 yyset_in (file, client->scanner);
155 client->request_strings = NULL;
156 client->num_request_strings = 0;
159 client->registered = FALSE;
160 client->num_wins = 0;
164 _ttt_client_fini (ttt_client_t *client)
166 pthread_mutex_lock (&client->mutex);
168 if (client->registered)
169 ttt_server_unregister_client (client->server, client);
174 yylex_destroy (client->scanner);
175 shutdown (client->socket, SHUT_RDWR);
177 _free_request (client);
179 pthread_mutex_unlock (&client->mutex);
181 pthread_mutex_destroy (&client->mutex);
184 /* XXX: The memory management for the request strings is pretty cheesy. */
186 _append_to_request (ttt_client_t *client,
189 client->num_request_strings++;
190 client->request_strings =
191 xrealloc (client->request_strings,
192 client->num_request_strings * sizeof (char *));
194 client->request_strings[client->num_request_strings - 1] = xstrdup (string);
198 _free_request (ttt_client_t *client)
202 for (i = 0; i < client->num_request_strings; i++)
203 free (client->request_strings[i]);
205 free (client->request_strings);
207 client->request_strings = NULL;
208 client->num_request_strings = 0;
212 _read_request (ttt_client_t *client)
216 _free_request (client);
219 token = yylex (client->scanner);
220 /* Yes, EOF in two different enums is pretty ugly. */
221 if (token == TTT_TOKEN_EOF)
222 return TTT_STATUS_EOF;
224 if (token == TTT_TOKEN_NEWLINE) {
225 if (client->num_request_strings)
226 return TTT_STATUS_SUCCESS;
231 assert (token == TTT_TOKEN_STRING);
233 _append_to_request (client, yyget_text (client->scanner));
238 _execute_request (ttt_client_t *client)
242 char *command = client->request_strings[0];
243 int num_args = client->num_request_strings-1;
244 ttt_command_description_t *desc;
246 for (i=0; i < strlen (command); i++)
247 command[i] = toupper (command[i]);
249 for (i=0; i < ARRAY_SIZE(command_descriptions); i++) {
250 desc = &command_descriptions[i];
251 if (strcmp(command, desc->command) == 0) {
252 if (num_args != desc->args_required)
253 return TTT_ERROR_SYNTAX;
254 return (desc->execute) (client, &client->request_strings[1], num_args);
258 return TTT_ERROR_COMMAND;
262 _handle_requests_thread (void *closure)
266 ttt_client_t *client = closure;
270 status = _read_request (client);
271 if (status == TTT_STATUS_EOF)
276 error = _execute_request (client);
277 if (error == TTT_ERROR_QUIT_REQUESTED)
280 ttt_client_send (client, ttt_error_string (error));
283 _ttt_client_fini (client);
289 /* Exported: See ttt-client.h for documentation. */
291 ttt_client_new (void *closure, int client_socket)
293 ttt_server_t *server = closure;
294 ttt_client_t *client;
297 client = xmalloc (sizeof (ttt_client_t));
299 _ttt_client_init (client, server, client_socket);
301 err = pthread_create (&client->thread, NULL,
302 _handle_requests_thread, client);
304 fprintf (stderr, "Error: pthread_create failed: %s. Aborting.\n",
310 /* Exported: See ttt-client.h for documentation. */
312 ttt_client_send (ttt_client_t *client, const char *message)
314 pthread_mutex_lock (&client->mutex);
316 ttt_socket_write (client->socket, message, strlen (message));
318 pthread_mutex_unlock (&client->mutex);
321 /* Exported: See ttt-client.h for documentation. */
323 ttt_client_get_name (ttt_client_t *client)
328 /* Exported: See ttt-client.h for documentation. */
330 ttt_client_set_name (ttt_client_t *client, const char *name)
333 client->name = xstrdup (name);
336 /* Exported: See ttt-client.h for documentation. */
338 ttt_client_get_num_wins (ttt_client_t *client)
340 return client->num_wins;