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