]> git.cworth.org Git - ttt/blobdiff - src/ttt-server.c
* PROTOCOL: Removed unused servername
[ttt] / src / ttt-server.c
index b42db7a95064811b95fff78b4f0070236b4fe69c..e9043b4e2cb25309e65e68ee202af9a617e2a90f 100644 (file)
@@ -1,4 +1,4 @@
-/* ttt.c - client-server tic-tac-toe game
+/* ttt-server.c - tic-tac-toe game server
  *
  * Copyright © 2005 Carl Worth
  *
  * Author: Carl Worth <cworth@cworth.org>
  */
 
-#include "ttt.h"
+#include "ttt-server.h"
+
+#include "ttt-args.h"
+#include "ttt-client.h"
+#include "ttt-error.h"
+#include "ttt-socket.h"
+
+struct _ttt_server {
+    pthread_mutex_t mutex;
+
+    const char *host;
+    const char *port;
+
+    int next_client_id;
+
+    ttt_client_t **clients;
+    int clients_size;
+    int num_clients;
+};
+
+static void
+ttt_server_init (ttt_server_t *server, const char *host, const char *port)
+{
+    pthread_mutex_init (&server->mutex, NULL);
+
+    server->host = host;
+    server->port = port;
+
+    server->next_client_id = 0;
+
+    server->clients = NULL;
+    server->clients_size = 0;
+    server->num_clients = 0;
+}
+
+/* Exported: See ttt-server.h for documentation. */
+ttt_error_t
+ttt_server_register_client (ttt_server_t *server, ttt_client_t *client)
+{
+    int i;
+    ttt_error_t error = TTT_ERROR_NONE;
+    char *name;
+
+    pthread_mutex_lock (&server->mutex);
+
+    name = xstrdup (ttt_client_get_name (client));
+
+    if (name == NULL) {
+       xasprintf(&name, "user%03d", server->next_client_id++);
+       ttt_client_set_name (client, name);
+    }
+
+    /* XXX: If generated name is not unique, this will return an error,
+       which violates the protocol. */
+    for (i = 0; i < server->num_clients; i++) {
+       if (strcmp (ttt_client_get_name (server->clients[i]), name) == 0) {
+           error = TTT_ERROR_INVALIDNAME;
+           goto CLEANUP_LOCK;
+       }
+    }
+
+    printf ("Client %s has joined.\n", name);
+    free (name);
+
+    server->num_clients++;
+
+    if (server->num_clients > server->clients_size) {
+       if (server->clients_size == 0)
+           server->clients_size = 1;
+       else
+           server->clients_size *= 2;
+
+       server->clients = xrealloc (server->clients,
+                                   server->clients_size * sizeof (ttt_client_t *));
+    }
+
+    server->clients [server->num_clients - 1] = client;
+
+ CLEANUP_LOCK:
+    pthread_mutex_unlock (&server->mutex);
+
+    return error;
+}
+
+/* Exported: See ttt-server.h for documentation. */
+void
+ttt_server_unregister_client (ttt_server_t *server, ttt_client_t *client)
+{
+    int i;
+
+    pthread_mutex_lock (&server->mutex);
+
+    for (i = 0; i < server->num_clients; i++)
+       if (server->clients[i] == client)
+           break;
+
+    assert (i < server->num_clients);
+
+    printf ("Client %s has left.\n", ttt_client_get_name (client));
+
+    memmove (&server->clients[i], &server->clients[i+1],
+            (server->num_clients - i - 1) * sizeof (ttt_client_t *));
+
+    server->num_clients--;
+
+    pthread_mutex_unlock (&server->mutex);
+}
+
+void
+ttt_server_broadcast (ttt_server_t *server, const char *message)
+{
+    int i;
+
+    pthread_mutex_lock (&server->mutex);
+
+    for (i = 0; i < server->num_clients; i++)
+       ttt_client_send (server->clients[i], message);
+
+    pthread_mutex_unlock (&server->mutex);
+}
+
+/* Exported: See ttt-server.h for documentation. */
+const char*
+ttt_server_get_host (ttt_server_t *server)
+{
+    return server->host;
+}
+
+/* Exported: See ttt-server.h for documentation. */
+const char*
+ttt_server_get_port (ttt_server_t *server)
+{
+    return server->port;
+}
+
+static const char *WELCOME_MESSAGE = 
+"Welcome to ttt-server. So far, this program is still a demonstration\n"
+"TCP/IP server, acting something like a rather braindead chat server.\n"
+"The server is currently listening on:\n"
+"\n    %s:%s\n"
+"\nTo test this, simply connect one or more clients to that host and port.\n"
+"For example:\n"
+"\n    telnet %s %s\n"
+"\nOnce you have connected a client, the server will send each line of text\n"
+"it receives to all connected clients. The server reports client joins and\n"
+"departures on stdout.\n"
+"\nNote that to terminate the telnet client you type Control-], then\n"
+"<Enter>, then \"close\" (and <Enter>) at the \"telnet> \" prompt.\n"
+"\nHave fun!\n"
+"-Carl\n"
+"\nPS. At this point we're ready to leave the demonstration phase and to\n"
+"begin implementing TTTP (tic-tac-toe protocol) as well as fixing the\n"
+"protocol specifcation. We don't need a custom client to move forward on\n"
+"the server (that is one of the ideas behind using a telnet-compatible\n"
+"protocol), but a custom client would still be a fine project for a\n"
+"motivated beginning programmer.\n\n";
+
+static void
+_ttt_server_accept (void *closure, int client_socket)
+{
+    ttt_server_t *server = closure;
+
+    ttt_client_new (server, client_socket);
+}
 
 int 
 main (int argc, char **argv)
 {
     ttt_args_t args;
-    int args_first;
+    ttt_server_t server;
+    int socket;
+
+    ttt_args_parse (&args, argc, argv);
+
+    if (args.log_file)
+       xfreopen (args.log_file, "a", stderr);
+
+    socket = ttt_socket_create_server (args.host, args.port);
+
+    printf (WELCOME_MESSAGE, args.host, args.port, args.host, args.port);
 
-    ttt_args_parse (&args, argc, argv, &args_first);
+    ttt_server_init (&server, args.host, args.port);
 
-    /* XXX: insert code here */
+    while (1)
+       ttt_socket_accept (socket, _ttt_server_accept, &server);
 
-    return 0;
+    /* We only reach here if something bad happened. */
+    return 1;
 }