Implemented yp2::odr which is a noncopyable wrapper for YAZ' ODR.
[metaproxy-moved-to-github.git] / src / ex_filter_frontend_net.cpp
1 /* $Id: ex_filter_frontend_net.cpp,v 1.16 2005-10-30 17:13:36 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_log.hpp"
21
22 #include "router_chain.hpp"
23 #include "session.hpp"
24 #include "package.hpp"
25
26 class HTTPFilter: public yp2::filter::Base {
27 public:
28     void process(yp2::Package & package) const {
29         if (package.session().is_closed())
30         {
31             // std::cout << "Got Close.\n";
32         }
33         
34         Z_GDU *gdu = package.request().get();
35         if (gdu && gdu->which == Z_GDU_HTTP_Request)
36         {
37             yp2::odr odr;
38             Z_GDU *gdu = z_get_HTTP_Response(odr, 200);
39             Z_HTTP_Response *http_res = gdu->u.HTTP_Response;
40             
41             z_HTTP_header_add(odr, &http_res->headers,
42                               "Content-Type", "text/plain");
43             
44             http_res->content_buf = 
45                 odr_strdup(odr, "Welcome to YP2");
46             http_res->content_len = strlen(http_res->content_buf);
47             
48             package.response() = gdu;
49         }
50         return package.move();
51     };
52     const std::string type() const {
53         return "HTTPFilter";
54     };
55 };
56
57 int main(int argc, char **argv)
58 {
59     try 
60     {
61         po::options_description desc("Allowed options");
62         desc.add_options()
63             ("help", "produce help message")
64             ("duration", po::value<int>(),
65              "number of seconds for server to exist")
66             ("port", po::value< std::vector<std::string> >(), "listener port")
67             ;
68
69         po::positional_options_description p;
70         p.add("port", -1);
71
72         po::variables_map vm;        
73         po::store(po::command_line_parser(argc, argv).
74                   options(desc).positional(p).run(), vm);
75         po::notify(vm);    
76
77         if (vm.count("help")) {
78             std::cout << desc << "\n";
79             return 1;
80         }
81
82         if (vm.count("port"))
83         {
84             std::vector<std::string> ports = 
85                 vm["port"].as< std::vector<std::string> >();
86
87             for (size_t i = 0; i<ports.size(); i++)
88                 std::cout << "port " << i << " " << ports[i] << "\n";
89
90             yp2::RouterChain router;
91
92             // put frontend filter in router
93             yp2::filter::FrontendNet filter_front;
94             filter_front.ports() = ports;
95
96             // 0=no time, >0 timeout in seconds
97             if (vm.count("duration")) {
98                 filter_front.listen_duration() = vm["duration"].as<int>();
99             }
100             router.append(filter_front);
101
102             // put log filter in router
103             yp2::filter::Log filter_log_front("FRONT");
104             router.append(filter_log_front);
105
106             // put Virt db filter in router
107             yp2::filter::Virt_db filter_virt_db;
108             filter_virt_db.add_map_db2vhost("Default", "indexdata.dk/gils");
109             filter_virt_db.add_map_db2vhost("Local", "localhost:9100/Default");
110
111             router.append(filter_virt_db);
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  */