1 /* xapian-tcpsrv.cc: tcp daemon for use with Xapian's remote backend
3 * Copyright 1999,2000,2001 BrightStation PLC
4 * Copyright 2001,2002 Ananova Ltd
5 * Copyright 2002,2003,2004,2006,2007,2008,2009,2010,2011,2013,2015 Olly Betts
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License as
9 * published by the Free Software Foundation; either version 2 of the
10 * License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
27 #include "safeerrno.h"
32 #include "gnu_getopt.h"
34 #include "xapian/constants.h"
35 #include "xapian/error.h"
36 #include "net/remotetcpserver.h"
37 #include "net/remoteserver.h"
38 #include "stringutils.h"
42 static void register_user_weighting_schemes(RemoteTcpServer
&server
) {
44 // If you have defined your own weighting scheme, register it here
46 // reg.register_weighting_scheme(FooWeight());
47 server
.set_registry(reg
);
50 const int MSECS_IDLE_TIMEOUT_DEFAULT
= 60000;
51 const int MSECS_ACTIVE_TIMEOUT_DEFAULT
= 15000;
53 #define PROG_NAME "xapian-tcpsrv"
54 #define PROG_DESC "TCP daemon for use with Xapian's remote backend"
59 static const char * opts
= "I:p:a:i:t:oqw";
60 static const struct option long_opts
[] = {
61 {"interface", required_argument
, 0, 'I'},
62 {"port", required_argument
, 0, 'p'},
63 {"active-timeout", required_argument
, 0, 'a'},
64 {"idle-timeout", required_argument
, 0, 'i'},
65 {"timeout", required_argument
, 0, 't'},
66 {"one-shot", no_argument
, 0, 'o'},
67 {"quiet", no_argument
, 0, 'q'},
68 {"writable", no_argument
, 0, 'w'},
69 {"help", no_argument
, 0, OPT_HELP
},
70 {"version", no_argument
, 0, OPT_VERSION
},
74 static void show_usage() {
75 cout
<< "Usage: " PROG_NAME
" [OPTIONS] DATABASE_DIRECTORY...\n\n"
77 " --port PORTNUM listen on port PORTNUM for connections (no default)\n"
78 " --interface ADDRESS listen on the interface associated with name or\n"
79 " address ADDRESS (default is all interfaces)\n"
80 " --idle-timeout MSECS set timeout for idle connections (default " STRINGIZE(MSECS_IDLE_TIMEOUT_DEFAULT
) "ms)\n"
81 " --active-timeout MSECS set timeout for active connections (default " STRINGIZE(MSECS_ACTIVE_TIMEOUT_DEFAULT
) "ms)\n"
82 " --timeout MSECS set both timeout values\n"
83 " --one-shot serve a single connection and exit\n"
84 " --quiet disable information messages to stdout\n"
85 " --writable allow updates (only one database directory allowed)\n"
86 " --help display this help and exit\n"
87 " --version output version information and exit" << endl
;
90 int main(int argc
, char **argv
) {
93 double active_timeout
= MSECS_ACTIVE_TIMEOUT_DEFAULT
* 1e-3;
94 double idle_timeout
= MSECS_IDLE_TIMEOUT_DEFAULT
* 1e-3;
96 bool one_shot
= false;
98 bool writable
= false;
99 bool syntax_error
= false;
102 while ((c
= gnu_getopt_long(argc
, argv
, opts
, long_opts
, NULL
)) != -1) {
105 cout
<< PROG_NAME
" - " PROG_DESC
"\n\n";
109 cout
<< PROG_NAME
" - " PACKAGE_STRING
<< endl
;
116 if (port
<= 0 || port
>= 65536) {
117 cerr
<< "Error: must specify a valid port number "
118 "(between 1 and 65535). "
119 "We actually got " << port
<< endl
;
124 active_timeout
= atoi(optarg
) * 1e-3;
127 idle_timeout
= atoi(optarg
) * 1e-3;
130 active_timeout
= idle_timeout
= atoi(optarg
) * 1e-3;
146 if (syntax_error
|| argv
[optind
] == NULL
) {
152 cerr
<< "Error: You must specify a port with --port" << endl
;
156 if (writable
&& (argc
- optind
) != 1) {
157 cerr
<< "Error: only one database directory allowed with '--writable'." << endl
;
162 vector
<string
> dbnames
;
163 // Try to open the database(s) so we report problems now instead of
164 // waiting for the first connection.
166 Xapian::WritableDatabase
db(argv
[optind
], Xapian::DB_CREATE_OR_OPEN
);
167 dbnames
.push_back(argv
[optind
]);
169 while (argv
[optind
]) {
170 dbnames
.push_back(argv
[optind
]);
171 Xapian::Database
db(argv
[optind
++]);
179 cout
<< " server on";
181 cout
<< " host " << host
<< ",";
182 cout
<< " port " << port
<< endl
;
185 RemoteTcpServer
server(dbnames
, host
, port
, active_timeout
,
186 idle_timeout
, writable
, verbose
);
189 cout
<< "Listening..." << endl
;
191 register_user_weighting_schemes(server
);
198 } catch (const Xapian::Error
&e
) {
199 cerr
<< e
.get_description() << endl
;
201 } catch (const exception
&e
) {
202 cerr
<< "Caught standard exception: " << e
.what() << endl
;
205 cerr
<< "Caught unknown exception" << endl
;