Browse Source

Implement konf_client_recv_data() instead receive_data()

git-svn-id: https://klish.googlecode.com/svn/trunk@219 0eaa4687-2ee9-07dd-09d9-bcdd2d2dd5fb
Serj Kalichev 13 years ago
parent
commit
b4cde206fa
4 changed files with 45 additions and 78 deletions
  1. 3 40
      bin/konf.c
  2. 1 38
      clish/clish_config_callback.c
  3. 3 0
      konf/net.h
  4. 38 0
      konf/net/net.c

+ 3 - 40
bin/konf.c

@@ -24,11 +24,10 @@
 #define VER_MIN 2
 #define VER_BUG 2
 
+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 receive_data(konf_client_t * client, konf_buf_t *buf, konf_buf_t **data);
 static int process_answer(konf_client_t * client, char *str, konf_buf_t *buf, konf_buf_t **data);
-static void help(int status, const char *argv0);
-static void version(void);
 
 static const char *escape_chars = "\"\\'";
 
@@ -175,42 +174,6 @@ static int receive_answer(konf_client_t * client, konf_buf_t **data)
 	return retval;
 }
 
-/*--------------------------------------------------------- */
-static int receive_data(konf_client_t * client, konf_buf_t *buf, konf_buf_t **data)
-{
-	konf_buf_t *tmpdata;
-	char *str;
-	int processed = 0;
-
-	if ((konf_client_connect(client) < 0))
-		return -1;
-
-	tmpdata = konf_buf_new(konf_client__get_sock(client));
-	do {
-		while ((str = konf_buf_parse(buf))) {
-#ifdef DEBUG
-			fprintf(stderr, "RECV DATA: [%s]\n", str);
-#endif
-			konf_buf_add(tmpdata, str, strlen(str) + 1);
-			if (strlen(str) == 0) {
-				processed = 1;
-				lub_string_free(str);
-				break;
-			}
-			lub_string_free(str);
-		}
-	} while ((!processed) && (konf_buf_read(buf)) > 0);
-	if (!processed) {
-		konf_buf_delete(tmpdata);
-		*data = NULL;
-		return -1;
-	}
-
-	*data = tmpdata;
-
-	return 0;
-}
-
 /*--------------------------------------------------------- */
 static int process_answer(konf_client_t * client, char *str, konf_buf_t *buf, konf_buf_t **data)
 {
@@ -245,7 +208,7 @@ static int process_answer(konf_client_t * client, char *str, konf_buf_t *buf, ko
 		break;
 
 	case KONF_QUERY_OP_STREAM:
-		if (receive_data(client, buf, data) < 0)
+		if (!(*data = konf_client_recv_data(client, buf)))
 			res = -1;
 		else
 			res = 1; /* wait for another answer */

+ 1 - 38
clish/clish_config_callback.c

@@ -22,7 +22,6 @@
 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);
-static int receive_data(konf_client_t * client, konf_buf_t *buf, konf_buf_t **data);
 
 /*--------------------------------------------------------- */
 bool_t
@@ -225,42 +224,6 @@ static int receive_answer(konf_client_t * client, konf_buf_t **data)
 	return retval;
 }
 
-static int receive_data(konf_client_t * client, konf_buf_t *buf, konf_buf_t **data)
-{
-	konf_buf_t *tmpdata;
-	char *str;
-	int retval = 0;
-	int processed = 0;
-
-	if ((konf_client_connect(client) < 0))
-		return -1;
-
-	tmpdata = konf_buf_new(konf_client__get_sock(client));
-	do {
-		while ((str = konf_buf_parse(buf))) {
-#ifdef DEBUG
-			fprintf(stderr, "RECV DATA: [%s]\n", str);
-#endif
-			konf_buf_add(tmpdata, str, strlen(str) + 1);
-			if (strlen(str) == 0) {
-				processed = 1;
-				lub_string_free(str);
-				break;
-			}
-			lub_string_free(str);
-		}
-	} while ((!processed) && (konf_buf_read(buf)) > 0);
-	if (!processed) {
-		konf_buf_delete(tmpdata);
-		*data = NULL;
-		return -1;
-	}
-
-	*data = tmpdata;
-
-	return 0;
-}
-
 static int process_answer(konf_client_t * client, char *str, konf_buf_t *buf, konf_buf_t **data)
 {
 	int res;
@@ -293,7 +256,7 @@ static int process_answer(konf_client_t * client, char *str, konf_buf_t *buf, ko
 		break;
 
 	case KONF_QUERY_OP_STREAM:
-		if (receive_data(client, buf, data) < 0)
+		if (!(*data = konf_client_recv_data(client, buf)))
 			res = -1;
 		else
 			res = 1; /* wait for another answer */

+ 3 - 0
konf/net.h

@@ -1,6 +1,8 @@
 #ifndef _konf_net_h
 #define _konf_net_h
 
+#include <konf/buf.h>
+
 typedef struct konf_client_s konf_client_t;
 
 #define KONFD_SOCKET_PATH "/tmp/konfd.socket"
@@ -12,5 +14,6 @@ void konf_client_disconnect(konf_client_t *client);
 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);
 
 #endif

+ 38 - 0
konf/net/net.c

@@ -17,6 +17,7 @@
 #define UNIX_PATH_MAX 108
 #endif
 
+/*--------------------------------------------------------- */
 konf_client_t *konf_client_new(const char *path)
 {
 	konf_client_t *client;
@@ -33,6 +34,7 @@ konf_client_t *konf_client_new(const char *path)
 	return client;
 }
 
+/*--------------------------------------------------------- */
 void konf_client_free(konf_client_t *client)
 {
 	if (!client)
@@ -44,6 +46,7 @@ void konf_client_free(konf_client_t *client)
 	free(client);
 }
 
+/*--------------------------------------------------------- */
 int konf_client_connect(konf_client_t *client)
 {
 	struct sockaddr_un raddr;
@@ -65,6 +68,7 @@ int konf_client_connect(konf_client_t *client)
 	return client->sock;
 }
 
+/*--------------------------------------------------------- */
 void konf_client_disconnect(konf_client_t *client)
 {
 	if (client->sock >= 0) {
@@ -73,12 +77,14 @@ void konf_client_disconnect(konf_client_t *client)
 	}
 }
 
+/*--------------------------------------------------------- */
 int konf_client_reconnect(konf_client_t *client)
 {
 	konf_client_disconnect(client);
 	return konf_client_connect(client);
 }
 
+/*--------------------------------------------------------- */
 int konf_client_send(konf_client_t *client, char *command)
 {
 	if (client->sock < 0)
@@ -87,7 +93,39 @@ int konf_client_send(konf_client_t *client, char *command)
 	return send(client->sock, command, strlen(command) + 1, MSG_NOSIGNAL);
 }
 
+/*--------------------------------------------------------- */
 int konf_client__get_sock(konf_client_t *client)
 {
 	return client->sock;
 }
+
+/*--------------------------------------------------------- */
+konf_buf_t * konf_client_recv_data(konf_client_t * this, konf_buf_t *buf)
+{
+	int processed = 0;
+	konf_buf_t *data;
+	char *str;
+
+	/* Check if socked is connected */
+	if ((konf_client_connect(this) < 0))
+		return NULL;
+
+	data = konf_buf_new(konf_client__get_sock(this));
+	do {
+		while ((str = konf_buf_parse(buf))) {
+			konf_buf_add(data, str, strlen(str) + 1);
+			if (strlen(str) == 0) {
+				processed = 1;
+				lub_string_free(str);
+				break;
+			}
+			lub_string_free(str);
+		}
+	} while ((!processed) && (konf_buf_read(buf)) > 0);
+	if (!processed) {
+		konf_buf_delete(data);
+		return NULL;
+	}
+
+	return data;
+}