All filters declare a yp2_filter_struct - used for both static and
[metaproxy-moved-to-github.git] / src / ex_filter_frontend_net.cpp
1 /* $Id: ex_filter_frontend_net.cpp,v 1.20 2006-01-04 11:19:04 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:9999/Default");
108             filter_virt_db.add_map_db2vhost("2", "localhost:9999/2");
109             // router.append(filter_virt_db);
110
111             yp2::filter::SessionShared filter_session_shared;
112             //router.append(filter_session_shared);
113
114             yp2::filter::Log filter_log_back("BACK");
115             router.append(filter_log_back);
116
117             // put HTTP backend filter in router
118             HTTPFilter filter_init;
119             router.append(filter_init);
120
121             // put Z39.50 backend filter in router
122             yp2::filter::Z3950Client z3950_client;
123             router.append(z3950_client);
124
125             yp2::Session session;
126             yp2::Origin origin;
127             yp2::Package pack(session, origin);
128             
129             pack.router(router).move(); 
130         }
131     }
132     catch ( ... ) {
133         std::cerr << "unknown exception\n";
134         std::exit(1);
135     }
136     std::exit(0);
137 }
138
139 /*
140  * Local variables:
141  * c-basic-offset: 4
142  * indent-tabs-mode: nil
143  * c-file-style: "stroustrup"
144  * End:
145  * vim: shiftwidth=4 tabstop=8 expandtab
146  */