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