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