Browse Source

Move receive_answer() to konf/net.h with name konf_client_recv_answer().

git-svn-id: https://klish.googlecode.com/svn/trunk@227 0eaa4687-2ee9-07dd-09d9-bcdd2d2dd5fb
Serj Kalichev 13 years ago
parent
commit
1d13ee420a
4 changed files with 129 additions and 214 deletions
  1. 1 90
      bin/konf.c
  2. 2 89
      clish/clish_config_callback.c
  3. 1 0
      konf/net.h
  4. 125 35
      konf/net/net.c

+ 1 - 90
bin/konf.c

@@ -26,8 +26,6 @@
 
 static void version(void);
 static void help(int status, const char *argv0);
-static int receive_answer(konf_client_t * client, konf_buf_t **data);
-static int process_answer(konf_client_t * client, char *str, konf_buf_t *buf, konf_buf_t **data);
 
 static const char *escape_chars = "\"\\'";
 
@@ -113,7 +111,7 @@ int main(int argc, char **argv)
 		goto err;
 	}
 
-	if (receive_answer(client, &buf) < 0) {
+	if (konf_client_recv_answer(client, &buf) < 0) {
 		fprintf(stderr, "Can't get answer from the config daemon.\n");
 	}
 
@@ -138,93 +136,6 @@ err:
 	return res;
 }
 
-/*--------------------------------------------------------- */
-static int receive_answer(konf_client_t * client, konf_buf_t **data)
-{
-	konf_buf_t *buf;
-	int nbytes;
-	char *str;
-	int retval = 0;
-	int processed = 0;
-
-	if ((konf_client_connect(client) < 0))
-		return -1;
-
-	buf = konf_buf_new(konf_client__get_sock(client));
-	while ((!processed) && (nbytes = konf_buf_read(buf)) > 0) {
-		while ((str = konf_buf_parse(buf))) {
-			konf_buf_t *tmpdata = NULL;
-			retval = process_answer(client, str, buf, &tmpdata);
-			lub_string_free(str);
-			if (retval < 0) {
-				konf_buf_delete(buf);
-				return retval;
-			}
-			if (retval == 0)
-				processed = 1;
-			if (tmpdata) {
-				if (*data)
-					konf_buf_delete(*data);
-				*data = tmpdata;
-			}
-		}
-	}
-	konf_buf_delete(buf);
-
-	return retval;
-}
-
-/*--------------------------------------------------------- */
-static int process_answer(konf_client_t * client, char *str, konf_buf_t *buf, konf_buf_t **data)
-{
-	int res;
-	konf_query_t *query;
-
-	/* Parse query */
-	query = konf_query_new();
-	res = konf_query_parse_str(query, str);
-	if (res < 0) {
-		konf_query_free(query);
-#ifdef DEBUG
-		fprintf(stderr, "CONFIG error: Cannot parse answer string.\n");
-#endif
-		return -1;
-	}
-
-#ifdef DEBUG
-	fprintf(stderr, "ANSWER: %s\n", str);
-/*	konf_query_dump(query);
-*/
-#endif
-
-	switch (konf_query__get_op(query)) {
-
-	case KONF_QUERY_OP_OK:
-		res = 0;
-		break;
-
-	case KONF_QUERY_OP_ERROR:
-		res = -1;
-		break;
-
-	case KONF_QUERY_OP_STREAM:
-		if (!(*data = konf_client_recv_data(client, buf)))
-			res = -1;
-		else
-			res = 1; /* wait for another answer */
-		break;
-
-	default:
-		res = -1;
-		break;
-	}
-
-	/* Free resources */
-	konf_query_free(query);
-
-	return res;
-}
-
 /*--------------------------------------------------------- */
 /* Print help message */
 static void help(int status, const char *argv0)

+ 2 - 89
clish/clish_config_callback.c

@@ -20,12 +20,9 @@
 #include "clish/variable.h"
 
 static int send_request(konf_client_t * client, char *command);
-static int receive_answer(konf_client_t * client, konf_buf_t **data);
-static int process_answer(konf_client_t * client, char *str, konf_buf_t *buf, konf_buf_t **data);
 
 /*--------------------------------------------------------- */
-bool_t
-clish_config_callback(const clish_shell_t * this,
+bool_t clish_config_callback(const clish_shell_t * this,
 	const clish_command_t * cmd, clish_pargv_t * pargv)
 {
 	unsigned i;
@@ -141,7 +138,7 @@ clish_config_callback(const clish_shell_t * this,
 	if (send_request(client, command) < 0) {
 		fprintf(stderr, "Cannot write to the running-config.\n");
 	}
-	if (receive_answer(client, &buf) < 0) {
+	if (konf_client_recv_answer(client, &buf) < 0) {
 		fprintf(stderr, "Cannot get answer from config daemon.\n");
 	}
 	lub_string_free(command);
@@ -188,87 +185,3 @@ static int send_request(konf_client_t * client, char *command)
 
 	return 0;
 }
-
-static int receive_answer(konf_client_t * client, konf_buf_t **data)
-{
-	konf_buf_t *buf;
-	int nbytes;
-	char *str;
-	int retval = 0;
-	int processed = 0;
-
-	if ((konf_client_connect(client) < 0))
-		return -1;
-
-	buf = konf_buf_new(konf_client__get_sock(client));
-	while ((!processed) && (nbytes = konf_buf_read(buf)) > 0) {
-		while ((str = konf_buf_parse(buf))) {
-			konf_buf_t *tmpdata = NULL;
-			retval = process_answer(client, str, buf, &tmpdata);
-			lub_string_free(str);
-			if (retval < 0) {
-				konf_buf_delete(buf);
-				return retval;
-			}
-			if (retval == 0)
-				processed = 1;
-			if (tmpdata) {
-				if (*data)
-					konf_buf_delete(*data);
-				*data = tmpdata;
-			}
-		}
-	}
-	konf_buf_delete(buf);
-
-	return retval;
-}
-
-static int process_answer(konf_client_t * client, char *str, konf_buf_t *buf, konf_buf_t **data)
-{
-	int res;
-	konf_query_t *query;
-
-	/* Parse query */
-	query = konf_query_new();
-	res = konf_query_parse_str(query, str);
-	if (res < 0) {
-		konf_query_free(query);
-#ifdef DEBUG
-		fprintf(stderr, "CONFIG error: Cannot parse answer string.\n");
-#endif
-		return -1;
-	}
-
-#ifdef DEBUG
-	fprintf(stderr, "CONFIG answer: %s\n", str);
-	// konf_query_dump(query);
-#endif
-
-	switch (konf_query__get_op(query)) {
-
-	case KONF_QUERY_OP_OK:
-		res = 0;
-		break;
-
-	case KONF_QUERY_OP_ERROR:
-		res = -1;
-		break;
-
-	case KONF_QUERY_OP_STREAM:
-		if (!(*data = konf_client_recv_data(client, buf)))
-			res = -1;
-		else
-			res = 1; /* wait for another answer */
-		break;
-
-	default:
-		res = -1;
-		break;
-	}
-
-	/* Free resources */
-	konf_query_free(query);
-
-	return res;
-}

+ 1 - 0
konf/net.h

@@ -15,5 +15,6 @@ int konf_client_reconnect(konf_client_t *client);
 int konf_client_send(konf_client_t *client, char *command);
 int konf_client__get_sock(konf_client_t *client);
 konf_buf_t * konf_client_recv_data(konf_client_t * instance, konf_buf_t *buf);
+int konf_client_recv_answer(konf_client_t * instance, konf_buf_t **data);
 
 #endif

+ 125 - 35
konf/net/net.c

@@ -9,10 +9,12 @@
 #include <string.h>
 #include <sys/un.h>
 
-#include "clish/private.h"
+#include "konf/buf.h"
+#include "konf/query.h"
 #include "lub/string.h"
 #include "private.h"
 
+/* Socket name in filesystem */
 #ifndef UNIX_PATH_MAX
 #define UNIX_PATH_MAX 108
 #endif
@@ -20,83 +22,83 @@
 /*--------------------------------------------------------- */
 konf_client_t *konf_client_new(const char *path)
 {
-	konf_client_t *client;
+	konf_client_t *this;
 
 	if (!path)
 		return NULL;
 
-	if (!(client = malloc(sizeof(*client))))
+	if (!(this = malloc(sizeof(*this))))
 		return NULL;
 
-	client->sock = -1; /* socket is not created yet */
-	client->path = lub_string_dup(path);
+	this->sock = -1; /* socket is not created yet */
+	this->path = lub_string_dup(path);
 
-	return client;
+	return this;
 }
 
 /*--------------------------------------------------------- */
-void konf_client_free(konf_client_t *client)
+void konf_client_free(konf_client_t *this)
 {
-	if (!client)
+	if (!this)
 		return;
-	if (client->sock != -1)
-		konf_client_disconnect(client);
-	lub_string_free(client->path);
+	if (this->sock != -1)
+		konf_client_disconnect(this);
+	lub_string_free(this->path);
 
-	free(client);
+	free(this);
 }
 
 /*--------------------------------------------------------- */
-int konf_client_connect(konf_client_t *client)
+int konf_client_connect(konf_client_t *this)
 {
 	struct sockaddr_un raddr;
 
-	if (client->sock >= 0)
-		return client->sock;
+	if (this->sock >= 0)
+		return this->sock;
 
-	if ((client->sock = socket(AF_UNIX, SOCK_STREAM, 0)) < 0)
-		return client->sock;
+	if ((this->sock = socket(AF_UNIX, SOCK_STREAM, 0)) < 0)
+		return this->sock;
 
 	raddr.sun_family = AF_UNIX;
-	strncpy(raddr.sun_path, client->path, UNIX_PATH_MAX);
+	strncpy(raddr.sun_path, this->path, UNIX_PATH_MAX);
 	raddr.sun_path[UNIX_PATH_MAX - 1] = '\0';
-	if (connect(client->sock, (struct sockaddr *)&raddr, sizeof(raddr))) {
-		close(client->sock);
-		client->sock = -1;
+	if (connect(this->sock, (struct sockaddr *)&raddr, sizeof(raddr))) {
+		close(this->sock);
+		this->sock = -1;
 	}
 
-	return client->sock;
+	return this->sock;
 }
 
 /*--------------------------------------------------------- */
-void konf_client_disconnect(konf_client_t *client)
+void konf_client_disconnect(konf_client_t *this)
 {
-	if (client->sock >= 0) {
-		close(client->sock);
-		client->sock = -1;
+	if (this->sock >= 0) {
+		close(this->sock);
+		this->sock = -1;
 	}
 }
 
 /*--------------------------------------------------------- */
-int konf_client_reconnect(konf_client_t *client)
+int konf_client_reconnect(konf_client_t *this)
 {
-	konf_client_disconnect(client);
-	return konf_client_connect(client);
+	konf_client_disconnect(this);
+	return konf_client_connect(this);
 }
 
 /*--------------------------------------------------------- */
-int konf_client_send(konf_client_t *client, char *command)
+int konf_client_send(konf_client_t *this, char *command)
 {
-	if (client->sock < 0)
-		return client->sock;
+	if (this->sock < 0)
+		return this->sock;
 
-	return send(client->sock, command, strlen(command) + 1, MSG_NOSIGNAL);
+	return send(this->sock, command, strlen(command) + 1, MSG_NOSIGNAL);
 }
 
 /*--------------------------------------------------------- */
-int konf_client__get_sock(konf_client_t *client)
+int konf_client__get_sock(konf_client_t *this)
 {
-	return client->sock;
+	return this->sock;
 }
 
 /*--------------------------------------------------------- */
@@ -129,3 +131,91 @@ konf_buf_t * konf_client_recv_data(konf_client_t * this, konf_buf_t *buf)
 
 	return data;
 }
+
+/*--------------------------------------------------------- */
+static int process_answer(konf_client_t * this, char *str, konf_buf_t *buf, konf_buf_t **data)
+{
+	int res;
+	konf_query_t *query;
+
+	/* Parse query */
+	query = konf_query_new();
+	res = konf_query_parse_str(query, str);
+	if (res < 0) {
+		konf_query_free(query);
+#ifdef DEBUG
+		fprintf(stderr, "CONFIG error: Cannot parse answer string.\n");
+#endif
+		return -1;
+	}
+
+#ifdef DEBUG
+	fprintf(stderr, "ANSWER: %s\n", str);
+/*	konf_query_dump(query);
+*/
+#endif
+
+	switch (konf_query__get_op(query)) {
+
+	case KONF_QUERY_OP_OK:
+		res = 0;
+		break;
+
+	case KONF_QUERY_OP_ERROR:
+		res = -1;
+		break;
+
+	case KONF_QUERY_OP_STREAM:
+		if (!(*data = konf_client_recv_data(this, buf)))
+			res = -1;
+		else
+			res = 1; /* wait for another answer */
+		break;
+
+	default:
+		res = -1;
+		break;
+	}
+
+	/* Free resources */
+	konf_query_free(query);
+
+	return res;
+}
+
+/*--------------------------------------------------------- */
+int konf_client_recv_answer(konf_client_t * this, konf_buf_t **data)
+{
+	konf_buf_t *buf;
+	int nbytes;
+	char *str;
+	int retval = 0;
+	int processed = 0;
+
+	if ((konf_client_connect(this) < 0))
+		return -1;
+
+	buf = konf_buf_new(konf_client__get_sock(this));
+	while ((!processed) && (nbytes = konf_buf_read(buf)) > 0) {
+		while ((str = konf_buf_parse(buf))) {
+			konf_buf_t *tmpdata = NULL;
+			retval = process_answer(this, str, buf, &tmpdata);
+			lub_string_free(str);
+			if (retval < 0) {
+				konf_buf_delete(buf);
+				return retval;
+			}
+			if (retval == 0)
+				processed = 1;
+			if (tmpdata) {
+				if (*data)
+					konf_buf_delete(*data);
+				*data = tmpdata;
+			}
+		}
+	}
+	konf_buf_delete(buf);
+
+	return retval;
+}
+