Version 1.0.23. Bump copyright year.
[metaproxy-moved-to-github.git] / src / metaproxy_prog.cpp
1 /* This file is part of Metaproxy.
2    Copyright (C) 2005-2010 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 <yaz/sc.h>
26 #include <iostream>
27 #include <stdexcept>
28 #include <libxml/xinclude.h>
29
30 #include "filter.hpp"
31 #include "package.hpp"
32 #include "router_flexml.hpp"
33 #include "factory_static.hpp"
34
35 #if HAVE_UNISTD_H
36 #include <unistd.h>
37 #endif
38 #ifdef WIN32
39 #include <direct.h>
40 #include <io.h>
41 #include <process.h>
42 #endif
43
44 namespace mp = metaproxy_1;
45
46 static void handler(void *data)
47 {
48     mp::RouterFleXML *routerp = (mp::RouterFleXML*) data;
49
50     mp::Package pack;
51     pack.router(*routerp).move();
52 }
53     
54 static int sc_main(
55     yaz_sc_t s,
56     int argc, char **argv)
57 {
58     try 
59     {
60         const char *fname = 0;
61         int ret;
62         char *arg;
63         unsigned mode = 0;
64         const char *pidfile = 0;
65         const char *uid = 0;
66
67         while ((ret = options("c{config}:Dh{help}l:p:u:V{version}w:X", 
68                               argv, argc, &arg)) != -2)
69         {
70             switch (ret)
71             {
72             case 'c':
73                 fname = arg;
74                 break;
75             case 'D':
76                 mode = YAZ_DAEMON_FORK|YAZ_DAEMON_KEEPALIVE;
77                 break;
78             case 'h':
79                 std::cerr << "metaproxy\n"
80                     " -h|--help     help\n"
81                     " -V|--version  version\n"
82                     " -c|--config f config filename\n"
83                     " -D            daemon and keepalive operation\n"
84                     " -l f          log file f\n"
85                     " -p f          pid file f\n"
86                     " -u id         change uid to id\n"
87                     " -w dir        changes working directory to dir\n"
88                     " -X            debug mode (no fork/daemon mode)\n"
89 #ifdef WIN32
90                     " -install      install windows service\n"
91                     " -remove       remove windows service\n"
92 #endif
93
94                           << std::endl;
95                 break;
96             case 'l':
97                 yaz_log_init_file(arg);
98                 break;
99             case 'p':
100                 pidfile = arg;
101                 break;
102             case 'u':
103                 uid = arg;
104                 break;
105             case 'V':
106                 std::cout << VERSION "\n";
107                 return 0;
108                 break;
109             case 'w':
110                 if (
111 #ifdef WIN32
112                     _chdir(arg)
113 #else
114                     chdir(arg)
115 #endif
116                    ) 
117                 {
118                     std::cerr << "chdir " << arg << " failed" << std::endl;
119                     return 1;
120                 }
121             case 'X':
122                 mode = YAZ_DAEMON_DEBUG;
123                 break;
124             case -1:
125                 std::cerr << "bad option: " << arg << std::endl;
126                 return 1;
127             }
128         }
129         if (!fname)
130         {
131             std::cerr << "No configuration given; use -h for help\n";
132             return 1;
133         }
134
135         yaz_log(YLOG_LOG, "Metaproxy " VERSION " started");
136         xmlDocPtr doc = xmlReadFile(fname,
137                                     NULL, 
138                                     XML_PARSE_XINCLUDE + XML_PARSE_NOBLANKS
139                                     + XML_PARSE_NSCLEAN + XML_PARSE_NONET );
140         
141         if (!doc)
142         {
143             yaz_log (YLOG_FATAL,"XML parsing failed");
144             return 1;
145         }
146         // and perform Xinclude then
147         int r = xmlXIncludeProcess(doc);
148         if (r == -1)
149         {
150             yaz_log(YLOG_FATAL, "XInclude processing failed");
151             return 1;
152         }
153         mp::FactoryStatic factory;
154         mp::RouterFleXML router(doc, factory, false);
155
156         yaz_sc_running(s);
157
158         yaz_daemon("metaproxy", mode, handler, &router, pidfile, uid);
159     }
160     catch (std::logic_error &e) {
161         yaz_log (YLOG_FATAL,"std::logic error: %s" , e.what() );
162         return 1;
163     }
164     catch (std::runtime_error &e) {
165         yaz_log(YLOG_FATAL, "std::runtime error: %s" , e.what() );
166         return 1;
167     }
168     catch ( ... ) {
169         yaz_log(YLOG_FATAL, "Unknown Exception");
170         return 1;
171     }
172     return 0;
173 }
174
175 static void sc_stop(yaz_sc_t s)
176 {
177     return;
178 }
179
180 int main(int argc, char **argv)
181 {
182     int ret;
183     yaz_sc_t s = yaz_sc_create("metaproxy", "metaproxy");
184
185     ret = yaz_sc_program(s, argc, argv, sc_main, sc_stop);
186
187     yaz_sc_destroy(&s);
188     exit(ret);
189 }
190
191
192
193 /*
194  * Local variables:
195  * c-basic-offset: 4
196  * c-file-style: "Stroustrup"
197  * indent-tabs-mode: nil
198  * End:
199  * vim: shiftwidth=4 tabstop=8 expandtab
200  */
201