source: roaraudio/roard/clients.c @ 4815:df2ef6edb97f

Last change on this file since 4815:df2ef6edb97f was 4815:df2ef6edb97f, checked in by phi, 13 years ago

added support to roard for record streams.

File size: 22.6 KB
Line 
1//clients.c:
2
3/*
4 *      Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2008-2011
5 *
6 *  This file is part of roard a part of RoarAudio,
7 *  a cross-platform sound system for both, home and professional use.
8 *  See README for details.
9 *
10 *  This file is free software; you can redistribute it and/or modify
11 *  it under the terms of the GNU General Public License version 3
12 *  as published by the Free Software Foundation.
13 *
14 *  RoarAudio is distributed in the hope that it will be useful,
15 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
16 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 *  GNU General Public License for more details.
18 *
19 *  You should have received a copy of the GNU General Public License
20 *  along with this software; see the file COPYING.  If not, write to
21 *  the Free Software Foundation, 51 Franklin Street, Fifth Floor,
22 *  Boston, MA 02110-1301, USA.
23 *
24 */
25
26#include "roard.h"
27
28struct roard_proto g_proto[MAX_PROTOS] = {
29#ifndef ROAR_WITHOUT_DCOMP_EMUL_ESD
30#ifdef ROAR_HAVE_H_ESD
31 {ROAR_PROTO_ESOUND, NULL, emul_esd_check_client, NULL, NULL},
32#endif
33#endif
34#ifndef ROAR_WITHOUT_DCOMP_EMUL_RPLAY
35 {ROAR_PROTO_RPLAY, NULL, emul_rplay_check_client, NULL, NULL},
36#endif
37#ifndef ROAR_WITHOUT_DCOMP_EMUL_GOPHER
38 {ROAR_PROTO_GOPHER, NULL, emul_gopher_check_client, NULL, emul_gopher_flushed_client},
39#endif
40 {-1, NULL}
41};
42
43#define _CHECK_CID_RET(id,ret) if ( (id) < 0 || (id) > ROAR_CLIENTS_MAX || g_clients[(id)] == NULL ) return (ret)
44#define _CHECK_CID(id)         _CHECK_CID_RET((id), -1)
45
46int clients_init (void) {
47 int i;
48
49 for (i = 0; i < ROAR_CLIENTS_MAX; i++)
50  g_clients[i] = NULL;
51
52 return 0;
53}
54
55int clients_free (void) {
56 int i;
57
58 for (i = 0; i < ROAR_CLIENTS_MAX; i++)
59  if ( g_clients[i] != NULL )
60   clients_delete(i);
61
62 return 0;
63}
64
65int clients_new (void) {
66 int i;
67 int s;
68 struct roar_client_server * ns;
69 struct roar_client * n;
70
71 for (i = 0; i < ROAR_CLIENTS_MAX; i++) {
72  if ( g_clients[i] == NULL ) {
73   ns = roar_mm_malloc(sizeof(struct roar_client_server));
74   n = ROAR_CLIENT(ns);
75
76   memset(ns, 0, sizeof(struct roar_client_server));
77
78   if ( n != NULL ) {
79    n->pid    = -1;
80    n->uid    = -1;
81    n->gid    = -1;
82    n->fh     = -1;
83
84    *n->name = 0;
85    *n->host = 0;
86
87    n->proto     = ROAR_PROTO_ROARAUDIO;
88    n->byteorder = ROAR_BYTEORDER_NETWORK;
89
90    n->acl   = NULL;
91
92    n->execed = -1;
93    for (s = 0; s < ROAR_CLIENTS_MAX_STREAMS_PER_CLIENT; s++)
94     n->streams[s] = -1;
95
96    if ( roar_nnode_new(&(n->nnode), ROAR_SOCKET_TYPE_UNKNOWN) == -1 ) {
97     roar_mm_free(n);
98     return -1;
99    }
100
101    ns->blockc   = 0;
102    ns->waits    = NULL;
103    ns->acclev   = ACCLEV_NONE;
104
105    g_clients[i] = ns;
106
107    counters_inc(clients, 1);
108    roar_notify_core_emit_snoargs(ROAR_OE_BASICS_NEW, -1, i, ROAR_OT_CLIENT);
109    ROAR_DBG("clients_new(void) = %i", i);
110    return i;
111   } else {
112    ROAR_ERR("clients_new(void): Can not alloc memory for new client: %s", strerror(errno));
113    ROAR_ERR("clients_new(void) = -1");
114    return -1;
115   }
116  }
117 }
118
119 return -1;
120}
121
122int clients_new_from_fh(int fh, int proto, int byteorder, int update_nnode) {
123 struct roar_client * c;
124 int client;
125
126 if ( fh == -1 )
127  return -1;
128
129 if ( proto != ROAR_PROTO_ROARAUDIO || byteorder != ROAR_BYTEORDER_NETWORK )
130  return -1;
131
132 if ( (client = clients_new()) == -1 )
133  return -1;
134
135 if ( clients_set_fh(client, fh) == -1 ) {
136  clients_delete(client);
137  return -1;
138 }
139
140 if ( update_nnode ) {
141  if ( clients_get(client, &c) != -1 ) {
142   if ( roar_nnode_free(&(c->nnode)) != -1 ) {
143    roar_nnode_new_from_fh(&(c->nnode), fh, 1);
144   }
145  }
146 }
147
148 return 0;
149}
150
151int clients_delete (int id) {
152 struct roar_client_server * cs;
153 int i;
154 int close_client_fh = 1;
155
156 ROAR_DBG("clients_delete(id=%i) = ?", id);
157
158 _CHECK_CID(id);
159
160 cs = g_clients[id];
161
162 if ( cs->waits != NULL ) {
163  for (i = 0; cs->waits[i] != NULL; i++)
164   roar_notify_core_unsubscribe(NULL, cs->waits[i]);
165
166  roar_mm_free(cs->waits);
167  cs->waits = NULL;
168 }
169
170 roar_notify_core_emit_snoargs(ROAR_OE_BASICS_DELETE, -1, id, ROAR_OT_CLIENT);
171
172 counters_inc(clients, -1);
173
174 if (ROAR_CLIENT(cs)->execed != -1) {
175//  return streams_delete(g_clients[id]->execed);
176  ROAR_CLIENT(cs)->execed = -1;
177  close_client_fh = 0;
178 }
179
180 for (i = 0; i < ROAR_CLIENTS_MAX_STREAMS_PER_CLIENT; i++) {
181  streams_delete(ROAR_CLIENT(cs)->streams[i]);
182 }
183
184 if ( ROAR_CLIENT(cs)->fh != -1 && close_client_fh )
185  close(ROAR_CLIENT(cs)->fh);
186
187 roar_nnode_free(&(ROAR_CLIENT(cs)->nnode));
188
189 if ( cs->inbuf != NULL )
190  roar_buffer_free(cs->inbuf);
191
192 if ( cs->outbuf != NULL )
193  roar_buffer_free(cs->outbuf);
194
195 roar_mm_free(cs);
196 g_clients[id] = NULL;
197
198 ROAR_DBG("clients_delete(id=%i) = 0", id);
199 return 0;
200}
201
202int clients_close      (int id, int nocheck_exec) {
203 struct roar_client * c;
204
205 ROAR_DBG("clients_close(id=%i) = ?", id);
206
207 _CHECK_CID(id);
208
209 c = ROAR_CLIENT(g_clients[id]);
210
211 if ( c->fh == -1 ) {
212  ROAR_DBG("clients_delete(id=%i) = 0", id);
213  return 0;
214 }
215
216 if (nocheck_exec || c->execed != -1) {
217  close(c->fh);
218  c->fh = -1;
219 }
220
221 ROAR_DBG("clients_delete(id=%i) = 0", id);
222 return 0;
223}
224
225int clients_get       (int id, struct roar_client ** client) {
226 _CHECK_CID(id);
227
228 *client = ROAR_CLIENT(g_clients[id]);
229
230 if ( *client == NULL )
231  return -1;
232
233 return 0;
234}
235
236int clients_get_server (int id, struct roar_client_server ** client) {
237 _CHECK_CID(id);
238
239 *client = g_clients[id];
240
241 if ( *client == NULL )
242  return -1;
243
244 return 0;
245}
246
247int clients_set_fh    (int id, int    fh) {
248 struct roar_client * c;
249#ifdef SO_PEERCRED
250 struct ucred cred;
251 socklen_t cred_len = sizeof(cred);
252#endif
253
254 _CHECK_CID(id);
255
256 if ( (c = ROAR_CLIENT(g_clients[id])) == NULL )
257  return -1;
258
259 c->fh = fh;
260
261#ifdef SO_PEERCRED
262 if (getsockopt(fh, SOL_SOCKET, SO_PEERCRED, &cred, &cred_len) != -1) {
263  if ( cred.pid != 0 ) {
264   c->pid = cred.pid;
265   c->uid = cred.uid;
266   c->gid = cred.gid;
267  }
268 } else {
269  ROAR_DBG("req_on_identify(): Can't get creds via SO_PEERCRED: %s", strerror(errno));
270 }
271#elif defined(ROAR_HAVE_GETPEEREID)
272 if (getpeereid(fh, &(c->uid), &(c->gid)) == -1) {
273  ROAR_DBG("req_on_identify(): Can't get creds via getpeereid(): %s", strerror(errno));
274 }
275#endif
276
277 return 0;
278}
279
280int clients_get_fh    (int id) {
281 _CHECK_CID(id);
282
283 return ROAR_CLIENT(g_clients[id])->fh;
284}
285
286int clients_set_pid   (int id, int    pid) {
287 _CHECK_CID(id);
288
289 ROAR_CLIENT(g_clients[id])->pid = pid;
290
291 return 0;
292}
293
294int clients_set_uid   (int id, int    uid) {
295 _CHECK_CID(id);
296
297 ROAR_CLIENT(g_clients[id])->uid = uid;
298
299 return 0;
300}
301
302int clients_set_gid   (int id, int    gid) {
303 _CHECK_CID(id);
304
305 ROAR_CLIENT(g_clients[id])->gid = gid;
306
307 return 0;
308}
309
310int clients_set_proto (int id, int    proto) {
311 int byteorder = ROAR_BYTEORDER_UNKNOWN;
312
313 _CHECK_CID(id);
314
315 switch (proto) {
316  case ROAR_PROTO_ROARAUDIO:
317  case ROAR_PROTO_ESOUND:
318  case ROAR_PROTO_RPLAY:
319  case ROAR_PROTO_SIMPLE:
320    byteorder = ROAR_BYTEORDER_NETWORK;
321   break;
322 }
323
324 ROAR_CLIENT(g_clients[id])->proto     = proto;
325 ROAR_CLIENT(g_clients[id])->byteorder = byteorder;
326
327 return 0;
328}
329
330int clients_block      (int id, int unblock) {
331 _CHECK_CID(id);
332
333 if ( unblock ) {
334  g_clients[id]->blockc--;
335 } else {
336  g_clients[id]->blockc++;
337 }
338
339 return 0;
340}
341
342
343#define MAX_STREAMLESS 8
344
345int clients_check_all (void) {
346#ifdef ROAR_HAVE_SELECT
347 struct roar_client * c;
348 struct timeval tv;
349 fd_set r, w, e;
350 int i, j;
351 int ret;
352 int fh;
353 int max_fh = -1;
354 int have = 0;
355 struct {
356  int id;
357  int fh;
358 } streamless[MAX_STREAMLESS];
359 int have_streamless = 0;
360 int have_stream;
361
362 ROAR_DBG("clients_check_all(void) = ?");
363
364 FD_ZERO(&r);
365 FD_ZERO(&w);
366 FD_ZERO(&e);
367
368 tv.tv_sec  = 0;
369 tv.tv_usec = 1;
370
371 for (i = 0; i < ROAR_CLIENTS_MAX; i++) {
372  if ( (c = ROAR_CLIENT(g_clients[i])) == NULL )
373   continue;
374
375  ROAR_DBG("clients_check_all(void): i(client)=%i", i);
376
377  if ( (fh = c->fh) != -1 ) {
378   have++;
379
380   ROAR_DBG("clients_check_all(*): fh=%i", fh);
381
382   FD_SET(fh, &r);
383   FD_SET(fh, &e);
384
385   if ( g_clients[i]->outbuf != NULL ) {
386    FD_SET(fh, &w);
387   }
388
389   if ( fh > max_fh )
390    max_fh = fh;
391  }
392
393  have_stream = 0;
394
395  for (j = 0; j < ROAR_CLIENTS_MAX_STREAMS_PER_CLIENT; j++) {
396   ROAR_DBG("clients_check_all(void): i(client)=%i, j(stream)=%i", i, j);
397   if ( (fh = streams_get_fh(c->streams[j])) != -1 ) {
398    ROAR_DBG("clients_check_all(*): g_clients[i=%i]->streams[j=%i] = %i, fh = %i", i, j, c->streams[j], fh);
399    if ( fh > -1 ) {
400     FD_SET(fh, &r);
401
402     if ( fh > max_fh )
403      max_fh = fh;
404    } else if ( fh == -2 ) {
405     streams_check(c->streams[j]);
406    }
407
408    have_stream = 1;
409   }
410   //printf("D: client=%i, stream=%i, fh=%i\n", i, j, fh);
411  }
412
413  if ( !have_stream && have_streamless < MAX_STREAMLESS ) {
414   streamless[have_streamless  ].id = i;
415   if ( (streamless[have_streamless++].fh = c->fh) == -1 )
416    have_streamless--;
417  }
418 }
419
420 ROAR_DBG("clients_check_all(void): max_fh=%i", max_fh);
421
422 if ( max_fh == -1 )
423  return 0;
424
425 if ( (ret = select(max_fh + 1, &r, &w, &e, &tv)) < 1 ) {
426  return ret < 0 ? ret : have;
427 }
428
429 for (i = 0; i < ROAR_CLIENTS_MAX; i++) {
430  if ( (c = ROAR_CLIENT(g_clients[i])) == NULL )
431   continue;
432
433  if ( (fh = c->fh) != -1 ) {
434   if ( FD_ISSET(fh, &r) ) {
435    if ( c->execed == -1 ) {
436     clients_check(i);
437     if ( g_clients[i] != NULL && ROAR_CLIENT(g_clients[i])->execed != -1 ) {
438      FD_CLR(fh, &r);
439     }
440/*
441    } else {
442     streams_check(g_clients[i]->execed);
443*/
444    }
445   }
446   if ( FD_ISSET(fh, &w) ) {
447    clients_flush(i);
448   }
449
450   if ( FD_ISSET(fh, &e) ) {
451    clients_delete(i);
452    continue;
453   }
454  }
455
456  if ( (c = ROAR_CLIENT(g_clients[i])) == NULL )
457   continue;
458
459  for (j = 0; j < ROAR_CLIENTS_MAX_STREAMS_PER_CLIENT; j++) {
460   ROAR_DBG("clients_check_all(*): D: client=%i, stream=%i, g_clients[i=%i] = %p", i, j, i, g_clients[i]);
461   if ( g_clients[i] == NULL ) // streams_check() bellow can delete our client (why?)
462    break;
463
464   //ROAR_WARN("clients_check_all(*): client=%i: client exists", i);
465   ROAR_DBG("clients_check_all(*): client=%i, stream=%i: id=%i", i, j, c->streams[j]);
466
467   if ( (fh = streams_get_fh(c->streams[j])) != -1 ) {
468    ROAR_DBG("clients_check_all(*): client=%i, stream=%i: fh=%i", i, j, fh);
469    if ( fh > -1 && FD_ISSET(fh, &r) ) {
470     streams_check(c->streams[j]);
471    }
472   }
473  }
474 }
475
476 if ( have_streamless ) {
477   FD_ZERO(&r);
478   FD_ZERO(&w);
479
480   tv.tv_sec  = 0;
481   tv.tv_usec = 1;
482
483   max_fh = -1;
484
485   for (i = 0; i < have_streamless; i++) {
486    if ( g_clients[j = streamless[i].id] == NULL )
487     continue;
488
489    if ( ROAR_CLIENT(g_clients[j])->execed != -1 )
490     continue;
491
492    fh = streamless[i].fh;
493
494    ROAR_DBG("clients_check_all(void): fh=%i", fh);
495    FD_SET(fh, &r);
496
497    if ( g_clients[j]->outbuf != NULL ) {
498     FD_SET(fh, &w);
499    }
500
501    if ( fh > max_fh )
502     max_fh = fh;
503   }
504
505   if ( (ret = select(max_fh + 1, &r, &w, NULL, &tv)) < 0 ) {
506    return ret;
507   }
508
509   for (i = 0; i < have_streamless; i++) {
510    if ( FD_ISSET(streamless[i].fh, &r) ) {
511     clients_check(streamless[i].id);
512    }
513    if ( FD_ISSET(streamless[i].fh, &w) ) {
514     clients_flush(streamless[i].id);
515    }
516   }
517 }
518
519 ROAR_DBG("clients_check_all(void) = %i // have value", have);
520 return have;
521#else
522 ROAR_DBG("clients_check_all(void) = -1");
523 return -1;
524#endif
525}
526
527int clients_check     (int id) {
528 struct roar_client   * c;
529 struct roar_message    m;
530 struct roar_connection con;
531 char * data = NULL;
532 int oldcmd;
533 int r;
534 int rv = 0;
535 uint32_t flags[2] = {COMMAND_FLAG_NONE, COMMAND_FLAG_NONE};
536 uint32_t event;
537 size_t i;
538
539 ROAR_DBG("clients_check(id=%i) = ?", id);
540
541 _CHECK_CID(id);
542
543 c = ROAR_CLIENT(g_clients[id]);
544
545 if ( c->fh == -1 )
546  return -1;
547
548 roar_connect_fh(&con, c->fh);
549
550 ROAR_DBG("clients_check(id=%i): c->proto=%i", id, c->proto);
551
552 switch (c->proto) {
553  case ROAR_PROTO_ROARAUDIO:
554    r = roar_recv_message(&con, &m, &data);
555
556    if ( r == -1 ) { // should we drop the client?
557     clients_delete(id);
558     return -1;
559    }
560
561    event = ROAR_NOTIFY_CMD2EVENT(m.cmd);
562
563    roar_debug_message_print(&m);
564
565    oldcmd = m.cmd;
566
567    if ( (r = command_exec(id, &m, &data, flags)) == -1 ) {
568     m.cmd     = ROAR_CMD_ERROR;
569     m.datalen = 0;
570     ROAR_DBG("clients_check(*): Exec of command faild!");
571    } else {
572     if ( m.cmd == oldcmd ) {
573      m.cmd     = ROAR_CMD_OK;
574      m.datalen = 0;
575     } else if ( m.cmd == ROAR_CMD_OK_STOP ) {
576      m.cmd     = ROAR_CMD_OK;
577      rv        = 1;
578     }
579    }
580    ROAR_DBG("clients_check(*): data=%p", data);
581
582    if ( flags[1] & COMMAND_FLAG_OUT_NOSEND ) {
583     roar_notify_core_emit_simple(event, id, -1, -1, -1, -1, NULL, 0);
584    } else {
585     roar_notify_core_emit_simple(event, id, -1, -1, m.cmd, -1, NULL, 0);
586     roar_send_message(&con, &m, flags[1] & COMMAND_FLAG_OUT_LONGDATA ? data : NULL);
587    }
588
589    if ( flags[1] & COMMAND_FLAG_OUT_CLOSECON )
590     clients_close(id, 1);
591
592   break;
593#ifndef ROAR_WITHOUT_DCOMP_EMUL_RSOUND
594  case ROAR_PROTO_RSOUND:
595    rv = emul_rsound_check_client(id, NULL);
596    if ( rv == 0 ) { // loop as long as we don't get an error.
597     while (rv == 0)
598      rv = emul_rsound_check_client(id, NULL);
599     rv = 0; // restore
600    } else { // in case of error delete the client
601     if (
602#ifdef EAGAIN
603          errno != EAGAIN      &&
604#endif
605#ifdef EWOULDBLOCK
606          errno != EWOULDBLOCK &&
607#endif
608#ifdef EINTR
609          errno != EINTR       &&
610#endif
611          1 ) {
612      rv = clients_delete(id);
613     } else {
614      rv = 0;
615     }
616    }
617   break;
618#endif
619  default:
620    rv = -1;
621    for (i = 0; g_proto[i].proto != -1; i++) {
622     if ( g_proto[i].proto == c->proto ) {
623      rv = g_proto[i].check_client(id, NULL);
624     }
625    }
626 }
627
628 if ( data != NULL )
629  free(data);
630
631 ROAR_DBG("clients_check(id=%i) = %i", id, rv);
632 return rv;
633}
634
635int clients_flush      (int id) {
636 struct roar_vio_calls         vio;
637 struct roar_client_server   * cs;
638 struct roar_client          * c;
639 struct roard_proto          * p = NULL;
640 size_t i;
641 size_t len;
642 ssize_t ret;
643 void * buf;
644
645 _CHECK_CID(id);
646
647 c = ROAR_CLIENT(cs = g_clients[id]);
648
649 for (i = 0; g_proto[i].proto != -1; i++) {
650  if ( g_proto[i].proto == c->proto ) {
651   p = &(g_proto[i]);
652   break;
653  }
654 }
655
656 if ( p == NULL )
657  return -1;
658
659 roar_vio_open_fh_socket(&vio, clients_get_fh(id));
660
661 if ( p->flush_client != NULL ) {
662  return p->flush_client(id, &vio);
663 }
664
665 if ( roar_buffer_get_len(cs->outbuf, &len) == -1 )
666  return -1;
667
668 if ( roar_buffer_get_data(cs->outbuf, &buf) == -1 )
669  return -1;
670
671 ret = roar_vio_write(&vio, buf, len);
672
673 if ( ret < 1 ) {
674  clients_delete(id);
675  return -1;
676 }
677
678 if ( ret == len ) {
679  roar_buffer_next(&(cs->outbuf));
680 } else {
681  if ( roar_buffer_set_offset(cs->outbuf, ret) == -1 ) {
682   clients_delete(id);
683   return -1;
684  }
685 }
686
687 if ( cs->outbuf == NULL ) {
688  if ( p->flushed_client != NULL ) {
689   return p->flushed_client(id, &vio);
690  }
691 }
692
693 return 0;
694}
695
696int clients_send_mon  (struct roar_audio_info * sa, uint32_t pos) {
697 int i;
698// int fh;
699 int j;
700 int keep_going;
701
702 for (i = 0; i < ROAR_CLIENTS_MAX; i++) {
703  if ( g_clients[i] == NULL )
704   continue;
705
706  keep_going = 1;
707
708/*
709  if ( (fh = g_clients[i]->fh) == -1 )
710   continue;
711*/
712
713  ROAR_DBG("clients_send_mon(*): client=%i, execed=%i", i, ROAR_CLIENT(g_clients[i])->execed);
714
715/*
716  if ( g_clients[i]->execed == -1 ) {
717   // TODO: add some code to send a message to the client insetd of the raw data.
718*/
719   for (j = 0; keep_going && j < ROAR_CLIENTS_MAX_STREAMS_PER_CLIENT; j++) {
720    //if ( (fh = streams_get_fh(g_clients[i]->streams[j])) != -1 ) {
721    ROAR_DBG("clients_send_mon(*): client=%i, stream=%i -> ?", i, j);
722    if ( ROAR_CLIENT(g_clients[i])->streams[j] != -1 ) {
723     ROAR_DBG("clients_send_mon(*): client=%i, stream=%i -> %i", i, j, ROAR_CLIENT(g_clients[i])->streams[j]);
724     streams_send_mon(ROAR_CLIENT(g_clients[i])->streams[j]);
725
726     // the client may be deleted here, check if it still exists:
727     if ( g_clients[i] == NULL )
728      keep_going = 0;
729    }
730   }
731/*
732  } else {
733//   streams_check(g_clients[i]->execed);
734   streams_send_mon(g_clients[i]->execed);
735//   if ( streams_send_mon(g_clients[i]->execed) == -1 )
736//    clients_delete(i); // delete client in case we could not write
737  }
738*/
739 }
740
741 // TODO: FIXME: should this really be -1?
742 return -1;
743}
744
745int clients_send_filter(struct roar_audio_info * sa, uint32_t pos) {
746 struct roar_client * c;
747 int i;
748 int fh;
749
750 for (i = 0; i < ROAR_CLIENTS_MAX; i++) {
751  if ( (c = ROAR_CLIENT(g_clients[i])) == NULL )
752   continue;
753
754  if ( (fh = c->fh) == -1 )
755   continue;
756
757  if ( c->execed == -1 ) {
758   // TODO: add some code to send a message to the client insetd of the raw data.
759  } else {
760//   streams_check(g_clients[i]->execed);
761   streams_send_filter(c->execed);
762//   if ( streams_send_mon(g_clients[i]->execed) == -1 )
763//    clients_delete(i); // delete client in case we could not write
764  }
765 }
766
767 return -1;
768}
769
770int clients_add_output (int id, struct roar_buffer * buf) {
771 struct roar_client_server   * cs;
772
773 _CHECK_CID(id);
774 cs = g_clients[id];
775
776 if ( cs->outbuf == NULL ) {
777  cs->outbuf = buf;
778 } else {
779  return roar_buffer_add(cs->outbuf, buf);
780 }
781
782 return 0;
783}
784
785// proto support
786int clients_register_proto(struct roard_proto * proto) {
787 const size_t len = sizeof(g_proto)/sizeof(*g_proto);
788 size_t i;
789
790 if ( proto == NULL )
791  return -1;
792
793 for (i = 0; g_proto[i].proto != -1; i++);
794
795 // i is now at pos of current EOS entry.
796
797 // test if we have space for one more entry:
798 if ( (i+1) >= len )
799  return -1;
800
801 memcpy(&(g_proto[i]), proto, sizeof(*g_proto));
802
803 i++;
804
805 memset(&(g_proto[i]), 0, sizeof(*g_proto));
806 g_proto[i].proto = -1;
807
808 return 0;
809}
810
811
812int client_stream_exec   (int client, int stream) {
813 struct roar_client * c;
814 int i;
815 int fh;
816
817 _CHECK_CID(client);
818
819#if 0
820 if ( g_clients[client] == NULL ) {
821  ROAR_WARN("client_stream_exec(client=%i, stream=%i) = -1 // client does not exist", client, stream);
822  return -1;
823 }
824#endif
825
826 c = ROAR_CLIENT(g_clients[client]);
827
828 for (i = 0; i < ROAR_CLIENTS_MAX_STREAMS_PER_CLIENT; i++) {
829  if ( c->streams[i] == stream ) {
830   c->execed = stream;
831   if ( streams_is_ready(stream) == 0 ) {
832    streams_set_fh(stream, c->fh);
833    streams_set_socktype(stream, ROAR_SOCKET_TYPE_GENSTR);
834   } else {
835    ROAR_DBG("client_stream_exec(client=%i, stream=%i): fh=?", client, stream);
836    if ( (fh = c->fh) != -1 ) {
837     close(fh);
838     c->fh = -1;
839    }
840   }
841   ROAR_DBG("client_stream_exec(client=%i, stream=%i) = 0", client, stream);
842   return 0;
843  }
844 }
845
846 ROAR_WARN("client_stream_exec(client=%i, stream=%i) = -1 // client does not own stream", client, stream);
847 return -1;
848}
849
850int client_stream_set_fh (int client, int stream, int fh) {
851 int i;
852
853 ROAR_DBG("client_stream_set_fh(client=%i, stream=%i, fh=%i) = ?", client, stream, fh);
854
855 _CHECK_CID(client);
856
857 for (i = 0; i < ROAR_CLIENTS_MAX_STREAMS_PER_CLIENT; i++) {
858  if ( ROAR_CLIENT(g_clients[client])->streams[i] == stream ) {
859   ROAR_DBG("client_stream_set_fh(client=%i, stream=%i, fh=%i): stream found, index %i", client, stream, fh, i);
860   return streams_set_fh(stream, fh);
861  }
862 }
863
864 ROAR_WARN("client_stream_set_fh(client=%i, stream=%i, fh=%i) = -1 // client does not own stream", client, stream, fh);
865 return -1;
866}
867
868int client_stream_add    (int client, int stream) {
869 int i;
870
871 _CHECK_CID(client);
872
873 for (i = 0; i < ROAR_CLIENTS_MAX_STREAMS_PER_CLIENT; i++) {
874  if ( ROAR_CLIENT(g_clients[client])->streams[i] == -1 ) {
875   ROAR_CLIENT(g_clients[client])->streams[i] = stream;
876   streams_set_client(stream, client);
877   return 0;
878  }
879 }
880
881 return -1;
882}
883
884int client_stream_delete (int client, int stream) {
885 int i;
886
887 _CHECK_CID(client);
888
889 for (i = 0; i < ROAR_CLIENTS_MAX_STREAMS_PER_CLIENT; i++) {
890  if ( ROAR_CLIENT(g_clients[client])->streams[i] == stream ) {
891   ROAR_CLIENT(g_clients[client])->streams[i] = -1;
892
893   if ( stream == ROAR_CLIENT(g_clients[client])->execed ) {
894    ROAR_DBG("client_stream_delete(client=%i, stream=%i): stream is execed one, deleting client!", client, stream);
895    clients_delete(client);
896   }
897
898   ROAR_DBG("client_stream_delete(client=%i, stream=%i) = 0", client, stream);
899   return 0;
900  }
901 }
902
903 ROAR_DBG("client_stream_delete(client=%i, stream=%i) = -1", client, stream);
904 return -1;
905}
906
907int client_stream_move   (int client, int stream) {
908 int old_client = streams_get_client(stream);
909
910 ROAR_DBG("client_stream_move(client=%i, stream=%i): old_client = %i", client, stream, old_client);
911
912 if ( old_client != -1 )
913  if ( client_stream_delete(old_client, stream) == -1 )
914   return -1;
915
916 return client_stream_add(client, stream);
917}
918
919
920// notify thingys
921int clients_wait    (int client, struct roar_event * events, size_t num) {
922 struct roar_client_server * cs;
923 size_t i, c;
924
925 ROAR_DBG("clients_wait(client=%i, events=%p, num=%llu) = ?", client, events, (long long unsigned int)num);
926
927 _CHECK_CID(client);
928
929 cs = g_clients[client];
930
931 if ( cs->waits != NULL )
932  return -1;
933
934 cs->waits = roar_mm_malloc((num+1) * sizeof(struct roar_subscriber *));
935
936 if ( cs->waits == NULL )
937  return -1;
938
939 if ( clients_block(client, 0) != 0 )
940  return -1;
941
942 for (i = 0; i < num; i++) {
943#if defined(DEBUG) && 0
944  dbg_notify_cb(NULL, &(events[i]), cs);
945#endif
946  cs->waits[i] = roar_notify_core_subscribe(NULL, &(events[i]), clients_ncb_wait, cs);
947  if ( cs->waits[i] == NULL ) {
948   for (c = 0; c < i; c++)
949    roar_notify_core_unsubscribe(NULL, cs->waits[c]);
950   roar_mm_free(cs->waits);
951   cs->waits = NULL;
952   clients_block(client, 1);
953   return -1;
954  }
955 }
956
957 cs->waits[num] = NULL;
958
959 ROAR_DBG("clients_wait(client=%i, events=%p, num=%llu) = 0", client, events, (long long unsigned int)num);
960 return 0;
961}
962
963void clients_ncb_wait(struct roar_notify_core * core, struct roar_event * event, void * userdata) {
964 struct roar_client_server * cs = userdata;
965 struct roar_message m;
966 struct roar_connection con;
967 uint16_t * u16 = (uint16_t *) m.data;
968 size_t tmp;
969 size_t i;
970
971 ROAR_DBG("clients_ncb_wait(core=%p, event=%p, userdata=%p) = ?", core, event, userdata);
972
973 for (i = 0; cs->waits[i] != NULL; i++)
974  roar_notify_core_unsubscribe(NULL, cs->waits[i]);
975
976 roar_mm_free(cs->waits);
977 cs->waits = NULL;
978
979 // protocol depended handling...
980 memset(&m, 0, sizeof(m));
981 m.cmd = ROAR_CMD_OK;
982 u16[0] = ROAR_HOST2NET16(0); // Version
983 u16[1] = ROAR_HOST2NET16(0); // flags
984
985 tmp = sizeof(m.data) - 4;
986
987 roar_event_to_blob(event, m.data + 4, &tmp);
988
989 m.datalen = tmp + 4;
990
991 roar_connect_fh(&con, ROAR_CLIENT(cs)->fh);
992 roar_send_message(&con, &m, NULL);
993 // ...end of protocol depended handling.
994
995// clients_block(, 1);
996 // TODO: FIXME: bad hack...
997 cs->blockc--;
998}
999
1000
1001// acclev:
1002static struct {
1003 const enum roard_client_acclev acclev;
1004 const char *                   name;
1005} _g_acclevs[] = {
1006 {ACCLEV_NONE,    "none"   },
1007 {ACCLEV_IDENTED, "idented"},
1008 {ACCLEV_CONCTL,  "conctl" },
1009 {ACCLEV_GUEST,   "guest"  },
1010 {ACCLEV_USER,    "user"   },
1011 {ACCLEV_PWRUSER, "pwruser"},
1012 {ACCLEV_ALL,     "all"    },
1013 {-1, NULL}
1014};
1015
1016enum roard_client_acclev clients_str2acclev(const char * acclev) {
1017 int i;
1018
1019 for (i = 0; _g_acclevs[i].name != NULL; i++)
1020  if ( !strcasecmp(_g_acclevs[i].name, acclev) )
1021   return _g_acclevs[i].acclev;
1022
1023 return -1;
1024}
1025
1026const char * clients_acclev2str(const enum roard_client_acclev acclev) {
1027 int i;
1028
1029 for (i = 0; _g_acclevs[i].name != NULL; i++)
1030  if ( _g_acclevs[i].acclev == acclev )
1031   return _g_acclevs[i].name;
1032
1033 return NULL;
1034}
1035
1036//ll
Note: See TracBrowser for help on using the repository browser.