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;
40 char **request_strings;
41 int num_request_strings;
45 _free_request (ttt_client_t *client);
48 _ttt_client_init (ttt_client_t *client,
54 pthread_mutex_init (&client->mutex, NULL);
56 client->server = server;
57 client->socket = socket;
59 file = xfdopen (socket, "r");
60 yylex_init(&client->scanner);
61 yyset_in (file, client->scanner);
63 client->request_strings = NULL;
64 client->num_request_strings = 0;
66 /* XXX: Probably want to register only as the result of the HELO
67 command. Not only will that match the protocol correctly, but
68 it will also eliminate a race condition here. */
69 client->id = ttt_server_register_client (server, client);
73 _ttt_client_fini (ttt_client_t *client)
75 pthread_mutex_lock (&client->mutex);
77 ttt_server_unregister_client (client->server, client);
79 yylex_destroy (client->scanner);
80 shutdown (client->socket, SHUT_RDWR);
82 _free_request (client);
84 pthread_mutex_unlock (&client->mutex);
86 pthread_mutex_destroy (&client->mutex);
89 /* XXX: The memory management for the request strings is pretty cheesy. */
91 _append_to_request (ttt_client_t *client,
94 client->num_request_strings++;
95 client->request_strings =
96 xrealloc (client->request_strings,
97 client->num_request_strings * sizeof (char *));
99 client->request_strings[client->num_request_strings - 1] = xstrdup (string);
103 _free_request (ttt_client_t *client)
107 for (i = 0; i < client->num_request_strings; i++)
108 free (client->request_strings[i]);
110 free (client->request_strings);
112 client->request_strings = NULL;
113 client->num_request_strings = 0;
117 _read_request (ttt_client_t *client)
121 _free_request (client);
124 token = yylex (client->scanner);
125 /* Yes, EOF in two different enums is pretty ugly. */
126 if (token == TTT_TOKEN_EOF)
127 return TTT_STATUS_EOF;
129 if (token == TTT_TOKEN_NEWLINE) {
130 if (client->num_request_strings)
131 return TTT_STATUS_SUCCESS;
136 assert (token == TTT_TOKEN_STRING);
138 _append_to_request (client, yyget_text (client->scanner));
143 _execute_request (ttt_client_t *client)
147 for (i=0; i < client->num_request_strings; i++)
148 ttt_server_broadcast (client->server, client->request_strings[i]);
150 return TTT_ERROR_NONE;
154 _handle_requests_thread (void *closure)
158 ttt_client_t *client = closure;
162 status = _read_request (client);
163 if (status == TTT_STATUS_EOF)
168 error = _execute_request (client);
170 ttt_client_send (client, ttt_error_string (error));
173 _ttt_client_fini (client);
179 /* Exported: See ttt-client.h for documentation. */
181 ttt_client_new (void *closure, int client_socket)
183 ttt_server_t *server = closure;
184 ttt_client_t *client;
187 client = xmalloc (sizeof (ttt_client_t));
189 _ttt_client_init (client, server, client_socket);
191 err = pthread_create (&client->thread, NULL,
192 _handle_requests_thread, client);
194 fprintf (stderr, "Error: pthread_create failed: %s. Aborting.\n",
200 /* Exported: See ttt-client.h for documentation. */
202 ttt_client_send (ttt_client_t *client, const char *message)
204 pthread_mutex_lock (&client->mutex);
206 ttt_socket_write (client->socket, message, strlen (message));
208 pthread_mutex_unlock (&client->mutex);
211 /* Exported: See ttt-client.h for documentation. */
213 ttt_client_get_id (ttt_client_t *client)