]> git.tuebingen.mpg.de Git - paraslash.git/blob - stat.c
ac5fe8c84bb0b45dafaa752e9692986b89a7728f
[paraslash.git] / stat.c
1 /*
2  * Copyright (C) 2005-2006 Andre Noll <maan@systemlinux.org>
3  *
4  *     This program is free software; you can redistribute it and/or modify
5  *     it under the terms of the GNU General Public License as published by
6  *     the Free Software Foundation; either version 2 of the License, or
7  *     (at your option) any later version.
8  *
9  *     This program is distributed in the hope that it will be useful,
10  *     but WITHOUT ANY WARRANTY; without even the implied warranty of
11  *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  *     GNU General Public License for more details.
13  *
14  *     You should have received a copy of the GNU General Public License
15  *     along with this program; if not, write to the Free Software
16  *     Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
17  */
18
19 /**
20  *  \file stat.c functions used for sending/receiving the status of para_server
21  *  and para_audiod
22  */
23 #include "para.h"
24 #include "close_on_fork.h"
25 #include "list.h"
26 #include "error.h"
27 #include "string.h"
28
29 /** the maximal number of simultaneous connections */
30 #define MAX_STAT_CLIENTS 50
31
32 /**
33  * The structure associated with a connected client that sent the 'stat' command
34 *
35  *
36  * A status client is identified by its file descriptor.  para_audiod
37  * keeps a list of connected status clients.
38  */
39 struct stat_client {
40 /**
41  *
42  *
43  * the stat client's file descriptor
44  */
45 int fd;
46 /**
47  *
48  * its entry in the list of stat clients
49 */
50 struct list_head node;
51 };
52
53 static struct list_head client_list;
54 static int initialized;
55 static int num_clients;
56
57 /**
58  * the list of all status items sent by para_server/para_audiod
59  */
60 const char *status_item_list[NUM_STAT_ITEMS] = {
61         [SI_STATUS_BAR] = "status_bar",
62         [SI_STATUS] = "status",
63         [SI_NUM_PLAYED] = "num_played",
64
65         [SI_MTIME] = "mtime",
66         [SI_LENGTH_MIN] = "length_min",
67         [SI_LENGTH_SEC] = "length_sec",
68
69         [SI_FILE_SIZE] = "file_size",
70         [SI_STATUS_FLAGS] = "status_flags",
71         [SI_FORMAT] = "format",
72
73         [SI_SCORE] = "score",
74         [SI_AUDIO_INFO1] = "audio_file_info1",
75         [SI_AUDIO_INFO2] = "audio_file_info2",
76
77         [SI_AUDIO_INFO3] = "audio_file_info3",
78         [SI_DBINFO1] = "dbinfo1",
79         [SI_DBINFO2] = "dbinfo2",
80
81         [SI_DBINFO3] = "dbinfo3",
82         [SI_DECODER_FLAGS] = "decoder_flags",
83         [SI_AUDIOD_STATUS] = "audiod_status",
84
85         [SI_PLAY_TIME] = "play_time",
86         [SI_UPTIME] = "uptime",
87         [SI_OFFSET] = "offset",
88
89         [SI_LENGTH] = "length",
90         [SI_STREAM_START] = "stream_start",
91         [SI_CURRENT_TIME] = "current_time",
92
93         [SI_AUDIOD_UPTIME] = "audiod_uptime",
94         [SI_SELECTOR] = "dbtool"
95 };
96 #define FOR_EACH_STAT_ITEM(i) for (i = 0; i < NUM_STAT_ITEMS; i++)
97
98 static void dump_stat_client_list(void)
99 {
100         struct stat_client *sc;
101
102         if (!initialized)
103                 return;
104         list_for_each_entry(sc, &client_list, node)
105                 PARA_INFO_LOG("stat client on fd %d\n", sc->fd);
106 }
107 /**
108  * add a status client to the list
109  *
110  * \return Positive value on success, or -E_TOO_MANY_CLIENTS if
111  * the number of connected clients exceeds #MAX_STAT_CLIENTS
112  */
113 int stat_client_add(int fd)
114 {
115         struct stat_client *new_client;
116
117         if (num_clients >= MAX_STAT_CLIENTS) {
118                 PARA_ERROR_LOG("maximal number of stat clients (%d) exceeded\n",
119                         MAX_STAT_CLIENTS);
120                 return -E_TOO_MANY_CLIENTS;
121         }
122         if (!initialized) {
123                 INIT_LIST_HEAD(&client_list);
124                 initialized = 1;
125         }
126         PARA_INFO_LOG("adding client on fd %d\n", fd);
127         new_client = para_malloc(sizeof(struct stat_client));
128         new_client->fd = fd;
129         add_close_on_fork_list(fd);
130         list_add(&new_client->node, &client_list);
131         dump_stat_client_list();
132         num_clients++;
133         return 1;
134 }
135 /**
136  * write a message to all connected stat clients
137  *
138  * \param msg a \p NULL terminated buffer
139  */
140 void stat_client_write(char *msg)
141 {
142         struct stat_client *sc, *tmp;
143         char *buf;
144         ssize_t len;
145         struct timeval tv = {0 , 0};
146
147         if (!initialized)
148                 return;
149         buf = make_message("%s\n", msg);
150         len = strlen(buf);
151         list_for_each_entry_safe(sc, tmp, &client_list, node) {
152                 int fd = sc->fd, ret;
153                 fd_set wfds;
154                 FD_ZERO(&wfds);
155                 FD_SET(fd, &wfds);
156 //              PARA_DEBUG_LOG("%s: p=%lx\n", __func__, (long)p);
157                 do
158                         ret = select(fd + 1, NULL, &wfds, NULL, &tv);
159                 while (ret < 0 && errno == EINTR);
160                 if (ret) {
161                         ret = write(fd, buf, len);
162                         PARA_DEBUG_LOG("dumped %s to fd %d, ret = %d\n", msg, fd, ret);
163                         if (ret == len )
164                                 continue;
165                 }
166                 /* write error or fd not ready for writing */
167                 close(fd);
168                 del_close_on_fork_list(fd);
169                 num_clients--;
170                 PARA_INFO_LOG("deleting client on fd %d\n", fd);
171                 list_del(&sc->node);
172                 free(sc);
173                 dump_stat_client_list();
174         }
175         free(buf);
176         PARA_DEBUG_LOG("%d client(s)\n", num_clients);
177 }
178
179 /**
180  * send empty status list
181  *
182  * Send to  each connected client the full status item list
183  * with empty values.
184  */
185 void dump_empty_status(void)
186 {
187         int i;
188         char *empty_items = NULL;
189
190         if (!initialized)
191                 return;
192         FOR_EACH_STAT_ITEM(i) {
193                 char *tmp = make_message("%s%s:\n", empty_items? empty_items : "",
194                         status_item_list[i]);
195                 free(empty_items);
196                 empty_items = tmp;
197         }
198         stat_client_write(empty_items);
199         free(empty_items);
200 }
201
202 /**
203  * check if line starts with known status item.
204  *
205  * \param line buffer containing the line
206  *
207  * \return If the beginning of \a line matches any paraslash status item and is
208  * followed by a colon, the number of that status item is returned. Otherwise,
209  * this function returns \p -E_UNKNOWN_STAT_ITEM.
210  */
211 int stat_line_valid(const char *line)
212 {
213         int i;
214         size_t line_len;
215
216         if (!line || !*line)
217                 return -E_UNKNOWN_STAT_ITEM;
218         line_len = strlen(line);
219         for (i = 0; i < NUM_STAT_ITEMS; i++) {
220                 const char *s = status_item_list[i];
221                 size_t item_len = strlen(s);
222
223                 if (line_len > item_len && line[item_len] == ':' &&
224                                 !strncmp(line, s, item_len))
225                         return i;
226         }
227         return -E_UNKNOWN_STAT_ITEM;
228 }
229
230 /**
231  * call a custom function for each complete line
232  *
233  * \param buf the buffer containing data seperated by newlines
234  * \param n the number of bytes in \a buf
235  * \param line_handler the custom function
236  *
237  * If \a line_handler is \p NULL, return number of complete lines in buf.
238  * Otherwise, call \a line_handler for each complete line.  The rest of the
239  * buffer (last chunk containing incomplete line is moved to the beginning of
240  * the buffer.
241  *
242  * \return If line_handler is not NULL, this function returns the number
243  * of bytes not handled to \a line_handler.
244  */
245 unsigned for_each_line(char *buf, int n, void (*line_handler)(char *))
246 {
247         char *start = buf, *end;
248         int i, num_lines = 0;
249
250         while (start < buf + n) {
251                 char *next_null;
252                 char *next_cr;
253
254                 next_cr = memchr(start, '\n', buf + n - start);
255                 next_null = memchr(start, '\0', buf + n - start);
256                 if (!next_cr && !next_null)
257                         break;
258                 if (next_cr && next_null) {
259                         end = next_cr < next_null? next_cr : next_null;
260                 } else if (next_null) {
261                         end = next_null;
262                 } else
263                         end = next_cr;
264                 num_lines++;
265                 if (line_handler) {
266                         *end = '\0';
267                         line_handler(start);
268                         start = ++end;
269                 } else
270                         start = ++end;
271         }
272         if (!line_handler)
273                 return num_lines;
274         i = buf + n - start;
275         if (i && i != n)
276                 memmove(buf, start, i);
277         return i;
278 }
279