Fix assertion for value range on empty slot
[xapian.git] / xapian-core / bin / xapian-progsrv.cc
blob872f6b323c4ac8c2b93cf5021920dd5bf91b23e4
1 /** @file xapian-progsrv.cc
2 * @brief Remote server for use with ProgClient.
3 */
4 /* Copyright (C) 2002,2003,2006,2007,2008,2010,2011 Olly Betts
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21 #include <config.h>
23 #include "net/remoteserver.h"
25 #include "gnu_getopt.h"
27 #include <cstdlib>
28 #include <iostream>
29 #include <string>
31 using namespace std;
33 #define PROG_NAME "xapian-progsrv"
34 #define PROG_DESC "Piped server for use with Xapian's remote backend"
36 #define OPT_HELP 1
37 #define OPT_VERSION 2
39 static const char * opts = "t:w";
40 static const struct option long_opts[] = {
41 {"timeout", required_argument, 0, 't'},
42 {"writable", no_argument, 0, 'w'},
43 {"help", no_argument, 0, OPT_HELP},
44 {"version", no_argument, 0, OPT_VERSION},
45 {NULL, 0, 0, 0}
48 static void show_usage() {
49 cout << "Usage: " PROG_NAME " [OPTIONS] DATABASE_DIRECTORY...\n\n"
50 "Options:\n"
51 " --timeout MSECS set timeout\n"
52 " --writable allow updates (only one database directory allowed)\n"
53 " --help display this help and exit\n"
54 " --version output version information and exit" << endl;
57 int main(int argc, char **argv)
59 double timeout = 60.0;
60 bool writable = false;
61 bool syntax_error = false;
63 int c;
64 while ((c = gnu_getopt_long(argc, argv, opts, long_opts, NULL)) != -1) {
65 switch (c) {
66 case OPT_HELP:
67 cout << PROG_NAME " - " PROG_DESC "\n\n";
68 show_usage();
69 exit(0);
70 case OPT_VERSION:
71 cout << PROG_NAME " - " PACKAGE_STRING << endl;
72 exit(0);
73 case 't':
74 timeout = atoi(optarg) * 1e-3;
75 break;
76 case 'w':
77 writable = true;
78 break;
79 default:
80 syntax_error = true;
84 if (syntax_error || optind == argc) {
85 show_usage();
86 exit(1);
89 if (writable && (argc - optind) != 1) {
90 cerr << "Error: only one database directory allowed with '--writable'."
91 << endl;
92 exit(1);
95 /* Unlike xapian-tcpsrv, xapian-progsrv only has a single 'connection'
96 * which is established immediately. So, there is no point in attempting
97 * to open the database(s) to check they work - if they can't be opened the
98 * client will get an exception right away anyway.
100 vector<string> dbnames(argv + optind, argv + argc);
102 try {
103 // We communicate with the client via stdin (fd 0) and stdout (fd 1).
104 // Note that RemoteServer closes these fds.
105 RemoteServer server(dbnames, 0, 1, timeout, timeout, writable);
107 // If you have defined your own weighting scheme, register it here
108 // like so:
109 // server.register_weighting_scheme(FooWeight());
111 server.run();
112 } catch (...) {
113 /* Catch and ignore any exceptions thrown by RemoteServer, since the
114 * RemoteServer will have passed the error to the client to be rethrown
115 * there.
117 * Our stdout is the (now closed) communication channel to the client,
118 * and our stderr is probably a closed fd so we don't have anywhere to
119 * send error messages to anyway!