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