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