Updated copyright headers. Omit CVS ID.
[metaproxy-moved-to-github.git] / src / metaproxy_prog.cpp
1 /* This file is part of Metaproxy.
2    Copyright (C) 2005-2008 Index Data
3
4 Metaproxy is free software; you can redistribute it and/or modify it under
5 the terms of the GNU General Public License as published by the Free
6 Software Foundation; either version 2, or (at your option) any later
7 version.
8
9 Metaproxy is distributed in the hope that it will be useful, but WITHOUT ANY
10 WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17 */
18
19 #include "config.hpp"
20
21 #include <yaz/log.h>
22 #include <yaz/options.h>
23 #include <yaz/daemon.h>
24
25 #include <iostream>
26 #include <stdexcept>
27 #include <libxml/xinclude.h>
28
29 #include "filter.hpp"
30 #include "package.hpp"
31 #include "router_flexml.hpp"
32 #include "factory_static.hpp"
33
34 namespace mp = metaproxy_1;
35
36 static void handler(void *data)
37 {
38     mp::RouterFleXML *routerp = (mp::RouterFleXML*) data;
39
40     mp::Package pack;
41     pack.router(*routerp).move();
42 }
43     
44 int main(int argc, char **argv)
45 {
46     try 
47     {
48         const char *fname = 0;
49         int ret;
50         char *arg;
51         unsigned mode = 0;
52         const char *pidfile = 0;
53         const char *uid = 0;
54
55         while ((ret = options("c{config}:Dh{help}l:p:u:V{version}X", 
56                               argv, argc, &arg)) != -2)
57         {
58             switch (ret)
59             {
60             case 'c':
61                 fname = arg;
62                 break;
63             case 'D':
64                 mode = YAZ_DAEMON_FORK|YAZ_DAEMON_KEEPALIVE;
65                 break;
66             case 'h':
67                 std::cerr << "metaproxy\n"
68                     " -h|--help     help\n"
69                     " -V|--version  version\n"
70                     " -c|--config f config filename\n"
71                     " -D            daemon and keepalive operation\n"
72                     " -l f          log file f\n"
73                     " -p f          pid file f\n"
74                     " -u id         change uid to id\n"
75                     " -X            debug mode (no fork/daemon mode)\n"
76                           << std::endl;
77                 break;
78             case 'l':
79                 yaz_log_init_file(arg);
80                 break;
81             case 'p':
82                 pidfile = arg;
83                 break;
84             case 'u':
85                 uid = arg;
86                 break;
87             case 'V':
88                 std::cout << "Metaproxy " VERSION "\n";
89                 break;
90             case 'X':
91                 mode = YAZ_DAEMON_DEBUG;
92                 break;
93             case -1:
94                 std::cerr << "bad option: " << arg << std::endl;
95                 std::exit(1);
96             }
97         }
98         if (!fname)
99         {
100             std::cerr << "No configuration given\n";
101             std::exit(1);
102         }
103
104         yaz_log(YLOG_LOG, "Metaproxy " VERSION " started");
105         xmlDocPtr doc = xmlReadFile(fname,
106                                     NULL, 
107                                     XML_PARSE_XINCLUDE + XML_PARSE_NOBLANKS
108                                     + XML_PARSE_NSCLEAN + XML_PARSE_NONET );
109         
110         if (!doc)
111         {
112             std::cerr << "XML parsing failed\n";
113             std::exit(1);
114         }
115         // and perform Xinclude then
116         if (xmlXIncludeProcess(doc) > 0) {
117             std::cerr << "processing XInclude directive\n";
118         }
119         mp::FactoryStatic factory;
120         mp::RouterFleXML router(doc, factory, false);
121
122         yaz_daemon("metaproxy", mode, handler, &router, pidfile, uid);
123     }
124     catch (std::logic_error &e) {
125         std::cerr << "std::logic error: " << e.what() << "\n";
126         std::exit(1);
127     }
128     catch (std::runtime_error &e) {
129         std::cerr << "std::runtime error: " << e.what() << "\n";
130         std::exit(1);
131     }
132     catch ( ... ) {
133         std::cerr << "Unknown Exception" << std::endl;
134         std::exit(1);
135     }
136     std::exit(0);
137 }
138
139
140 /*
141  * Local variables:
142  * c-basic-offset: 4
143  * indent-tabs-mode: nil
144  * c-file-style: "stroustrup"
145  * End:
146  * vim: shiftwidth=4 tabstop=8 expandtab
147  */