]> git.cworth.org Git - ttt/blob - src/ttt-client.c
* Missing files from rev235
[ttt] / src / ttt-client.c
1 /* ttt-client.c - client handling code for tic-tac-toe game server
2  *
3  * Copyright © 2005 Carl Worth
4  *
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)
8  * any later version.
9  *
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.
14  *
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.
18  *
19  * Author: Carl Worth <cworth@cworth.org>
20  */
21
22 #include "ttt-client.h"
23
24 #include "ttt-command.h"
25 #include "ttt-error.h"
26 #include "ttt-lex.h"
27 #include "ttt-server.h"
28 #include "ttt-socket.h"
29 #include "ttt-token.h"
30
31 struct _ttt_client {
32     pthread_mutex_t mutex;
33     pthread_t thread;
34
35     ttt_server_t *server;
36     int socket;
37     yyscan_t scanner;
38
39     char *name;
40
41     char **request_strings;
42     int num_request_strings;
43 };
44
45 typedef ttt_error_t (*ttt_command_func_t) (ttt_client_t *client,
46                                            char **args,
47                                            int num_args);
48
49 static ttt_error_t
50 _ttt_client_execute_helo (ttt_client_t *client,
51                           char         **args,
52                           int          num_args);
53
54 typedef struct _ttt_command_description {
55     const char *command;
56     int args_min;
57     int args_max;
58     ttt_command_func_t execute;
59 } ttt_command_description_t;
60
61 ttt_command_description_t command_descriptions[] = {
62     {"HELO", 0, 1, _ttt_client_execute_helo}
63 };
64
65 static ttt_error_t
66 _ttt_client_execute_helo (ttt_client_t *client,
67                           char         **args,
68                           int          num_args)
69 {
70     ttt_error_t error;
71     char *response;
72
73     if (num_args == 1)
74         ttt_client_set_name (client, args[0]);
75
76     error = ttt_server_register_client (client->server, client);
77     if (error)
78         return error;
79
80     xasprintf (&response, "HELO %s %s %s\n", client->name,
81                ttt_server_get_host (client->server),
82                ttt_server_get_port (client->server));
83
84     ttt_client_send (client, response);
85
86     free (response);
87
88     return TTT_ERROR_NONE;
89 }
90
91 #define ARRAY_SIZE(arr) (sizeof(arr)/sizeof(arr[0]))
92
93 static void
94 _free_request (ttt_client_t *client);
95
96 static void
97 _ttt_client_init (ttt_client_t  *client,
98                   ttt_server_t  *server,
99                   int            socket)
100 {
101     FILE *file;
102
103     pthread_mutex_init (&client->mutex, NULL);
104
105     client->server = server;
106     client->socket = socket;
107
108     file = xfdopen (socket, "r");
109     yylex_init(&client->scanner);
110     yyset_in (file, client->scanner);
111
112     client->request_strings = NULL;
113     client->num_request_strings = 0;
114
115     client->name = NULL;
116 }
117
118 static void
119 _ttt_client_fini (ttt_client_t *client)
120 {
121     pthread_mutex_lock (&client->mutex);
122
123     ttt_server_unregister_client (client->server, client);
124
125     free (client->name);
126     client->name = NULL;
127
128     yylex_destroy (client->scanner);
129     shutdown (client->socket, SHUT_RDWR);
130
131     _free_request (client);
132
133     pthread_mutex_unlock (&client->mutex);
134
135     pthread_mutex_destroy (&client->mutex);
136 }
137
138 /* XXX: The memory management for the request strings is pretty cheesy. */
139 static void
140 _append_to_request (ttt_client_t        *client,
141                     const char          *string)
142 {
143     client->num_request_strings++;
144     client->request_strings =
145         xrealloc (client->request_strings,
146                   client->num_request_strings * sizeof (char *));
147
148     client->request_strings[client->num_request_strings - 1] = xstrdup (string);
149 }
150
151 static void
152 _free_request (ttt_client_t *client)
153 {
154     int i;
155
156     for (i = 0; i < client->num_request_strings; i++)
157         free (client->request_strings[i]);
158
159     free (client->request_strings);
160
161     client->request_strings = NULL;
162     client->num_request_strings = 0;
163 }
164
165 static ttt_status_t
166 _read_request (ttt_client_t *client)
167 {
168     ttt_token_t token;
169
170     _free_request (client);
171
172     while (1) {
173         token = yylex (client->scanner);
174         /* Yes, EOF in two different enums is pretty ugly. */
175         if (token == TTT_TOKEN_EOF)
176             return TTT_STATUS_EOF;
177
178         if (token == TTT_TOKEN_NEWLINE) {
179             if (client->num_request_strings)
180                 return TTT_STATUS_SUCCESS;
181             else
182                 continue;
183         }
184
185         assert (token == TTT_TOKEN_STRING);
186
187         _append_to_request (client, yyget_text (client->scanner));
188     }
189 }
190
191 static ttt_error_t
192 _execute_request (ttt_client_t *client)
193 {
194     int i;
195
196     char *command = client->request_strings[0];
197     int num_args = client->num_request_strings-1;
198     ttt_command_description_t *desc;
199
200     for (i=0; i < strlen (command); i++)
201         command[i] = toupper (command[i]);
202
203     for (i=0; i < ARRAY_SIZE(command_descriptions); i++) {
204         desc = &command_descriptions[i];
205         if (strcmp(command, desc->command) == 0) {
206             if ((num_args < desc->args_min) || (num_args > desc->args_max))
207                 return TTT_ERROR_SYNTAX;
208             return (desc->execute) (client, &client->request_strings[1], num_args);
209             }
210         }
211
212     return TTT_ERROR_COMMAND;
213 }
214
215 static void *
216 _handle_requests_thread (void *closure)
217 {
218     ttt_status_t status;
219     ttt_error_t error;
220     ttt_client_t *client = closure;
221
222     while (1) {
223
224         status = _read_request (client);
225         if (status == TTT_STATUS_EOF)
226             break;
227         if (status)
228             ASSERT_NOT_REACHED;
229
230         error = _execute_request (client);
231         if (error)
232             ttt_client_send (client, ttt_error_string (error));
233     }
234
235     _ttt_client_fini (client);
236     free (client);
237
238     return (void *) 0;
239 }
240
241 /* Exported: See ttt-client.h for documentation. */
242 void
243 ttt_client_new (void *closure, int client_socket)
244 {
245     ttt_server_t *server = closure;
246     ttt_client_t *client;
247     int err;
248
249     client = xmalloc (sizeof (ttt_client_t));
250     
251     _ttt_client_init (client, server, client_socket);
252
253     err = pthread_create (&client->thread, NULL,
254                           _handle_requests_thread, client);
255     if (err != 0) {
256         fprintf (stderr, "Error: pthread_create failed: %s. Aborting.\n",
257                  strerror (err));
258         exit (1);
259     }
260 }
261
262 /* Exported: See ttt-client.h for documentation. */
263 void
264 ttt_client_send (ttt_client_t *client, const char *message)
265 {
266     pthread_mutex_lock (&client->mutex);
267
268     ttt_socket_write (client->socket, message, strlen (message));
269
270     pthread_mutex_unlock (&client->mutex);
271 }
272
273 /* Exported: See ttt-client.h for documentation. */
274 const char*
275 ttt_client_get_name (ttt_client_t *client)
276 {
277     return client->name;
278 }
279
280 /* Exported: See ttt-client.h for documentation. */
281 void
282 ttt_client_set_name (ttt_client_t *client, const char *name)
283 {
284     free (client->name);
285     client->name = xstrdup (name);
286 }