test_filter1
test_filter2
test_filter_frontend_net
+test_filter_log
test_package1
test_thread_pool_observer
test_session1
-## $Id: Makefile.am,v 1.20 2005-10-16 16:05:44 adam Exp $
+## $Id: Makefile.am,v 1.21 2005-10-19 22:45:59 marc Exp $
MAINTAINERCLEANFILES = Makefile.in config.in config.hpp
test_thread_pool_observer \
test_boost_threads test_boost_time \
test_filter_frontend_net \
+ test_filter_log \
test_filter_z3950_client
TESTS=$(check_PROGRAMS)
test_boost_time_SOURCES=test_boost_time.cpp
test_thread_pool_observer_SOURCES = test_thread_pool_observer.cpp
test_filter_frontend_net_SOURCES = test_filter_frontend_net.cpp
+test_filter_log_SOURCES = test_filter_log.cpp
test_filter_z3950_client_SOURCES = test_filter_z3950_client.cpp
TESTLDADD = $(LDADD) -lboost_unit_test_framework
test_thread_pool_observer_LDADD = $(TESTLDADD)
test_package1_LDADD = $(TESTLDADD)
test_filter_frontend_net_LDADD = $(TESTLDADD)
+test_filter_log_LDADD = $(TESTLDADD)
test_filter_z3950_client_LDADD = $(TESTLDADD)
# doxygen target
-/* $Id: filter_log.cpp,v 1.4 2005-10-16 16:05:44 adam Exp $
+/* $Id: filter_log.cpp,v 1.5 2005-10-19 22:45:59 marc Exp $
Copyright (c) 2005, Index Data.
%LICENSE%
#include <yaz/zgdu.h>
#include <yaz/log.h>
+#include <boost/date_time/posix_time/posix_time.hpp>
#include <iostream>
+
yp2::filter::Log::Log() {}
void yp2::filter::Log::process(Package &package) const {
Z_GDU *gdu;
- std::cout << "---- req id=" << package.session().id();
-
- std::cout << " close=" << (package.session().is_closed() ? "yes" : "no")
- << "\n";
+ // getting timestamp for receiving of package
+ boost::posix_time::ptime receive_time
+ = boost::posix_time::microsec_clock::local_time();
+
+ // scope for locking Ostream
+ {
+ boost::mutex::scoped_lock scoped_lock(m_log_mutex);
+ std::cout << receive_time << " ";
+ std::cout << "request id=" << package.session().id();
+ std::cout << " close="
+ << (package.session().is_closed() ? "yes" : "no")
+ << "\n";
+ }
+
gdu = package.request().get();
if (gdu)
{
z_GDU(odr, &gdu, 0, 0);
odr_destroy(odr);
}
- package.move();
+ // unlocked during move
+ package.move();
- std::cout << "---- res id=" << package.session().id();
-
- std::cout << " close=" << (package.session().is_closed() ? "yes" : "no")
- << "\n";
+ // getting timestamp for sending of package
+ boost::posix_time::ptime send_time
+ = boost::posix_time::microsec_clock::local_time();
+
+ boost::posix_time::time_duration duration = send_time - receive_time;
+
+ // scope for locking Ostream
+ {
+ boost::mutex::scoped_lock scoped_lock(m_log_mutex);
+ std::cout << send_time << " ";
+ std::cout << "response id=" << package.session().id();
+ std::cout << " close="
+ << (package.session().is_closed() ? "yes " : "no ")
+ << "duration=" << duration
+ << "\n";
+ //<< "duration=" << duration.total_seconds()
+ // << "." << duration.fractional_seconds()
+ // << "\n";
+ }
+
gdu = package.response().get();
if (gdu)
{
}
}
+// defining and initializing static members
+boost::mutex yp2::filter::Log::m_log_mutex;
/*
* Local variables:
-/* $Id: filter_log.hpp,v 1.4 2005-10-19 21:44:01 marc Exp $
+/* $Id: filter_log.hpp,v 1.5 2005-10-19 22:45:59 marc Exp $
Copyright (c) 2005, Index Data.
%LICENSE%
#include "filter.hpp"
+#include <boost/thread/mutex.hpp>
+
+
namespace yp2 {
namespace filter {
class Log : public Base {
public:
Log();
- void process(yp2::Package & package) const;
+ void process(yp2::Package & package) const;
+ private:
+ /// static mutex to lock Ostream during logging operation
+ static boost::mutex m_log_mutex;
};
}
}
--- /dev/null
+/* $Id: test_filter_log.cpp,v 1.1 2005-10-19 22:45:59 marc Exp $
+ Copyright (c) 2005, Index Data.
+
+%LICENSE%
+ */
+
+#include "config.hpp"
+#include <iostream>
+#include <stdexcept>
+
+#include "filter_log.hpp"
+
+#include "router.hpp"
+#include "session.hpp"
+#include "package.hpp"
+
+#define BOOST_AUTO_TEST_MAIN
+#include <boost/test/auto_unit_test.hpp>
+
+using namespace boost::unit_test;
+
+class FilterBounceInit: public yp2::filter::Base {
+public:
+ void process(yp2::Package & package) const {
+
+ if (package.session().is_closed())
+ {
+ // std::cout << "Got Close.\n";
+ }
+
+ Z_GDU *gdu = package.request().get();
+ if (gdu)
+ {
+ // std::cout << "Got PDU. Sending init response\n";
+ ODR odr = odr_createmem(ODR_ENCODE);
+ Z_APDU *apdu = zget_APDU(odr, Z_APDU_initResponse);
+
+ apdu->u.initResponse->implementationName = "YP2/YAZ";
+
+ package.response() = apdu;
+ odr_destroy(odr);
+ }
+ return package.move();
+ };
+};
+
+
+BOOST_AUTO_TEST_CASE( test_filter_log_1 )
+{
+ try
+ {
+ {
+ yp2::filter::Log lf;
+ }
+ }
+ catch ( ... ) {
+ BOOST_CHECK (false);
+ }
+}
+
+BOOST_AUTO_TEST_CASE( test_filter_log_2 )
+{
+ try
+ {
+ {
+ yp2::RouterChain router;
+
+ yp2::filter::Log lf;
+ FilterBounceInit bf;
+
+ router.rule(lf);
+ router.rule(bf);
+
+ // Create package with Z39.50 init request in it
+ yp2::Package pack;
+
+ ODR odr = odr_createmem(ODR_ENCODE);
+ Z_APDU *apdu = zget_APDU(odr, Z_APDU_initRequest);
+
+ pack.request() = apdu;
+ odr_destroy(odr);
+ // Done creating query.
+
+ // Put it in router
+ pack.router(router).move();
+
+ // Inspect that we got Z39.50 init response
+ yazpp_1::GDU *gdu = &pack.response();
+
+ Z_GDU *z_gdu = gdu->get();
+ BOOST_CHECK(z_gdu);
+ if (z_gdu) {
+ BOOST_CHECK_EQUAL(z_gdu->which, Z_GDU_Z3950);
+ BOOST_CHECK_EQUAL(z_gdu->u.z3950->which, Z_APDU_initResponse);
+ }
+ }
+ }
+ catch ( ... ) {
+ BOOST_CHECK (false);
+ }
+}
+
+/*
+ * Local variables:
+ * c-basic-offset: 4
+ * indent-tabs-mode: nil
+ * c-file-style: "stroustrup"
+ * End:
+ * vim: shiftwidth=4 tabstop=8 expandtab
+ */