xapian-core  1.4.26
xapian-progsrv.cc
Go to the documentation of this file.
1 
4 /* Copyright (C) 2002,2003,2006,2007,2008,2010,2011,2023 Olly Betts
5  *
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.
10  *
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.
15  *
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
19  */
20 
21 #include <config.h>
22 
23 #include "net/remoteserver.h"
24 
25 #include "gnu_getopt.h"
26 
27 #include <cstdlib>
28 #include <iostream>
29 #include <string>
30 
31 using namespace std;
32 
33 #define PROG_NAME "xapian-progsrv"
34 #define PROG_DESC "Piped server for use with Xapian's remote backend"
35 
36 #define OPT_HELP 1
37 #define OPT_VERSION 2
38 
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}
46 };
47 
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\n"
53 " --help display this help and exit\n"
54 " --version output version information and exit\n";
55 }
56 
57 int main(int argc, char **argv)
58 {
59  double timeout = 60.0;
60  bool writable = false;
61  bool syntax_error = false;
62 
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 "\n";
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;
81  }
82  }
83 
84  if (syntax_error || optind == argc) {
85  show_usage();
86  exit(1);
87  }
88 
89  /* Unlike xapian-tcpsrv, xapian-progsrv only has a single 'connection'
90  * which is established immediately. So, there is no point in attempting
91  * to open the database(s) to check they work - if they can't be opened the
92  * client will get an exception right away anyway.
93  */
94  vector<string> dbnames(argv + optind, argv + argc);
95 
96  try {
97  // We communicate with the client via stdin (fd 0) and stdout (fd 1).
98  // Note that RemoteServer closes these fds.
99  RemoteServer server(dbnames, 0, 1, timeout, timeout, writable);
100 
101  // If you have defined your own weighting scheme, register it here
102  // like so:
103  // server.register_weighting_scheme(FooWeight());
104 
105  server.run();
106  } catch (...) {
107  /* Catch and ignore any exceptions thrown by RemoteServer, since the
108  * RemoteServer will have passed the error to the client to be rethrown
109  * there.
110  *
111  * Our stdout is the (now closed) communication channel to the client,
112  * and our stderr is probably a closed fd so we don't have anywhere to
113  * send error messages to anyway!
114  */
115  }
116 }
Wrappers to allow GNU getopt to be used cleanly from C++ code.
int optind
Definition: getopt.cc:94
#define PROG_DESC
unsigned timeout
A timeout value in milliseconds.
Definition: types.h:100
int gnu_getopt_long(int argc_, char *const *argv_, const char *shortopts_, const struct option *longopts_, int *optind_)
Definition: gnu_getopt.h:97
static const char * opts
static const char * dbnames
STL namespace.
#define OPT_HELP
#define no_argument
Definition: gnu_getopt.h:79
void run()
Repeatedly accept messages from the client and process them.
int main(int argc, char **argv)
char * optarg
Definition: getopt.cc:79
Xapian remote backend server base class.
#define required_argument
Definition: gnu_getopt.h:80
#define PROG_NAME
static void show_usage()
#define OPT_VERSION
#define PACKAGE_STRING
Definition: config.h:337
Remote backend server base class.
Definition: remoteserver.h:36
static const struct option long_opts[]