Files
lcdproc/server/clients.c
T

139 lines
2.7 KiB
C
Raw Normal View History

2008-11-30 22:31:20 +00:00
/** \file server/clients.c
* This file contains code allowing LCDd to handle client connections and
* data structures. It contains functions to initialize the internal list
* of clients, terminate client connections, add new clients to the list,
* and locating a client's socket.
2008-11-30 22:31:20 +00:00
*/
/* This file is part of LCDd, the lcdproc server.
*
2008-11-30 22:31:20 +00:00
* This file is released under the GNU General Public License.
* Refer to the COPYING file distributed with this package.
*
2011-01-05 23:34:37 +00:00
* Copyright (c) 1999, William Ferrell, Selene Scriven
* 2002, Joris Robijn
1999-12-09 23:15:14 +00:00
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
2012-02-22 22:25:32 +00:00
#include "shared/report.h"
#include "shared/LL.h"
#include "client.h"
1999-12-09 23:15:14 +00:00
#include "clients.h"
#include "render.h"
1999-12-09 23:15:14 +00:00
LinkedList *clientlist = NULL;
1999-12-09 23:15:14 +00:00
2001-12-30 00:15:25 +00:00
/* Initialize and kill client list...*/
int
2007-04-02 21:29:53 +00:00
clients_init(void)
1999-12-09 23:15:14 +00:00
{
debug(RPT_DEBUG, "%s()", __FUNCTION__);
2007-04-02 21:29:53 +00:00
clientlist = LL_new();
if (!clientlist) {
2007-04-02 21:29:53 +00:00
report(RPT_ERR, "%s: Unable to create client list", __FUNCTION__);
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
2007-04-02 21:29:53 +00:00
clients_shutdown(void)
1999-12-09 23:15:14 +00:00
{
Client *c;
1999-12-09 23:15:14 +00:00
2007-04-02 21:29:53 +00:00
debug(RPT_DEBUG, "%s()", __FUNCTION__);
2007-04-02 21:29:53 +00:00
if (!clientlist) {
/* Program shutdown before completed startup */
return -1;
}
/* Free all client structures... */
2007-04-02 21:29:53 +00:00
for (c = LL_GetFirst(clientlist); c; c = LL_GetNext(clientlist)) {
debug(RPT_DEBUG, "%s: ...", __FUNCTION__);
2000-04-03 22:13:57 +00:00
if (c) {
2007-04-02 21:29:53 +00:00
debug(RPT_DEBUG, "%s: ... %i ...", __FUNCTION__, c->sock);
if (client_destroy(c) != 0) {
report(RPT_ERR, "%s: Error freeing client", __FUNCTION__);
2000-04-03 22:13:57 +00:00
} else {
2007-04-02 21:29:53 +00:00
debug(RPT_DEBUG, "%s: Freed client...", __FUNCTION__);
2000-04-03 22:13:57 +00:00
}
} else {
2007-04-02 21:29:53 +00:00
debug(RPT_DEBUG, "%s: No client!", __FUNCTION__);
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...*/
2007-04-02 21:29:53 +00:00
LL_Destroy(clientlist);
2007-04-02 21:29:53 +00:00
debug(RPT_DEBUG, "%s: done", __FUNCTION__);
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
}
/* Add the client to the clients list... */
Client *
2007-04-02 21:29:53 +00:00
clients_add_client(Client *c)
{
if (LL_Push(clientlist, c) == 0)
return c;
return NULL;
}
/* Remove the client from the clients list... */
Client *
clients_remove_client(Client *c, Direction whereto)
{
Client *client = LL_Remove(clientlist, c, whereto);
2012-02-22 22:25:32 +00:00
return client;
}
Client *
2007-04-02 21:29:53 +00:00
clients_getfirst(void)
{
return (Client *) LL_GetFirst(clientlist);
}
Client *
2007-04-02 21:29:53 +00:00
clients_getnext(void)
{
return (Client *) LL_GetNext(clientlist);
}
int
2007-04-02 21:29:53 +00:00
clients_client_count(void)
{
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 *
2007-04-02 21:29:53 +00:00
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
2007-04-02 21:29:53 +00:00
debug(RPT_DEBUG, "%s(sock=%i)", __FUNCTION__, sock);
1999-12-09 23:15:14 +00:00
2007-04-02 21:29:53 +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;
}
}
2007-04-02 21:29:53 +00:00
debug(RPT_ERR, "%s: failed", __FUNCTION__);
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
}