]> git.cworth.org Git - ttt/blob - src/ttt-client.c
2005-11-22 Carl Worth <cworth@cworth.org>
[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     int id;
39
40     char **request_strings;
41     int num_request_strings;
42 };
43
44 static void
45 _free_request (ttt_client_t *client);
46
47 static void
48 _ttt_client_init (ttt_client_t  *client,
49                   ttt_server_t  *server,
50                   int            socket)
51 {
52     FILE *file;
53
54     pthread_mutex_init (&client->mutex, NULL);
55
56     client->server = server;
57     client->socket = socket;
58
59     file = xfdopen (socket, "r");
60     yylex_init(&client->scanner);
61     yyset_in (file, client->scanner);
62
63     client->request_strings = NULL;
64     client->num_request_strings = 0;
65
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);
70 }
71
72 static void
73 _ttt_client_fini (ttt_client_t *client)
74 {
75     pthread_mutex_lock (&client->mutex);
76
77     ttt_server_unregister_client (client->server, client);
78
79     yylex_destroy (client->scanner);
80     shutdown (client->socket, SHUT_RDWR);
81
82     _free_request (client);
83
84     pthread_mutex_unlock (&client->mutex);
85
86     pthread_mutex_destroy (&client->mutex);
87 }
88
89 /* XXX: The memory management for the request strings is pretty cheesy. */
90 static void
91 _append_to_request (ttt_client_t        *client,
92                     const char          *string)
93 {
94     client->num_request_strings++;
95     client->request_strings =
96         xrealloc (client->request_strings,
97                   client->num_request_strings * sizeof (char *));
98
99     client->request_strings[client->num_request_strings - 1] = xstrdup (string);
100 }
101
102 static void
103 _free_request (ttt_client_t *client)
104 {
105     int i;
106
107     for (i = 0; i < client->num_request_strings; i++)
108         free (client->request_strings[i]);
109
110     free (client->request_strings);
111
112     client->request_strings = NULL;
113     client->num_request_strings = 0;
114 }
115
116 static ttt_status_t
117 _read_request (ttt_client_t *client)
118 {
119     ttt_token_t token;
120
121     _free_request (client);
122
123     while (1) {
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;
128
129         if (token == TTT_TOKEN_NEWLINE) {
130             if (client->num_request_strings)
131                 return TTT_STATUS_SUCCESS;
132             else
133                 continue;
134         }
135
136         assert (token == TTT_TOKEN_STRING);
137
138         _append_to_request (client, yyget_text (client->scanner));
139     }
140 }
141
142 static ttt_error_t
143 _execute_request (ttt_client_t *client)
144 {
145     int i;
146
147     for (i=0; i < client->num_request_strings; i++)
148         ttt_server_broadcast (client->server, client->request_strings[i]);
149
150     return TTT_ERROR_NONE;
151 }
152
153 static void *
154 _handle_requests_thread (void *closure)
155 {
156     ttt_status_t status;
157     ttt_error_t error;
158     ttt_client_t *client = closure;
159
160     while (1) {
161
162         status = _read_request (client);
163         if (status == TTT_STATUS_EOF)
164             break;
165         if (status)
166             ASSERT_NOT_REACHED;
167
168         error = _execute_request (client);
169         if (error)
170             ttt_client_send (client, ttt_error_string (error));
171     }
172
173     _ttt_client_fini (client);
174     free (client);
175
176     return (void *) 0;
177 }
178
179 /* Exported: See ttt-client.h for documentation. */
180 void
181 ttt_client_new (void *closure, int client_socket)
182 {
183     ttt_server_t *server = closure;
184     ttt_client_t *client;
185     int err;
186
187     client = xmalloc (sizeof (ttt_client_t));
188     
189     _ttt_client_init (client, server, client_socket);
190
191     err = pthread_create (&client->thread, NULL,
192                           _handle_requests_thread, client);
193     if (err != 0) {
194         fprintf (stderr, "Error: pthread_create failed: %s. Aborting.\n",
195                  strerror (err));
196         exit (1);
197     }
198 }
199
200 /* Exported: See ttt-client.h for documentation. */
201 void
202 ttt_client_send (ttt_client_t *client, const char *message)
203 {
204     pthread_mutex_lock (&client->mutex);
205
206     ttt_socket_write (client->socket, message, strlen (message));
207
208     pthread_mutex_unlock (&client->mutex);
209 }
210
211 /* Exported: See ttt-client.h for documentation. */
212 int
213 ttt_client_get_id (ttt_client_t *client)
214 {
215     return client->id;
216 }