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;
39 ttt_bool_t registered;
41 char **request_strings;
42 int num_request_strings;
45 typedef ttt_error_t (*ttt_command_func_t) (ttt_client_t *client,
50 _ttt_client_execute_helo (ttt_client_t *client,
55 _ttt_client_execute_who (ttt_client_t *client,
60 _ttt_client_execute_quit (ttt_client_t *client,
64 typedef struct _ttt_command_description {
67 ttt_command_func_t execute;
68 } ttt_command_description_t;
70 ttt_command_description_t command_descriptions[] = {
71 {"HELO", 1, _ttt_client_execute_helo},
72 {"WHO", 0, _ttt_client_execute_who},
73 {"QUIT", 0, _ttt_client_execute_quit}
75 #define ARRAY_SIZE(arr) (sizeof(arr)/sizeof(arr[0]))
78 _ttt_client_execute_helo (ttt_client_t *client,
85 assert (num_args == 1);
87 ttt_client_set_name (client, args[0]);
89 error = ttt_server_register_client (client->server, client);
93 client->registered = TRUE;
95 xasprintf (&response, "HELO %s %s %s\n", client->name,
96 ttt_server_get_host (client->server),
97 ttt_server_get_port (client->server));
99 ttt_client_send (client, response);
103 return TTT_ERROR_NONE;
107 _ttt_client_execute_who (ttt_client_t *client,
111 const char *response;
113 assert (num_args == 0);
115 response = ttt_server_who (client->server);
117 ttt_client_send (client, response);
121 return TTT_ERROR_NONE;
125 _ttt_client_execute_quit (ttt_client_t *client,
129 assert (num_args == 0);
131 return TTT_ERROR_QUIT_REQUESTED;
135 _free_request (ttt_client_t *client);
138 _ttt_client_init (ttt_client_t *client,
139 ttt_server_t *server,
144 pthread_mutex_init (&client->mutex, NULL);
146 client->server = server;
147 client->socket = socket;
149 file = xfdopen (socket, "r");
150 yylex_init(&client->scanner);
151 yyset_in (file, client->scanner);
153 client->request_strings = NULL;
154 client->num_request_strings = 0;
157 client->registered = FALSE;
161 _ttt_client_fini (ttt_client_t *client)
163 pthread_mutex_lock (&client->mutex);
165 if (client->registered)
166 ttt_server_unregister_client (client->server, client);
171 yylex_destroy (client->scanner);
172 shutdown (client->socket, SHUT_RDWR);
174 _free_request (client);
176 pthread_mutex_unlock (&client->mutex);
178 pthread_mutex_destroy (&client->mutex);
181 /* XXX: The memory management for the request strings is pretty cheesy. */
183 _append_to_request (ttt_client_t *client,
186 client->num_request_strings++;
187 client->request_strings =
188 xrealloc (client->request_strings,
189 client->num_request_strings * sizeof (char *));
191 client->request_strings[client->num_request_strings - 1] = xstrdup (string);
195 _free_request (ttt_client_t *client)
199 for (i = 0; i < client->num_request_strings; i++)
200 free (client->request_strings[i]);
202 free (client->request_strings);
204 client->request_strings = NULL;
205 client->num_request_strings = 0;
209 _read_request (ttt_client_t *client)
213 _free_request (client);
216 token = yylex (client->scanner);
217 /* Yes, EOF in two different enums is pretty ugly. */
218 if (token == TTT_TOKEN_EOF)
219 return TTT_STATUS_EOF;
221 if (token == TTT_TOKEN_NEWLINE) {
222 if (client->num_request_strings)
223 return TTT_STATUS_SUCCESS;
228 assert (token == TTT_TOKEN_STRING);
230 _append_to_request (client, yyget_text (client->scanner));
235 _execute_request (ttt_client_t *client)
239 char *command = client->request_strings[0];
240 int num_args = client->num_request_strings-1;
241 ttt_command_description_t *desc;
243 for (i=0; i < strlen (command); i++)
244 command[i] = toupper (command[i]);
246 for (i=0; i < ARRAY_SIZE(command_descriptions); i++) {
247 desc = &command_descriptions[i];
248 if (strcmp(command, desc->command) == 0) {
249 if (num_args != desc->args_required)
250 return TTT_ERROR_SYNTAX;
251 return (desc->execute) (client, &client->request_strings[1], num_args);
255 return TTT_ERROR_COMMAND;
259 _handle_requests_thread (void *closure)
263 ttt_client_t *client = closure;
267 status = _read_request (client);
268 if (status == TTT_STATUS_EOF)
273 error = _execute_request (client);
274 if (error == TTT_ERROR_QUIT_REQUESTED)
277 ttt_client_send (client, ttt_error_string (error));
280 _ttt_client_fini (client);
286 /* Exported: See ttt-client.h for documentation. */
288 ttt_client_new (void *closure, int client_socket)
290 ttt_server_t *server = closure;
291 ttt_client_t *client;
294 client = xmalloc (sizeof (ttt_client_t));
296 _ttt_client_init (client, server, client_socket);
298 err = pthread_create (&client->thread, NULL,
299 _handle_requests_thread, client);
301 fprintf (stderr, "Error: pthread_create failed: %s. Aborting.\n",
307 /* Exported: See ttt-client.h for documentation. */
309 ttt_client_send (ttt_client_t *client, const char *message)
311 pthread_mutex_lock (&client->mutex);
313 ttt_socket_write (client->socket, message, strlen (message));
315 pthread_mutex_unlock (&client->mutex);
318 /* Exported: See ttt-client.h for documentation. */
320 ttt_client_get_name (ttt_client_t *client)
325 /* Exported: See ttt-client.h for documentation. */
327 ttt_client_set_name (ttt_client_t *client, const char *name)
330 client->name = xstrdup (name);