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