4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
6 // The contents of this file are subject to the Fraunhofer FOKUS Public License
7 // Version 1.0 (the "License"); you may not use this file except in compliance
8 // with the License. You may obtain a copy of the License at
9 // http://senf.berlios.de/license.html
11 // The Fraunhofer FOKUS Public License Version 1.0 is based on,
12 // but modifies the Mozilla Public License Version 1.1.
13 // See the full license text for the amendments.
15 // Software distributed under the License is distributed on an "AS IS" basis,
16 // WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
17 // for the specific language governing rights and limitations under the License.
19 // The Original Code is Fraunhofer FOKUS code.
21 // The Initial Developer of the Original Code is Fraunhofer-Gesellschaft e.V.
22 // (registered association), Hansastraße 27 c, 80686 Munich, Germany.
23 // All Rights Reserved.
26 // Stefan Bund <g0dil@berlios.de>
29 \brief AnnotationRouter public header */
31 #ifndef HH_SENF_PPI_AnnotationRouter_
32 #define HH_SENF_PPI_AnnotationRouter_ 1
35 #include <boost/ptr_container/ptr_map.hpp>
36 #include <senf/Utils/String.hh>
38 #include "Connectors.hh"
39 #include "MultiConnectorMixin.hh"
41 //#include "AnnotationRouter.mpp"
42 //-/////////////////////////////////////////////////////////////////////////////////////////////////
48 /** \brief %Route packets to destination according to some annotation value
50 This router takes packet on a single input and directs them to one of it outputs depending
51 on a \ref packet_usage_annotation "packet annotation". Each output connected
52 will be associated with a single annotation value. Incoming packets for which no matching
53 output is found are directed to a default output. If this output is left unconnected, those
54 packets will be dropped.
56 The \a AnnotationType template parameter defines the routing key. This annotation must
57 support the following operations:
58 \li Comparison with '<' (\c LessThanComparable concept)
59 \li Copy construction and copy assignment (\c Copyable and \c Assignable concepts)
60 (e.g. via compiler synthesized copy constructor and assignment operator)
61 \li Output streaming to an ostream via '\c <<' (for error description purposes) (\c
62 OutputStreamable concept)
64 The following annotation can be used to route the packets according to a mac address.
66 struct TargetInterface
70 bool operator< (TargetInterface const & other)
71 { return mac < other.mac; }
73 TargetInterface(senf::MACAddress const & m)
77 std::ostream & operator<<(std::ostream & os, TargetInterface const & v)
78 { os << v.mac; return os; }
81 The additional senf::MACAddress constructor allows to construct an instance directly from a
82 mac address and allows to pass a senf::MACAddress value as routing key directly:
85 senf::ppi::module::AnnotationRouter<TargetInterface> router;
87 senf::ppi::connect(router, target1, senf::MACAddress::from_string("00:1a:2b:04:06:08"));
90 The special senf::ppi::connect() overload takes a third argument, the routing key. This must
91 be an AnnotationType value or must be (explicitly) convertible to AnnotationType.
93 The input will be throttled whenever any of the outputs except \a defaultOutput are
96 \ingroup routing_modules
98 \todo Call Module::v_init() on every connection change and remove disconnected connections
101 template <class AnnotationType>
102 class AnnotationRouter
104 public MultiConnectorMixin< AnnotationRouter<AnnotationType>,
105 connector::ActiveOutput<>,
108 SENF_PPI_MODULE(AnnotationRouter);
110 connector::PassiveInput<> input;
111 connector::ActiveOutput<> defaultOutput;
115 struct DuplicateKeyException : public senf::Exception
116 { DuplicateKeyException(AnnotationType const & key)
117 : senf::Exception("Duplicate senf::ppi::module::AnnotationRouter routing key ")
118 { append( senf::str(key)); } };
121 AnnotationType connectorSetup(connector::ActiveOutput<> & conn, AnnotationType const & key);
124 friend class MultiConnectorMixin< AnnotationRouter<AnnotationType>,
125 connector::ActiveOutput<>,
131 //-/////////////////////////////////////////////////////////////////////////////////////////////////
132 //#include "AnnotationRouter.cci"
133 #include "AnnotationRouter.ct"
134 //#include "AnnotationRouter.cti"
141 // comment-column: 40
142 // c-file-style: "senf"
143 // indent-tabs-mode: nil
144 // ispell-local-dictionary: "american"
145 // compile-command: "scons -u test"