b3bcd69cb78d9a8fe5971ed2676643c6fc5c33a7
[senf.git] / senf / PPI / AnnotationRouter.hh
1 // $Id$
2 //
3 // Copyright (C) 2008
4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 // Competence Center NETwork research (NET), St. Augustin, GERMANY
6 //     Stefan Bund <g0dil@berlios.de>
7 //
8 // This program is free software; you can redistribute it and/or modify
9 // it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation; either version 2 of the License, or
11 // (at your option) any later version.
12 //
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 // GNU General Public License for more details.
17 //
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the
20 // Free Software Foundation, Inc.,
21 // 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
22
23 /** \file
24     \brief AnnotationRouter public header */
25
26 #ifndef HH_SENF_PPI_AnnotationRouter_
27 #define HH_SENF_PPI_AnnotationRouter_ 1
28
29 // Custom includes
30 #include <boost/ptr_container/ptr_map.hpp>
31 #include <senf/Utils/String.hh>
32 #include "Module.hh"
33 #include "Connectors.hh"
34 #include "MultiConnectorMixin.hh"
35
36 //#include "AnnotationRouter.mpp"
37 //-/////////////////////////////////////////////////////////////////////////////////////////////////
38
39 namespace senf {
40 namespace ppi {
41 namespace module {
42
43     /** \brief %Route packets to destination according to some annotation value
44
45         This router takes packet on a single input and directs them to one of it outputs depending
46         on a \ref packet_usage_annotation "packet annotation". Each output connected
47         will be associated with a single annotation value. Incoming packets for which no matching
48         output is found are directed to a default output. If this output is left unconnected, those
49         packets will be dropped.
50
51         The \a AnnotationType template parameter defines the routing key. This annotation must
52         support the following operations:
53         \li Comparison with '<' (\c LessThanComparable concept)
54         \li Copy construction and copy assignment (\c Copyable and \c Assignable concepts)
55             (e.g. via compiler synthesized copy constructor and assignment operator)
56         \li Output streaming to an ostream via '\c <<' (for error description purposes) (\c
57             OutputStreamable concept)
58
59         The following annotation can be used to route the packets according to a mac address.
60         \code
61         struct TargetInterface
62         {
63             senf::MACAddress mac;
64
65             bool operator< (TargetInterface const & other)
66                 { return mac < other.mac; }
67
68             TargetInterface(senf::MACAddress const & m)
69                 : mac (m) {}
70         };
71
72         std::ostream & operator<<(std::ostream & os, TargetInterface const & v)
73             { os << v.mac; return os; }
74         \endcode
75
76         The additional senf::MACAddress constructor allows to construct an instance directly from a
77         mac address and allows to pass a senf::MACAddress value as routing key directly:
78
79         \code
80         senf::ppi::module::AnnotationRouter<TargetInterface> router;
81
82         senf::ppi::connect(router, target1, senf::MACAddress::from_string("00:1a:2b:04:06:08"));
83         \endcode
84
85         The special senf::ppi::connect() overload takes a third argument, the routing key. This must
86         be an AnnotationType value or must be (explicitly) convertible to AnnotationType.
87
88         The input will be throttled whenever any of the outputs except \a defaultOutput are
89         throttled.
90
91         \ingroup routing_modules
92
93         \todo Call Module::v_init() on every connection change and remove disconnected connections
94         from the container
95      */
96     template <class AnnotationType>
97     class AnnotationRouter
98         : public Module,
99           public MultiConnectorMixin< AnnotationRouter<AnnotationType>,
100                                         connector::ActiveOutput<>,
101                                         AnnotationType >
102     {
103         SENF_PPI_MODULE(AnnotationRouter);
104     public:
105         connector::PassiveInput<> input;
106         connector::ActiveOutput<> defaultOutput;
107
108         AnnotationRouter();
109
110         struct DuplicateKeyException : public senf::Exception
111         { DuplicateKeyException(AnnotationType const & key)
112               : senf::Exception("Duplicate senf::ppi::module::AnnotationRouter routing key ")
113                 { append( senf::str(key)); } };
114
115     private:
116         AnnotationType connectorSetup(connector::ActiveOutput<> & conn, AnnotationType const & key);
117         void request();
118
119         friend class MultiConnectorMixin< AnnotationRouter<AnnotationType>,
120                                             connector::ActiveOutput<>,
121                                             AnnotationType >;
122     };
123
124 }}}
125
126 //-/////////////////////////////////////////////////////////////////////////////////////////////////
127 //#include "AnnotationRouter.cci"
128 #include "AnnotationRouter.ct"
129 //#include "AnnotationRouter.cti"
130 #endif
131
132 \f
133 // Local Variables:
134 // mode: c++
135 // fill-column: 100
136 // comment-column: 40
137 // c-file-style: "senf"
138 // indent-tabs-mode: nil
139 // ispell-local-dictionary: "american"
140 // compile-command: "scons -u test"
141 // End: