cli protocol

git-svn-id: file:///home/lennart/svn/public/pulseaudio/trunk@28 fefdeb5f-60dc-0310-8127-8f9354f1896f
This commit is contained in:
Lennart Poettering 2004-06-19 19:27:47 +00:00
parent 6eddcc2f85
commit 81447ed392
7 changed files with 94 additions and 8 deletions

View file

@ -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);

View 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;
}

View 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

View 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);

View 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);
}

59
src/protocol-cli.c Normal file
View file

@ -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);
}

12
src/protocol-cli.h Normal file
View file

@ -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