1
0
mirror of git://sourceware.org/git/lvm2.git synced 2025-01-24 06:04:19 +03:00

Implement daemon_send_simple and use it in the testclient.

This commit is contained in:
Petr Rockai 2011-06-27 13:15:49 +00:00
parent 0d7965aeca
commit 0783429215
4 changed files with 49 additions and 3 deletions

View File

@ -2,12 +2,15 @@
#include <stdio.h>
#include <malloc.h>
#include <string.h>
#include "daemon-shared.h"
/*
* Read a single message from a (socket) filedescriptor. Messages are delimited
* by blank lines. This call will block until all of a message is received. The
* memory will be allocated from heap. Upon error, all memory is freed and the
* buffer pointer is set to NULL.
*
* See also write_buffer about blocking (read_buffer has identical behaviour).
*/
int read_buffer(int fd, char **buffer) {
int bytes = 0;
@ -66,3 +69,43 @@ int write_buffer(int fd, char *buffer, int length) {
}
return 0;
}
char *format_buffer(char *id, va_list ap)
{
char *buffer, *old;
char *next;
char *format;
dm_asprintf(&buffer, "request = \"%s\"\n", id);
if (!buffer) goto fail;
while (next = va_arg(ap, char *)) {
old = buffer;
if (strstr(next, "%d") || strstr(next, "%s")) {
dm_asprintf(&format, "%%s%s\n", next);
if (!format) goto fail;
if (strstr(format, "%d"))
dm_asprintf(&buffer, format, buffer, va_arg(ap, int));
else
dm_asprintf(&buffer, format, buffer, va_arg(ap, char *));
dm_free(format);
dm_free(old);
if (!buffer) goto fail;
} else {
dm_asprintf(&buffer, "%s%s", buffer, next);
dm_free(old);
if (!buffer) goto fail;
}
}
old = buffer;
dm_asprintf(&buffer, "%s\n", buffer);
dm_free(old);
return buffer;
fail:
dm_free(buffer);
return NULL;
}

View File

@ -1,2 +1,6 @@
#include <stdarg.h>
#include <libdevmapper.h>
int read_buffer(int fd, char **buffer);
int write_buffer(int fd, char *buffer, int length);
char *format_buffer(char *id, va_list ap);

View File

@ -7,7 +7,7 @@ typedef struct {
static response handler(daemon_state s, client_handle h, request r)
{
response res;
fprintf(stderr, "handling client request: %s\n", r.buffer);
fprintf(stderr, "---- server obtained:\n%s\n----------------------\n", r.buffer);
res.error = 1;
res.buffer = strdup("hey hey.\n\n");
return res;

View File

@ -2,10 +2,9 @@
int main() {
daemon_handle h = lvmetad_open();
daemon_request rq = { .buffer= "hello worldn\n" };
int i;
for (i = 0; i < 5; ++i ) {
daemon_reply reply = daemon_send(h, rq);
daemon_reply reply = daemon_send_simple(h, "hello world", "param = %d", 3, NULL);
fprintf(stderr, "daemon says: %s\n", reply.buffer);
}
daemon_close(h);