Modify BlockList to template based

This commit is contained in:
Ai Lin Chia 2018-02-04 16:58:04 +01:00
parent b0529fb488
commit 0f99e4b03f
4 changed files with 35 additions and 22 deletions

@ -25,18 +25,20 @@
#include <sys/stat.h>
#include <atomic>
BlockList::BlockList(const char *filename)
template <class T>
BlockList<T>::BlockList(const char *filename)
: m_filename(filename)
, m_loading(false)
, m_blockList(new blocklist_t)
, m_blockList(new blocklist_t<T>)
, m_lastModifiedTime(0) {
}
bool BlockList::init() {
template <class T>
bool BlockList<T>::init() {
log(LOG_INFO, "Initializing BlockList with %s", m_filename);
if (!g_loop.registerSleepCallback(60000, this, &reload, "BlockList::reload", 0)) {
log(LOG_WARN, "BlockList:: Failed to register callback.");
if (!g_loop.registerSleepCallback(60000, this, &reload, "BlockList<T>::reload", 0)) {
log(LOG_WARN, "BlockList<T>:: Failed to register callback.");
return false;
}
@ -47,7 +49,8 @@ bool BlockList::init() {
return true;
}
void BlockList::reload(int /*fd*/, void *state) {
template <class T>
void BlockList<T>::reload(int /*fd*/, void *state) {
if (g_jobScheduler.submit(reload, nullptr, state, thread_type_config_load, 0)) {
return;
}
@ -56,7 +59,8 @@ void BlockList::reload(int /*fd*/, void *state) {
reload(state);
}
void BlockList::reload(void *state) {
template <class T>
void BlockList<T>::reload(void *state) {
BlockList *blockList = static_cast<BlockList*>(state);
// don't load multiple times at the same time
@ -68,13 +72,14 @@ void BlockList::reload(void *state) {
blockList->m_loading = false;
}
bool BlockList::load() {
template <class T>
bool BlockList<T>::load() {
logTrace(g_conf.m_logTraceBlockList, "Loading %s", m_filename);
struct stat st;
if (stat(m_filename, &st) != 0) {
// probably not found
log(LOG_INFO, "BlockList::load: Unable to stat %s", m_filename);
log(LOG_INFO, "BlockList<T>::load: Unable to stat %s", m_filename);
return false;
}
@ -84,7 +89,7 @@ bool BlockList::load() {
return true;
}
blocklist_ptr_t tmpBlockList(new blocklist_t);
blocklist_ptr_t<T> tmpBlockList(new blocklist_t<T>);
std::ifstream file(m_filename);
std::string line;
@ -105,11 +110,16 @@ bool BlockList::load() {
return true;
}
blocklistconst_ptr_t BlockList::getBlockList() {
template <class T>
blocklistconst_ptr_t<T> BlockList<T>::getBlockList() {
return m_blockList;
}
void BlockList::swapBlockList(blocklistconst_ptr_t blockList) {
template <class T>
void BlockList<T>::swapBlockList(blocklistconst_ptr_t<T> blockList) {
std::atomic_store(&m_blockList, blockList);
}
// explicit instantiations
template class BlockList<std::string>;

@ -25,11 +25,15 @@
#include <string>
#include <atomic>
typedef std::vector<std::string> blocklist_t;
typedef std::shared_ptr<blocklist_t> blocklist_ptr_t;
typedef std::shared_ptr<const blocklist_t> blocklistconst_ptr_t;
template <typename T> using blocklist_t = std::vector<T>;
template <typename T> using blocklist_ptr_t = std::shared_ptr<std::vector<T>>;
template <typename T> using blocklistconst_ptr_t = std::shared_ptr<const std::vector<T>>;
//typedef std::vector<std::string> blocklist_t;
//typedef std::shared_ptr<blocklist_t> blocklist_ptr_t;
//typedef std::shared_ptr<const blocklist_t> blocklistconst_ptr_t;
class BlockList {
template<class T> class BlockList {
//using blocklist_t = std::vector<T>, using blocklist_ptr_t = std::shared_ptr<blocklist_t>, using blocklistconst_ptr_t = std::shared_ptr<const blocklist_t>{
public:
BlockList(const char *filename);
@ -43,16 +47,15 @@ protected:
const char *m_filename;
blocklistconst_ptr_t getBlockList();
blocklistconst_ptr_t<T> getBlockList();
private:
void swapBlockList(blocklistconst_ptr_t blockList);
void swapBlockList(blocklistconst_ptr_t<T> blockList);
std::atomic_bool m_loading;
blocklistconst_ptr_t m_blockList;
blocklistconst_ptr_t<T> m_blockList;
time_t m_lastModifiedTime;
};
#endif //FX_BLOCKLIST_H

@ -24,7 +24,7 @@
#include <pthread.h>
#include <vector>
class ContentTypeBlockList : public BlockList {
class ContentTypeBlockList : public BlockList<std::string> {
public:
ContentTypeBlockList();

@ -21,7 +21,7 @@
#include "BlockList.h"
class DnsBlockList : public BlockList {
class DnsBlockList : public BlockList<std::string> {
public:
DnsBlockList();
bool isDnsBlocked(const char *dns);