Base: LCDproc 0.5.2
[lcdproc-de200c.git] / server / clients.c
blobc6d9e4d983ae19abda0574e0f69eda272879fd77
1 /*
2 * clients.c
3 * This file is part of LCDd, the lcdproc server.
5 * This file is released under the GNU General Public License. Refer to the
6 * COPYING file distributed with this package.
8 * Copyright (c) 1999, William Ferrell, Scott Scriven
9 * 2002, Joris Robijn
12 * Inits/shuts down client system,
13 * and searches for clients in the list.
14 * On short: manages the list of clients that are connected.
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <unistd.h>
21 #include <string.h>
23 #include "client.h"
24 #include "clients.h"
25 #include "shared/report.h"
26 #include "render.h"
28 LinkedList *clientlist = NULL;
30 /* Initialize and kill client list...*/
31 int
32 clients_init(void)
34 debug(RPT_DEBUG, "%s()", __FUNCTION__);
36 clientlist = LL_new();
37 if (!clientlist) {
38 report(RPT_ERR, "%s: Unable to create client list", __FUNCTION__);
39 return -1;
42 return 0;
45 int
46 clients_shutdown(void)
48 Client *c;
50 debug(RPT_DEBUG, "%s()", __FUNCTION__);
52 if (!clientlist) {
53 /* Program shutdown before completed startup */
54 return -1;
57 /* Free all client structures... */
58 for (c = LL_GetFirst(clientlist); c; c = LL_GetNext(clientlist)) {
59 debug(RPT_DEBUG, "%s: ...", __FUNCTION__);
60 if (c) {
61 debug(RPT_DEBUG, "%s: ... %i ...", __FUNCTION__, c->sock);
62 if (client_destroy(c) != 0) {
63 report(RPT_ERR, "%s: Error freeing client", __FUNCTION__);
64 } else {
65 debug(RPT_DEBUG, "%s: Freed client...", __FUNCTION__);
67 } else {
68 debug(RPT_DEBUG, "%s: No client!", __FUNCTION__);
72 /* Then, free the list...*/
73 LL_Destroy(clientlist);
75 debug(RPT_DEBUG, "%s: done", __FUNCTION__);
77 return 0;
80 int
81 clients_add_client(Client *c)
83 /* Add the client to the clients list... */
84 return LL_Push(clientlist, c);
87 int
88 clients_remove_client(Client *c)
90 /* Remove the client from the clients list... */
91 return(LL_Remove(clientlist, c) == NULL)?-1:0;
94 Client *
95 clients_getfirst(void)
97 return (Client *) LL_GetFirst(clientlist);
100 Client *
101 clients_getnext(void)
103 return (Client *) LL_GetNext(clientlist);
107 clients_client_count(void)
109 return LL_Length(clientlist);
113 /* A client is identified by the file descriptor
114 * associated with it. Find one.
117 Client *
118 clients_find_client_by_sock(int sock)
120 Client *c;
122 debug(RPT_DEBUG, "%s(sock=%i)", __FUNCTION__, sock);
124 for (c = LL_GetFirst(clientlist); c; c = LL_GetNext(clientlist)) {
125 if (c->sock == sock) {
126 return c;
130 debug(RPT_ERR, "%s: failed", __FUNCTION__);
132 return NULL;