home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / info-service / gopher / Unix / gopher+1.2b4 / gopherd / mindexd.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-02-11  |  5.0 KB  |  204 lines

  1. /********************************************************************
  2.  * lindner
  3.  * 3.1.1.1
  4.  * 1993/02/11 18:02:56
  5.  * /home/mudhoney/GopherSrc/CVS/gopher+/gopherd/mindexd.c,v
  6.  * Exp
  7.  *
  8.  * Paul Lindner, University of Minnesota CIS.
  9.  *
  10.  * Copyright 1991, 1992 by the Regents of the University of Minnesota
  11.  * see the file "Copyright" in the distribution for conditions of use.
  12.  *********************************************************************
  13.  * MODULE: mindexd.c
  14.  * Routines to implement a fanout search server
  15.  *********************************************************************
  16.  * Revision History:
  17.  * mindexd.c,v
  18.  * Revision 3.1.1.1  1993/02/11  18:02:56  lindner
  19.  * Gopher+1.2beta release
  20.  *
  21.  * Revision 1.1  1992/12/29  23:27:21  lindner
  22.  * Initial revision
  23.  *
  24.  *
  25.  *********************************************************************/
  26.  
  27. #include "gopherd.h"
  28.  
  29. #define MAXSLAVES 64    
  30.  
  31. struct one_slave {
  32.    int  port;
  33.    char host[128];
  34.    char pathname[512];
  35.    int  sockfd;
  36.    int  pid;
  37. };
  38.  
  39. struct one_slave slaves[MAXSLAVES];
  40.  
  41. int LastSlave;
  42.  
  43.  
  44. /*  read the config file and fill the slaves struct, returns
  45.     the number of slave servers */
  46. int
  47. getconfig( the_filename )
  48.   char *the_filename;
  49. {
  50.     char *cp;
  51.     int  n;
  52.     char *local;
  53.     int i;
  54.     FILE *fp;
  55.     char    line[1024];      /* line buffer for reading config */
  56.     char    *linep;         /* pointer to 'line' */
  57.  
  58.  
  59.     local = the_filename;
  60.  
  61.     if ((fp = rfopen(local, "r")) == NULL) {
  62.             exit(1);
  63.     }
  64.  
  65.     LastSlave = -1;
  66.     for (;;) {
  67.         if (fgets(line, sizeof line, fp) == NULL)
  68.                break;  /* done */
  69.  
  70.         if ((i = strlen(line)))
  71.                line[i-1] = '\0';  /* remove trailing newline */
  72.         if (line[0] == '#' || line[0] == '\0')
  73.                continue;       /* skip comment lines */
  74.         else {
  75.          char *pathpart;
  76.  
  77.                LastSlave++;
  78.                linep = line;
  79.                /* the port number is seperated from the host name by a space */
  80.            /* The path is everything after the port */
  81.          
  82.             pathpart = strchr(strchr(linep, ' ')+1, ' ');
  83.            *pathpart = '\0';
  84.            pathpart++;
  85.            cp = strchr(linep, ' ');
  86.                slaves[LastSlave].port = atoi(cp+1);
  87.                *cp = '\0'; /* trim off stuff after the hostname */
  88.                cp = slaves[LastSlave].host;
  89.                strcpy(cp, linep);
  90.            cp = slaves[LastSlave].pathname;
  91.            if (pathpart != NULL) 
  92.             strcpy(cp, pathpart);
  93.            else
  94.             cp = "";
  95.         }
  96.     }
  97.     close(fp);
  98.  
  99.     return( LastSlave );
  100. }
  101.  
  102.  
  103. /* take the query the client passed us, and send it on to the slave index
  104.    servers, gather up their responses and return them to our client 
  105.  
  106.   still need to add code to handle the case where a slave dies on us
  107. */
  108. void
  109. HandleQuery(sockfd, queryline )
  110.   int  sockfd;
  111.   char *queryline;
  112. {
  113.      char      answerline[512];
  114.      int       length_answer;
  115.      int       i;
  116.      int       childpid;
  117.      GopherObj *gs;
  118.  
  119.      gs = GSnew();
  120.  
  121.      if (DEBUG)
  122.       printf("Number slaves is %d, query is %s\n", LastSlave, queryline);
  123.  
  124.      /* send queries to all the slaves and gather up responces*/
  125.      for (i = 0; i < ( LastSlave + 1); i++ ) {
  126.  
  127.         /* fork child processes to handle each of the slaves */
  128.       if (DEBUG)
  129.            printf("Forking...\n");
  130.  
  131.         if ( (childpid = fork()) < 0)
  132.             err_dump("server: fork error");
  133.  
  134.         else if (childpid == 0) {     /* Child process */
  135.          GSsetType(gs, '7');
  136.          GSsetTitle(gs,"");
  137.          GSsetHost(gs, slaves[i].host);
  138.          GSsetPort(gs, slaves[i].port);
  139.          GSsetPath(gs, slaves[i].pathname);
  140.  
  141.  
  142.          slaves[i].sockfd = GSconnect(gs);
  143.          if (DEBUG) {
  144.           printf("value of sockfd: %d\n", slaves[i].sockfd);
  145.           fflush(stdout);
  146.          }
  147.         writestring(slaves[i].sockfd, slaves[i].pathname);
  148.         writestring(slaves[i].sockfd, "\t");
  149.             writestring(slaves[i].sockfd, queryline );
  150.             writestring(slaves[i].sockfd, "\r\n");
  151.               for(;;) {
  152.                    length_answer = readline(slaves[i].sockfd, answerline, 512);
  153.                    if (length_answer > 0) {
  154.                         if (answerline[0] == '.') {
  155.                  close(slaves[i].sockfd);
  156.                  exit(0);
  157.                         }
  158.                         else {
  159.                            writestring(sockfd, answerline);
  160.                         }
  161.                    }
  162.               }
  163.         } else {  /** Parent process **/
  164.          slaves[i].pid = childpid;
  165.     }
  166.          
  167.  
  168.      }
  169.  
  170.      /* make sure all the children are done */
  171.      while (wait((int * ) 0) != -1) 
  172.             ;
  173.  
  174.      /* all done now, tell the client we are finished */
  175.      writestring(sockfd, ".\r\n");
  176.  
  177. }
  178.  
  179.  
  180.  
  181. int
  182. do_mindexd(sockfd, config_filename, search)
  183.   int sockfd;
  184.   char *config_filename;
  185.   char *search;
  186. {
  187.      char inputline[MAXLINE];
  188.      char *cp;
  189.      int length;        /* Length of the command line */
  190.      char out_line[MAXLINE];    /* for outgoing messages */
  191.      FILE *foofile;
  192.  
  193.  
  194.      if ((LastSlave = getconfig(config_filename)) == -1) 
  195.          err_dump("can't read config file" );
  196.      
  197.      HandleQuery(sockfd, search);
  198.      close(sockfd); 
  199.      return(0);
  200. }
  201.  
  202.  
  203.  
  204.