From: Richard Worth Date: Thu, 24 Nov 2005 16:42:21 +0000 (+0000) Subject: * AUTHORS: Add Richard D. Worth X-Git-Url: https://git.cworth.org/git?p=ttt;a=commitdiff_plain;h=980a478f042f8cf48f4484bb117fab8dafd8b450 * AUTHORS: Add Richard D. Worth * TODO: Add newline before EOF * src/ttt-client.c: (_ttt_client_execute_who): * src/ttt-server.h: * src/ttt-server.c: (ttt_server_who): Implement WHO. --- diff --git a/AUTHORS b/AUTHORS index eb9c6f1..5be78c9 100644 --- a/AUTHORS +++ b/AUTHORS @@ -1,2 +1,3 @@ Carl Worth +Richard D. Worth diff --git a/ChangeLog b/ChangeLog index dc9d723..a545b64 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,12 @@ +2005-11-24 Richard D. Worth + + * AUTHORS: Add Richard D. Worth + * TODO: Add newline before EOF + + * src/ttt-client.c: (_ttt_client_execute_who): + * src/ttt-server.h: + * src/ttt-server.c: (ttt_server_who): Implement WHO. + 2005-11-24 Carl Worth * PROTOCOL: Document QUIT. @@ -26,9 +35,9 @@ * TODO: Note that HELO and ERROR INVALIDNAME are implemented in the server. -2005-11-23 Richard Worth +2005-11-23 Richard D. Worth - * PROTOCOL: Removed unused servername + * PROTOCOL: Remove unused servername * src/ttt-client.h: * src/ttt-client.c: (_ttt_client_execute_helo), (_ttt_client_init), diff --git a/TODO b/TODO index 98b90f5..79d35fb 100644 --- a/TODO +++ b/TODO @@ -49,4 +49,4 @@ S C 3.6.1.2. ERROR NOTPLAYING 3.6.2. Moving errors 3.6.2.1. ERROR NOTYOURTURN - \ No newline at end of file + diff --git a/src/ttt-client.c b/src/ttt-client.c index 1c23fcc..9c9b819 100644 --- a/src/ttt-client.c +++ b/src/ttt-client.c @@ -51,6 +51,11 @@ _ttt_client_execute_helo (ttt_client_t *client, char **args, int num_args); +static ttt_error_t +_ttt_client_execute_who (ttt_client_t *client, + char **args, + int num_args); + static ttt_error_t _ttt_client_execute_quit (ttt_client_t *client, char **args, @@ -64,6 +69,7 @@ typedef struct _ttt_command_description { ttt_command_description_t command_descriptions[] = { {"HELO", 1, _ttt_client_execute_helo}, + {"WHO", 0, _ttt_client_execute_who}, {"QUIT", 0, _ttt_client_execute_quit} }; #define ARRAY_SIZE(arr) (sizeof(arr)/sizeof(arr[0])) @@ -97,6 +103,24 @@ _ttt_client_execute_helo (ttt_client_t *client, return TTT_ERROR_NONE; } +static ttt_error_t +_ttt_client_execute_who (ttt_client_t *client, + char **args, + int num_args) +{ + const char *response; + + assert (num_args == 0); + + response = ttt_server_who (client->server); + + ttt_client_send (client, response); + + free (response); + + return TTT_ERROR_NONE; +} + static ttt_error_t _ttt_client_execute_quit (ttt_client_t *client, char **args, diff --git a/src/ttt-server.c b/src/ttt-server.c index 450582b..60276d3 100644 --- a/src/ttt-server.c +++ b/src/ttt-server.c @@ -120,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) { @@ -133,6 +134,27 @@ 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_name(server->clients[i])); + + xasprintf (&response, "%s\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) diff --git a/src/ttt-server.h b/src/ttt-server.h index 7cf3d97..fef7030 100644 --- a/src/ttt-server.h +++ b/src/ttt-server.h @@ -59,6 +59,20 @@ ttt_server_unregister_client (ttt_server_t *server, ttt_client_t *client); void ttt_server_broadcast (ttt_server_t *server, const char *message); + +/* Returns the WHO response. The return string is allocated in this + * function and will need to be free'd by the caller. + * + * Locking: The server mutex will be acquired and held throughout the + * execution of this function. Each client mutex may also be acquired + * and held by functions called during the execution of this function. + * + * Errors: If an error such as an IO error occurs, this function will + * not return. + */ +const char* +ttt_server_who (ttt_server_t *server); + /* Gets the server hostname. * */