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