[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; |
---|
[84] | 137 | } |
---|
[0] | 138 | |
---|
[84] | 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 | |
---|
[84] | 143 | if ( fh > max_fh ) |
---|
| 144 | max_fh = fh; |
---|
[66] | 145 | } |
---|
[84] | 146 | //printf("D: client=%i, stream=%i, fh=%i\n", i, j, fh); |
---|
[66] | 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 | |
---|
[84] | 168 | if ( FD_ISSET(fh, &e) ) { |
---|
[66] | 169 | clients_delete(i); |
---|
[84] | 170 | continue; |
---|
| 171 | } |
---|
| 172 | } |
---|
| 173 | |
---|
| 174 | if ( g_clients[i] == NULL ) |
---|
| 175 | continue; |
---|
| 176 | |
---|
| 177 | for (j = 0; j < ROAR_CLIENTS_MAX_STREAMS_PER_CLIENT; j++) { |
---|
| 178 | //printf("D: client=%i, stream=%i, g_clients[i=%i] = %p\n", i, j, i, g_clients[i]); |
---|
[136] | 179 | if ( g_clients[i] == NULL ) // streams_check() bellow can delete our client (why?) |
---|
| 180 | break; |
---|
[84] | 181 | if ( (fh = streams_get_fh(g_clients[i]->streams[j])) != -1 ) { |
---|
| 182 | if ( FD_ISSET(fh, &r) ) { |
---|
| 183 | streams_check(g_clients[i]->streams[j]); |
---|
[66] | 184 | } |
---|
[0] | 185 | } |
---|
| 186 | } |
---|
| 187 | } |
---|
| 188 | |
---|
[71] | 189 | ROAR_DBG("clients_check_all(void) = %i // have value", have); |
---|
| 190 | return have; |
---|
[0] | 191 | } |
---|
| 192 | |
---|
| 193 | int clients_check (int id) { |
---|
| 194 | struct roar_message m; |
---|
| 195 | struct roar_connection con; |
---|
| 196 | char * data = NULL; |
---|
| 197 | int oldcmd; |
---|
| 198 | int r; |
---|
| 199 | |
---|
| 200 | if ( g_clients[id] == NULL ) |
---|
| 201 | return -1; |
---|
| 202 | if ( g_clients[id]->fh == -1 ) |
---|
| 203 | return -1; |
---|
| 204 | |
---|
| 205 | con.fh = g_clients[id]->fh; |
---|
| 206 | |
---|
| 207 | r = roar_recv_message(&con, &m, &data); |
---|
| 208 | |
---|
| 209 | if ( r == -1 ) { // should we drop the client? |
---|
| 210 | clients_delete(id); |
---|
| 211 | return -1; |
---|
| 212 | } |
---|
| 213 | |
---|
| 214 | roar_debug_message_print(&m); |
---|
| 215 | |
---|
| 216 | oldcmd = m.cmd; |
---|
| 217 | |
---|
| 218 | if ( (r = command_exec(id, &m, data)) == -1 ) { |
---|
| 219 | m.cmd = ROAR_CMD_ERROR; |
---|
| 220 | m.datalen = 0; |
---|
| 221 | ROAR_DBG("clients_check(*): Exec of command faild!"); |
---|
| 222 | } else { |
---|
| 223 | if ( m.cmd == oldcmd ) { |
---|
| 224 | m.cmd = ROAR_CMD_OK; |
---|
| 225 | m.datalen = 0; |
---|
| 226 | } |
---|
| 227 | } |
---|
| 228 | |
---|
| 229 | roar_send_message(&con, &m, NULL); |
---|
| 230 | |
---|
| 231 | if ( data ) |
---|
| 232 | free(data); |
---|
| 233 | |
---|
| 234 | ROAR_DBG("clients_check(id=%i) = 0", id); |
---|
| 235 | return 0; |
---|
| 236 | } |
---|
| 237 | |
---|
| 238 | int clients_send_mon (struct roar_audio_info * sa, uint32_t pos) { |
---|
| 239 | int i; |
---|
| 240 | int fh; |
---|
| 241 | |
---|
| 242 | for (i = 0; i < ROAR_CLIENTS_MAX; i++) { |
---|
| 243 | if ( g_clients[i] == NULL ) |
---|
| 244 | continue; |
---|
| 245 | |
---|
| 246 | if ( (fh = g_clients[i]->fh) == -1 ) |
---|
| 247 | continue; |
---|
| 248 | |
---|
| 249 | if ( g_clients[i]->execed == -1 ) { |
---|
| 250 | // TODO: add some code to send a message to the client insetd of the raw data. |
---|
| 251 | } else { |
---|
| 252 | // streams_check(g_clients[i]->execed); |
---|
| 253 | streams_send_mon(g_clients[i]->execed); |
---|
| 254 | // if ( streams_send_mon(g_clients[i]->execed) == -1 ) |
---|
| 255 | // clients_delete(i); // delete client in case we could not write |
---|
| 256 | } |
---|
| 257 | } |
---|
| 258 | |
---|
| 259 | return -1; |
---|
| 260 | } |
---|
| 261 | |
---|
| 262 | int clients_send_filter(struct roar_audio_info * sa, uint32_t pos) { |
---|
| 263 | int i; |
---|
| 264 | int fh; |
---|
| 265 | |
---|
| 266 | for (i = 0; i < ROAR_CLIENTS_MAX; i++) { |
---|
| 267 | if ( g_clients[i] == NULL ) |
---|
| 268 | continue; |
---|
| 269 | |
---|
| 270 | if ( (fh = g_clients[i]->fh) == -1 ) |
---|
| 271 | continue; |
---|
| 272 | |
---|
| 273 | if ( g_clients[i]->execed == -1 ) { |
---|
| 274 | // TODO: add some code to send a message to the client insetd of the raw data. |
---|
| 275 | } else { |
---|
| 276 | // streams_check(g_clients[i]->execed); |
---|
| 277 | streams_send_filter(g_clients[i]->execed); |
---|
| 278 | // if ( streams_send_mon(g_clients[i]->execed) == -1 ) |
---|
| 279 | // clients_delete(i); // delete client in case we could not write |
---|
| 280 | } |
---|
| 281 | } |
---|
| 282 | |
---|
| 283 | return -1; |
---|
| 284 | } |
---|
| 285 | |
---|
| 286 | int client_stream_exec (int client, int stream) { |
---|
| 287 | int i; |
---|
| 288 | |
---|
| 289 | if ( g_clients[client] == NULL ) |
---|
| 290 | return -1; |
---|
| 291 | |
---|
| 292 | for (i = 0; i < ROAR_CLIENTS_MAX_STREAMS_PER_CLIENT; i++) { |
---|
| 293 | if ( g_clients[client]->streams[i] == stream ) { |
---|
| 294 | g_clients[client]->execed = stream; |
---|
| 295 | streams_set_fh(stream, g_clients[client]->fh); |
---|
| 296 | return 0; |
---|
| 297 | } |
---|
| 298 | } |
---|
| 299 | |
---|
| 300 | return -1; |
---|
| 301 | } |
---|
| 302 | |
---|
[78] | 303 | int client_stream_set_fh (int client, int stream, int fh) { |
---|
| 304 | int i; |
---|
| 305 | |
---|
| 306 | if ( g_clients[client] == NULL ) |
---|
| 307 | return -1; |
---|
| 308 | |
---|
| 309 | for (i = 0; i < ROAR_CLIENTS_MAX_STREAMS_PER_CLIENT; i++) { |
---|
| 310 | if ( g_clients[client]->streams[i] == stream ) { |
---|
| 311 | streams_set_fh(stream, fh); |
---|
| 312 | return 0; |
---|
| 313 | } |
---|
| 314 | } |
---|
| 315 | |
---|
| 316 | return -1; |
---|
| 317 | } |
---|
| 318 | |
---|
[0] | 319 | int client_stream_add (int client, int stream) { |
---|
| 320 | int i; |
---|
| 321 | |
---|
| 322 | if ( g_clients[client] == NULL ) |
---|
| 323 | return -1; |
---|
| 324 | |
---|
| 325 | for (i = 0; i < ROAR_CLIENTS_MAX_STREAMS_PER_CLIENT; i++) { |
---|
| 326 | if ( g_clients[client]->streams[i] == -1 ) { |
---|
| 327 | g_clients[client]->streams[i] = stream; |
---|
| 328 | streams_set_client(stream, client); |
---|
| 329 | return 0; |
---|
| 330 | } |
---|
| 331 | } |
---|
| 332 | |
---|
| 333 | return -1; |
---|
| 334 | } |
---|
| 335 | |
---|
| 336 | int client_stream_delete (int client, int stream) { |
---|
| 337 | int i; |
---|
| 338 | |
---|
| 339 | if ( g_clients[client] == NULL ) |
---|
| 340 | return -1; |
---|
| 341 | |
---|
| 342 | for (i = 0; i < ROAR_CLIENTS_MAX_STREAMS_PER_CLIENT; i++) { |
---|
| 343 | if ( g_clients[client]->streams[i] == stream ) { |
---|
| 344 | g_clients[client]->streams[i] = -1; |
---|
| 345 | |
---|
| 346 | if ( stream == g_clients[client]->execed ) { |
---|
| 347 | ROAR_DBG("client_stream_delete(client=%i, stream=%i): stream is execed one, deleting client!", client, stream); |
---|
| 348 | clients_delete(client); |
---|
| 349 | } |
---|
| 350 | |
---|
| 351 | ROAR_DBG("client_stream_delete(client=%i, stream=%i) = 0", client, stream); |
---|
| 352 | return 0; |
---|
| 353 | } |
---|
| 354 | } |
---|
| 355 | |
---|
| 356 | ROAR_DBG("client_stream_delete(client=%i, stream=%i) = -1", client, stream); |
---|
| 357 | return -1; |
---|
| 358 | } |
---|
| 359 | |
---|
| 360 | //ll |
---|