]> git.cworth.org Git - ttt/commitdiff
2005-11-11 Carl Worth <cworth@cworth.org>
authorCarl Worth <carl@theworths.org>
Sat, 12 Nov 2005 01:21:21 +0000 (01:21 +0000)
committerCarl Worth <carl@theworths.org>
Sat, 12 Nov 2005 01:21:21 +0000 (01:21 +0000)
        * src/ttt-server.h:
        * src/ttt-server.c: (ttt_server_init), (ttt_server_add_client),
        (_accept_client), (main): Add a new ttt_server_t object with a
        mutex-protected list of clients. Move client handling code down
        into new ttt-client.c.

        * src/ttt-client.h:
        * src/ttt-client.c: (ttt_client_create), (ttt_client_destroy),
        (ttt_client_handle_requests): New ttt_client_t object that simply
        holds a pointer back to the server as well as the client socket
        file descriptor.

        * src/ttt-socket.h:
        * src/ttt-socket.c: (ttt_socket_accept): Add closure argument. Add
        a call to fork before calling the accept callback.

        * src/ttt-args.c: Hide unused ttt_args_usage from the compiler to
        keep it from complaining.

        * src/ttt.c: (main): Rename the client main program from
        ttt-client to ttt.c, (since the server code now has its own
        ttt-client.c).

        * src/ttt.h:
        * src/Makefile.am: Include and link with pthread now that we have
        pthread-based mutex locking.

ChangeLog
src/Makefile.am
src/ttt-args.c
src/ttt-client.c
src/ttt-client.h [new file with mode: 0644]
src/ttt-server.c
src/ttt-server.h [new file with mode: 0644]
src/ttt-socket.c
src/ttt-socket.h
src/ttt.c [new file with mode: 0644]
src/ttt.h

index d7cc7f95f8f80595d4f353e8473325c8d79f753e..85d24c63102f69a8328e8028f306419eb9901e10 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,32 @@
+2005-11-11  Carl Worth  <cworth@cworth.org>
+
+       * src/ttt-server.h:
+       * src/ttt-server.c: (ttt_server_init), (ttt_server_add_client),
+       (_accept_client), (main): Add a new ttt_server_t object with a
+       mutex-protected list of clients. Move client handling code down
+       into new ttt-client.c.
+
+       * src/ttt-client.h:
+       * src/ttt-client.c: (ttt_client_create), (ttt_client_destroy),
+       (ttt_client_handle_requests): New ttt_client_t object that simply
+       holds a pointer back to the server as well as the client socket
+       file descriptor.
+
+       * src/ttt-socket.h:
+       * src/ttt-socket.c: (ttt_socket_accept): Add closure argument. Add
+       a call to fork before calling the accept callback.
+
+       * src/ttt-args.c: Hide unused ttt_args_usage from the compiler to
+       keep it from complaining.
+
+       * src/ttt.c: (main): Rename the client main program from
+       ttt-client to ttt.c, (since the server code now has its own
+       ttt-client.c).
+
+       * src/ttt.h:
+       * src/Makefile.am: Include and link with pthread now that we have
+       pthread-based mutex locking.
+
 2005-11-11  Carl Worth  <cworth@pippin.local>
 
        * src/ttt-server.c: (main): Don't assign to stderr which is
 2005-11-11  Carl Worth  <cworth@pippin.local>
 
        * src/ttt-server.c: (main): Don't assign to stderr which is
index 792b1ecc7ce42e5b1803e1eb24df180b8c0f405f..5619e522b5bd9ff35598c95fcb2c9ca0447a9402 100644 (file)
@@ -12,12 +12,13 @@ ttt_common_sources =                \
 
 ttt_client_SOURCES =           \
        $(ttt_common_sources)   \
 
 ttt_client_SOURCES =           \
        $(ttt_common_sources)   \
-       ttt-client.c
+       ttt.c
 
 ttt_server_SOURCES =           \
        $(ttt_common_sources)   \
 
 ttt_server_SOURCES =           \
        $(ttt_common_sources)   \
+       ttt-client.c            \
        ttt-server.c
 
 AM_CFLAGS = $(WARN_CFLAGS) $(TTT_CFLAGS)
 ttt_client_LDFLAGS = $(TTT_LIBS)
        ttt-server.c
 
 AM_CFLAGS = $(WARN_CFLAGS) $(TTT_CFLAGS)
 ttt_client_LDFLAGS = $(TTT_LIBS)
-ttt_server_LDFLAGS = $(TTT_LIBS)
+ttt_server_LDFLAGS = $(TTT_LIBS) -lpthread
index 18a7d255405cd49c0fda6cbf6be7120dccdf5cf2..54cc0dbbd823ac5c9ddebe464e99ceec63500985 100644 (file)
@@ -81,12 +81,14 @@ ttt_args_help (const char *argv0)
     printf ("           --log-file=FILE\tFile to use for logging\n");
 }
 
     printf ("           --log-file=FILE\tFile to use for logging\n");
 }
 
+#if 0
 static void
 ttt_args_usage (const char *argv0)
 {
     printf ("Usage: %s [OPTION] %s\n", argv0, TTT_ARGS_PROGRAM_ARGDOC);
     printf ("Try `%s --help' for more information.\n", argv0);
 }
 static void
 ttt_args_usage (const char *argv0)
 {
     printf ("Usage: %s [OPTION] %s\n", argv0, TTT_ARGS_PROGRAM_ARGDOC);
     printf ("Try `%s --help' for more information.\n", argv0);
 }
+#endif
 
 int
 ttt_args_parse(ttt_args_t *args, int argc, char *argv[])
 
 int
 ttt_args_parse(ttt_args_t *args, int argc, char *argv[])
index d6b6fe7b4e474d9c7ce4f7962bdbe1fdaab876f4..80fca846e13ee46e4fae37b87ee966901a91de9a 100644 (file)
@@ -1,4 +1,4 @@
-/* ttt.c - client-server tic-tac-toe game
+/* ttt-client.c - client handling code for tic-tac-toe game server
  *
  * Copyright © 2005 Carl Worth
  *
  *
  * Copyright © 2005 Carl Worth
  *
  * Author: Carl Worth <cworth@cworth.org>
  */
 
  * Author: Carl Worth <cworth@cworth.org>
  */
 
-#include "ttt.h"
+#include "ttt-client.h"
 
 
-int 
-main (int argc, char **argv)
+struct _ttt_client {
+    ttt_server_t *server;
+    int socket;
+};
+
+ttt_client_t *
+ttt_client_create (ttt_server_t *server, int socket)
 {
 {
-    ttt_args_t args;
+    ttt_client_t *client;
+
+    client = xmalloc (sizeof (ttt_client_t));
+
+    client->server = server;
+    client->socket = socket;
 
 
-    ttt_args_parse (&args, argc, argv);
+    return client;
+}
 
 
-    /* XXX: insert code here */
+void
+ttt_client_destroy (ttt_client_t *client)
+{
+    close (client->socket);
 
 
-    return 0;
+    free (client);
 }
 }
+
+void
+ttt_client_handle_requests (ttt_client_t *client)
+{
+#define BUF_SIZE 1024
+
+    while (1) {
+       char buf[BUF_SIZE];
+       int cnt;
+       cnt = read (client->socket, buf, BUF_SIZE);
+       if (cnt == 0)
+           break;
+       write (0, buf, cnt);
+       write (client->socket, buf, cnt);
+    }
+}
+
+
diff --git a/src/ttt-client.h b/src/ttt-client.h
new file mode 100644 (file)
index 0000000..b067674
--- /dev/null
@@ -0,0 +1,50 @@
+/* ttt-client.c - client handling code for tic-tac-toe game server
+ *
+ * Copyright © 2005 Carl Worth
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2, or (at your option)
+ * any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ *
+ * Author: Carl Worth <cworth@cworth.org>
+ */
+
+#include "ttt.h"
+
+#include "ttt-server.h"
+
+#ifndef _TTT_CLIENT_H_
+#define _TTT_CLIENT_H_
+
+typedef struct _ttt_client ttt_client_t;
+
+/* Create a new ttt_client_t for the given server and given a
+ * connected socket.
+ *
+ * Returns: A new ttt_client_t. Call ttt_client_destroy when finished
+ * with it.
+ *
+ * Errors: If any error occurs, (such as out-of-memory), this function
+ * will not return.
+ */
+ttt_client_t *
+ttt_client_create (ttt_server_t *server, int socket);
+
+void
+ttt_client_destroy (ttt_client_t *client);
+
+/* Loop forever handling client requests. Never returns. */
+void
+ttt_client_handle_requests (ttt_client_t *client);
+
+#endif /* _TTT_CLIENT_H_ */
index 62c02cc2cc22e3d6547dc7425828ca245cb0d7bd..316a27021ec6919a7985e80df95fafde2c3b5fc3 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
  *
  *
  * Copyright © 2005 Carl Worth
  *
  */
 
 #include "ttt.h"
  */
 
 #include "ttt.h"
+#include "ttt-client.h"
 #include "ttt-socket.h"
 
 #include "ttt-socket.h"
 
+struct _ttt_server {
+    pthread_mutex_t mutex;
+
+    ttt_client_t **clients;
+    int num_clients;
+};
+
 static void
 static void
-ttt_dispatch (int connected_socket)
+ttt_server_init (ttt_server_t *server)
 {
 {
-#define BUF_SIZE 1024
-
-    while (1) {
-       char buf[BUF_SIZE];
-       int cnt;
-       cnt = read (connected_socket, buf, BUF_SIZE);
-       if (cnt == 0)
-           break;
-       write (0, buf, cnt);
-       write (connected_socket, buf, cnt);
-    }
+    pthread_mutex_init (&server->mutex, NULL);
+
+    server->clients = NULL;
+    server->num_clients = 0;
+}
+
+static void
+ttt_server_add_client (ttt_server_t *server, ttt_client_t *client)
+{
+    pthread_mutex_lock (&server->mutex);
+
+    server->num_clients++;
+    server->clients = xrealloc (server->clients,
+                               server->num_clients * sizeof (ttt_client_t *));
+
+    server->clients [server->num_clients - 1] = client;
+
+    pthread_mutex_unlock (&server->mutex);
+}
+
+static void
+_accept_client (void *closure, int client_socket)
+{
+    ttt_server_t *server = closure;
+    ttt_client_t *client;
+    
+    client = ttt_client_create (server, client_socket);
+
+    ttt_server_add_client (server, client);
+
+    ttt_client_handle_requests (client);
 }
 
 static const char *WELCOME_MESSAGE = 
 "Welcome to ttt-server. So far, this program is simply a demonstration\n"
 }
 
 static const char *WELCOME_MESSAGE = 
 "Welcome to ttt-server. So far, this program is simply a demonstration\n"
-"of a TCP/IP server that handles one client at a time. The server is\n"
-"currently listening on:\n"
+"of a TCP/IP server capable of handling multiple simultaneous clients.\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"
 "For example:\n"
 "\n    %s:%s\n"
 "\nTo test this program, simply connect a client to that host and port.\n"
 "For example:\n"
@@ -52,16 +80,16 @@ static const char *WELCOME_MESSAGE =
 "<Enter>, then \"close\" (and <Enter>) at the \"telnet> \" prompt.\n"
 "\nHave fun!\n"
 "-Carl\n"
 "<Enter>, then \"close\" (and <Enter>) at the \"telnet> \" prompt.\n"
 "\nHave fun!\n"
 "-Carl\n"
-"\nPS. The server handles only one client at a time, but multiple clients\n"
-"may be able to connect simultaneously. Subsequent clients will see no\n"
-"output from the server until all previous clients exit.\n"
-"\nExtending the server to fork a new process for each client would be a fun\n"
-"project for a motivated student (as would writing a custom client program).\n\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";
 
 int 
 main (int argc, char **argv)
 {
     ttt_args_t args;
 
 int 
 main (int argc, char **argv)
 {
     ttt_args_t args;
+    ttt_server_t server;
     int socket;
 
     ttt_args_parse (&args, argc, argv);
     int socket;
 
     ttt_args_parse (&args, argc, argv);
@@ -73,8 +101,10 @@ main (int argc, char **argv)
 
     printf (WELCOME_MESSAGE, args.host, args.port, args.host, args.port);
 
 
     printf (WELCOME_MESSAGE, args.host, args.port, args.host, args.port);
 
+    ttt_server_init (&server);
+
     while (1)
     while (1)
-       ttt_socket_accept (socket, ttt_dispatch);
+       ttt_socket_accept (socket, _accept_client, &server);
 
     /* We only reach here if something bad happened. */
     return 1;
 
     /* We only reach here if something bad happened. */
     return 1;
diff --git a/src/ttt-server.h b/src/ttt-server.h
new file mode 100644 (file)
index 0000000..773b034
--- /dev/null
@@ -0,0 +1,29 @@
+/* ttt-server.c - tic-tac-toe game server
+ *
+ * Copyright © 2005 Carl Worth
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2, or (at your option)
+ * any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ *
+ * Author: Carl Worth <cworth@cworth.org>
+ */
+
+#include "ttt.h"
+
+#ifndef _TTT_SERVER_H_
+#define _TTT_SERVER_H_
+
+typedef struct _ttt_server ttt_server_t;
+
+#endif /* _TTT_SERVER_H_ */
index 18577ffa378468088938985545f8d758507ee50a..f185d363780af7e5c43cb334f9e7ae42c52a258d 100644 (file)
@@ -146,11 +146,20 @@ ttt_socket_create_server (const char *host, const char *port)
 
 /* Exported: see ttt-socket.h for documentation. */
 void
 
 /* Exported: see ttt-socket.h for documentation. */
 void
-ttt_socket_accept (int listen_socket, ttt_socket_dispatch_func_t dispatch)
+ttt_socket_accept (int                          listen_socket,
+                  ttt_socket_accept_func_t      accept,
+                  void                         *closure)
 {
 {
+    pid_t pid;
     int connected_socket;
 
     connected_socket = _wait_for_connection (listen_socket);
 
     int connected_socket;
 
     connected_socket = _wait_for_connection (listen_socket);
 
-    (dispatch) (connected_socket);
+    pid = xfork();
+
+    if (pid == 0) {
+       /* Child process. */
+       (accept) (closure, connected_socket);
+       return;
+    }
 }
 }
index 59e89a514b6fd1d2d92af21cfe1e9f4dba2e2eb6..909501e0e37880c0f44d178d12e5d71b069b058d 100644 (file)
@@ -24,7 +24,8 @@
 
 #include "ttt.h"
 
 
 #include "ttt.h"
 
-typedef void (*ttt_socket_dispatch_func_t) (int connected_socket);
+typedef void (*ttt_socket_accept_func_t) (void *closure,
+                                         int    connected_socket);
 
 /* Create a socket, bind it to the given host and port, and listen in
  * preparation for incoming connections. See ttt_socket_accept for a
 
 /* Create a socket, bind it to the given host and port, and listen in
  * preparation for incoming connections. See ttt_socket_accept for a
@@ -45,10 +46,13 @@ ttt_socket_create_server (const char *host, const char *port);
 
 /* Wait for an incoming connection on listen_socket, (which should be
  * a valid socket on which bind and listen have already been
 
 /* Wait for an incoming connection on listen_socket, (which should be
  * a valid socket on which bind and listen have already been
- * called---see ttt_socket_create_server), then call the dispatch
- * function with the new socket from the connection.
+ * called---see ttt_socket_create_server), then call the accept
+ * function with the closure argument and the new socket from the
+ * connection.
  */
 void
  */
 void
-ttt_socket_accept (int listen_socket, ttt_socket_dispatch_func_t dispatch);
+ttt_socket_accept (int                          listen_socket,
+                  ttt_socket_accept_func_t      accept,
+                  void                         *closure);
 
 #endif
 
 #endif
diff --git a/src/ttt.c b/src/ttt.c
new file mode 100644 (file)
index 0000000..f211ae6
--- /dev/null
+++ b/src/ttt.c
@@ -0,0 +1,34 @@
+/* ttt.c - tic-tac-toe game client
+ *
+ * Copyright © 2005 Carl Worth
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2, or (at your option)
+ * any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ *
+ * Author: Carl Worth <cworth@cworth.org>
+ */
+
+#include "ttt.h"
+
+int 
+main (int argc, char **argv)
+{
+    ttt_args_t args;
+
+    ttt_args_parse (&args, argc, argv);
+
+    /* XXX: insert code here */
+
+    return 0;
+}
index 203c637b042a6a12a2158120eec18ffbf829cda9..316d74494c6e34ae1fde8d7684cbfc84f5d24cea 100644 (file)
--- a/src/ttt.h
+++ b/src/ttt.h
@@ -40,6 +40,7 @@
 #include <ctype.h>
 #include <sys/types.h>
 #include <sys/socket.h>
 #include <ctype.h>
 #include <sys/types.h>
 #include <sys/socket.h>
+#include <pthread.h>
 
 #define ASSERT_NOT_REACHED             \
 do {                                   \
 
 #define ASSERT_NOT_REACHED             \
 do {                                   \