Make all read-only data arrays static and const
[xapian.git] / xapian-core / bin / xapian-replicate-server.cc
blob9184be237042594502d904451d59e7c8f08b173c
1 /** @file xapian-replicate-server.cc
2 * @brief Service database replication requests from clients.
3 */
4 /* Copyright (C) 2008,2011 Olly Betts
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.
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
19 * USA
22 #include <config.h>
24 #include "net/replicatetcpserver.h"
26 #include <xapian.h>
28 #include "gnu_getopt.h"
30 #include <cstdlib>
31 #include <iostream>
33 using namespace std;
35 #define PROG_NAME "xapian-replicate-server"
36 #define PROG_DESC "Service database replication requests from clients"
38 #define OPT_HELP 1
39 #define OPT_VERSION 2
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" << endl;
51 int
52 main(int argc, char **argv)
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}
64 string host;
65 int port = 0;
67 bool one_shot = false;
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 port = atoi(optarg);
77 break;
78 case 'o':
79 one_shot = true;
80 break;
81 case OPT_HELP:
82 cout << PROG_NAME " - " PROG_DESC "\n\n";
83 show_usage();
84 exit(0);
85 case OPT_VERSION:
86 cout << PROG_NAME " - " PACKAGE_STRING << endl;
87 exit(0);
88 default:
89 show_usage();
90 exit(1);
94 if (argc - optind != 1) {
95 show_usage();
96 exit(1);
99 // Path to the database to create/update.
100 string dbpath(argv[optind]);
102 try {
103 ReplicateTcpServer server(host, port, dbpath);
104 if (one_shot) {
105 server.run_once();
106 } else {
107 server.run();
109 } catch (const Xapian::Error &error) {
110 cerr << argv[0] << ": " << error.get_description() << endl;
111 exit(1);