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