From 96cda52223e9a0a4d31908b2b0c5fbe9cf6fa4d2 Mon Sep 17 00:00:00 2001 From: Michael Andres Date: Mon, 29 Apr 2013 10:27:44 +0200 Subject: [PATCH] Workaround boost::thread being not header only (boost ticket 7085) --- tests/lib/WebServer.cc | 85 ++++++++++++++++++++++++++++++++++---------------- tests/lib/WebServer.h | 9 ++---- 2 files changed, 61 insertions(+), 33 deletions(-) diff --git a/tests/lib/WebServer.cc b/tests/lib/WebServer.cc index 4a215ed..adccf08 100644 --- a/tests/lib/WebServer.cc +++ b/tests/lib/WebServer.cc @@ -2,6 +2,8 @@ #include #include #include "boost/bind.hpp" +#include "boost/thread.hpp" +#include "boost/version.hpp" #include "zypp/base/Logger.h" #include "zypp/base/String.h" @@ -14,6 +16,35 @@ using namespace zypp; using namespace std; +#if ( BOOST_VERSION >= 105000 ) +// https://svn.boost.org/trac/boost/ticket/7085 +namespace boost +{ + namespace system + { + class fake_error_category : public error_category + { + virtual const char * name() const + { return "falke_name"; } + virtual std::string message( int ev ) const + { return "falke_message"; } + }; + const error_category & generic_category() + { + static fake_error_category _e; + return _e; + throw std::exception(/*"boost/ticket/7085 workaound sucks :("*/); + } + const error_category & system_category() + { + static fake_error_category _e; + return _e; + throw std::exception(/*"boost/ticket/7085 workaound sucks :("*/); + } + } +} +#endif + static inline string hostname() { static char buf[256]; @@ -32,19 +63,19 @@ class WebServer::Impl public: Impl() {} - + virtual ~Impl() {} - + virtual string log() const { return string(); } - + virtual void start() {} virtual void stop() {} - + virtual void worker_thread() {} @@ -52,15 +83,15 @@ public: { return 0; } - - + + private: friend Impl * rwcowClone( const Impl * rhs ); /** clone for RWCOW_pointer */ Impl * clone() const { return new Impl( *this ); } -}; +}; class WebServerWebrickImpl : public WebServer::Impl { @@ -69,7 +100,7 @@ public: : _docroot(root), _port(port), _stop(false), _stopped(true) { } - + ~WebServerWebrickImpl() { if ( ! _stopped ) @@ -81,15 +112,15 @@ public: return _port; } - + virtual void worker_thread() { _log.clear(); - + stringstream strlog(_log); string webrick_code = str::form("require \"webrick\"; s = WEBrick::HTTPServer.new(:Port => %d, :DocumentRoot => \"%s\"); trap(\"INT\"){ s.shutdown }; trap(\"SIGKILL\") { s.shutdown }; s.start;", _port, _docroot.c_str()); - + const char* argv[] = { "/usr/bin/ruby", @@ -100,18 +131,18 @@ public: ExternalProgram prog(argv,ExternalProgram::Discard_Stderr, false, -1, true); string line; - + _stopped = false; - + while ( ! _stop ); - + MIL << "Thread end requested" << endl; //prog.close(); if ( prog.running() ) prog.kill(); MIL << "Thread about to finish" << endl; } - + virtual string log() const { return _log; @@ -126,12 +157,12 @@ public: _thrd.reset(); _stopped = true; } - + virtual void start() { - _thrd.reset( new boost::thread( boost::bind(&WebServerWebrickImpl::worker_thread, this) ) ); + //_thrd.reset( new boost::thread( boost::bind(&WebServerWebrickImpl::worker_thread, this) ) ); } - + zypp::Pathname _docroot; unsigned int _port; zypp::shared_ptr _thrd; @@ -149,15 +180,15 @@ public: , _stopped(true) { } - + ~WebServerMongooseImpl() { MIL << "Destroying web server" << endl; - + if ( ! _stopped ) stop(); } - + virtual void start() { if ( ! _stopped ) @@ -165,7 +196,7 @@ public: MIL << "mongoose server already running, stopping." << endl; stop(); } - + MIL << "Starting shttpd (mongoose)" << endl; _log.clear(); _ctx = mg_start(); @@ -174,7 +205,7 @@ public: ret = mg_set_option(_ctx, "ports", str::form("%d", _port).c_str()); if ( ret != 1 ) ZYPP_THROW(Exception(str::form("Failed to set port: %d", ret))); - + MIL << "Setting root directory to : '" << _docroot << "'" << endl; ret = mg_set_option(_ctx, "root", _docroot.c_str()); if ( ret != 1 ) @@ -188,12 +219,12 @@ public: return _port; } - + virtual string log() const { return _log; } - + virtual void stop() { MIL << "Stopping shttpd" << endl; @@ -202,7 +233,7 @@ public: _ctx = 0; _stopped = true; } - + mg_context *_ctx; zypp::Pathname _docroot; unsigned int _port; @@ -217,7 +248,7 @@ WebServer::WebServer(const Pathname &root, unsigned int port) #else : _pimpl(new WebServerMongooseImpl(root, port)) #endif -{ +{ } void WebServer::start() diff --git a/tests/lib/WebServer.h b/tests/lib/WebServer.h index ec62e41..be0f8e8 100644 --- a/tests/lib/WebServer.h +++ b/tests/lib/WebServer.h @@ -2,9 +2,6 @@ #ifndef ZYPP_TEST_WEBSERVER_H #define ZYPP_TEST_WEBSERVER_H -#include "boost/thread.hpp" -#include "boost/smart_ptr.hpp" - #include "zypp/Url.h" #include "zypp/Pathname.h" #include "zypp/base/PtrTypes.h" @@ -25,9 +22,9 @@ * web.start(); * * MediaSetAccess media( Url("http://localhost:9099"), "/" ); - * + * * // do something with the url - * + * * * web.stop(); * @@ -59,7 +56,7 @@ class WebServer * returns the base url where the webserver is listening */ zypp::Url url() const; - + /** * shows the log of last run */ -- 2.7.4