xapian-core  2.0.0
xapian-replicate-server.cc
Go to the documentation of this file.
1 
4 /* Copyright (C) 2008,2011 Olly Betts
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License as
8  * published by the Free Software Foundation; either version 2 of the
9  * License, or (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/replicatetcpserver.h"
24 
25 #include <xapian.h>
26 
27 #include "gnu_getopt.h"
28 #include "parseint.h"
29 
30 #include <cstdlib>
31 #include <iostream>
32 
33 using namespace std;
34 
35 #define PROG_NAME "xapian-replicate-server"
36 #define PROG_DESC "Service database replication requests from clients"
37 
38 #define OPT_HELP 1
39 #define OPT_VERSION 2
40 
41 static void show_usage() {
42  cout << "Usage: " PROG_NAME " [OPTIONS] DATABASE_PARENT_DIRECTORY\n\n"
43 "Options:\n"
44 " -I, --interface=ADDR listen on interface ADDR\n"
45 " -p, --port=PORT port to listen on\n"
46 " -o, --one-shot serve a single connection and exit\n"
47 " --help display this help and exit\n"
48 " --version output version information and exit\n";
49 }
50 
51 int
52 main(int argc, char **argv)
53 {
54  const char * opts = "I:p:o";
55  static const struct option long_opts[] = {
56  {"interface", required_argument, 0, 'I'},
57  {"port", required_argument, 0, 'p'},
58  {"one-shot", no_argument, 0, 'o'},
59  {"help", no_argument, 0, OPT_HELP},
60  {"version", no_argument, 0, OPT_VERSION},
61  {NULL, 0, 0, 0}
62  };
63 
64  string host;
65  int port = 0;
66 
67  bool one_shot = false;
68 
69  int c;
70  while ((c = gnu_getopt_long(argc, argv, opts, long_opts, 0)) != -1) {
71  switch (c) {
72  case 'I':
73  host.assign(optarg);
74  break;
75  case 'p':
76  if (!parse_signed(optarg, port) ||
77  (port < 1 || port > 65535)) {
78  cerr << "Error: must specify a valid port number "
79  "(between 1 and 65535). \n";
80  exit(1);
81  }
82  break;
83  case 'o':
84  one_shot = true;
85  break;
86  case OPT_HELP:
87  cout << PROG_NAME " - " PROG_DESC "\n\n";
88  show_usage();
89  exit(0);
90  case OPT_VERSION:
91  cout << PROG_NAME " - " PACKAGE_STRING "\n";
92  exit(0);
93  default:
94  show_usage();
95  exit(1);
96  }
97  }
98 
99  if (argc - optind != 1) {
100  show_usage();
101  exit(1);
102  }
103 
104  // Path to the database to create/update.
105  string dbpath(argv[optind]);
106 
107  try {
108  ReplicateTcpServer server(host, port, dbpath);
109  if (one_shot) {
110  server.run_once();
111  } else {
112  server.run();
113  }
114  } catch (const Xapian::Error &error) {
115  cerr << argv[0] << ": " << error.get_description() << '\n';
116  exit(1);
117  }
118 }
All exceptions thrown by Xapian are subclasses of Xapian::Error.
Definition: error.h:41
std::string get_description() const
Return a string describing this object.
Definition: error.cc:93
#define PACKAGE_STRING
Definition: config.h:361
int optind
Definition: getopt.cc:93
char * optarg
Definition: getopt.cc:78
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_signed(const char *p, T &res)
Definition: parseint.h:44
TCP/IP replication server class.
static const char * opts
static const struct option long_opts[]
static void show_usage()
#define OPT_VERSION
int main(int argc, char **argv)
#define PROG_NAME
#define PROG_DESC
#define OPT_HELP
Public interfaces for the Xapian library.