]> git.cworth.org Git - ttt/blobdiff - src/ttt-server.c
2005-11-15 Carl Worth <cworth@cworth.org>
[ttt] / src / ttt-server.c
index 316a27021ec6919a7985e80df95fafde2c3b5fc3..c50a797b259ec8bf1dbb6b74886bba2468ca4131 100644 (file)
  * Author: Carl Worth <cworth@cworth.org>
  */
 
-#include "ttt.h"
+#include "ttt-server.h"
+
+#include "ttt-args.h"
 #include "ttt-client.h"
 #include "ttt-socket.h"
 
 struct _ttt_server {
     pthread_mutex_t mutex;
 
+    int next_client_id;
+
     ttt_client_t **clients;
+    int clients_size;
     int num_clients;
 };
 
@@ -35,55 +40,110 @@ ttt_server_init (ttt_server_t *server)
 {
     pthread_mutex_init (&server->mutex, NULL);
 
+    server->next_client_id = 0;
+
     server->clients = NULL;
+    server->clients_size = 0;
     server->num_clients = 0;
 }
 
-static void
-ttt_server_add_client (ttt_server_t *server, ttt_client_t *client)
+/* Exported: See ttt-server.h for documentation. */
+int
+ttt_server_register_client (ttt_server_t *server, ttt_client_t *client)
 {
+    int id;
+
     pthread_mutex_lock (&server->mutex);
 
+    id = server->next_client_id++;
+
+    printf ("Client %d has joined.\n", id);
+
     server->num_clients++;
-    server->clients = xrealloc (server->clients,
-                               server->num_clients * sizeof (ttt_client_t *));
+
+    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;
 
     pthread_mutex_unlock (&server->mutex);
+
+    return id;
 }
 
-static void
-_accept_client (void *closure, int client_socket)
+/* Exported: See ttt-server.h for documentation. */
+void
+ttt_server_unregister_client (ttt_server_t *server, ttt_client_t *client)
 {
-    ttt_server_t *server = closure;
-    ttt_client_t *client;
-    
-    client = ttt_client_create (server, client_socket);
+    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 %d has left.\n", ttt_client_get_id (client));
 
-    ttt_server_add_client (server, client);
+    memmove (&server->clients[i], &server->clients[i+1],
+            (server->num_clients - i - 1) * sizeof (ttt_client_t *));
 
-    ttt_client_handle_requests (client);
+    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);
 }
 
 static const char *WELCOME_MESSAGE = 
-"Welcome to ttt-server. So far, this program is simply a demonstration\n"
-"of a TCP/IP server capable of handling multiple simultaneous clients.\n"
+"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 program, simply connect a client to that host and port.\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 echo any characters\n"
-"it receives back to the client as well as to stdout.\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. The server does support multiple clients, but there is not yet any\n"
-"interaction between the clients. The next step is probably to turn the\n"
-"server into a simple chat server. After that, we should have the necessary\n"
-"structure in place to start implementing the real tic-tac-toe protocol.\n\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)
@@ -104,7 +164,7 @@ main (int argc, char **argv)
     ttt_server_init (&server);
 
     while (1)
-       ttt_socket_accept (socket, _accept_client, &server);
+       ttt_socket_accept (socket, _ttt_server_accept, &server);
 
     /* We only reach here if something bad happened. */
     return 1;