From 1c435b4adaf19fa6676c70db8c674a4c9c08b344 Mon Sep 17 00:00:00 2001
From: mh17 <mh17>
Date: Mon, 7 Dec 2009 12:53:42 +0000
Subject: [PATCH] replaced fileServer (code removed from make - needs
 runbootstrap and runconfig) by pipeServer option

---
 src/configure.ac                           |   1 -
 src/zmapServer/Makefile.am                 |   3 +-
 src/zmapServer/pipe/pipeServer.c           | 117 +++++++++++++--------
 src/zmapServer/pipe/pipeServer_P.h         |   7 +-
 src/zmapServer/zmapServer.c                |  11 +-
 src/zmapServer/zmapServerProtocolHandler.c |   9 +-
 src/zmapUtils/zmapConfigLoader.c           |   3 +-
 src/zmapView/zmapViewUtils.c               |   4 +-
 8 files changed, 97 insertions(+), 58 deletions(-)

diff --git a/src/configure.ac b/src/configure.ac
index 907b9b000..5f758ce3e 100755
--- a/src/configure.ac
+++ b/src/configure.ac
@@ -392,7 +392,6 @@ zmapManager/Makefile
 zmapServer/Makefile
 zmapServer/acedb/Makefile
 zmapServer/das/Makefile
-zmapServer/file/Makefile
 zmapServer/pipe/Makefile
 zmapThreads/Makefile
 zmapUtils/Makefile
diff --git a/src/zmapServer/Makefile.am b/src/zmapServer/Makefile.am
index 3f7125290..350108a53 100755
--- a/src/zmapServer/Makefile.am
+++ b/src/zmapServer/Makefile.am
@@ -1,7 +1,7 @@
 
 include $(top_srcdir)/Makefile.common
 
-SUBDIRS = acedb das file pipe .
+SUBDIRS = acedb das pipe
 
 noinst_LTLIBRARIES = libZMapServer.la libZMapServers.la
 
@@ -18,7 +18,6 @@ libZMapServer_la_LDFLAGS =
 libZMapServers_la_SOURCES = 
 libZMapServers_la_LIBADD  = \
 acedb/libAcedbServer.la \
-file/libFileServer.la   \
 pipe/libPipeServer.la   \
 das/libDasServer.la     \
 $(NULL)
diff --git a/src/zmapServer/pipe/pipeServer.c b/src/zmapServer/pipe/pipeServer.c
index 7cbfb4346..0f7dee628 100755
--- a/src/zmapServer/pipe/pipeServer.c
+++ b/src/zmapServer/pipe/pipeServer.c
@@ -34,7 +34,7 @@
  * HISTORY:
  * Last edited: Nov 30 09:18 2009 (edgrif)
  * Created: 2009-11-26 12:02:40 (mh17)
- * CVS info:   $Id: pipeServer.c,v 1.6 2009-12-07 10:48:31 mh17 Exp $
+ * CVS info:   $Id: pipeServer.c,v 1.7 2009-12-07 12:53:42 mh17 Exp $
  *-------------------------------------------------------------------
  */
 
@@ -136,36 +136,44 @@ void pipeGetServerFuncs(ZMapServerFuncs pipe_funcs)
 static gboolean globalInit(void)
 {
   gboolean result = TRUE ;
-
-
-
   return result ;
 }
 
 
 
 /* Read ZMap application defaults. */
-static gboolean getConfiguration(PipeServer server)
+static void getConfiguration(PipeServer server)
 {
-  gboolean result = FALSE ;
   ZMapConfigIniContext context;
 
   if((context = zMapConfigIniContextProvide()))
     {
       char *tmp_string  = NULL;
 
-      /* script directory to use */
+      /* default script directory to use */
       if(zMapConfigIniContextGetString(context, ZMAPSTANZA_APP_CONFIG, ZMAPSTANZA_APP_CONFIG, 
 				       ZMAPSTANZA_APP_SCRIPTS, &tmp_string))
 	{
 	  server->script_dir = tmp_string;
-        result = TRUE;
 	}
+      else
+      {
+        server->script_dir = g_get_current_dir();
+      }
+
+       /* default directory to use */
+      if(zMapConfigIniContextGetString(context, ZMAPSTANZA_APP_CONFIG, ZMAPSTANZA_APP_CONFIG, 
+                               ZMAPSTANZA_APP_DATA, &tmp_string))
+      {
+        server->data_dir = tmp_string;
+      }
+      else
+      {
+        server->data_dir = g_get_current_dir();
+      }
+
       zMapConfigIniContextDestroy(context);
     }
-  if(!result)
-    server->script_dir = g_get_current_dir();
-  return result ;
 }
 
 
@@ -184,6 +192,7 @@ static gboolean createConnection(void **server_out,
 {
   gboolean result = TRUE ;
   PipeServer server ;
+  gchar *dir;
 
   zMapAssert(url->path) ;
 
@@ -193,6 +202,8 @@ static gboolean createConnection(void **server_out,
 
   getConfiguration(server);	// get scripts directory
 
+  server->scheme = url->scheme;
+  dir = (server->scheme == SCHEME_PIPE) ? server->script_dir : server->data_dir;
   /* We could check both format and version here if we wanted to..... */
 
 
@@ -203,7 +214,7 @@ static gboolean createConnection(void **server_out,
     char *tmp_path = url->path ;
 
     if (*(url->path) != '/')
-      tmp_path = g_strdup_printf("%s/%s", server->script_dir,url->path) ;
+      tmp_path = g_strdup_printf("%s/%s", dir,url->path) ;
 
     server->script_path = zMapGetPath(tmp_path) ;
 
@@ -211,7 +222,8 @@ static gboolean createConnection(void **server_out,
       g_free(tmp_path) ;
   }
 
-  server->query = url->query;
+
+  server->query = g_strdup(url->query);
   server->zmap_start = 0;
   server->zmap_end = 0; // default to all of it
 
@@ -289,16 +301,22 @@ gchar *pipe_server_get_stderr(PipeServer server)
   GError *gff_pipe_err = NULL;
   gchar * msg = NULL;
   GString *line;
-//  GIOCondition gc;
-  
+
+  if(!server->gff_stderr)
+      return(NULL);
+
   line = g_string_sized_new(2000) ;      /* Probably not many lines will be > 2k chars. */
 
   while (1)
     {
-// this does'nt work: doesn't hang, but doesn't read the data anyway
-//      gc = g_io_channel_get_buffer_condition(server->gff_stderr);
-//      if(!(gc & G_IO_IN))
-//            break;
+/* this doesn't work: doesn't hang, but doesn't read the error message anyway
+ * there is a race condition on big files (i think) where on notsupported errors we look at stderr
+ * and wait for ever as the script didn't finish yet as we didn't read the data.
+ *      gc = g_io_channel_get_buffer_condition(server->gff_stderr);
+ *      GIOCondition gc;
+ *      if(!(gc & G_IO_IN))
+ *            break;
+ */
       status = g_io_channel_read_line_string(server->gff_stderr, line,
             &terminator_pos,&gff_pipe_err);
       if(status != G_IO_STATUS_NORMAL)
@@ -320,6 +338,7 @@ static ZMapServerResponseType openConnection(void *server_in)
 {
   ZMapServerResponseType result = ZMAP_SERVERRESPONSE_REQFAIL ;
   PipeServer server = (PipeServer)server_in ;
+  int retval;
 
   if (server->gff_pipe)
     {
@@ -329,16 +348,20 @@ static ZMapServerResponseType openConnection(void *server_in)
   else
     {
       GError *gff_pipe_err = NULL ;
-
-      if(pipe_server_spawn(server,&gff_pipe_err))
-	    {
-	      result = ZMAP_SERVERRESPONSE_OK ;
-	    }
-	    else
-	    {
-	      setLastErrorMsg(server, &gff_pipe_err) ;
-	      result = ZMAP_SERVERRESPONSE_REQFAIL ;
-	    }
+      
+      if(server->scheme == SCHEME_FILE)   // could spawn /bin/cat but there is no need
+        retval = (gboolean) (server->gff_pipe = g_io_channel_new_file(server->script_path, "r", &gff_pipe_err));
+      else
+        retval = pipe_server_spawn(server,&gff_pipe_err);
+      if(retval)
+	  {
+	    result = ZMAP_SERVERRESPONSE_OK ;
+	  }
+	  else
+	  {
+	    setLastErrorMsg(server, &gff_pipe_err) ;
+	    result = ZMAP_SERVERRESPONSE_REQFAIL ;
+	  }
     }
 
   return result ;
@@ -795,22 +818,26 @@ static ZMapServerResponseType closeConnection(void *server_in)
   if(server->child_pid)
     g_spawn_close_pid(server->child_pid);
 
-  if (server->gff_pipe && g_io_channel_shutdown(server->gff_pipe, FALSE, &gff_pipe_err) != G_IO_STATUS_NORMAL)
-    {
-      zMapLogCritical("Could not close feature pipe \"%s\"", server->script_path) ;
-
-      setLastErrorMsg(server, &gff_pipe_err) ;
+  if (server->gff_pipe)
+  {
+    if(g_io_channel_shutdown(server->gff_pipe, FALSE, &gff_pipe_err) != G_IO_STATUS_NORMAL)
+      {
+        zMapLogCritical("Could not close feature pipe \"%s\"", server->script_path) ;
 
-      result = ZMAP_SERVERRESPONSE_REQFAIL ;
-    }
-  else
-    {
-      /* this seems to be required to destroy the GIOChannel.... */
-      g_io_channel_unref(server->gff_pipe) ;
-      server->gff_pipe = NULL ;
-    }
+        setLastErrorMsg(server, &gff_pipe_err) ;
 
-  if (server->gff_stderr && g_io_channel_shutdown(server->gff_stderr, FALSE, &gff_pipe_err) != G_IO_STATUS_NORMAL)
+        result = ZMAP_SERVERRESPONSE_REQFAIL ;
+      }
+    else
+      {
+        /* this seems to be required to destroy the GIOChannel.... */
+        g_io_channel_unref(server->gff_pipe) ;
+        server->gff_pipe = NULL ;
+      }
+  }
+  if (server->gff_stderr )
+  {
+    if(g_io_channel_shutdown(server->gff_stderr, FALSE, &gff_pipe_err) != G_IO_STATUS_NORMAL)
     {
       zMapLogCritical("Could not close error pipe \"%s\"", server->script_path) ;
 
@@ -818,13 +845,13 @@ static ZMapServerResponseType closeConnection(void *server_in)
 
       result = ZMAP_SERVERRESPONSE_REQFAIL ;
     }
-  else
+  else 
     {
       /* this seems to be required to destroy the GIOChannel.... */
       g_io_channel_unref(server->gff_stderr) ;
       server->gff_stderr = NULL ;
     }
-
+  }
   return result ;
 }
 
diff --git a/src/zmapServer/pipe/pipeServer_P.h b/src/zmapServer/pipe/pipeServer_P.h
index 29abbe5a8..badd39830 100755
--- a/src/zmapServer/pipe/pipeServer_P.h
+++ b/src/zmapServer/pipe/pipeServer_P.h
@@ -28,7 +28,7 @@
  *              
  * HISTORY:
  * Created: Thu Nov 26 10:30:21 2009 (mh17)
- * CVS info:   $Id: pipeServer_P.h,v 1.4 2009-12-07 10:48:31 mh17 Exp $
+ * CVS info:   $Id: pipeServer_P.h,v 1.5 2009-12-07 12:53:42 mh17 Exp $
  *-------------------------------------------------------------------
  */
 #ifndef PIPE_SERVER_P_H
@@ -48,13 +48,16 @@
 typedef struct _PipeServerStruct
 {
   gchar *script_dir;		// default location for relative paths
-  gchar *script_path ;	      // where our configured script is, includign script-dir
+  gchar *script_path ;	      // where our configured script is, including script-dir
   gchar *query;		    	// from query string
   GIOChannel *gff_pipe ;      // the pipe we read the script's output from
   GIOChannel *gff_stderr ;    // the pipe we read the script's error output from
   GPid child_pid;
   gint zmap_start,zmap_end;   // display coordinates of interesting region
   gint wait;                  // delay before gettign data, mainly for testing
+  
+  ZMapURLScheme scheme;       // pipe:// or file://
+  gchar *data_dir;            // default location for data files (when protocol is file://)
 
   char *styles_file ;
 
diff --git a/src/zmapServer/zmapServer.c b/src/zmapServer/zmapServer.c
index 5734f5370..f1a5b3492 100755
--- a/src/zmapServer/zmapServer.c
+++ b/src/zmapServer/zmapServer.c
@@ -28,7 +28,7 @@
  * HISTORY:
  * Last edited: 2009-11-26 12:57:05 (mgh)
  * Created: Wed Aug  6 15:46:38 2003 (edgrif)
- * CVS info:   $Id: zmapServer.c,v 1.40 2009-11-26 15:40:18 mh17 Exp $
+ * CVS info:   $Id: zmapServer.c,v 1.41 2009-12-07 12:53:42 mh17 Exp $
  *-------------------------------------------------------------------
  */
 
@@ -69,12 +69,13 @@ gboolean zMapServerGlobalInit(ZMapURL url, void **server_global_data_out)
     /* if(strcasecmp(format, 'das') == 0) */
     dasGetServerFuncs(serverfuncs);
     break;
-  case SCHEME_FILE:
+  case SCHEME_FILE:     // DAS only: file gets handled by pipe
     if(url->params)
+    {
       dasGetServerFuncs(serverfuncs);
-    else
-      fileGetServerFuncs(serverfuncs);
-    break;
+      break;
+    }
+    // fall through for real files
   case SCHEME_PIPE:
     pipeGetServerFuncs(serverfuncs);
     break;
diff --git a/src/zmapServer/zmapServerProtocolHandler.c b/src/zmapServer/zmapServerProtocolHandler.c
index 787d1a264..9c068635a 100755
--- a/src/zmapServer/zmapServerProtocolHandler.c
+++ b/src/zmapServer/zmapServerProtocolHandler.c
@@ -27,7 +27,7 @@
  * HISTORY:
  * Last edited: Nov 26 08:26 2009 (edgrif)
  * Created: Thu Jan 27 13:17:43 2005 (edgrif)
- * CVS info:   $Id: zmapServerProtocolHandler.c,v 1.48 2009-12-03 15:03:08 mh17 Exp $
+ * CVS info:   $Id: zmapServerProtocolHandler.c,v 1.49 2009-12-07 12:53:42 mh17 Exp $
  *-------------------------------------------------------------------
  */
 
@@ -328,6 +328,10 @@ ZMapThreadReturnCode zMapServerRequestHandler(void **slave_data,
   if (*slave_data)
     server = (ZMapServer)*slave_data ;
 
+#ifdef MH_NEVER_INCLUDE_THIS_CODE
+if(*slave_data) zMapLogMessage("req %s/%s %d",server->url->protocol,server->url->query,request->type);
+#endif
+
   switch (request->type)
     {
     case ZMAP_SERVERREQ_CREATE:
@@ -502,6 +506,9 @@ ZMapThreadReturnCode zMapServerRequestHandler(void **slave_data,
 
   /* Return server. */
   *slave_data = (void *)server ;
+#ifdef MH_NEVER_INCLUDE_THIS_CODE
+if(*slave_data) zMapLogMessage("req %s/%s req %d returns %d",server->url->protocol,server->url->query,request->type,thread_rc);
+#endif
 
   return thread_rc ;
 }
diff --git a/src/zmapUtils/zmapConfigLoader.c b/src/zmapUtils/zmapConfigLoader.c
index 88995f548..185db2f04 100755
--- a/src/zmapUtils/zmapConfigLoader.c
+++ b/src/zmapUtils/zmapConfigLoader.c
@@ -31,7 +31,7 @@
  * HISTORY:
  * Last edited: Nov  6 13:34 2009 (edgrif)
  * Created: Thu Sep 25 14:12:05 2008 (rds)
- * CVS info:   $Id: zmapConfigLoader.c,v 1.13 2009-12-03 14:58:29 mh17 Exp $
+ * CVS info:   $Id: zmapConfigLoader.c,v 1.14 2009-12-07 12:53:42 mh17 Exp $
  *-------------------------------------------------------------------
  */
 
@@ -202,6 +202,7 @@ static ZMapConfigIniContextKeyEntry get_app_group_data(char **stanza_name, char
     { ZMAPSTANZA_APP_PFETCH_MODE,  G_TYPE_STRING,  NULL, FALSE },
     { ZMAPSTANZA_APP_PFETCH_LOCATION, G_TYPE_STRING, NULL, FALSE },
     { ZMAPSTANZA_APP_SCRIPTS,      G_TYPE_STRING, NULL, FALSE },
+    { ZMAPSTANZA_APP_DATA,         G_TYPE_STRING, NULL, FALSE },
     {NULL}
   };
   static char *name = ZMAPSTANZA_APP_CONFIG;
diff --git a/src/zmapView/zmapViewUtils.c b/src/zmapView/zmapViewUtils.c
index f0051d43c..3715c355f 100755
--- a/src/zmapView/zmapViewUtils.c
+++ b/src/zmapView/zmapViewUtils.c
@@ -29,7 +29,7 @@
  * HISTORY:
  * Last edited: Oct  1 15:42 2009 (edgrif)
  * Created: Mon Sep 20 10:29:15 2004 (edgrif)
- * CVS info:   $Id: zmapViewUtils.c,v 1.15 2009-12-03 15:06:40 mh17 Exp $
+ * CVS info:   $Id: zmapViewUtils.c,v 1.16 2009-12-07 12:53:42 mh17 Exp $
  *-------------------------------------------------------------------
  */
 
@@ -609,6 +609,8 @@ void zmapViewSessionAddServer(ZMapViewSession session_data, ZMapURL url, char *f
     
     case SCHEME_FILE:
       {
+      // mgh: file:// is now handled by pipe://, but as this is a view struct it is unchanged
+      // consider also DAS, which is still known as a file://
 	server_data->scheme_data.file.path = g_strdup(url->path) ;
 	break ;
       }
-- 
GitLab