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