metaproxy  1.13.0
package.cpp
Go to the documentation of this file.
1 /* This file is part of Metaproxy.
2  Copyright (C) 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 namespace metaproxy_1 {
29  class Package::PackageLogger : boost::noncopyable {
30  friend class Package;
31  std::string str;
32  };
33 }
34 
35 mp::Package::Package()
36  : m_route_pos(0)
37 {
38 }
39 
40 mp::Package::~Package()
41 {
42  delete m_route_pos;
43 }
44 
45 mp::Package::Package(mp::Session &session, const mp::Origin &origin)
46  : m_session(session), m_origin(origin),
47  m_route_pos(0)
48 {
49 }
50 
51 mp::Package & mp::Package::copy_filter(const Package &p)
52 {
53  p_logger = p.p_logger;
54  copy_route(p);
55  return *this;
56 }
57 
58 void mp::Package::copy_route(const Package &p)
59 {
60  delete m_route_pos;
61  m_route_pos = p.m_route_pos->clone();
62 }
63 
64 void mp::Package::move()
65 {
66  if (m_route_pos)
67  {
68  const filter::Base *next_filter = m_route_pos->move(0);
69  if (next_filter)
70  next_filter->process(*this);
71  }
72 }
73 
74 void mp::Package::move(std::string route)
75 {
76  if (m_route_pos)
77  {
78  const char *r_cstr = route.length() ? route.c_str() : 0;
79  const filter::Base *next_filter = m_route_pos->move(r_cstr);
80  if (next_filter)
81  next_filter->process(*this);
82  }
83 }
84 
85 
86 mp::Session & mp::Package::session()
87 {
88  return m_session;
89 }
90 
91 mp::Origin mp::Package::origin() const
92 {
93  return m_origin;
94 }
95 
96 mp::Origin & mp::Package::origin()
97 {
98  return m_origin;
99 }
100 
101 mp::Package & mp::Package::router(const mp::Router &router)
102 {
103  m_route_pos = router.createpos();
104  return *this;
105 }
106 
107 yazpp_1::GDU &mp::Package::request()
108 {
109  return m_request_gdu;
110 }
111 
112 
113 yazpp_1::GDU &mp::Package::response()
114 {
115  return m_response_gdu;
116 }
117 
118 mp::Session mp::Package::session() const
119 {
120  return m_session;
121 }
122 
123 std::ostream& std::operator<<(std::ostream& os, const mp::Package& p)
124 {
125  os << p.origin() << " ";
126  os << p.session().id();
127  return os;
128 }
129 
130 void mp::Package::log(const char *module, int level, const char *fmt, ...)
131 {
132  char buf[4096];
133  va_list ap;
134  va_start(ap, fmt);
135 
136  buf[0] = ' ';
137  yaz_vsnprintf(buf + 1, sizeof(buf)-30, fmt, ap);
138 
139  std::ostringstream os;
140 
141  os << module << " " << *this << buf;
142 
143  va_end(ap);
144  yaz_log(level, "%s", os.str().c_str());
145 
146  if (p_logger)
147  p_logger->str += std::string(module) + std::string(buf) + std::string("\n");
148 }
149 
150 void mp::Package::log_enable(void)
151 {
152  p_logger.reset(new PackageLogger);
153 }
154 
155 void mp::Package::log_write(const char *buf, size_t sz)
156 {
157  if (p_logger)
158  p_logger->str += std::string(buf, sz);
159 }
160 
161 void mp::Package::log_reset(std::string &res)
162 {
163  if (p_logger)
164  {
165  res = p_logger->str;
166  // p_logger->str.clear();
167  p_logger.reset();
168  }
169 }
170 
171 /*
172  * Local variables:
173  * c-basic-offset: 4
174  * c-file-style: "Stroustrup"
175  * indent-tabs-mode: nil
176  * End:
177  * vim: shiftwidth=4 tabstop=8 expandtab
178  */
179 
std::ostream & operator<<(std::ostream &os, Z_GDU &zgdu)
Definition: gduutil.cpp:33