modify definitation of any

This commit is contained in:
an-tao 2018-06-13 17:30:40 +08:00
parent 567bd65955
commit 50bbd2d946
9 changed files with 102 additions and 23 deletions

View File

@ -113,6 +113,7 @@ int main()
drogon::HttpAppFramework::registerHttpApiMethod("/api/v1/handle4/{4}/{3}/{1}",func); drogon::HttpAppFramework::registerHttpApiMethod("/api/v1/handle4/{4}/{3}/{1}",func);
//start app framework //start app framework
//drogon::HttpAppFramework::instance().enableDynamicSharedLibLoading();
drogon::HttpAppFramework::instance().run(); drogon::HttpAppFramework::instance().run();
} }

View File

@ -79,5 +79,6 @@ namespace drogon
virtual const std::string & getDocumentRoot() const =0; virtual const std::string & getDocumentRoot() const =0;
virtual void setDocumentRoot(const std::string &rootPath)=0; virtual void setDocumentRoot(const std::string &rootPath)=0;
virtual void setFileTypes(const std::vector<std::string> &types)=0; virtual void setFileTypes(const std::vector<std::string> &types)=0;
virtual void enableDynamicSharedLibLoading(const std::string &viewPth="views")=0;
}; };
} }

View File

@ -18,14 +18,14 @@
#ifdef USE_STD_ANY #ifdef USE_STD_ANY
#include <any> #include <any>
typedef std::any Any; using std::any;
#define Any_cast std::any_cast using std::any_cast;
#elif USE_BOOST #elif USE_BOOST
#include <boost/any.hpp> #include <boost/any.hpp>
typedef boost::any Any; using boost::any;
#define Any_cast boost::any_cast using boost::any_cast;
#else #else
#error,must use c++17 or boost #error,must use c++17 or boost
@ -35,7 +35,7 @@ typedef boost::any Any;
#include <unordered_map> #include <unordered_map>
#include <string> #include <string>
typedef std::unordered_map<std::string,Any> ViewDataMap; typedef std::unordered_map<std::string,any> ViewDataMap;
namespace drogon namespace drogon
{ {
class HttpViewData class HttpViewData
@ -48,7 +48,7 @@ public:
if(it!=viewData_.end()) if(it!=viewData_.end())
{ {
try { try {
return Any_cast<T>(it->second); return any_cast<T>(it->second);
} }
catch (std::exception& e) catch (std::exception& e)
{ {
@ -58,11 +58,11 @@ public:
T tmp; T tmp;
return tmp; return tmp;
} }
void insert(const std::string& key,Any &&obj) void insert(const std::string& key,any &&obj)
{ {
viewData_[key]=std::move(obj); viewData_[key]=std::move(obj);
} }
void insert(const std::string& key,const Any &obj) void insert(const std::string& key,const any &obj)
{ {
viewData_[key]=obj; viewData_[key]=obj;
} }

View File

@ -21,20 +21,20 @@
#ifdef USE_STD_ANY #ifdef USE_STD_ANY
#include <any> #include <any>
typedef std::any Any; using std::any;
#define Any_cast std::any_cast using std::any_cast;
#elif USE_BOOST #elif USE_BOOST
#include <boost/any.hpp> #include <boost/any.hpp>
typedef boost::any Any; using boost::any;
#define Any_cast boost::any_cast using boost::any_cast;
#else #else
#error,must use c++17 or boost #error,must use c++17 or boost
#endif #endif
typedef std::map<std::string,Any> SessionMap; typedef std::map<std::string,any> SessionMap;
namespace drogon namespace drogon
{ {
class Session class Session
@ -45,21 +45,21 @@ namespace drogon
auto it=sessionMap_.find(key); auto it=sessionMap_.find(key);
if(it!=sessionMap_.end()) if(it!=sessionMap_.end())
{ {
return Any_cast<T>(it->second); return any_cast<T>(it->second);
} }
T tmp; T tmp;
return tmp; return tmp;
}; };
Any &operator[](const std::string &key){ any &operator[](const std::string &key){
std::lock_guard<std::mutex> lck(mutex_); std::lock_guard<std::mutex> lck(mutex_);
return sessionMap_[key]; return sessionMap_[key];
}; };
void insert(const std::string& key,const Any &obj) void insert(const std::string& key,const any &obj)
{ {
std::lock_guard<std::mutex> lck(mutex_); std::lock_guard<std::mutex> lck(mutex_);
sessionMap_[key]=obj; sessionMap_[key]=obj;
}; };
void insert(const std::string& key,Any &&obj) void insert(const std::string& key,any &&obj)
{ {
std::lock_guard<std::mutex> lck(mutex_); std::lock_guard<std::mutex> lck(mutex_);
sessionMap_[key]=std::move(obj); sessionMap_[key]=std::move(obj);

View File

@ -11,6 +11,7 @@
* @section DESCRIPTION * @section DESCRIPTION
* *
*/ */
#include "SharedLibManager.h"
#include "Utilities.h" #include "Utilities.h"
#include "HttpRequestImpl.h" #include "HttpRequestImpl.h"
#include "HttpResponseImpl.h" #include "HttpResponseImpl.h"
@ -51,6 +52,7 @@ namespace drogon
virtual const std::string & getDocumentRoot() const override {return _rootPath;} virtual const std::string & getDocumentRoot() const override {return _rootPath;}
virtual void setDocumentRoot(const std::string &rootPath) override {_rootPath=rootPath;} virtual void setDocumentRoot(const std::string &rootPath) override {_rootPath=rootPath;}
virtual void setFileTypes(const std::vector<std::string> &types) override; virtual void setFileTypes(const std::vector<std::string> &types) override;
virtual void enableDynamicSharedLibLoading(const std::string &viewPth="views") override;
~HttpAppFrameworkImpl(){} ~HttpAppFrameworkImpl(){}
private: private:
std::vector<std::pair<std::string,uint16_t>> _listeners; std::vector<std::pair<std::string,uint16_t>> _listeners;
@ -100,7 +102,7 @@ namespace drogon
std::set<std::string> _fileTypeSet={"html","jpg"}; std::set<std::string> _fileTypeSet={"html","jpg"};
std::string _rootPath="."; std::string _rootPath=".";
std::atomic_bool _running=false;
//tool funcs //tool funcs
@ -109,11 +111,25 @@ namespace drogon
size_t _threadNum=1; size_t _threadNum=1;
std::string _viewFilePath;
std::unique_ptr<SharedLibManager>_sharedLibManagerPtr;
trantor::EventLoop _loop;
}; };
} }
using namespace drogon; using namespace drogon;
using namespace std::placeholders; using namespace std::placeholders;
void HttpAppFrameworkImpl::enableDynamicSharedLibLoading(const std::string &viewPath)
{
assert(!_running);
if(_viewFilePath.empty())
{
_viewFilePath=_rootPath+"/"+viewPath;
_sharedLibManagerPtr=std::unique_ptr<SharedLibManager>(new SharedLibManager(&_loop,_viewFilePath));
}
}
void HttpAppFrameworkImpl::setFileTypes(const std::vector<std::string> &types) void HttpAppFrameworkImpl::setFileTypes(const std::vector<std::string> &types)
{ {
for(auto type : types) for(auto type : types)
@ -226,12 +242,14 @@ void HttpAppFrameworkImpl::setThreadNum(size_t threadNum)
} }
void HttpAppFrameworkImpl::addListener(const std::string &ip, uint16_t port) void HttpAppFrameworkImpl::addListener(const std::string &ip, uint16_t port)
{ {
assert(!_running);
_listeners.push_back(std::make_pair(ip,port)); _listeners.push_back(std::make_pair(ip,port));
} }
void HttpAppFrameworkImpl::run() void HttpAppFrameworkImpl::run()
{ {
// //
_running=true;
std::vector<std::shared_ptr<HttpServer>> servers; std::vector<std::shared_ptr<HttpServer>> servers;
std::vector<std::shared_ptr<EventLoopThread>> loopThreads; std::vector<std::shared_ptr<EventLoopThread>> loopThreads;
initRegex(); initRegex();
@ -263,9 +281,9 @@ void HttpAppFrameworkImpl::run()
interval=_sessionTimeout/1000; interval=_sessionTimeout/1000;
limit=_sessionTimeout; limit=_sessionTimeout;
} }
trantor::EventLoop loop;
_sessionMapPtr=std::unique_ptr<CacheMap<std::string,SessionPtr>>(new CacheMap<std::string,SessionPtr>(&loop,interval,limit)); _sessionMapPtr=std::unique_ptr<CacheMap<std::string,SessionPtr>>(new CacheMap<std::string,SessionPtr>(&_loop,interval,limit));
loop.loop(); _loop.loop();
} }
bool HttpAppFrameworkImpl::doFilters(const std::vector<std::string> &filters, bool HttpAppFrameworkImpl::doFilters(const std::vector<std::string> &filters,

View File

@ -83,7 +83,7 @@ void HttpServer::onConnection(const TcpConnectionPtr& conn)
void HttpServer::onMessage(const TcpConnectionPtr& conn, void HttpServer::onMessage(const TcpConnectionPtr& conn,
MsgBuffer* buf) MsgBuffer* buf)
{ {
HttpContext* context = Any_cast<HttpContext>(conn->getMutableContext()); HttpContext* context = any_cast<HttpContext>(conn->getMutableContext());
// LOG_INFO << "###:" << string(buf->peek(), buf->readableBytes()); // LOG_INFO << "###:" << string(buf->peek(), buf->readableBytes());
if (!context->parseRequest(buf)) { if (!context->parseRequest(buf)) {

30
lib/src/SharedLibManager.cc Executable file
View File

@ -0,0 +1,30 @@
/**
*
* @file
* @author An Tao
* @section LICENSE
*
* Copyright 2018, An Tao. All rights reserved.
* Use of this source code is governed by a MIT license
* that can be found in the License file.
*
* @section DESCRIPTION
*
*/
#include "SharedLibManager.h"
#include <trantor/utils/Logger.h>
using namespace drogon;
SharedLibManager::SharedLibManager(trantor::EventLoop *loop,const std::string viewPath):
_loop(loop),
_viewPath(viewPath)
{
_loop->runEvery(5.0,[=](){
managerLibs();
});
}
void SharedLibManager::managerLibs()
{
//LOG_DEBUG<<"manager .so libs in path "<<_viewPath;
}

29
lib/src/SharedLibManager.h Executable file
View File

@ -0,0 +1,29 @@
/**
*
* @file
* @author An Tao
* @section LICENSE
*
* Copyright 2018, An Tao. All rights reserved.
* Use of this source code is governed by a MIT license
* that can be found in the License file.
*
* @section DESCRIPTION
*
*/
#pragma once
#include <trantor/utils/NonCopyable.h>
#include <trantor/net/EventLoop.h>
namespace drogon{
class SharedLibManager:public trantor::NonCopyable
{
public:
SharedLibManager(trantor::EventLoop *loop,const std::string viewPath);
~SharedLibManager(){}
private:
void managerLibs();
trantor::EventLoop *_loop;
std::string _viewPath;
};
}

@ -1 +1 @@
Subproject commit 8cafcd2ab9720ac21f4cd0af9b3af3ea79385d3b Subproject commit 3028e451c7d81a2e7301d7013a2166003f9cabe1