Files
lcdproc/server/clients.c
T

129 lines
2.3 KiB
C
Raw Normal View History

1999-12-09 23:15:14 +00:00
/*
* clients.c
* This file is part of LCDd, the lcdproc server.
*
* This file is released under the GNU General Public License. Refer to the
* COPYING file distributed with this package.
*
* Copyright (c) 1999, William Ferrell, Scott Scriven
* 2002, Joris Robijn
*
*
* Inits/shuts down client system,
* and searches for clients in the list.
* On short: manages the list of clients that are connected.
*
1999-12-09 23:15:14 +00:00
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include "client.h"
1999-12-09 23:15:14 +00:00
#include "clients.h"
2001-11-29 14:09:13 +00:00
#include "shared/report.h"
#include "render.h"
1999-12-09 23:15:14 +00:00
LinkedList *clientlist;
1999-12-09 23:15:14 +00:00
2001-12-30 00:15:25 +00:00
/* Initialize and kill client list...*/
int
clients_init ()
1999-12-09 23:15:14 +00:00
{
2001-11-29 14:09:13 +00:00
debug(RPT_INFO, "client_init()");
clientlist = LL_new ();
if (!clientlist) {
2001-11-29 14:09:13 +00:00
report( RPT_ERR, "client_init: Unable to create client list");
2000-04-03 22:13:57 +00:00
return -1;
}
1999-12-09 23:15:14 +00:00
2000-04-03 22:13:57 +00:00
return 0;
1999-12-09 23:15:14 +00:00
}
int
clients_shutdown ()
1999-12-09 23:15:14 +00:00
{
Client *c;
1999-12-09 23:15:14 +00:00
debug (RPT_INFO, "clients_shutdown()");
/* Free all client structures... */
for (c=LL_GetFirst (clientlist); c; c=LL_GetNext (clientlist) ) {
debug (RPT_DEBUG, "clients_shutdown: ...");
2000-04-03 22:13:57 +00:00
if (c) {
debug (RPT_DEBUG, "clients_shutdown: ... %i ...", c->sock);
if (client_destroy (c) != 0) {
report (RPT_ERR, "clients_shutdown: Error freeing client");
2000-04-03 22:13:57 +00:00
} else {
debug (RPT_DEBUG, "clients_shutdown: Freed client...");
2000-04-03 22:13:57 +00:00
}
} else {
debug (RPT_DEBUG, "clients_shutdown: No client!");
2000-04-03 22:13:57 +00:00
}
}
1999-12-09 23:15:14 +00:00
2001-12-30 00:15:25 +00:00
/* Then, free the list...*/
LL_Destroy (clientlist);
debug (RPT_DEBUG, "clients_shutdown: done");
1999-12-09 23:15:14 +00:00
2000-04-03 22:13:57 +00:00
return 0;
1999-12-09 23:15:14 +00:00
}
int
clients_add_client (Client *c)
{
/* Add the client to the clients list... */
return LL_Push (clientlist, c);
}
int
clients_remove_client (Client *c)
{
/* Remove the client from the clients list... */
return (LL_Remove (clientlist, c) == NULL)?-1:0;
}
Client *
clients_getfirst ()
{
return (Client *) LL_GetFirst(clientlist);
}
Client *
clients_getnext ()
{
return (Client *) LL_GetNext(clientlist);
}
int
clients_client_count ()
{
return LL_Length(clientlist);
}
2001-12-30 00:15:25 +00:00
/* A client is identified by the file descriptor
* associated with it. Find one.
2001-12-30 00:15:25 +00:00
*/
Client *
clients_find_client_by_sock (int sock)
1999-12-09 23:15:14 +00:00
{
Client *c;
1999-12-09 23:15:14 +00:00
debug(RPT_INFO, "clients_find_client_by_sock(%i)", sock);
1999-12-09 23:15:14 +00:00
for( c=LL_GetFirst(clientlist); c; c=LL_GetNext(clientlist) ) {
2000-04-03 22:13:57 +00:00
if (c->sock == sock) {
return c;
}
}
2001-11-29 14:09:13 +00:00
debug (RPT_ERR, "client_find_sock: failed");
1999-12-09 23:15:14 +00:00
2000-04-03 22:13:57 +00:00
return NULL;
1999-12-09 23:15:14 +00:00
}