]> code.delx.au - pulseaudio/commitdiff
cli protocol
authorLennart Poettering <lennart@poettering.net>
Sat, 19 Jun 2004 19:27:47 +0000 (19:27 +0000)
committerLennart Poettering <lennart@poettering.net>
Sat, 19 Jun 2004 19:27:47 +0000 (19:27 +0000)
git-svn-id: file:///home/lennart/svn/public/pulseaudio/trunk@28 fefdeb5f-60dc-0310-8127-8f9354f1896f

src/cli.c
src/iochannel.c
src/iochannel.h
src/module-cli.c
src/module-protocol-stub.c
src/protocol-cli.c [new file with mode: 0644]
src/protocol-cli.h [new file with mode: 0644]

index 4f67f8b722744ba26dd130037e8c206444c4e643..ec484ace3284d7e752a3d98bae84a4ca9242b37a 100644 (file)
--- a/src/cli.c
+++ b/src/cli.c
@@ -111,7 +111,7 @@ static void line_callback(struct ioline *line, const char *s, void *userdata) {
         l = strcspn(s, delimiter);
 
         for (command = commands; command->name; command++) 
-            if (!strncmp(s, command->name, l)) {
+            if (strlen(command->name) == l && !strncmp(s, command->name, l)) {
                 struct tokenizer *t = tokenizer_new(s, command->args);
                 assert(t);
                 command->proc(c, t);
index 2044d561561a6dc9aa89f84e69997b6ae5248151..f0c4c4998b7fb1420bfd183b869cf90b9554da67 100644 (file)
@@ -15,6 +15,8 @@ struct iochannel {
     int readable;
     int writable;
 
+    int no_close;
+
     struct mainloop_source* input_source, *output_source;
 };
 
@@ -83,6 +85,7 @@ struct iochannel* iochannel_new(struct mainloop*m, int ifd, int ofd) {
     io->callback = NULL;
     io->readable = 0;
     io->writable = 0;
+    io->no_close = 0;
 
     if (ifd == ofd) {
         assert(ifd >= 0);
@@ -109,10 +112,12 @@ struct iochannel* iochannel_new(struct mainloop*m, int ifd, int ofd) {
 void iochannel_free(struct iochannel*io) {
     assert(io);
 
-    if (io->ifd >= 0)
-        close(io->ifd);
-    if (io->ofd >= 0 && io->ofd != io->ifd)
-        close(io->ofd);
+    if (!io->no_close) {
+        if (io->ifd >= 0)
+            close(io->ifd);
+        if (io->ofd >= 0 && io->ofd != io->ifd)
+            close(io->ofd);
+    }
 
     if (io->input_source)
         mainloop_source_free(io->input_source);
@@ -162,3 +167,8 @@ void iochannel_set_callback(struct iochannel*io, void (*callback)(struct iochann
     io->callback = callback;
     io->userdata = userdata;
 }
+
+void iochannel_set_noclose(struct iochannel*io, int b) {
+    assert(io);
+    io->no_close = b;
+}
index f97fabbab02cadd2be02c0b6544db633abcc624f..8ed8b87899b55545c2154284c1eacca8f5cdc1cc 100644 (file)
@@ -15,6 +15,8 @@ ssize_t iochannel_read(struct iochannel*io, void*data, size_t l);
 int iochannel_is_readable(struct iochannel*io);
 int iochannel_is_writable(struct iochannel*io);
 
+void iochannel_set_noclose(struct iochannel*io, int b);
+
 void iochannel_set_callback(struct iochannel*io, void (*callback)(struct iochannel*io, void *userdata), void *userdata);
 
 #endif
index 4af37f67b4307a6804cfef35d61be631169b37a6..883f4f53b2bac505e6f70ab4fb0adf5b6615176b 100644 (file)
@@ -19,6 +19,7 @@ int module_init(struct core *c, struct module*m) {
     stdin_inuse = stdout_inuse = 1;
     io = iochannel_new(c->mainloop, STDIN_FILENO, STDOUT_FILENO);
     assert(io);
+    iochannel_set_noclose(io, 1);
 
     m->userdata = cli_new(c, io);
     assert(m->userdata);
index 905594c6136ee04d337a593df7499eacd1662179..9cbf236ec038169820678cc485102220be334dda 100644 (file)
@@ -6,12 +6,14 @@
 
 #ifdef USE_PROTOCOL_SIMPLE
   #include "protocol-simple.h"
-  #define protocol_free protcol_simple_free
+  #define protocol_free protocol_simple_free
+  #define IPV4_PORT 4712
 #else
   #ifdef USE_PROTOCOL_CLI
     #include "protocol-cli.h" 
     #define protocol_new protocol_cli_new
     #define protocol_free protocol_cli_free
+    #define IPV4_PORT 4711
   #else
     #error "Broken build system"
   #endif
@@ -22,7 +24,7 @@ int module_init(struct core *c, struct module*m) {
     assert(c && m);
 
 #ifdef USE_TCP_SOCKETS
-    if (!(s = socket_server_new_ipv4(c->mainloop, INADDR_LOOPBACK, 4712)))
+    if (!(s = socket_server_new_ipv4(c->mainloop, INADDR_LOOPBACK, IPV4_PORT)))
         return -1;
 #else
     if (!(s = socket_server_new_unix(c->mainloop, "/tmp/polypsimple")))
@@ -42,5 +44,5 @@ int module_init(struct core *c, struct module*m) {
 void module_done(struct core *c, struct module*m) {
     assert(c && m);
 
-    protocol_simple_free(m->userdata);
+    protocol_free(m->userdata);
 }
diff --git a/src/protocol-cli.c b/src/protocol-cli.c
new file mode 100644 (file)
index 0000000..c0c93d9
--- /dev/null
@@ -0,0 +1,59 @@
+#include <assert.h>
+#include <stdlib.h>
+
+#include "protocol-cli.h"
+#include "cli.h"
+
+struct protocol_cli {
+    struct core *core;
+    struct socket_server*server;
+    struct idxset *connections;
+};
+
+static void cli_eof_cb(struct cli*c, void*userdata) {
+    struct protocol_cli *p = userdata;
+    assert(c && p);
+
+    idxset_remove_by_data(p->connections, c, NULL);
+    cli_free(c);
+}
+
+static void on_connection(struct socket_server*s, struct iochannel *io, void *userdata) {
+    struct protocol_cli *p = userdata;
+    struct cli *c;
+    assert(s && io && p);
+    
+    c = cli_new(p->core, io);
+    assert(c);
+    cli_set_eof_callback(c, cli_eof_cb, p);
+
+    idxset_put(p->connections, c, NULL);
+}
+
+struct protocol_cli* protocol_cli_new(struct core *core, struct socket_server *server) {
+    struct protocol_cli* p;
+    assert(core && server);
+
+    p = malloc(sizeof(struct protocol_cli));
+    assert(p);
+    p->core = core;
+    p->server = server;
+    p->connections = idxset_new(NULL, NULL);
+
+    socket_server_set_callback(p->server, on_connection, p);
+    
+    return p;
+}
+
+static void free_connection(void *p, void *userdata) {
+    assert(p);
+    cli_free(p);
+}
+
+void protocol_cli_free(struct protocol_cli *p) {
+    assert(p);
+
+    idxset_free(p->connections, free_connection, NULL);
+    socket_server_free(p->server);
+    free(p);
+}
diff --git a/src/protocol-cli.h b/src/protocol-cli.h
new file mode 100644 (file)
index 0000000..8c150ce
--- /dev/null
@@ -0,0 +1,12 @@
+#ifndef fooprotocolclihfoo
+#define fooprotocolclihfoo
+
+#include "core.h"
+#include "socket-server.h"
+
+struct protocol_cli;
+
+struct protocol_cli* protocol_cli_new(struct core *core, struct socket_server *server);
+void protocol_cli_free(struct protocol_cli *n);
+
+#endif