7ce538878047c4f48b6ab464e07d343b6dd8beb3
[profile/ivi/pulseaudio-panda.git] / polyp / protocol-cli.c
1 /* $Id$ */
2
3 /***
4   This file is part of polypaudio.
5  
6   polypaudio is free software; you can redistribute it and/or modify
7   it under the terms of the GNU Lesser General Public License as published
8   by the Free Software Foundation; either version 2 of the License,
9   or (at your option) any later version.
10  
11   polypaudio is distributed in the hope that it will be useful, but
12   WITHOUT ANY WARRANTY; without even the implied warranty of
13   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14   General Public License for more details.
15  
16   You should have received a copy of the GNU Lesser General Public License
17   along with polypaudio; if not, write to the Free Software
18   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19   USA.
20 ***/
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <assert.h>
27 #include <stdlib.h>
28
29 #include "protocol-cli.h"
30 #include "cli.h"
31 #include "xmalloc.h"
32
33 struct pa_protocol_cli {
34     struct pa_module *module;
35     struct pa_core *core;
36     struct pa_socket_server*server;
37     struct pa_idxset *connections;
38 };
39
40 static void cli_eof_cb(struct pa_cli*c, void*userdata) {
41     struct pa_protocol_cli *p = userdata;
42     assert(p);
43     pa_idxset_remove_by_data(p->connections, c, NULL);
44     pa_cli_free(c);
45 }
46
47 static void on_connection(struct pa_socket_server*s, struct pa_iochannel *io, void *userdata) {
48     struct pa_protocol_cli *p = userdata;
49     struct pa_cli *c;
50     assert(s && io && p);
51
52     c = pa_cli_new(p->core, io, p->module);
53     assert(c);
54     pa_cli_set_eof_callback(c, cli_eof_cb, p);
55
56     pa_idxset_put(p->connections, c, NULL);
57 }
58
59 struct pa_protocol_cli* pa_protocol_cli_new(struct pa_core *core, struct pa_socket_server *server, struct pa_module *m, struct pa_modargs *ma) {
60     struct pa_protocol_cli* p;
61     assert(core && server);
62
63     p = pa_xmalloc(sizeof(struct pa_protocol_cli));
64     p->module = m;
65     p->core = core;
66     p->server = server;
67     p->connections = pa_idxset_new(NULL, NULL);
68
69     pa_socket_server_set_callback(p->server, on_connection, p);
70     
71     return p;
72 }
73
74 static void free_connection(void *p, void *userdata) {
75     assert(p);
76     pa_cli_free(p);
77 }
78
79 void pa_protocol_cli_free(struct pa_protocol_cli *p) {
80     assert(p);
81
82     pa_idxset_free(p->connections, free_connection, NULL);
83     pa_socket_server_unref(p->server);
84     pa_xfree(p);
85 }