7eada36a6ee50828054c4bde55687313a5159060
[senf.git] / 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 "Module.hh"
32 #include "Connectors.hh"
33
34 //#include "AnnotationRouter.mpp"
35 ///////////////////////////////hh.p////////////////////////////////////////
36
37 namespace senf {
38 namespace ppi {
39 namespace module {
40
41     /** \brief Route packets to destination according to some annotation value
42
43         This router takes packet on a single input and directs them to one of it outputs depending
44         on a \link packet_usage_annotation packet annotation\endlink. Each output connected
45         will be associated with a single annotation value. Incoming packets for which no matching
46         output is found are directed to a default output. If this output is left unconnected, those
47         packets will be dropped.
48
49         The \a AnnotationType template parameter defines the routing key. This annotation must
50         support the following operations:
51         \li Comparison with '<' (\c LessThanCompatable concept)
52         \li Copy construction and copy assignment (\c Copyable und \c Assignable concepts)
53             (e.g. via compiler synthesized copy constructor and assignment operator)
54         \li Output streaming to an ostream via '\c <<' (\c OutputStreamable concept)
55
56         The following annotation can be used to route the packets according to a mac address.
57         \code
58         struct TargetInterface
59         {
60             senf::MACAddress mac;
61
62             bool operator< (TargetInterface const & other) 
63                 { return mac < other.mac; }
64
65             TargetInterface(senf::MACAddress const & m) 
66                 : mac (m) {}
67         };
68
69         std::ostream & operator<<(std::ostream & os, TargetInterface const & v)
70             { os << v.mac; return os; }
71         \endcode
72
73         The additional senf::MACAddress constructor allows to construct an instance directly from a
74         mac address and allows to pass a senf::MACAddress value as routing key directly:
75
76         \code
77         senf::ppi::module::AnnotationRouter router;
78
79         senf::ppi::connect(router, target1, senf::MACAddress::from_string("00:1a:2b:04:06:08"));
80         \endcode
81
82         The special senf::ppi::connect() overload takes a third argument, the routing key. This must
83         be an AnnotationType value or must be (explicitly) convertible to AnnotationType.
84
85         The input will be throttled whenever any of the outputs except \a defaultOutput are
86         throttled.
87
88         \ingroup routing_modules
89
90         \todo Call Module::v_init() on every connection change and remove disconnected connections
91         from the container 
92      */
93     template <class AnnotationType>
94     class AnnotationRouter : public Module
95     {
96         SENF_PPI_MODULE(AnnotationRouter);
97     public:
98         connector::PassiveInput<> input;
99         connector::ActiveOutput<> defaultOutput;
100         
101         AnnotationRouter();
102
103         struct DuplicateKeyException : public senf::Exception
104         { DuplicateKeyException(AnnotationType const & key) 
105               : senf::Exception("Duplicate senf::ppi::module::AnnotationRouter routing key ")
106                 { append(boost::lexical_cast<std::string>(key)); } };
107
108     private:
109         connector::ActiveOutput<> & newOutput(AnnotationType const & key);
110
111 #ifndef DOXYGEN
112         // I didn't get template friend functions to work ...
113     public:
114 #endif
115         template <class Target>
116         connector::GenericActiveOutput & connect(Target & target, AnnotationType const & key);
117
118     private:
119         void request();
120         
121         typedef boost::ptr_map<AnnotationType, connector::ActiveOutput<> > Outputs;
122         Outputs outputs_;
123     };
124
125 }
126
127 #ifndef DOXYGEN
128
129     template <class Target, class AnnotationType, class ArgType>
130     connector::GenericActiveOutput & connect(
131         module::AnnotationRouter<AnnotationType> & source, Target & target, 
132         ArgType const & key);
133
134 #endif
135
136 }}
137
138 ///////////////////////////////hh.e////////////////////////////////////////
139 //#include "AnnotationRouter.cci"
140 #include "AnnotationRouter.ct"
141 #include "AnnotationRouter.cti"
142 #endif
143
144 \f
145 // Local Variables:
146 // mode: c++
147 // fill-column: 100
148 // comment-column: 40
149 // c-file-style: "senf"
150 // indent-tabs-mode: nil
151 // ispell-local-dictionary: "american"
152 // compile-command: "scons -u test"
153 // End: