1 /* $Id: ex_filter_frontend_net.cpp,v 1.15 2005-10-29 22:23:36 marc Exp $
2 Copyright (c) 2005, Index Data.
11 #include <boost/program_options.hpp>
12 namespace po = boost::program_options;
16 #include "filter_frontend_net.hpp"
17 #include "filter_z3950_client.hpp"
18 #include "filter_virt_db.hpp"
19 #include "filter_log.hpp"
21 #include "router_chain.hpp"
22 #include "session.hpp"
23 #include "package.hpp"
25 class HTTPFilter: public yp2::filter::Base {
27 void process(yp2::Package & package) const {
28 if (package.session().is_closed())
30 // std::cout << "Got Close.\n";
33 Z_GDU *gdu = package.request().get();
34 if (gdu && gdu->which == Z_GDU_HTTP_Request)
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;
40 z_HTTP_header_add(odr, &http_res->headers,
41 "Content-Type", "text/plain");
43 http_res->content_buf =
44 odr_strdup(odr, "Welcome to YP2");
45 http_res->content_len = strlen(http_res->content_buf);
47 package.response() = gdu;
50 return package.move();
52 const std::string type() const {
57 int main(int argc, char **argv)
61 po::options_description desc("Allowed 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")
69 po::positional_options_description p;
73 po::store(po::command_line_parser(argc, argv).
74 options(desc).positional(p).run(), vm);
77 if (vm.count("help")) {
78 std::cout << desc << "\n";
84 std::vector<std::string> ports =
85 vm["port"].as< std::vector<std::string> >();
87 for (size_t i = 0; i<ports.size(); i++)
88 std::cout << "port " << i << " " << ports[i] << "\n";
90 yp2::RouterChain router;
92 // put frontend filter in router
93 yp2::filter::FrontendNet filter_front;
94 filter_front.ports() = ports;
96 // 0=no time, >0 timeout in seconds
97 if (vm.count("duration")) {
98 filter_front.listen_duration() = vm["duration"].as<int>();
100 router.append(filter_front);
102 // put log filter in router
103 yp2::filter::Log filter_log_front("FRONT");
104 router.append(filter_log_front);
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");
111 router.append(filter_virt_db);
113 yp2::filter::Log filter_log_back("BACK");
114 router.append(filter_log_back);
116 // put HTTP backend filter in router
117 HTTPFilter filter_init;
118 router.append(filter_init);
120 // put Z39.50 backend filter in router
121 yp2::filter::Z3950Client z3950_client;
122 router.append(z3950_client);
124 yp2::Session session;
126 yp2::Package pack(session, origin);
128 pack.router(router).move();
132 std::cerr << "unknown exception\n";
141 * indent-tabs-mode: nil
142 * c-file-style: "stroustrup"
144 * vim: shiftwidth=4 tabstop=8 expandtab