Skip to content

Commit

Permalink
增加ServletCreator和相关接口
Browse files Browse the repository at this point in the history
Signed-off-by: sylar-yin <564628276@qq.com>
  • Loading branch information
sylar-yin committed Jul 19, 2019
1 parent 6929b08 commit 4f2390d
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 10 deletions.
1 change: 1 addition & 0 deletions sylar/http/http.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include <memory>
#include <string>
#include <map>
#include <vector>
#include <iostream>
#include <sstream>
#include <boost/lexical_cast.hpp>
Expand Down
35 changes: 27 additions & 8 deletions sylar/http/servlet.cc
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,31 @@ int32_t ServletDispatch::handle(sylar::http::HttpRequest::ptr request

void ServletDispatch::addServlet(const std::string& uri, Servlet::ptr slt) {
RWMutexType::WriteLock lock(m_mutex);
m_datas[uri] = slt;
m_datas[uri] = std::make_shared<HoldServletCreator>(slt);
}

void ServletDispatch::addServletCreator(const std::string& uri, IServletCreator::ptr creator) {
RWMutexType::WriteLock lock(m_mutex);
m_datas[uri] = creator;
}

void ServletDispatch::addGlobServletCreator(const std::string& uri, IServletCreator::ptr creator) {
RWMutexType::WriteLock lock(m_mutex);
for(auto it = m_globs.begin();
it != m_globs.end(); ++it) {
if(it->first == uri) {
m_globs.erase(it);
break;
}
}
m_globs.push_back(std::make_pair(uri, creator));
}

void ServletDispatch::addServlet(const std::string& uri
,FunctionServlet::callback cb) {
RWMutexType::WriteLock lock(m_mutex);
m_datas[uri].reset(new FunctionServlet(cb));
m_datas[uri] = std::make_shared<HoldServletCreator>(
std::make_shared<FunctionServlet>(cb));
}

void ServletDispatch::addGlobServlet(const std::string& uri
Expand All @@ -53,12 +71,13 @@ void ServletDispatch::addGlobServlet(const std::string& uri
break;
}
}
m_globs.push_back(std::make_pair(uri, slt));
m_globs.push_back(std::make_pair(uri
, std::make_shared<HoldServletCreator>(slt)));
}

void ServletDispatch::addGlobServlet(const std::string& uri
,FunctionServlet::callback cb) {
return addGlobServlet(uri, FunctionServlet::ptr(new FunctionServlet(cb)));
return addGlobServlet(uri, std::make_shared<FunctionServlet>(cb));
}

void ServletDispatch::delServlet(const std::string& uri) {
Expand All @@ -80,15 +99,15 @@ void ServletDispatch::delGlobServlet(const std::string& uri) {
Servlet::ptr ServletDispatch::getServlet(const std::string& uri) {
RWMutexType::ReadLock lock(m_mutex);
auto it = m_datas.find(uri);
return it == m_datas.end() ? nullptr : it->second;
return it == m_datas.end() ? nullptr : it->second->get();
}

Servlet::ptr ServletDispatch::getGlobServlet(const std::string& uri) {
RWMutexType::ReadLock lock(m_mutex);
for(auto it = m_globs.begin();
it != m_globs.end(); ++it) {
if(it->first == uri) {
return it->second;
return it->second->get();
}
}
return nullptr;
Expand All @@ -98,12 +117,12 @@ Servlet::ptr ServletDispatch::getMatchedServlet(const std::string& uri) {
RWMutexType::ReadLock lock(m_mutex);
auto mit = m_datas.find(uri);
if(mit != m_datas.end()) {
return mit->second;
return mit->second->get();
}
for(auto it = m_globs.begin();
it != m_globs.end(); ++it) {
if(!fnmatch(it->first.c_str(), uri.c_str(), 0)) {
return it->second;
return it->second->get();
}
}
return m_default;
Expand Down
49 changes: 47 additions & 2 deletions sylar/http/servlet.h
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,38 @@ class FunctionServlet : public Servlet {
callback m_cb;
};

class IServletCreator {
public:
typedef std::shared_ptr<IServletCreator> ptr;
virtual ~IServletCreator() {}
virtual Servlet::ptr get() const = 0;
};

class HoldServletCreator : public IServletCreator {
public:
typedef std::shared_ptr<HoldServletCreator> ptr;
HoldServletCreator(Servlet::ptr slt)
:m_servlet(slt) {
}

Servlet::ptr get() const override {
return m_servlet;
}
private:
Servlet::ptr m_servlet;
};

template<class T>
class ServletCreator : public IServletCreator {
public:
typedef std::shared_ptr<ServletCreator> ptr;
ServletCreator() {
}
Servlet::ptr get() const override {
return Servlet::ptr(new T);
}
};

/**
* @brief Servlet分发器
*/
Expand Down Expand Up @@ -133,6 +165,19 @@ class ServletDispatch : public Servlet {
*/
void addGlobServlet(const std::string& uri, FunctionServlet::callback cb);

void addServletCreator(const std::string& uri, IServletCreator::ptr creator);
void addGlobServletCreator(const std::string& uri, IServletCreator::ptr creator);

template<class T>
void addServletCreator(const std::string& uri) {
addServletCreator(uri, std::make_shared<ServletCreator<T> >());
}

template<class T>
void addGlobServletCreator(const std::string& uri) {
addGlobServletCreator(uri, std::make_shared<ServletCreator<T> >());
}

/**
* @brief 删除servlet
* @param[in] uri uri
Expand Down Expand Up @@ -182,10 +227,10 @@ class ServletDispatch : public Servlet {
RWMutexType m_mutex;
/// 精准匹配servlet MAP
/// uri(/sylar/xxx) -> servlet
std::unordered_map<std::string, Servlet::ptr> m_datas;
std::unordered_map<std::string, IServletCreator::ptr> m_datas;
/// 模糊匹配servlet 数组
/// uri(/sylar/*) -> servlet
std::vector<std::pair<std::string, Servlet::ptr> > m_globs;
std::vector<std::pair<std::string, IServletCreator::ptr> > m_globs;
/// 默认servlet,所有路径都没匹配到时使用
Servlet::ptr m_default;
};
Expand Down

0 comments on commit 4f2390d

Please sign in to comment.