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
  • File List
  • File Members

PacketRegistry.ct

Go to the documentation of this file.
00001 // $Id: PacketRegistry.ct 1781 2011-04-11 12:10:19Z tho $
00002 //
00003 // Copyright (C) 2006
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 #include "PacketRegistry.ih"
00027 
00028 // Custom includes
00029 #include <iostream>
00030 #include <iomanip>
00031 #include <senf/Utils/TypeInfo.hh>
00032 #include <senf/Utils/Format.hh>
00033 #include <senf/Utils/senfassert.hh>
00034 
00035 #define prefix_
00036 //-/////////////////////////////////////////////////////////////////////////////////////////////////
00037 
00038 //-/////////////////////////////////////////////////////////////////////////////////////////////////
00039 // senf::detail::PacketRegistryImpl<KeyType>::Entry
00040 
00041 template <class KeyType>
00042 prefix_ senf::detail::PacketRegistryImpl<KeyType>::Entry::Entry(KeyType const & key_,
00043                                                                 int priority_)
00044     : key (key_), priority (priority_)
00045 {}
00046 
00047 template <class KeyType>
00048 prefix_ senf::detail::PacketRegistryImpl<KeyType>::Entry::~Entry()
00049 {}
00050 
00051 //-/////////////////////////////////////////////////////////////////////////////////////////////////
00052 // senf::detail::PacketRegistryImpl<KeyType>::EntryImpl<PacketType>
00053 
00054 template <class KeyType>
00055 template <class PacketType>
00056 prefix_ senf::detail::PacketRegistryImpl<KeyType>::EntryImpl<PacketType>::
00057 EntryImpl(KeyType const & key, int priority)
00058     : Entry (key, priority)
00059 {}
00060 
00061 template <class KeyType>
00062 template <class PacketType>
00063 prefix_ senf::Packet::factory_t
00064 senf::detail::PacketRegistryImpl<KeyType>::EntryImpl<PacketType>::factory()
00065     const
00066 {
00067     return PacketType::factory();
00068 }
00069 
00070 template <class KeyType>
00071 template <class PacketType>
00072 prefix_ std::string senf::detail::PacketRegistryImpl<KeyType>::EntryImpl<PacketType>::name()
00073     const
00074 {
00075     return prettyName(typeid(PacketType));
00076 }
00077 
00078 template <class KeyType>
00079 template <class PacketType>
00080 prefix_ std::type_info const &
00081 senf::detail::PacketRegistryImpl<KeyType>::EntryImpl<PacketType>::type()
00082     const
00083 {
00084     return typeid(PacketType);
00085 }
00086 
00087 //-/////////////////////////////////////////////////////////////////////////////////////////////////
00088 // senf::detail::PacketRegistryImpl<KeyType>:
00089 
00090 template <class KeyType>
00091 template <class PacketType>
00092 prefix_ void senf::detail::PacketRegistryImpl<KeyType>::registerPacket(key_t key, int priority)
00093 {
00094     SENF_ASSERT_EXPRESSION(
00095         registry_.insert(
00096             typename Entry::ptr(new EntryImpl<PacketType>(key,priority))).second,
00097         "Duplicate packet registration");
00098 }
00099 
00100 template <class KeyType>
00101 template <class PacketType>
00102 prefix_ void senf::detail::PacketRegistryImpl<KeyType>::unregisterPacket()
00103 {
00104     registry_.template get<ByType>().erase(typeid(PacketType));
00105 }
00106 
00107 template <class KeyType>
00108 prefix_ void senf::detail::PacketRegistryImpl<KeyType>::unregisterPacket(key_t key, int priority)
00109 {
00110     // Why doesn't this work:
00111     // registry_.erase(boost::make_tuple(key,priority));
00112     typename Registry::iterator i (registry_.find(boost::make_tuple(key,priority)));
00113     if (i != registry_.end())
00114         registry_.erase(i);
00115 }
00116 
00117 template <class KeyType>
00118 prefix_ typename senf::detail::PacketRegistryImpl<KeyType>::key_t
00119 senf::detail::PacketRegistryImpl<KeyType>::key(senf::TypeIdValue const & type)
00120 {
00121     boost::optional<KeyType> k (key(type,true));
00122     if (! k)
00123         throw PacketTypeNotRegisteredException();
00124     return *k;
00125 }
00126 
00127 template <class KeyType>
00128 prefix_ boost::optional<typename senf::detail::PacketRegistryImpl<KeyType>::key_t>
00129 senf::detail::PacketRegistryImpl<KeyType>::key(senf::TypeIdValue const & type, bool)
00130 {
00131     typedef typename Registry::template index<ByType>::type TypeIndex;
00132     TypeIndex const & typeIndex (registry_.template get<ByType>());
00133     typename TypeIndex::const_iterator i (typeIndex.find(type.id()));
00134     if (i == typeIndex.end())
00135         return boost::optional<key_t>();
00136     return (*i)->key;
00137 }
00138 
00139 template <class KeyType>
00140 prefix_ typename senf::detail::PacketRegistryImpl<KeyType>::Entry const &
00141 senf::detail::PacketRegistryImpl<KeyType>::lookup(key_t key)
00142 {
00143     Entry const * e (lookup(key, true));
00144     if (!e)
00145         throw PacketTypeNotRegisteredException();
00146     return *e;
00147 }
00148 
00149 template <class KeyType>
00150 prefix_ typename senf::detail::PacketRegistryImpl<KeyType>::Entry const *
00151 senf::detail::PacketRegistryImpl<KeyType>::lookup(key_t key, bool)
00152 {
00153     typedef typename Registry::template index<ByKey>::type KeyIndex;
00154     KeyIndex const & keyIndex (registry_.template get<ByKey>());
00155     typename KeyIndex::const_iterator i (keyIndex.lower_bound(key));
00156     if (i == keyIndex.end() || (*i)->key != key)
00157         return 0;
00158     return i->get();
00159 }
00160 
00161 template <class KeyType>
00162 prefix_ bool senf::detail::PacketRegistryImpl<KeyType>::v_empty()
00163     const
00164 {
00165     return registry_.empty();
00166 }
00167 
00168 template <class KeyType>
00169 prefix_ void senf::detail::PacketRegistryImpl<KeyType>::v_dump(std::ostream & os)
00170     const
00171 {
00172     typedef typename Registry::template index<ByKey>::type KeyIndex;
00173     KeyIndex const & keyIndex (registry_.template get<ByKey>());
00174     for (typename KeyIndex::iterator i (keyIndex.begin()), i_end (keyIndex.end());
00175          i != i_end; ++i) {
00176         std::string n ((*i)->name());
00177         senf::detail::DumpKey<KeyType>::dump((*i)->key, os);
00178         os << ' ' << std::setw(6) << (*i)->priority << ' ' << n.substr(21,n.size()-22) << '\n';
00179     }
00180 }
00181 
00182 template <class KeyType>
00183 prefix_ void senf::detail::PacketRegistryImpl<KeyType>::v_clear()
00184 {
00185     registry_.clear();
00186 }
00187 
00188 //-/////////////////////////////////////////////////////////////////////////////////////////////////
00189 // senf::detail::DumpKey<KeyType,is_integral>
00190 
00191 template <class KeyType, bool is_integral>
00192 prefix_ void senf::detail::DumpKey<KeyType,is_integral>::dump(KeyType const & v,
00193                                                               std::ostream & os)
00194 {
00195     os << "  " << std::setw(16) << std::left << v << std::setw(0) << std::right;
00196 }
00197 
00198 // senf::detail::DumpKey<KeyType, true>
00199 
00200 template <class KeyType>
00201 prefix_ void senf::detail::DumpKey<KeyType, true>::dump(KeyType const & v, std::ostream & os)
00202 {
00203     os << "  " << senf::format::dumpint(v);
00204 }
00205 
00206 //-/////////////////////////////////////////////////////////////////////////////////////////////////
00207 #undef prefix_
00208 
00209 
00210 // Local Variables:
00211 // mode: c++
00212 // fill-column: 100
00213 // c-file-style: "senf"
00214 // indent-tabs-mode: nil
00215 // ispell-local-dictionary: "american"
00216 // compile-command: "scons -u test"
00217 // comment-column: 40
00218 // End:

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