command/commit

Started C-based client

authorJan Wielemaker
Thu Mar 10 14:30:16 2011 +0100
committerJan Wielemaker
Thu Mar 10 14:30:16 2011 +0100
commit5624439350e2cb659db1507c6e83a02c78dd83f8
tree9aee689dd9afd36275239010c565316a53a5614a
parent5db90c877a66bf12223b27a13083601396971257
Diff style: patch stat
diff --git a/api/command.pl b/api/command.pl
index eca6cd6..93ae579 100644
--- a/api/command.pl
+++ b/api/command.pl
@@ -43,6 +43,15 @@ http:location(cmd, root(cmd), []).
 
 :- http_handler(cmd(ping), ping, []).
 
+%%	ping(+Request)
+%
+%	HTTP handler to test the connection.  If ok, the server responds
+%	with the single line
+%
+%	  ==
+%	  alive
+%	  ==
+
 ping(_Request) :-
 	format('Content-type: text/plain\n\n'),
 	format('alive\n').
diff --git a/script/Makefile b/script/Makefile
new file mode 100644
index 0000000..4da51f2
--- /dev/null
+++ b/script/Makefile
@@ -0,0 +1,7 @@
+CC=gcc
+CFLAGS=$(shell curl-config --cflags) -gdwarf-2 -g3
+LIBS=$(shell curl-config --libs)
+
+
+cp-client: cp-client.c
+	$(CC) -o $@ $(CFLAGS) $< $(LIBS)
diff --git a/script/cp-client.c b/script/cp-client.c
new file mode 100644
index 0000000..b193c7d
--- /dev/null
+++ b/script/cp-client.c
@@ -0,0 +1,154 @@
+/*  $Id$
+
+    Part of ClioPatria
+
+    Author:        Jan Wielemaker
+    E-mail:        J.Wielemaker@cs.vu.nl
+    WWW:           http://www.swi-prolog.org
+    Copyright (C): 2011, VU University Amsterdam
+
+    This library is free software; you can redistribute it and/or
+    modify it under the terms of the GNU Lesser General Public
+    License as published by the Free Software Foundation; either
+    version 2.1 of the License, or (at your option) any later version.
+
+    This library is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+    Lesser General Public License for more details.
+
+    You should have received a copy of the GNU Lesser General Public
+    License along with this library; if not, write to the Free Software
+    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+*/
+
+#include <curl/curl.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+char *program = NULL;
+char *prefix  = "";
+char *host    = "localhost";
+int   port    = 3020;
+CURL *curl    = NULL;
+
+#define FALSE 0
+#define TRUE 1
+
+static int
+isoption(const char *arg, const char *name, const char **val)
+{ size_t len = strlen(name);
+
+  if ( arg[0] == '-' && arg[1] == '-' &&
+       strncmp(arg, name, len) == 0 &&
+       arg[2+len] == '=' )
+  { *val = &arg[3+len];
+    return TRUE;
+  }
+
+  return FALSE;
+}
+
+
+char *
+file_base_name(const char *f)
+{ const char *base;
+
+  for(base = f; *f; f++)
+  { if (*f == '/')
+      base = f+1;
+  }
+
+  return (char *)base;
+}
+
+
+static int
+usage(int code)
+{ fprintf(stderr, "Usage: %s [option ...] command [arg ...]\n\n", program);
+  fprintf(stderr, "Options:\n");
+  fprintf(stderr, "  --host=host      Specify ClioPatria host (localhost)\n");
+  fprintf(stderr, "  --port=port      Specify ClioPatria port (3020)\n");
+  fprintf(stderr, "\n");
+  fprintf(stderr, "Commands:\n");
+  fprintf(stderr, "  ping             Check that server is alive\n");
+
+  exit(code);
+}
+
+
+const char *
+cmd_url(const char *cmd)
+{ static char url[1024];
+
+  snprintf(url, sizeof(url),
+	   "http://%s:%d%s/cmd/%s\n", host, port, prefix, cmd);
+
+  return url;
+}
+
+
+
+		 /*******************************
+		 *	      PING		*
+		 *******************************/
+
+static int
+ping()
+{ CURLcode rc;
+
+  curl_easy_setopt(curl, CURLOPT_URL, cmd_url("ping"));
+  rc = curl_easy_perform(curl);
+
+  if ( rc )
+    fprintf(stderr, "%s: %s\n", program, curl_easy_strerror(rc));
+
+  return rc;
+}
+
+
+		 /*******************************
+		 *	      MAIN		*
+		 *******************************/
+
+int
+main(int argc, char **argv)
+{ int i;
+  int rc;
+  const char *command;
+
+  program = file_base_name(argv[0]);
+  for(i=1; i<argc; i++)
+  { const char *s = argv[i];
+    const char *val;
+
+    if ( isoption(s, "host", &val) )
+      host = (char*)val;
+    else if ( isoption(s, "port", &val) )
+      port = atoi(val);
+    else if ( s[0] == '-' )
+      usage(1);
+    else
+      break;
+  }
+
+  if ( i == argc )
+    usage(1);
+
+  if ( !(curl = curl_easy_init()) )
+  { perror("curl");
+    exit(1);
+  }
+
+  command = argv[i];
+  if ( strcmp(command, "ping") == 0 )
+    rc = ping();
+  else
+    usage(1);
+
+  curl_easy_cleanup(curl);
+  curl = NULL;
+
+  return 0;
+}