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