Search:

SENF Extensible Network Framework

  • Home
  • Download
  • Wiki
  • BerliOS
  • ChangeLog
  • Browse SVN
  • Bug Tracker
  • Overview
  • Examples
  • HowTos
  • Glossary
  • PPI
  • Packets
  • Scheduler
  • Socket
  • Utils
  • Console
  • Daemon
  • Logger
  • Termlib
  • Main Page
  • Related Pages
  • Modules
  • Namespaces
  • Classes
  • Files
  • Directories
  • File List
  • File Members

Route.ih

Go to the documentation of this file.
00001 // $Id: Route.ih 1742 2010-11-04 14:51:56Z g0dil $
00002 //
00003 // Copyright (C) 2007
00004 // Fraunhofer (FOKUS)
00005 // Competence Center NETwork research (NET), St. Augustin, GERMANY
00006 //     Stefan Bund <g0dil@berlios.de>
00007 //
00008 // This program is free software; you can redistribute it and/or modify
00009 // it under the terms of the GNU General Public License as published by
00010 // the Free Software Foundation; either version 2 of the License, or
00011 // (at your option) any later version.
00012 //
00013 // This program is distributed in the hope that it will be useful,
00014 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00015 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016 // GNU General Public License for more details.
00017 //
00018 // You should have received a copy of the GNU General Public License
00019 // along with this program; if not, write to the
00020 // Free Software Foundation, Inc.,
00021 // 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00022 
00026 #ifndef IH_SENF_PPI_Route_
00027 #define IH_SENF_PPI_Route_ 1
00028 
00029 // Custom includes
00030 #include <boost/type_traits/is_convertible.hpp>
00031 #include <boost/type_traits/is_base_of.hpp>
00032 #include <boost/mpl/if.hpp>
00033 #include <boost/mpl/bool.hpp>
00034 #include <boost/static_assert.hpp>
00035 
00036 //-/////////////////////////////////////////////////////////////////////////////////////////////////
00037 
00038 #ifndef DOXYGEN
00039 
00040 namespace senf {
00041 namespace ppi {
00042 namespace detail {
00043 
00044     // This is the RoutingTraits implementation for Connectors. Events are handled in the
00045     // specialization below
00046     template <class Connector, bool isEvent>
00047     struct RoutingTraitsImplementation
00048     {
00049         BOOST_STATIC_ASSERT((boost::is_base_of<connector::Connector, Connector>::value));
00050 
00051         static bool const event = false;
00052 
00053         static bool const notifySource = boost::is_base_of<
00054             connector::ActiveConnector, Connector>::value;
00055         static bool const notifyTarget = boost::is_base_of<
00056             connector::PassiveConnector, Connector>::value;
00057 
00058         static bool const dataSource = boost::is_base_of<
00059             connector::InputConnector, Connector>::value;
00060         static bool const dataTarget = boost::is_base_of<
00061             connector::OutputConnector, Connector>::value;
00062 
00063         typedef Connector type;
00064     };
00065 
00066     // RoutingTraits specialization for Event types. Events may be both dataSource or dataTarget but
00067     // cannot be notifySource.
00068     template <class Event>
00069     struct RoutingTraitsImplementation<Event,true>
00070     {
00071         static bool const event = true;
00072 
00073         static bool const notifySource = false;
00074         static bool const notifyTarget = true;
00075 
00076         static bool const dataSource = true;
00077         static bool const dataTarget = true;
00078 
00079         typedef EventDescriptor type;
00080     };
00081 
00082     // The RoutingTraits give routing related information about the argument type:
00083     //  - Wether the type is a notifySource or notifyTarget
00084     //  - Wether the type is dataSource or dataTarget
00085     //  - Provide the generalized target type
00086     //
00087     // The real implementation is in RoutingTraitsImplementation which is appropriately specialized
00088     // for Events
00089     template <class Object>
00090     struct RoutingTraits
00091         : public RoutingTraitsImplementation<Object,
00092                                              boost::is_convertible<Object*,
00093                                                                    EventDescriptor*>::value>
00094     {};
00095 
00096     // This is the generic route implementation for all routes. It just provides access to the
00097     // source and target.
00098     template <class Source, class Target, class Base>
00099     class BaseRouteImplementation
00100         : public Base
00101     {
00102     public:
00103         typedef Source source_type;
00104         typedef Target target_type;
00105 
00106         Source & source() const;
00107         Target & target() const;
00108 
00109     protected:
00110         BaseRouteImplementation(module::Module & module, Source & source, Target & target);
00111 
00112     private:
00113         bool v_hasConnector(connector::Connector const & conn) const;
00114         bool v_hasEvent(EventDescriptor const & event) const;
00115 
00116         bool isSame(connector::Connector const & conn, connector::Connector const & other) const;
00117         bool isSame(connector::Connector const & conn, EventDescriptor const & other) const;
00118         bool isSame(EventDescriptor const & event, connector::Connector const & other) const;
00119         bool isSame(EventDescriptor const & event, EventDescriptor const & other) const;
00120 
00121         Source * source_;
00122         Target * target_;
00123     };
00124 
00125     // The ForwardingRouteImplementation is based on the same BaseRouteImplementation
00126     // as non-forwarding routes are but injects a different base-class (the third template
00127     // argument to BaseRouteImplementation). ForwardingRouteImplementation has two additional
00128     // functions:
00129     //  1) Register the ForwardingRoute with the notifySource
00130     //  2) Implement the abstract ForwardingRoute interface
00131     //
00132     // Since we don't know explicitly, which of Source or Target is the notifySource or
00133     // notifyTarget, the implementation calls registerRoute and notifyThrottle/notifyUnthrottle on
00134     // *both*, the source and target, however qualified with an additional argument of type
00135     // boost::mpl::bool_ which is used to select the correct overloads, of which the 'false'
00136     // overload always is a no-op. This way, only the correct call will generate any code, the
00137     // disabled call will be optimized away.
00138     template <class Source, class Target>
00139     class ForwardingRouteImplementation
00140         : public BaseRouteImplementation<Source, Target, ForwardingRoute>
00141     {
00142         typedef BaseRouteImplementation<Source, Target, ForwardingRoute> Base;
00143 
00144     protected:
00145         ForwardingRouteImplementation(module::Module & module, Source & source, Target & target);
00146         ~ForwardingRouteImplementation();
00147 
00148     private:
00149         // send a throttle/unthrottle notification  only if the second argument is a 'true' type
00150         template <class T> void notifyThrottle(T & ob, boost::mpl::bool_<true> const &);
00151         template <class T> void notifyThrottle(T & ob, boost::mpl::bool_<false> const &);
00152         template <class T> void notifyUnthrottle(T & ob, boost::mpl::bool_<true> const &);
00153         template <class T> void notifyUnthrottle(T & ob, boost::mpl::bool_<false> const &);
00154 
00155         template <class T> bool throttled(T & ob, boost::mpl::bool_<true> const &) const;
00156         template <class T> bool throttled(T & ob, boost::mpl::bool_<false> const &) const;
00157 
00158         virtual void v_notifyThrottle();
00159         virtual void v_notifyUnthrottle();
00160         virtual bool v_throttled() const;
00161     };
00162 
00163     // This helper class finds the base-class suitable for a specific route. Routes are classified
00164     // into two groups:
00165     //  1) A forwarding routes is a routed which forwards notifications from a notifySource to a
00166     //     notifyTarget. Forwarding routes are implemneted using ForwardingRouteImplementation
00167     //  2) Non-forwarding routes don't forward notifications. They are implemented directly
00168     //     using BaseRouteImplementation
00169     template <class Source, class Target>
00170     struct RouteImplementationBase
00171     {
00172         typedef RoutingTraits<Source> srcTrait;
00173         typedef RoutingTraits<Target> trgTrait;
00174 
00175         static bool const isForwarding = (srcTrait::notifySource && trgTrait::notifyTarget)
00176             || (srcTrait::notifyTarget && trgTrait::notifySource);
00177 
00178         typedef typename boost::mpl::if_c<
00179             isForwarding,
00180             ForwardingRouteImplementation<Source,Target>,
00181             BaseRouteImplementation<Source,Target,RouteBase> >::type base;
00182     };
00183 
00184     // RouteImplementation2 has two purposes:
00185     //  1) Ensure, that routing is always from a data source to a data target
00186     //  2) To find the correct base-class. This is delegated to RouteImplementationBase
00187     template <class Source, class Target>
00188     class RouteImplementation2
00189         : public RouteImplementationBase<Source,Target>::base
00190     {
00191         typedef typename RouteImplementationBase<Source,Target>::base Base;
00192 
00193         BOOST_STATIC_ASSERT( RoutingTraits<Source>::dataSource &&
00194                              RoutingTraits<Target>::dataTarget );
00195 
00196     protected:
00197         RouteImplementation2(module::Module & module, Source & source, Target & target);
00198     };
00199 
00200     // RouteImplementation just forwards to RouteImplementation2 replacing the template arguments
00201     // with the appropriately generalized type: If either Source or Target is an Event type, it is
00202     // replaced with the general Event base-class EventDescriptor. Connector types are left as is.
00203     template <class Source, class Target>
00204     class RouteImplementation
00205         : public RouteImplementation2<typename RoutingTraits<Source>::type,
00206                                       typename RoutingTraits<Target>::type>
00207     {
00208         typedef RouteImplementation2<typename RoutingTraits<Source>::type,
00209                                      typename RoutingTraits<Target>::type> Base;
00210 
00211     protected:
00212         RouteImplementation(module::Module & module, Source & source, Target & target);
00213     };
00214 
00215 }}}
00216 
00217 #endif
00218 
00219 //-/////////////////////////////////////////////////////////////////////////////////////////////////
00220 #endif
00221 
00222 
00223 // Local Variables:
00224 // mode: c++
00225 // fill-column: 100
00226 // comment-column: 40
00227 // c-file-style: "senf"
00228 // indent-tabs-mode: nil
00229 // ispell-local-dictionary: "american"
00230 // compile-command: "scons -u test"
00231 // End:

Contact: senf-dev@lists.berlios.de | © 2006-2010 Fraunhofer Institute for Open Communication Systems, Network Research