Add path to configure method of filter.
[metaproxy-moved-to-github.git] / src / ex_filter_frontend_net.cpp
1 /* This file is part of Metaproxy.
2    Copyright (C) 2005-2011 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 <cstdlib>
22 #include <iostream>
23 #include <stdexcept>
24
25 #include <yaz/options.h>
26 #include <metaproxy/util.hpp>
27 #include "filter_frontend_net.hpp"
28 #include "filter_z3950_client.hpp"
29 #include "filter_virt_db.hpp"
30 #include "filter_session_shared.hpp"
31 #include "filter_log.hpp"
32
33 #include "router_chain.hpp"
34 #include <metaproxy/package.hpp>
35
36 namespace mp = metaproxy_1;
37
38 class HTTPFilter: public mp::filter::Base {
39 public:
40     void process(mp::Package & package) const {
41         if (package.session().is_closed())
42         {
43             // std::cout << "Got Close.\n";
44         }
45         
46         Z_GDU *gdu = package.request().get();
47         if (gdu && gdu->which == Z_GDU_HTTP_Request)
48         {
49             mp::odr odr;
50             Z_GDU *gdu = z_get_HTTP_Response(odr, 200);
51             Z_HTTP_Response *http_res = gdu->u.HTTP_Response;
52             
53             z_HTTP_header_add(odr, &http_res->headers,
54                               "Content-Type", "text/plain");
55             
56             http_res->content_buf = 
57                 odr_strdup(odr, "Welcome to Metaproxy");
58             http_res->content_len = strlen(http_res->content_buf);
59             
60             package.response() = gdu;
61         }
62         return package.move();
63     };
64     void configure(const xmlNode * ptr, bool test_only,
65                    const char *path) { };
66 };
67
68 int main(int argc, char **argv)
69 {
70     try 
71     {
72         std::vector<std::string> ports;
73         int duration = -1;
74         int ret;
75         char *arg;
76
77         while ((ret = options("h{help}d{duration}:p{port}:", 
78                               argv, argc, &arg)) != -2)
79         {
80             switch(ret)
81             {
82             case -1:
83                 std::cerr << "bad option " << arg << std::endl;
84             case 'h':
85                 std::cerr << "ex_filter_frontend_net\n"
86                     " -h|--help       help\n"
87                     " -d|--duration n duration\n"
88                     " -p|--port n     port number\n"
89                           << std::endl;
90                 break;
91             case 'p':
92                 ports.push_back(arg);
93                 break;
94             case 'd':
95                 duration = atoi(arg);
96                 break;
97             }
98         }
99         {
100             for (size_t i = 0; i<ports.size(); i++)
101                 std::cout << "port " << i << " " << ports[i] << "\n";
102
103             mp::RouterChain router;
104
105             // put frontend filter in router
106             mp::filter::FrontendNet filter_front;
107             filter_front.set_ports(ports);
108
109             // 0=no time, >0 timeout in seconds
110             if (duration != -1)
111                 filter_front.set_listen_duration(duration);
112
113             router.append(filter_front);
114
115             // put log filter in router
116             mp::filter::Log filter_log_front("FRONT");
117             router.append(filter_log_front);
118
119             // put Virt db filter in router
120             mp::filter::VirtualDB filter_virt_db;
121             filter_virt_db.add_map_db2target("gils", "indexdata.dk/gils",
122                                             "");
123             filter_virt_db.add_map_db2target("Default", "localhost:9999/Default",
124                                             "");
125             filter_virt_db.add_map_db2target("2", "localhost:9999/Slow", "");
126             router.append(filter_virt_db);
127
128             mp::filter::SessionShared filter_session_shared;
129             //router.append(filter_session_shared);
130
131             mp::filter::Log filter_log_back("BACK");
132             router.append(filter_log_back);
133
134             // put HTTP backend filter in router
135             HTTPFilter filter_init;
136             router.append(filter_init);
137
138             // put Z39.50 backend filter in router
139             mp::filter::Z3950Client z3950_client;
140             router.append(z3950_client);
141
142             mp::Session session;
143             mp::Origin origin;
144             mp::Package pack(session, origin);
145             
146             pack.router(router).move(); 
147         }
148     }
149     catch ( ... ) {
150         std::cerr << "unknown exception\n";
151         std::exit(1);
152     }
153     std::exit(0);
154 }
155
156 /*
157  * Local variables:
158  * c-basic-offset: 4
159  * c-file-style: "Stroustrup"
160  * indent-tabs-mode: nil
161  * End:
162  * vim: shiftwidth=4 tabstop=8 expandtab
163  */
164