bin/xapian-progsrv.cc

Go to the documentation of this file.
00001 
00004 /* Copyright (C) 2002,2003,2006,2007,2008 Olly Betts
00005  *
00006  * This program is free software; you can redistribute it and/or modify
00007  * it under the terms of the GNU General Public License as published by
00008  * the Free Software Foundation; either version 2 of the License, or
00009  * (at your option) any later version.
00010  *
00011  * This program is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  * GNU General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU General Public License
00017  * along with this program; if not, write to the Free Software
00018  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
00019  */
00020 
00021 #include <config.h>
00022 
00023 #include "remoteserver.h"
00024 
00025 #include "gnu_getopt.h"
00026 
00027 #include <cstdlib>
00028 #include <iostream>
00029 #include <string>
00030 
00031 using namespace std;
00032 
00033 #define PROG_NAME "xapian-progsrv"
00034 #define PROG_DESC "Piped server for use with Xapian's remote backend"
00035 
00036 #define OPT_HELP 1
00037 #define OPT_VERSION 2
00038 
00039 static const char * opts = "t:w";
00040 static const struct option long_opts[] = {
00041     {"timeout",         required_argument,      0, 't'},
00042     {"writable",        no_argument,            0, 'w'},
00043     {"help",            no_argument,            0, OPT_HELP},
00044     {"version",         no_argument,            0, OPT_VERSION},
00045     {NULL, 0, 0, 0}
00046 };
00047 
00048 static void show_usage() {
00049     cout << "Usage: "PROG_NAME" [OPTIONS] DATABASE_DIRECTORY...\n\n"
00050 "Options:\n"
00051 "  --timeout MSECS         set timeout\n"
00052 "  --writable              allow updates (only one database directory allowed)\n"
00053 "  --help                  display this help and exit\n"
00054 "  --version               output version information and exit" << endl;
00055 }
00056 
00057 int main(int argc, char **argv)
00058 {
00059     unsigned int timeout = 60000;
00060     bool writable = false;
00061     bool syntax_error = false;
00062 
00063     int c;
00064     while ((c = gnu_getopt_long(argc, argv, opts, long_opts, NULL)) != -1) {
00065         switch (c) {
00066             case OPT_HELP:
00067                 cout << PROG_NAME" - "PROG_DESC"\n\n";
00068                 show_usage();
00069                 exit(0);
00070             case OPT_VERSION:
00071                 cout << PROG_NAME" - "PACKAGE_STRING << endl;
00072                 exit(0);
00073             case 't':
00074                 timeout = atoi(optarg);
00075                 break;
00076             case 'w':
00077                 writable = true;
00078                 break;
00079             default:
00080                 syntax_error = true;
00081         }
00082     }
00083 
00084     if (syntax_error || optind == argc) {
00085         show_usage();
00086         exit(1);
00087     }
00088 
00089     if (writable && (argc - optind) != 1) {
00090         cerr << "Error: only one database directory allowed with '--writable'."
00091              << endl;
00092         exit(1);
00093     }
00094 
00095     /* Unlike xapian-tcpsrv, xapian-progsrv only has a single 'connection'
00096      * which is established immediately.  So, there is no point in attempting
00097      * to open the database(s) to check they work - if they can't be opened the
00098      * client get an exception right away anyway.
00099      */
00100     vector<string> dbnames(argv + optind, argv + argc);
00101 
00102     try {
00103         // We communicate with the client via stdin (fd 0) and stdout (fd 1).
00104         // Note that RemoteServer closes these fds.
00105         RemoteServer server(dbnames, 0, 1, timeout, timeout, writable);
00106 
00107         // If you have defined your own weighting scheme, register it here
00108         // like so:
00109         // server.register_weighting_scheme(FooWeight());
00110 
00111         server.run();
00112     } catch (...) {
00113         /* Catch and ignore any exceptions thrown by RemoteServer, since the
00114          * RemoteServer will have passed the error to the client to be rethrown
00115          * there.
00116          *
00117          * Our stdout is the (now closed) communication channel to the client,
00118          * and our stderr is probably a closed fd so we don't have anywhere to
00119          * send error messages to anyway!
00120          */
00121     }
00122 }

Documentation for Xapian (version 1.0.10).
Generated on 24 Dec 2008 by Doxygen 1.5.2.