2005-11-24 Carl Worth <cworth@cworth.org>
[ttt] / src / ttt-client.c
index e6c97a1c6ad9c2f16ba1f07d9bbc0336e68daa93..508aa88e91ecffba87e9c7e63a3caaa92db521ea 100644 (file)
  */
 
 #include "ttt-client.h"
+
+#include "ttt-error.h"
+#include "ttt-lex.h"
+#include "ttt-server.h"
 #include "ttt-socket.h"
+#include "ttt-token.h"
+
+struct _ttt_client {
+    pthread_mutex_t mutex;
+    pthread_t thread;
+
+    ttt_server_t *server;
+    int socket;
+    yyscan_t scanner;
+
+    char *name;
+
+    char **request_strings;
+    int num_request_strings;
+};
+
+typedef ttt_error_t (*ttt_command_func_t) (ttt_client_t *client,
+                                          char **args,
+                                          int num_args);
+
+static ttt_error_t
+_ttt_client_execute_helo (ttt_client_t *client,
+                         char         **args,
+                         int          num_args);
+
+typedef struct _ttt_command_description {
+    const char *command;
+    int args_min;
+    int args_max;
+    ttt_command_func_t execute;
+} ttt_command_description_t;
+
+ttt_command_description_t command_descriptions[] = {
+    {"HELO", 1, 1, _ttt_client_execute_helo}
+};
 
-ttt_client_t *
-ttt_client_create (ttt_server_t *server, int socket, int id)
+static ttt_error_t
+_ttt_client_execute_helo (ttt_client_t *client,
+                         char         **args,
+                         int          num_args)
 {
-    ttt_client_t *client;
+    ttt_error_t error;
+    char *response;
 
-    client = xmalloc (sizeof (ttt_client_t));
+    if (num_args == 1)
+       ttt_client_set_name (client, args[0]);
+
+    error = ttt_server_register_client (client->server, client);
+    if (error)
+       return error;
+
+    xasprintf (&response, "HELO %s %s %s\n", client->name,
+              ttt_server_get_host (client->server),
+              ttt_server_get_port (client->server));
+
+    ttt_client_send (client, response);
+
+    free (response);
+
+    return TTT_ERROR_NONE;
+}
+
+#define ARRAY_SIZE(arr) (sizeof(arr)/sizeof(arr[0]))
+
+static void
+_free_request (ttt_client_t *client);
+
+static void
+_ttt_client_init (ttt_client_t *client,
+                 ttt_server_t  *server,
+                 int            socket)
+{
+    FILE *file;
+
+    pthread_mutex_init (&client->mutex, NULL);
 
     client->server = server;
     client->socket = socket;
 
-    client->id = id;
-
-    client->buf_head = client->buf;
-    client->buf_tail = client->buf;
+    file = xfdopen (socket, "r");
+    yylex_init(&client->scanner);
+    yyset_in (file, client->scanner);
 
-    client->request = NULL;
-    client->request_size = 0;
-    client->request_len = 0;
+    client->request_strings = NULL;
+    client->num_request_strings = 0;
 
-    return client;
+    client->name = NULL;
 }
 
-void
-ttt_client_destroy (ttt_client_t *client)
+static void
+_ttt_client_fini (ttt_client_t *client)
 {
+    pthread_mutex_lock (&client->mutex);
+
+    ttt_server_unregister_client (client->server, client);
+
+    free (client->name);
+    client->name = NULL;
+
+    yylex_destroy (client->scanner);
     shutdown (client->socket, SHUT_RDWR);
 
-    free (client->request);
+    _free_request (client);
 
-    free (client);
+    pthread_mutex_unlock (&client->mutex);
+
+    pthread_mutex_destroy (&client->mutex);
 }
 
+/* XXX: The memory management for the request strings is pretty cheesy. */
 static void
 _append_to_request (ttt_client_t       *client,
-                   const char          *buf,
-                   int                  size)
+                   const char          *string)
 {
-    int size_needed = client->request_len + size;
-
-    if (size_needed > client->request_size) {
-       if (client->request_size == 0) {
-           client->request_size = size_needed;
-       } else {
-           while (size_needed > client->request_size)
-               client->request_size *= 2;
-       }
+    client->num_request_strings++;
+    client->request_strings =
+       xrealloc (client->request_strings,
+                 client->num_request_strings * sizeof (char *));
 
-       client->request = xrealloc (client->request, client->request_size);
-    }
+    client->request_strings[client->num_request_strings - 1] = xstrdup (string);
+}
 
-    memcpy (client->request + client->request_len,
-           buf, size);
+static void
+_free_request (ttt_client_t *client)
+{
+    int i;
+
+    for (i = 0; i < client->num_request_strings; i++)
+       free (client->request_strings[i]);
 
-    client->request_len += size;
+    free (client->request_strings);
+
+    client->request_strings = NULL;
+    client->num_request_strings = 0;
 }
 
 static ttt_status_t
-ttt_client_read_into_request_until (ttt_client_t *client, char delimeter)
+_read_request (ttt_client_t *client)
 {
-    ttt_bool_t found_delimeter = FALSE;
-    int bytes_read;
-    char *s;
+    ttt_token_t token;
 
-    client->request_len = 0;
+    _free_request (client);
 
     while (1) {
+       token = yylex (client->scanner);
+       /* Yes, EOF in two different enums is pretty ugly. */
+       if (token == TTT_TOKEN_EOF)
+           return TTT_STATUS_EOF;
 
-       if (client->buf_tail >= client->buf_head) {
-           bytes_read = xread (client->socket,
-                               client->buf,
-                               TTT_CLIENT_BUF_SIZE);
-           if (bytes_read == 0)
-               return TTT_STATUS_EOF;
-           client->buf_head = client->buf;
-           client->buf_tail = client->buf_head + bytes_read;
+       if (token == TTT_TOKEN_NEWLINE) {
+           if (client->num_request_strings)
+               return TTT_STATUS_SUCCESS;
+           else
+               continue;
        }
 
-       for (s = client->buf_head; s < client->buf_tail; s++) {
-           if (*s == delimeter) {
-               found_delimeter = TRUE;
-               s++;
-               break;
+       assert (token == TTT_TOKEN_STRING);
+
+       _append_to_request (client, yyget_text (client->scanner));
+    }
+}
+
+static ttt_error_t
+_execute_request (ttt_client_t *client)
+{
+    int i;
+
+    char *command = client->request_strings[0];
+    int num_args = client->num_request_strings-1;
+    ttt_command_description_t *desc;
+
+    for (i=0; i < strlen (command); i++)
+       command[i] = toupper (command[i]);
+
+    for (i=0; i < ARRAY_SIZE(command_descriptions); i++) {
+       desc = &command_descriptions[i];
+       if (strcmp(command, desc->command) == 0) {
+           if ((num_args < desc->args_min) || (num_args > desc->args_max))
+               return TTT_ERROR_SYNTAX;
+           return (desc->execute) (client, &client->request_strings[1], num_args);
            }
        }
 
-       _append_to_request (client,
-                           client->buf_head,
-                           s - client->buf_head);
-       client->buf_head = s;
-
-       if (found_delimeter)
-           return TTT_STATUS_SUCCESS;
-    }
+    return TTT_ERROR_COMMAND;
 }
 
-char *
-ttt_client_read_line (ttt_client_t *client)
+static void *
+_handle_requests_thread (void *closure)
 {
     ttt_status_t status;
-    static const char null_terminator = '\0';
+    ttt_error_t error;
+    ttt_client_t *client = closure;
+
+    while (1) {
+
+       status = _read_request (client);
+       if (status == TTT_STATUS_EOF)
+           break;
+       if (status)
+           ASSERT_NOT_REACHED;
 
-    status = ttt_client_read_into_request_until (client, '\n');
-    if (status) {
-       assert (status == TTT_STATUS_EOF);
-       return NULL;
+       error = _execute_request (client);
+       if (error)
+           ttt_client_send (client, ttt_error_string (error));
     }
 
-    _append_to_request (client, &null_terminator, 1);
+    _ttt_client_fini (client);
+    free (client);
 
-    return client->request;
+    return (void *) 0;
 }
 
+/* Exported: See ttt-client.h for documentation. */
+void
+ttt_client_new (void *closure, int client_socket)
+{
+    ttt_server_t *server = closure;
+    ttt_client_t *client;
+    int err;
+
+    client = xmalloc (sizeof (ttt_client_t));
+    
+    _ttt_client_init (client, server, client_socket);
+
+    err = pthread_create (&client->thread, NULL,
+                         _handle_requests_thread, client);
+    if (err != 0) {
+       fprintf (stderr, "Error: pthread_create failed: %s. Aborting.\n",
+                strerror (err));
+       exit (1);
+    }
+}
+
+/* Exported: See ttt-client.h for documentation. */
 void
 ttt_client_send (ttt_client_t *client, const char *message)
 {
+    pthread_mutex_lock (&client->mutex);
+
     ttt_socket_write (client->socket, message, strlen (message));
+
+    pthread_mutex_unlock (&client->mutex);
 }
 
+/* Exported: See ttt-client.h for documentation. */
+const char*
+ttt_client_get_name (ttt_client_t *client)
+{
+    return client->name;
+}
 
+/* Exported: See ttt-client.h for documentation. */
+void
+ttt_client_set_name (ttt_client_t *client, const char *name)
+{
+    free (client->name);
+    client->name = xstrdup (name);
+}