ac8e911f89a1833a8e05354a4342fded9c7d4a38
[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::Origin & mp::Package::origin()
86 {
87     return m_origin;
88 }
89
90 mp::Package & mp::Package::origin(const Origin & origin)
91 {
92     m_origin = origin;
93     return *this;
94 }
95
96 mp::Package & mp::Package::router(const mp::Router &router)
97 {
98     m_route_pos = router.createpos();
99     return *this;
100 }
101
102 yazpp_1::GDU &mp::Package::request()
103 {
104     return m_request_gdu;
105 }
106
107
108 yazpp_1::GDU &mp::Package::response()
109 {
110     return m_response_gdu;
111 }
112
113 mp::Session mp::Package::session() const
114 {
115     return m_session;
116 }
117
118 std::ostream& std::operator<<(std::ostream& os, const mp::Package& p)
119 {
120     os << p.origin() << " ";
121     os << p.session().id();
122     return os;
123 }
124
125 void mp::Package::log(const char *module, int level, const char *fmt, ...) const
126 {
127     char buf[4096];
128     va_list ap;
129     va_start(ap, fmt);
130
131     yaz_vsnprintf(buf, sizeof(buf)-30, fmt, ap);
132
133     std::ostringstream os;
134
135     os << module << " " << *this << " " << buf;
136
137     va_end(ap);
138     yaz_log(level, "%s", os.str().c_str());
139 }
140                 
141 /*
142  * Local variables:
143  * c-basic-offset: 4
144  * c-file-style: "Stroustrup"
145  * indent-tabs-mode: nil
146  * End:
147  * vim: shiftwidth=4 tabstop=8 expandtab
148  */
149