[0] | 1 | //clients.c: |
---|
| 2 | |
---|
| 3 | #include "roard.h" |
---|
| 4 | |
---|
| 5 | int clients_init (void) { |
---|
| 6 | int i; |
---|
| 7 | |
---|
| 8 | for (i = 0; i < ROAR_CLIENTS_MAX; i++) |
---|
| 9 | g_clients[i] = NULL; |
---|
| 10 | |
---|
| 11 | return 0; |
---|
| 12 | } |
---|
| 13 | |
---|
| 14 | int clients_free (void) { |
---|
| 15 | int i; |
---|
| 16 | |
---|
| 17 | for (i = 0; i < ROAR_CLIENTS_MAX; i++) |
---|
| 18 | if ( g_clients[i] ) |
---|
| 19 | clients_delete(i); |
---|
| 20 | |
---|
| 21 | return 0; |
---|
| 22 | } |
---|
| 23 | |
---|
| 24 | int clients_new (void) { |
---|
| 25 | int i; |
---|
| 26 | int s; |
---|
| 27 | struct roar_client * n; |
---|
| 28 | |
---|
| 29 | for (i = 0; i < ROAR_CLIENTS_MAX; i++) { |
---|
| 30 | if ( g_clients[i] == NULL ) { |
---|
| 31 | n = malloc(sizeof(struct roar_client)); |
---|
| 32 | if ( n != NULL ) { |
---|
| 33 | n->pid = -1; |
---|
| 34 | n->fh = -1; |
---|
| 35 | |
---|
| 36 | *n->name = 0; |
---|
| 37 | *n->host = 0; |
---|
| 38 | |
---|
| 39 | n->execed = -1; |
---|
| 40 | for (s = 0; s < ROAR_CLIENTS_MAX_STREAMS_PER_CLIENT; s++) |
---|
| 41 | n->streams[s] = -1; |
---|
| 42 | |
---|
| 43 | g_clients[i] = n; |
---|
| 44 | |
---|
| 45 | ROAR_DBG("clients_new(void) = %i", i); |
---|
| 46 | return i; |
---|
| 47 | } else { |
---|
| 48 | ROAR_ERR("clients_new(void): Can not alloc memory for new client: %s", strerror(errno)); |
---|
| 49 | ROAR_ERR("clients_new(void) = -1"); |
---|
| 50 | return -1; |
---|
| 51 | } |
---|
| 52 | } |
---|
| 53 | } |
---|
| 54 | |
---|
| 55 | return -1; |
---|
| 56 | } |
---|
| 57 | |
---|
| 58 | int clients_delete (int id) { |
---|
| 59 | int i; |
---|
| 60 | |
---|
| 61 | if ( g_clients[id] == NULL ) |
---|
| 62 | return -1; |
---|
| 63 | |
---|
| 64 | if (g_clients[id]->execed != -1) { |
---|
| 65 | // return streams_delete(g_clients[id]->execed); |
---|
| 66 | g_clients[id]->execed = -1; |
---|
| 67 | } |
---|
| 68 | |
---|
| 69 | for (i = 0; i < ROAR_CLIENTS_MAX_STREAMS_PER_CLIENT; i++) { |
---|
| 70 | streams_delete(g_clients[id]->streams[i]); |
---|
| 71 | } |
---|
| 72 | |
---|
| 73 | if ( g_clients[id]->fh != -1 ) |
---|
| 74 | close(g_clients[id]->fh); |
---|
| 75 | |
---|
| 76 | free(g_clients[id]); |
---|
| 77 | g_clients[id] = NULL; |
---|
| 78 | |
---|
| 79 | ROAR_DBG("clients_delete(id=%i) = 0", id); |
---|
| 80 | return 0; |
---|
| 81 | } |
---|
| 82 | |
---|
| 83 | int clients_get (int id, struct roar_client ** client) { |
---|
| 84 | *client = g_clients[id]; |
---|
| 85 | |
---|
| 86 | if ( *client == NULL ) |
---|
| 87 | return -1; |
---|
| 88 | |
---|
| 89 | return 0; |
---|
| 90 | } |
---|
| 91 | |
---|
| 92 | int clients_set_fh (int id, int fh) { |
---|
| 93 | if ( g_clients[id] == NULL ) |
---|
| 94 | return -1; |
---|
| 95 | |
---|
| 96 | g_clients[id]->fh = fh; |
---|
| 97 | |
---|
| 98 | return 0; |
---|
| 99 | } |
---|
| 100 | |
---|
| 101 | int clients_set_pid (int id, int pid) { |
---|
| 102 | if ( g_clients[id] == NULL ) |
---|
| 103 | return -1; |
---|
| 104 | |
---|
| 105 | g_clients[id]->pid = pid; |
---|
| 106 | |
---|
| 107 | return 0; |
---|
| 108 | } |
---|
| 109 | |
---|
| 110 | int clients_check_all (void) { |
---|
| 111 | struct timeval tv; |
---|
| 112 | fd_set r, e; |
---|
[66] | 113 | int i, j; |
---|
[0] | 114 | int ret; |
---|
| 115 | int fh; |
---|
| 116 | int max_fh = -1; |
---|
[71] | 117 | int have = 0; |
---|
[0] | 118 | |
---|
| 119 | FD_ZERO(&r); |
---|
| 120 | FD_ZERO(&e); |
---|
| 121 | |
---|
| 122 | tv.tv_sec = 0; |
---|
| 123 | tv.tv_usec = 1; |
---|
| 124 | |
---|
| 125 | for (i = 0; i < ROAR_CLIENTS_MAX; i++) { |
---|
| 126 | if ( g_clients[i] == NULL ) |
---|
| 127 | continue; |
---|
| 128 | |
---|
[66] | 129 | if ( (fh = g_clients[i]->fh) != -1 ) { |
---|
[71] | 130 | have++; |
---|
| 131 | |
---|
[66] | 132 | FD_SET(fh, &r); |
---|
| 133 | FD_SET(fh, &e); |
---|
| 134 | |
---|
| 135 | if ( fh > max_fh ) |
---|
| 136 | max_fh = fh; |
---|
| 137 | } else { |
---|
[0] | 138 | |
---|
[66] | 139 | for (j = 0; j < ROAR_CLIENTS_MAX_STREAMS_PER_CLIENT; j++) { |
---|
| 140 | if ( (fh = streams_get_fh(g_clients[i]->streams[j])) != -1 ) { |
---|
| 141 | FD_SET(fh, &r); |
---|
[0] | 142 | |
---|
[66] | 143 | if ( fh > max_fh ) |
---|
| 144 | max_fh = fh; |
---|
| 145 | } |
---|
| 146 | } |
---|
| 147 | } |
---|
| 148 | |
---|
[0] | 149 | } |
---|
| 150 | |
---|
| 151 | if ( (ret = select(max_fh + 1, &r, NULL, &e, &tv)) < 1 ) { |
---|
[71] | 152 | return ret < 0 ? ret : have; |
---|
[0] | 153 | } |
---|
| 154 | |
---|
| 155 | for (i = 0; i < ROAR_CLIENTS_MAX; i++) { |
---|
| 156 | if ( g_clients[i] == NULL ) |
---|
| 157 | continue; |
---|
| 158 | |
---|
[66] | 159 | if ( (fh = g_clients[i]->fh) != -1 ) { |
---|
| 160 | if ( FD_ISSET(fh, &r) ) { |
---|
| 161 | if ( g_clients[i]->execed == -1 ) { |
---|
| 162 | clients_check(i); |
---|
| 163 | } else { |
---|
| 164 | streams_check(g_clients[i]->execed); |
---|
| 165 | } |
---|
| 166 | } |
---|
[0] | 167 | |
---|
[66] | 168 | if ( FD_ISSET(fh, &e) ) |
---|
| 169 | clients_delete(i); |
---|
| 170 | } else { |
---|
| 171 | for (j = 0; j < ROAR_CLIENTS_MAX_STREAMS_PER_CLIENT; j++) { |
---|
| 172 | if ( (fh = streams_get_fh(g_clients[i]->streams[j])) != -1 ) { |
---|
| 173 | if ( FD_ISSET(fh, &r) ) { |
---|
| 174 | streams_check(g_clients[i]->streams[j]); |
---|
| 175 | } |
---|
| 176 | } |
---|
[0] | 177 | } |
---|
| 178 | } |
---|
| 179 | } |
---|
| 180 | |
---|
[71] | 181 | ROAR_DBG("clients_check_all(void) = %i // have value", have); |
---|
| 182 | return have; |
---|
[0] | 183 | } |
---|
| 184 | |
---|
| 185 | int clients_check (int id) { |
---|
| 186 | struct roar_message m; |
---|
| 187 | struct roar_connection con; |
---|
| 188 | char * data = NULL; |
---|
| 189 | int oldcmd; |
---|
| 190 | int r; |
---|
| 191 | |
---|
| 192 | if ( g_clients[id] == NULL ) |
---|
| 193 | return -1; |
---|
| 194 | if ( g_clients[id]->fh == -1 ) |
---|
| 195 | return -1; |
---|
| 196 | |
---|
| 197 | con.fh = g_clients[id]->fh; |
---|
| 198 | |
---|
| 199 | r = roar_recv_message(&con, &m, &data); |
---|
| 200 | |
---|
| 201 | if ( r == -1 ) { // should we drop the client? |
---|
| 202 | clients_delete(id); |
---|
| 203 | return -1; |
---|
| 204 | } |
---|
| 205 | |
---|
| 206 | roar_debug_message_print(&m); |
---|
| 207 | |
---|
| 208 | oldcmd = m.cmd; |
---|
| 209 | |
---|
| 210 | if ( (r = command_exec(id, &m, data)) == -1 ) { |
---|
| 211 | m.cmd = ROAR_CMD_ERROR; |
---|
| 212 | m.datalen = 0; |
---|
| 213 | ROAR_DBG("clients_check(*): Exec of command faild!"); |
---|
| 214 | } else { |
---|
| 215 | if ( m.cmd == oldcmd ) { |
---|
| 216 | m.cmd = ROAR_CMD_OK; |
---|
| 217 | m.datalen = 0; |
---|
| 218 | } |
---|
| 219 | } |
---|
| 220 | |
---|
| 221 | roar_send_message(&con, &m, NULL); |
---|
| 222 | |
---|
| 223 | if ( data ) |
---|
| 224 | free(data); |
---|
| 225 | |
---|
| 226 | ROAR_DBG("clients_check(id=%i) = 0", id); |
---|
| 227 | return 0; |
---|
| 228 | } |
---|
| 229 | |
---|
| 230 | int clients_send_mon (struct roar_audio_info * sa, uint32_t pos) { |
---|
| 231 | int i; |
---|
| 232 | int fh; |
---|
| 233 | |
---|
| 234 | for (i = 0; i < ROAR_CLIENTS_MAX; i++) { |
---|
| 235 | if ( g_clients[i] == NULL ) |
---|
| 236 | continue; |
---|
| 237 | |
---|
| 238 | if ( (fh = g_clients[i]->fh) == -1 ) |
---|
| 239 | continue; |
---|
| 240 | |
---|
| 241 | if ( g_clients[i]->execed == -1 ) { |
---|
| 242 | // TODO: add some code to send a message to the client insetd of the raw data. |
---|
| 243 | } else { |
---|
| 244 | // streams_check(g_clients[i]->execed); |
---|
| 245 | streams_send_mon(g_clients[i]->execed); |
---|
| 246 | // if ( streams_send_mon(g_clients[i]->execed) == -1 ) |
---|
| 247 | // clients_delete(i); // delete client in case we could not write |
---|
| 248 | } |
---|
| 249 | } |
---|
| 250 | |
---|
| 251 | return -1; |
---|
| 252 | } |
---|
| 253 | |
---|
| 254 | int clients_send_filter(struct roar_audio_info * sa, uint32_t pos) { |
---|
| 255 | int i; |
---|
| 256 | int fh; |
---|
| 257 | |
---|
| 258 | for (i = 0; i < ROAR_CLIENTS_MAX; i++) { |
---|
| 259 | if ( g_clients[i] == NULL ) |
---|
| 260 | continue; |
---|
| 261 | |
---|
| 262 | if ( (fh = g_clients[i]->fh) == -1 ) |
---|
| 263 | continue; |
---|
| 264 | |
---|
| 265 | if ( g_clients[i]->execed == -1 ) { |
---|
| 266 | // TODO: add some code to send a message to the client insetd of the raw data. |
---|
| 267 | } else { |
---|
| 268 | // streams_check(g_clients[i]->execed); |
---|
| 269 | streams_send_filter(g_clients[i]->execed); |
---|
| 270 | // if ( streams_send_mon(g_clients[i]->execed) == -1 ) |
---|
| 271 | // clients_delete(i); // delete client in case we could not write |
---|
| 272 | } |
---|
| 273 | } |
---|
| 274 | |
---|
| 275 | return -1; |
---|
| 276 | } |
---|
| 277 | |
---|
| 278 | int client_stream_exec (int client, int stream) { |
---|
| 279 | int i; |
---|
| 280 | |
---|
| 281 | if ( g_clients[client] == NULL ) |
---|
| 282 | return -1; |
---|
| 283 | |
---|
| 284 | for (i = 0; i < ROAR_CLIENTS_MAX_STREAMS_PER_CLIENT; i++) { |
---|
| 285 | if ( g_clients[client]->streams[i] == stream ) { |
---|
| 286 | g_clients[client]->execed = stream; |
---|
| 287 | streams_set_fh(stream, g_clients[client]->fh); |
---|
| 288 | return 0; |
---|
| 289 | } |
---|
| 290 | } |
---|
| 291 | |
---|
| 292 | return -1; |
---|
| 293 | } |
---|
| 294 | |
---|
[78] | 295 | int client_stream_set_fh (int client, int stream, int fh) { |
---|
| 296 | int i; |
---|
| 297 | |
---|
| 298 | if ( g_clients[client] == NULL ) |
---|
| 299 | return -1; |
---|
| 300 | |
---|
| 301 | for (i = 0; i < ROAR_CLIENTS_MAX_STREAMS_PER_CLIENT; i++) { |
---|
| 302 | if ( g_clients[client]->streams[i] == stream ) { |
---|
| 303 | streams_set_fh(stream, fh); |
---|
| 304 | return 0; |
---|
| 305 | } |
---|
| 306 | } |
---|
| 307 | |
---|
| 308 | return -1; |
---|
| 309 | } |
---|
| 310 | |
---|
[0] | 311 | int client_stream_add (int client, int stream) { |
---|
| 312 | int i; |
---|
| 313 | |
---|
| 314 | if ( g_clients[client] == NULL ) |
---|
| 315 | return -1; |
---|
| 316 | |
---|
| 317 | for (i = 0; i < ROAR_CLIENTS_MAX_STREAMS_PER_CLIENT; i++) { |
---|
| 318 | if ( g_clients[client]->streams[i] == -1 ) { |
---|
| 319 | g_clients[client]->streams[i] = stream; |
---|
| 320 | streams_set_client(stream, client); |
---|
| 321 | return 0; |
---|
| 322 | } |
---|
| 323 | } |
---|
| 324 | |
---|
| 325 | return -1; |
---|
| 326 | } |
---|
| 327 | |
---|
| 328 | int client_stream_delete (int client, int stream) { |
---|
| 329 | int i; |
---|
| 330 | |
---|
| 331 | if ( g_clients[client] == NULL ) |
---|
| 332 | return -1; |
---|
| 333 | |
---|
| 334 | for (i = 0; i < ROAR_CLIENTS_MAX_STREAMS_PER_CLIENT; i++) { |
---|
| 335 | if ( g_clients[client]->streams[i] == stream ) { |
---|
| 336 | g_clients[client]->streams[i] = -1; |
---|
| 337 | |
---|
| 338 | if ( stream == g_clients[client]->execed ) { |
---|
| 339 | ROAR_DBG("client_stream_delete(client=%i, stream=%i): stream is execed one, deleting client!", client, stream); |
---|
| 340 | clients_delete(client); |
---|
| 341 | } |
---|
| 342 | |
---|
| 343 | ROAR_DBG("client_stream_delete(client=%i, stream=%i) = 0", client, stream); |
---|
| 344 | return 0; |
---|
| 345 | } |
---|
| 346 | } |
---|
| 347 | |
---|
| 348 | ROAR_DBG("client_stream_delete(client=%i, stream=%i) = -1", client, stream); |
---|
| 349 | return -1; |
---|
| 350 | } |
---|
| 351 | |
---|
| 352 | //ll |
---|