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