X-Git-Url: https://git.cworth.org/git?p=ttt;a=blobdiff_plain;f=src%2Fttt-server.c;h=c94d3d23a44c5fc3d624b6d87bd30ddd7c9fa0b4;hp=e9043b4e2cb25309e65e68ee202af9a617e2a90f;hb=c25df0131987fe52be7430c5be03ec83fbaea9b3;hpb=84db9bf52aa25c19f7d547ea2dbec69ca4452300 diff --git a/src/ttt-server.c b/src/ttt-server.c index e9043b4..c94d3d2 100644 --- a/src/ttt-server.c +++ b/src/ttt-server.c @@ -32,8 +32,6 @@ struct _ttt_server { const char *host; const char *port; - int next_client_id; - ttt_client_t **clients; int clients_size; int num_clients; @@ -47,8 +45,6 @@ ttt_server_init (ttt_server_t *server, const char *host, const char *port) server->host = host; server->port = port; - server->next_client_id = 0; - server->clients = NULL; server->clients_size = 0; server->num_clients = 0; @@ -60,28 +56,25 @@ ttt_server_register_client (ttt_server_t *server, ttt_client_t *client) { int i; ttt_error_t error = TTT_ERROR_NONE; - char *name; + const char *username; pthread_mutex_lock (&server->mutex); - name = xstrdup (ttt_client_get_name (client)); + username = ttt_client_get_username (client); - if (name == NULL) { - xasprintf(&name, "user%03d", server->next_client_id++); - ttt_client_set_name (client, name); - } + assert (username != NULL); + + if (username[0] == '\0') + return TTT_ERROR_INVALID_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; + if (strcmp (ttt_client_get_username (server->clients[i]), username) == 0) { + error = TTT_ERROR_INVALID_NAME; goto CLEANUP_LOCK; } } - printf ("Client %s has joined.\n", name); - free (name); + printf ("Client %s has joined.\r\n", username); server->num_clients++; @@ -117,7 +110,7 @@ ttt_server_unregister_client (ttt_server_t *server, ttt_client_t *client) assert (i < server->num_clients); - printf ("Client %s has left.\n", ttt_client_get_name (client)); + printf ("Client %s has left.\r\n", ttt_client_get_username (client)); memmove (&server->clients[i], &server->clients[i+1], (server->num_clients - i - 1) * sizeof (ttt_client_t *)); @@ -127,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) { @@ -140,6 +134,84 @@ 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", + response, + ttt_client_get_username (server->clients[i])); + + xasprintf (&response, "%s\r\n", response); + + pthread_mutex_unlock (&server->mutex); + + return response; +} + +/* Exported: See ttt-server.h for documentation. */ +ttt_error_t +ttt_server_statistics (ttt_server_t *server, const char *username, char **response) +{ + ttt_bool_t usernamefound = FALSE; + char *client_username; + int client_num_wins; + int i; + + pthread_mutex_lock (&server->mutex); + + for (i = 0; i < server->num_clients; i++) { + client_username = ttt_client_get_username (server->clients[i]); + if (strcasecmp (username, client_username) == 0) { + usernamefound = TRUE; + client_num_wins = ttt_client_get_num_wins (server->clients[i]); + xasprintf (response, "STATISTICS %s \"\r\n" + "TICTACTOE WINS %d\r\n\"\r\n", + client_username, + client_num_wins); + } + } + + pthread_mutex_unlock (&server->mutex); + + if (!usernamefound) + return TTT_ERROR_NO_USER; + + return TTT_ERROR_NONE; +} + +/* Exported: See ttt-server.h for documentation. */ +ttt_error_t +ttt_server_verify_username (ttt_server_t *server, const char *username) +{ + ttt_bool_t usernamefound = FALSE; + char *client_username; + int i; + + pthread_mutex_lock (&server->mutex); + + for (i = 0; i < server->num_clients; i++) { + client_username = ttt_client_get_username (server->clients[i]); + if (strcasecmp (username, client_username) == 0) + usernamefound = TRUE; + } + + pthread_mutex_unlock (&server->mutex); + + if (!usernamefound) + return TTT_ERROR_NO_USER; + + return TTT_ERROR_NONE; +} + /* Exported: See ttt-server.h for documentation. */ const char* ttt_server_get_host (ttt_server_t *server) @@ -155,26 +227,18 @@ ttt_server_get_port (ttt_server_t *server) } 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)