X-Git-Url: https://git.cworth.org/git?p=ttt;a=blobdiff_plain;f=src%2Fttt-server.c;h=23fd138cb375d24f611eb85b45bc7d0460267f1e;hp=c50a797b259ec8bf1dbb6b74886bba2468ca4131;hb=f2187ebd49f78b84bd2dca6172abc81e54dda199;hpb=907321c065fb1383e800f0794981df91d4327f57 diff --git a/src/ttt-server.c b/src/ttt-server.c index c50a797..23fd138 100644 --- a/src/ttt-server.c +++ b/src/ttt-server.c @@ -23,12 +23,14 @@ #include "ttt-args.h" #include "ttt-client.h" +#include "ttt-error.h" #include "ttt-socket.h" struct _ttt_server { pthread_mutex_t mutex; - int next_client_id; + const char *host; + const char *port; ttt_client_t **clients; int clients_size; @@ -36,11 +38,12 @@ struct _ttt_server { }; static void -ttt_server_init (ttt_server_t *server) +ttt_server_init (ttt_server_t *server, const char *host, const char *port) { pthread_mutex_init (&server->mutex, NULL); - server->next_client_id = 0; + server->host = host; + server->port = port; server->clients = NULL; server->clients_size = 0; @@ -48,16 +51,30 @@ ttt_server_init (ttt_server_t *server) } /* Exported: See ttt-server.h for documentation. */ -int +ttt_error_t ttt_server_register_client (ttt_server_t *server, ttt_client_t *client) { - int id; + int i; + ttt_error_t error = TTT_ERROR_NONE; + const char *name; pthread_mutex_lock (&server->mutex); - id = server->next_client_id++; + name = ttt_client_get_name (client); + + assert (name != NULL); - printf ("Client %d has joined.\n", id); + if (name[0] == '\0') + return TTT_ERROR_INVALID_NAME; + + for (i = 0; i < server->num_clients; i++) { + if (strcmp (ttt_client_get_name (server->clients[i]), name) == 0) { + error = TTT_ERROR_INVALID_NAME; + goto CLEANUP_LOCK; + } + } + + printf ("Client %s has joined.\r\n", name); server->num_clients++; @@ -73,9 +90,10 @@ ttt_server_register_client (ttt_server_t *server, ttt_client_t *client) server->clients [server->num_clients - 1] = client; + CLEANUP_LOCK: pthread_mutex_unlock (&server->mutex); - return id; + return error; } /* Exported: See ttt-server.h for documentation. */ @@ -92,7 +110,7 @@ ttt_server_unregister_client (ttt_server_t *server, ttt_client_t *client) assert (i < server->num_clients); - printf ("Client %d has left.\n", ttt_client_get_id (client)); + printf ("Client %s has left.\r\n", ttt_client_get_name (client)); memmove (&server->clients[i], &server->clients[i+1], (server->num_clients - i - 1) * sizeof (ttt_client_t *)); @@ -102,6 +120,7 @@ ttt_server_unregister_client (ttt_server_t *server, ttt_client_t *client) pthread_mutex_unlock (&server->mutex); } +/* Exported: See ttt-server.h for documentation. */ void ttt_server_broadcast (ttt_server_t *server, const char *message) { @@ -115,27 +134,57 @@ ttt_server_broadcast (ttt_server_t *server, const char *message) pthread_mutex_unlock (&server->mutex); } +/* Exported: See ttt-server.h for documentation. */ +const char* +ttt_server_who (ttt_server_t *server) +{ + int i; + char *response; + + pthread_mutex_lock (&server->mutex); + + xasprintf (&response, "WHO"); + + for (i = 0; i < server->num_clients; i++) + xasprintf (&response, "%s %s %d", + response, + ttt_client_get_name (server->clients[i]), + ttt_client_get_num_wins (server->clients[i])); + + xasprintf (&response, "%s\r\n", response); + + pthread_mutex_unlock (&server->mutex); + + return response; +} + +/* 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" -", then \"close\" (and ) 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"; +"Welcome to ttt-server. The server is currently listening on:\r\n" +"\r\n" +" %s:%s\r\n" +"\r\n" +"To test this, simply connect one or more clients to that host and port.\r\n" +"For example:\r\n" +"\r\n" +" telnet %s %s\r\n" +"\r\n" +"The TTTP (tic-tac-toe protocol) has been partially implemented.\r\n" +"The following commands should work: HELO, HELP, MESSAGE, VERSION, QUIT, WHO.\r\n" +"\r\n"; static void _ttt_server_accept (void *closure, int client_socket) @@ -161,7 +210,7 @@ main (int argc, char **argv) printf (WELCOME_MESSAGE, args.host, args.port, args.host, args.port); - ttt_server_init (&server); + ttt_server_init (&server, args.host, args.port); while (1) ttt_socket_accept (socket, _ttt_server_accept, &server);