Update of /cvsroot/monetdb/sql/src/backends/monet5/merovingian
In directory 23jxhf1.ch3.sourceforge.com:/tmp/cvs-serv11591

Modified Files:
        control.c control.h monetdb.c 
Log Message:
add a waiting mode for control_send which is extremely useful with multi-line 
responses

U control.h
Index: control.h
===================================================================
RCS file: /cvsroot/monetdb/sql/src/backends/monet5/merovingian/control.h,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -d -r1.1 -r1.2
--- control.h   27 Aug 2009 15:14:52 -0000      1.1
+++ control.h   18 Sep 2009 11:16:12 -0000      1.2
@@ -25,6 +25,7 @@
                char* host,
                int port,
                char* database,
-               char* command);
+               char* command,
+               char wait);
 
 #endif

U control.c
Index: control.c
===================================================================
RCS file: /cvsroot/monetdb/sql/src/backends/monet5/merovingian/control.c,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -d -r1.2 -r1.3
--- control.c   9 Sep 2009 11:46:27 -0000       1.2
+++ control.c   18 Sep 2009 11:16:12 -0000      1.3
@@ -19,6 +19,7 @@
 
 #include "sql_config.h"
 #include <stdio.h>
+#include <stdlib.h> /* malloc, realloc */
 #include <unistd.h> /* close */
 #include <string.h> /* strerror */
 #include <sys/socket.h> /* socket */
@@ -32,7 +33,11 @@
 /* Sends command for database to merovingian listening at host and port.
  * If host is a path, and port is 0, a UNIX socket connection for host
  * is opened.  The response of merovingian is returned as a malloced
- * string.
+ * string.  If wait is set to a non-zero value, this function will only
+ * return after it has seen an EOF from the server.  This is useful with
+ * multi-line responses, but can lock up for single line responses where
+ * the server allows pipelining (and hence doesn't close the
+ * connection).
  * TODO: implement TCP connect
  */
 char* control_send(
@@ -40,9 +45,11 @@
                char* host,
                int port,
                char* database,
-               char* command)
+               char* command,
+               char wait)
 {
-       char buf[8096];
+       char sbuf[8096];
+       char *buf;
        int sock = -1;
        struct sockaddr_un server;
        size_t len;
@@ -50,26 +57,50 @@
        (void)port;
        /* UNIX socket connect */
        if ((sock = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) {
-               snprintf(buf, sizeof(buf), "cannot open connection: %s\n",
+               snprintf(sbuf, sizeof(sbuf), "cannot open connection: %s",
                                strerror(errno));
-               return(strdup(buf));
+               return(strdup(sbuf));
        }
        memset(&server, 0, sizeof(struct sockaddr_un));
        server.sun_family = AF_UNIX;
        strncpy(server.sun_path, host, sizeof(server.sun_path) - 1);
        if (connect(sock, (SOCKPTR) &server, sizeof(struct sockaddr_un))) {
-               snprintf(buf, sizeof(buf), "cannot connect: %s\n", 
strerror(errno));
-               return(strdup(buf));
+               snprintf(sbuf, sizeof(sbuf), "cannot connect: %s", 
strerror(errno));
+               return(strdup(sbuf));
        }
 
-       len = snprintf(buf, sizeof(buf), "%s %s\n", database, command);
-       send(sock, buf, len, 0);
-       if ((len = recv(sock, buf, sizeof(buf), 0)) <= 0)
-               return(strdup("no response from merovingian\n"));
-       buf[len == 0 ? 0 : len - 1] = '\0';
+       len = snprintf(sbuf, sizeof(sbuf), "%s %s\n", database, command);
+       send(sock, sbuf, len, 0);
+       if (wait != 0) {
+               size_t buflen = sizeof(sbuf);
+               size_t bufpos = 0;
+               char *bufp;
+               bufp = buf = malloc(sizeof(char) * buflen);
+               if (buf == NULL)
+                       return(strdup("failed to allocate memory"));
+               while ((len = recv(sock, buf + bufpos, buflen - bufpos, 0)) > 
0) {
+                       if (len == buflen - bufpos) {
+                               bufp = realloc(buf, sizeof(char) * buflen * 2);
+                               if (bufp == NULL) {
+                                       free(buf);
+                                       return(strdup("failed to allocate more 
memory"));
+                               }
+                               buf = bufp;
+                       }
+                       bufpos += len;
+               }
+               if (bufpos == 0)
+                       return(strdup("no response from merovingian"));
+               buf[bufpos - 1] = '\0';
+               *ret = buf;
+       } else {
+               if ((len = recv(sock, sbuf, sizeof(sbuf), 0)) <= 0)
+                       return(strdup("no response from merovingian"));
+               sbuf[len - 1] = '\0';
+               *ret = strdup(sbuf);
+       }
 
        close(sock);
 
-       *ret = strdup(buf);
        return(NULL);
 }

U monetdb.c
Index: monetdb.c
===================================================================
RCS file: /cvsroot/monetdb/sql/src/backends/monet5/merovingian/monetdb.c,v
retrieving revision 1.53
retrieving revision 1.54
diff -u -d -r1.53 -r1.54
--- monetdb.c   16 Sep 2009 12:17:26 -0000      1.53
+++ monetdb.c   18 Sep 2009 11:16:12 -0000      1.54
@@ -452,7 +452,7 @@
                char *p;
                sabdb *w = NULL;
 
-               e = control_send(&buf, mero_control, -1, "flyghende", 
"hollander");
+               e = control_send(&buf, mero_control, -1, "flyghende", 
"hollander", 1);
                if (e != NULL) {
                        fprintf(stderr, "status: internal error: %s\n", e);
                        free(e);
@@ -617,7 +617,7 @@
         * merovingian.  Anelosimus eximius is a social species of spiders,
         * which help each other, just like merovingians do among each
         * other. */
-       p = control_send(&buf, mero_control, -1, "anelosimus", "eximius");
+       p = control_send(&buf, mero_control, -1, "anelosimus", "eximius", 1);
        if (p != NULL) {
                printf("FAILED:\n%s\n", p);
                free(p);
@@ -783,7 +783,7 @@
                        if (stats->state == SABdbRunning) {
                                printf("%s%sing database '%s'... ", type, mode 
== STOP ? "p" : "", stats->dbname);
                                fflush(stdout);
-                               out = control_send(&res, mero_control, 0, 
stats->dbname, type);
+                               out = control_send(&res, mero_control, 0, 
stats->dbname, type, 0);
                                if (out == NULL && strcmp(res, "OK") == 0) {
                                        printf("done\n");
                                } else {
@@ -799,7 +799,7 @@
                        if (stats->state != SABdbRunning) {
                                printf("starting database '%s'... ", 
stats->dbname);
                                fflush(stdout);
-                               out = control_send(&res, mero_control, 0, 
stats->dbname, type);
+                               out = control_send(&res, mero_control, 0, 
stats->dbname, type, 0);
                                if (out == NULL && strcmp(res, "OK") == 0) {
                                        printf("done\n");
                                } else {
@@ -918,7 +918,7 @@
                        char *res;
                        char *out;
 
-                       out = control_send(&res, mero_control, -1, argv[2], p);
+                       out = control_send(&res, mero_control, -1, argv[2], p, 
0);
                        if (out != NULL || strcmp(res, "OK") != 0) {
                                res = out == NULL ? res : out;
                                fprintf(stderr, "%s: %s\n", argv[0], res);
@@ -964,7 +964,7 @@
                                strncat(property, "=", sizeof(property));
                                p = property;
                        }
-                       out = control_send(&res, mero_control, 0, argv[i], p);
+                       out = control_send(&res, mero_control, 0, argv[i], p, 
0);
                        if (out != NULL || strcmp(res, "OK") != 0) {
                                res = out == NULL ? res : out;
                                fprintf(stderr, "%s: %s\n", argv[0], res);
@@ -1017,7 +1017,7 @@
                        ret = db_create(argv[i]);
                } else {
                        char *out;
-                       ret = control_send(&out, mero_control, -1, argv[i], 
"create");
+                       ret = control_send(&out, mero_control, -1, argv[i], 
"create", 0);
                        if (ret == NULL && strcmp(out, "OK") != 0)
                                ret = out;
                }
@@ -1103,7 +1103,7 @@
                        ret = db_destroy(argv[i]);
                } else {
                        char *out;
-                       ret = control_send(&out, mero_control, -1, argv[i], 
"destroy");
+                       ret = control_send(&out, mero_control, -1, argv[i], 
"destroy", 0);
                        if (ret == NULL && strcmp(out, "OK") != 0)
                                ret = out;
                }
@@ -1155,7 +1155,7 @@
                        char *res;
                        char *out;
 
-                       out = control_send(&res, mero_control, -1, argv[i], 
argv[0]);
+                       out = control_send(&res, mero_control, -1, argv[i], 
argv[0], 0);
                        if (out != NULL || strcmp(res, "OK") != 0) {
                                res = out == NULL ? res : out;
                                fprintf(stderr, "%s: %s\n", argv[0], res);
@@ -1204,7 +1204,7 @@
                        char *res;
                        char *out;
 
-                       out = control_send(&res, mero_control, -1, argv[i], 
argv[0]);
+                       out = control_send(&res, mero_control, -1, argv[i], 
argv[0], 0);
                        if (out != NULL || strcmp(res, "OK") != 0) {
                                res = out == NULL ? res : out;
                                fprintf(stderr, "%s: %s\n", argv[0], res);


------------------------------------------------------------------------------
Come build with us! The BlackBerry&reg; Developer Conference in SF, CA
is the only developer event you need to attend this year. Jumpstart your
developing skills, take BlackBerry mobile applications to market and stay 
ahead of the curve. Join us from November 9&#45;12, 2009. Register now&#33;
http://p.sf.net/sfu/devconf
_______________________________________________
Monetdb-sql-checkins mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/monetdb-sql-checkins

Reply via email to