xapian-core  2.0.0
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, see
18  * <https://www.gnu.org/licenses/>.
19  */
20 
21 #include <config.h>
22 
23 #include "net/remoteserver.h"
24 
25 #include "gnu_getopt.h"
26 #include "parseint.h"
27 
28 #include <cstdlib>
29 #include <iostream>
30 #include <string>
31 
32 using namespace std;
33 
34 #define PROG_NAME "xapian-progsrv"
35 #define PROG_DESC "Piped server for use with Xapian's remote backend"
36 
37 #define OPT_HELP 1
38 #define OPT_VERSION 2
39 
40 static const char * opts = "t:w";
41 static const struct option long_opts[] = {
42  {"timeout", required_argument, 0, 't'},
43  {"writable", no_argument, 0, 'w'},
44  {"help", no_argument, 0, OPT_HELP},
45  {"version", no_argument, 0, OPT_VERSION},
46  {NULL, 0, 0, 0}
47 };
48 
49 static void show_usage() {
50  cout << "Usage: " PROG_NAME " [OPTIONS] DATABASE_DIRECTORY...\n\n"
51 "Options:\n"
52 " --timeout MSECS set timeout\n"
53 " --writable allow updates\n"
54 " --help display this help and exit\n"
55 " --version output version information and exit\n";
56 }
57 
58 int main(int argc, char **argv)
59 {
60  double timeout = 60.0;
61  bool writable = false;
62  bool syntax_error = false;
63 
64  int c;
65  while ((c = gnu_getopt_long(argc, argv, opts, long_opts, NULL)) != -1) {
66  switch (c) {
67  case OPT_HELP:
68  cout << PROG_NAME " - " PROG_DESC "\n\n";
69  show_usage();
70  exit(0);
71  case OPT_VERSION:
72  cout << PROG_NAME " - " PACKAGE_STRING "\n";
73  exit(0);
74  case 't':
75  unsigned int temp;
76  if (!parse_unsigned(optarg, temp)) {
77  cout << "timeout must be a non-negative integer\n";
78  show_usage();
79  exit(1);
80  }
81  timeout = temp * 1e-3;
82  break;
83  case 'w':
84  writable = true;
85  break;
86  default:
87  syntax_error = true;
88  }
89  }
90 
91  if (syntax_error || optind == argc) {
92  show_usage();
93  exit(1);
94  }
95 
96  /* Unlike xapian-tcpsrv, xapian-progsrv only has a single 'connection'
97  * which is established immediately. So, there is no point in attempting
98  * to open the database(s) to check they work - if they can't be opened the
99  * client will get an exception right away anyway.
100  */
101  vector<string> dbnames(argv + optind, argv + argc);
102 
103  try {
104  // We communicate with the client via stdin (fd 0) and stdout (fd 1).
105  // Note that RemoteServer closes these fds.
106  RemoteServer server(dbnames, 0, 1, timeout, timeout, writable);
107 
108  // If you have defined your own weighting scheme, register it here
109  // like so:
110  // server.register_weighting_scheme(FooWeight());
111 
112  server.run();
113  } catch (...) {
114  /* Catch and ignore any exceptions thrown by RemoteServer, since the
115  * RemoteServer will have passed the error to the client to be rethrown
116  * there.
117  *
118  * Our stdout is the (now closed) communication channel to the client,
119  * and our stderr is probably a closed fd so we don't have anywhere to
120  * send error messages to anyway!
121  */
122  }
123 }
Remote backend server base class.
Definition: remoteserver.h:36
void run()
Repeatedly accept messages from the client and process them.
#define PACKAGE_STRING
Definition: config.h:361
int optind
Definition: getopt.cc:93
char * optarg
Definition: getopt.cc:78
static const char * dbnames
Wrappers to allow GNU getopt to be used cleanly from C++ code.
#define no_argument
Definition: gnu_getopt.h:78
#define required_argument
Definition: gnu_getopt.h:79
int gnu_getopt_long(int argc_, char *const *argv_, const char *shortopts_, const struct option *longopts_, int *optind_)
Definition: gnu_getopt.h:96
Parse signed and unsigned type from string and check for trailing characters.
bool parse_unsigned(const char *p, T &res)
Definition: parseint.h:29
Xapian remote backend server base class.
static const char * opts
static void show_usage()
#define OPT_VERSION
int main(int argc, char **argv)
#define PROG_NAME
static const struct option long_opts[]
#define PROG_DESC
#define OPT_HELP