f5fa411dd10542ab40aa54e9a514856c39821d7e
[metaproxy-moved-to-github.git] / src / package.cpp
1 /* This file is part of Metaproxy.
2    Copyright (C) 2005-2011 Index Data
3
4 Metaproxy is free software; you can redistribute it and/or modify it under
5 the terms of the GNU General Public License as published by the Free
6 Software Foundation; either version 2, or (at your option) any later
7 version.
8
9 Metaproxy is distributed in the hope that it will be useful, but WITHOUT ANY
10 WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17 */
18
19 #include "config.hpp"
20 #include <metaproxy/package.hpp>
21 #include <yaz/snprintf.h>
22 #include <yaz/log.h>
23
24 #include <sstream>
25
26 namespace mp = metaproxy_1;
27
28 mp::Package::Package() 
29     : m_route_pos(0)
30 {
31 }
32
33 mp::Package::~Package()
34 {
35     delete m_route_pos;
36 }
37
38 mp::Package::Package(mp::Session &session, const mp::Origin &origin) 
39     : m_session(session), m_origin(origin),
40       m_route_pos(0)
41 {
42 }
43
44
45 mp::Package & mp::Package::copy_filter(const Package &p)
46 {
47     delete m_route_pos;
48     m_route_pos = p.m_route_pos->clone();
49     return *this;
50 }
51
52
53 void mp::Package::move()
54 {
55     if (m_route_pos)
56     {
57         const filter::Base *next_filter = m_route_pos->move(0);
58         if (next_filter)
59             next_filter->process(*this);
60     }
61 }
62
63 void mp::Package::move(std::string route)
64 {
65     if (m_route_pos)
66     {
67         const char *r_cstr = route.length() ? route.c_str() : 0;
68         const filter::Base *next_filter = m_route_pos->move(r_cstr);
69         if (next_filter)
70             next_filter->process(*this);
71     }
72 }
73
74
75 mp::Session & mp::Package::session()
76 {
77     return m_session;
78 }
79
80 mp::Origin mp::Package::origin() const 
81 {
82     return m_origin;
83 }
84         
85 mp::Package & mp::Package::router(const mp::Router &router)
86 {
87     m_route_pos = router.createpos();
88     return *this;
89 }
90
91 yazpp_1::GDU &mp::Package::request()
92 {
93     return m_request_gdu;
94 }
95
96
97 yazpp_1::GDU &mp::Package::response()
98 {
99     return m_response_gdu;
100 }
101
102 mp::Session mp::Package::session() const
103 {
104     return m_session;
105 }
106
107 std::ostream& std::operator<<(std::ostream& os, const mp::Package& p)
108 {
109     os << p.origin() << " ";
110     os << p.session().id();
111     return os;
112 }
113
114 void mp::Package::log(const char *module, int level, const char *fmt, ...) const
115 {
116     char buf[4096];
117     va_list ap;
118     va_start(ap, fmt);
119
120     yaz_vsnprintf(buf, sizeof(buf)-30, fmt, ap);
121
122     std::ostringstream os;
123
124     os << module << " " << *this << " " << buf;
125
126     va_end(ap);
127     yaz_log(level, "%s", os.str().c_str());
128 }
129                 
130 /*
131  * Local variables:
132  * c-basic-offset: 4
133  * c-file-style: "Stroustrup"
134  * indent-tabs-mode: nil
135  * End:
136  * vim: shiftwidth=4 tabstop=8 expandtab
137  */
138