From 7969e482bd427717e116917116e52958432a9c16 Mon Sep 17 00:00:00 2001 From: Marc Cromme Date: Fri, 7 Oct 2005 09:54:13 +0000 Subject: [PATCH] design.h split up into filter.hpp package.hpp router.hpp session.hpp --- src/filter.hpp | 59 ++++++++++++++++++++++++++++++ src/package.hpp | 90 ++++++++++++++++++++++++++++++++++++++++++++++ src/router.hpp | 97 ++++++++++++++++++++++++++++++++++++++++++++++++++ src/session.hpp | 45 +++++++++++++++++++++++ src/test_filter2.cpp | 5 ++- 5 files changed, 295 insertions(+), 1 deletion(-) create mode 100644 src/filter.hpp create mode 100644 src/package.hpp create mode 100644 src/router.hpp create mode 100644 src/session.hpp diff --git a/src/filter.hpp b/src/filter.hpp new file mode 100644 index 0000000..4632a3b --- /dev/null +++ b/src/filter.hpp @@ -0,0 +1,59 @@ + +#ifndef FILTER_HPP +#define FILTER_HPP + +#include + +namespace yp2 { + + class Package; + + class Filter { + public: + virtual ~Filter(){}; + + ///sends Package off to next Filter, returns altered Package + virtual Package & process(Package & package) const { + return package; + }; + virtual void configure(){}; + + /// get function - right val in assignment + std::string name() const { + return m_name; + } + + /// set function - left val in assignment + std::string & name() { + return m_name; + } + + /// set function - can be chained + Filter & name(const std::string & name){ + m_name = name; + return *this; + } + + private: + std::string m_name; + }; + + + class FilterException : public std::runtime_error { + public: + FilterException(const std::string message) + : std::runtime_error("FilterException: " + message){ + }; + }; + + +} + +#endif +/* + * Local variables: + * c-basic-offset: 4 + * indent-tabs-mode: nil + * End: + * vim: shiftwidth=4 tabstop=8 expandtab + */ diff --git a/src/package.hpp b/src/package.hpp new file mode 100644 index 0000000..a4924dd --- /dev/null +++ b/src/package.hpp @@ -0,0 +1,90 @@ + +#ifndef PACKAGE_HPP +#define PACKAGE_HPP + +#include + + +namespace yp2 { + + class Package { + public: + + Package(unsigned long int id = 0, bool close = 0) + : m_session_id(id), m_session_close(close), + m_filter(0), m_router(0), m_data(0) {} + + /// send Package to it's next Filter defined in Router + Package & move() { + m_filter = m_router->move(m_filter, this); + if (m_filter) + return m_filter->process(*this); + else + return *this; + } + + + /// get function - right val in assignment + unsigned int session_id() const { + return m_session_id; + } + + /// get function - right val in assignment + unsigned int session_close() const { + return m_session_close; + } + + + /// get function - right val in assignment + unsigned int data() const { + return m_data; + } + + /// set function - left val in assignment + unsigned int & data() { + return m_data; + } + + /// set function - can be chained + Package & data(const unsigned int & data){ + m_data = data; + return *this; + } + + + //Router router() const { + // return m_router; + //} + + //Router & router() { + // return m_router; + //} + + /// set function - can be chained + Package & router(const Router &router){ + m_filter = 0; + m_router = &router; + return *this; + } + + + private: + unsigned long int m_session_id; + bool m_session_close; + const Filter *m_filter; + const Router *m_router; + unsigned int m_data; + }; + + + +} + +#endif +/* + * Local variables: + * c-basic-offset: 4 + * indent-tabs-mode: nil + * End: + * vim: shiftwidth=4 tabstop=8 expandtab + */ diff --git a/src/router.hpp b/src/router.hpp new file mode 100644 index 0000000..72be7f9 --- /dev/null +++ b/src/router.hpp @@ -0,0 +1,97 @@ + +#ifndef ROUTER_HPP +#define ROUTER_HPP + +#include +#include + +namespace yp2 { + + class Package; + + class Filter; + class RouterException : public std::runtime_error { + public: + RouterException(const std::string message) + : std::runtime_error("RouterException: " + message){}; + }; + + + class Router { + public: + Router(){}; + virtual ~Router(){}; + + /// determines next Filter to use from current Filter and Package + virtual const Filter *move(const Filter *filter, + const Package *package) const { + return 0; + }; + + /// re-read configuration of routing tables + virtual void configure(){}; + + /// add routing rule expressed as Filter to Router + virtual Router & rule(const Filter &filter){ + return *this; + } + private: + /// disabled because class is singleton + Router(const Router &); + + /// disabled because class is singleton + Router& operator=(const Router &); + }; + + + class RouterChain : public Router { + public: + RouterChain(){}; + virtual ~RouterChain(){}; + virtual const Filter *move(const Filter *filter, + const Package *package) const { + std::list::const_iterator it; + it = m_filter_list.begin(); + if (filter) + { + for (; it != m_filter_list.end(); it++) + if (*it == filter) + { + it++; + break; + } + } + if (it == m_filter_list.end()) + { + //throw RouterException("no routing rules known"); + return 0; + } + return *it; + }; + virtual void configure(){}; + RouterChain & rule(const Filter &filter){ + m_filter_list.push_back(&filter); + return *this; + } + protected: + std::list m_filter_list; + private: + /// disabled because class is singleton + RouterChain(const RouterChain &); + + /// disabled because class is singleton + RouterChain& operator=(const RouterChain &); + }; + + + +} + +#endif +/* + * Local variables: + * c-basic-offset: 4 + * indent-tabs-mode: nil + * End: + * vim: shiftwidth=4 tabstop=8 expandtab + */ diff --git a/src/session.hpp b/src/session.hpp new file mode 100644 index 0000000..c157ffd --- /dev/null +++ b/src/session.hpp @@ -0,0 +1,45 @@ + +#ifndef SESSION_HPP +#define SESSION_HPP + +#include + + +#include + +namespace yp2 { + + class Session + { + public: + Session() : m_id(0){}; + /// returns next id, global state of id protected by boost::mutex + long unsigned int id() { + boost::mutex::scoped_lock scoped_lock(m_mutex); + ++m_id; + return m_id; + }; + private: + /// disabled because class is singleton + Session(const Session &); + + /// disabled because class is singleton + Session& operator=(const Session &); + + boost::mutex m_mutex; + unsigned long int m_id; + + }; + + + +} + +#endif +/* + * Local variables: + * c-basic-offset: 4 + * indent-tabs-mode: nil + * End: + * vim: shiftwidth=4 tabstop=8 expandtab + */ diff --git a/src/test_filter2.cpp b/src/test_filter2.cpp index e5aa309..ff98627 100644 --- a/src/test_filter2.cpp +++ b/src/test_filter2.cpp @@ -1,6 +1,9 @@ #include -#include "design.h" +//#include "design.h" +#include "filter.hpp" +#include "router.hpp" +#include "package.hpp" class FilterConstant: public yp2::Filter { public: -- 1.7.10.4