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

GenericTLV.ct

Go to the documentation of this file.
00001 // $Id: GenericTLV.ct 1772 2011-03-10 12:45:21Z tho $
00002 //
00003 // Copyright (C) 2009
00004 // Fraunhofer (FOKUS)
00005 // Competence Center NETwork research (NET), St. Augustin, GERMANY
00006 //     Thorsten Horstmann <tho@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 "GenericTLV.ih"
00027 
00028 // Custom includes
00029 #include <boost/io/ios_state.hpp>
00030 #include <senf/Utils/hexdump.hh>
00031 #include <senf/Utils/TypeInfo.hh>
00032 #include <senf/Utils/Format.hh>
00033 
00034 #define prefix_
00035 //-/////////////////////////////////////////////////////////////////////////////////////////////////
00036 
00037 //-/////////////////////////////////////////////////////////////////////////////////////////////////
00038 // senf::GenericTLVParserBase<Base>
00039 
00040 template <class Base>
00041 template <class Parser>
00042 prefix_ Parser senf::GenericTLVParserBase<Base>::init()
00043 {
00044     PacketParserBase::size_type oldSize (bytes() );
00045     PacketParserBase::size_type newParserSize ( senf::init_bytes<Parser>::value );
00046     this->resize(  oldSize, newParserSize);
00047     std::fill(this->i(), boost::next(this->i(), newParserSize), 0u);
00048     Parser concreteParser = Parser(this->i(), this->state() );
00049     concreteParser.init();
00050 //    concreteParser.length() = newParserSize - senf::init_bytes<Base>::value;
00051     return concreteParser;
00052 }
00053 
00054 template <class Base>
00055 prefix_  void senf::GenericTLVParserBase<Base>::dump(std::ostream & os)
00056     const
00057 {
00058     if (Base::Registry::instance().isRegistered( *this)) {
00059         Base::Registry::instance().dump( *this, os);
00060     } else {
00061         boost::io::ios_all_saver ias(os);
00062         os << "  GenericTLVParser<" << prettyName(typeid(Base)) << ">\n"
00063            << "    type:   " << format::dumpint(this->type()) << "\n"
00064            << "    length: " << format::dumpint(this->length()) << "\n"
00065            << "    value:\n";
00066         hexdump(value().begin(), value().end(), os);
00067     }
00068 }
00069 
00070 template <class Base>
00071 prefix_  senf::PacketInterpreterBase::range senf::GenericTLVParserBase<Base>::value()
00072     const
00073 {
00074     PacketData::iterator begin ( boost::next(this->i(), senf::bytes( self())) );
00075     return PacketInterpreterBase::range(begin, boost::next( begin, this->length()) );
00076 }
00077 
00078 template <class Base>
00079 template <class ForwardReadableRange>
00080 prefix_ void senf::GenericTLVParserBase<Base>::value_(ForwardReadableRange const &range)
00081 {
00082     //typename boost::range_difference<ForwardReadableRange>::type rangeSize ( boost::size(range));
00083     unsigned rangeSize ( boost::size(range));
00084     if ( rangeSize != this->length() )
00085         resize( bytes(), rangeSize + senf::bytes(self()) );
00086     std::copy( boost::begin(range), boost::end(range), boost::next(
00087             this->i(), senf::bytes( self())) );
00088     this->length_() = rangeSize;
00089 }
00090 
00091 
00092 //-/////////////////////////////////////////////////////////////////////////////////////////////////
00093 // senf::GenericTLVParserRegistry<BaseParser,Keytype>
00094 
00095 template <class BaseParser, class Keytype>
00096 template <typename Parser>
00097 prefix_ void senf::GenericTLVParserRegistry<BaseParser,Keytype>::registerParser()
00098 {
00099     Keytype key (Parser::typeId+0);
00100     typename Map::iterator i (map_.find( key ));
00101     if (i == map_.end() )
00102         map_.insert(key, new detail::GenericTLVParserRegistry_Entry<BaseParser, Parser>() );
00103 }
00104 
00105 // Wow ... this is stupid .. Boost 1.33 ptr_map API is broken ...
00106 
00107 #if BOOST_VERSION < 103400
00108 #define PTRMAP_GET_CONTENTS(v) (v)
00109 #else
00110 #define PTRMAP_GET_CONTENTS(v) (*(v).second)
00111 #endif
00112 
00113 template <class BaseParser, class Keytype>
00114 prefix_ void senf::GenericTLVParserRegistry<BaseParser,Keytype>::dump(
00115         GenericTLVParser const & parser, std::ostream & os)
00116     const
00117 {
00118     typename Map::const_iterator i (map_.find( parser.type()));
00119     if (i != map_.end())
00120         PTRMAP_GET_CONTENTS(*i).dump(parser, os);
00121 }
00122 
00123 template <class BaseParser, class Keytype>
00124 prefix_ void senf::GenericTLVParserRegistry<BaseParser,Keytype>::dump(
00125         GenericTLVParser const & parser, Keytype const & key, std::ostream & os)
00126     const
00127 {
00128     typename Map::const_iterator i (map_.find( key));
00129     if (i != map_.end())
00130         PTRMAP_GET_CONTENTS(*i).dump(parser, os);
00131 }
00132 
00133 template <class BaseParser, class Keytype>
00134 prefix_ senf::PacketParserBase::size_type senf::GenericTLVParserRegistry<BaseParser,Keytype>::bytes(
00135         GenericTLVParser const & parser)
00136     const
00137 {
00138     typename Map::const_iterator i (map_.find( parser.type()));
00139     if (i != map_.end())
00140         return PTRMAP_GET_CONTENTS(*i).bytes(parser);
00141     else
00142         throw TLVParserNotRegisteredException();
00143 }
00144 
00145 template <class BaseParser, class Keytype>
00146 prefix_ senf::PacketParserBase::size_type senf::GenericTLVParserRegistry<BaseParser,Keytype>::bytes(
00147         GenericTLVParser const & parser, Keytype const & key)
00148     const
00149 {
00150     typename Map::const_iterator i (map_.find( key));
00151     if (i != map_.end())
00152         return PTRMAP_GET_CONTENTS(*i).bytes(parser);
00153     else
00154         throw TLVParserNotRegisteredException();
00155 }
00156 
00157 #undef PTRMAP_GET_CONTENTS
00158 
00159 //-/////////////////////////////////////////////////////////////////////////////////////////////////
00160 #undef prefix_
00161 
00162 
00163 // Local Variables:
00164 // mode: c++
00165 // fill-column: 100
00166 // comment-column: 40
00167 // c-file-style: "senf"
00168 // indent-tabs-mode: nil
00169 // ispell-local-dictionary: "american"
00170 // compile-command: "scons -u test"
00171 // End:

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