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

PacketInterpreter.hh

Go to the documentation of this file.
00001 // $Id: PacketInterpreter.hh 1742 2010-11-04 14:51:56Z g0dil $
00002 //
00003 // Copyright (C) 2007
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 #ifndef HH_SENF_Packets_PacketInterpreter_
00027 #define HH_SENF_Packets_PacketInterpreter_ 1
00028 
00029 // Custom includes
00030 #include <senf/boost_intrusive/ilist.hpp>
00031 #include <boost/optional.hpp>
00032 #include <boost/range.hpp>
00033 #include <senf/Utils/intrusive_refcount.hh>
00034 #include <senf/Utils/pool_alloc_mixin.hh>
00035 #include <senf/Utils/Tags.hh>
00036 #include "PacketData.hh"
00037 #include <senf/Utils/TypeIdValue.hh>
00038 
00039 //#include "PacketInterpreter.mpp"
00040 //-/////////////////////////////////////////////////////////////////////////////////////////////////
00041 
00042 namespace senf {
00043 
00044     template <class PacketType> class PacketInterpreter;
00045 
00053     class PacketInterpreterBase
00054         : protected PacketData,
00055           public detail::packet::interpreter_list_base,
00056           public intrusive_refcount_t<PacketInterpreterBase>
00057     {
00058     public:
00059         //-////////////////////////////////////////////////////////////////////////
00060         // Types
00061 
00062         typedef senf::detail::packet::smart_pointer<
00063             PacketInterpreterBase>::ptr_t ptr;
00064 
00065         typedef senf::detail::packet::iterator iterator;
00066         typedef senf::detail::packet::const_iterator const_iterator;
00067         typedef senf::detail::packet::size_type size_type;
00068         typedef senf::detail::packet::difference_type difference_type;
00069         typedef senf::detail::packet::byte byte;
00070 
00071         typedef boost::iterator_range<iterator> range;
00072         typedef boost::optional< boost::iterator_range<iterator> > optional_range;
00073         typedef optional_range no_range;
00074 
00075         enum Append_t { Append };
00076         enum Prepend_t { Prepend };
00077 
00086         struct Factory {
00087             virtual ~Factory();
00088 
00089             // Create completely new packet
00090 
00091             virtual ptr create() const = 0;
00092             virtual ptr create(senf::NoInit_t) const = 0;
00093             virtual ptr create(size_type size) const = 0;
00094             virtual ptr create(size_type size, senf::NoInit_t) const = 0;
00095             template <class ForwardReadableRange>
00096             ptr create(ForwardReadableRange const & range) const;
00097 
00098             // Create packet as new packet after a given packet
00099 
00100             virtual ptr createAfter(PacketInterpreterBase::ptr packet) const = 0;
00101             virtual ptr createAfter(PacketInterpreterBase::ptr packet, senf::NoInit_t) const = 0;
00102             virtual ptr createAfter(PacketInterpreterBase::ptr packet, size_type size) const = 0;
00103             virtual ptr createAfter(PacketInterpreterBase::ptr packet, size_type size,
00104                                     senf::NoInit_t) const = 0;
00105             template <class ForwardReadableRange>
00106             ptr createAfter(PacketInterpreterBase::ptr packet,
00107                             ForwardReadableRange const & range) const;
00108 
00109             // Create packet as new packet (header) const before a given packet
00110 
00111             virtual ptr createBefore(PacketInterpreterBase::ptr packet) const = 0;
00112             virtual ptr createBefore(PacketInterpreterBase::ptr packet, senf::NoInit_t) const = 0;
00113 
00114             virtual ptr createInsertBefore(PacketInterpreterBase::ptr packet) const = 0;
00115             virtual ptr createInsertBefore(PacketInterpreterBase::ptr packet, senf::NoInit_t) const = 0;
00116 
00117             // Parse next packet in chain
00118 
00119             virtual ptr parseNext(ptr packet) const = 0;
00120         };
00121 
00122         typedef Factory const * factory_t;
00123 
00124         //-////////////////////////////////////////////////////////////////////////
00126         //\{
00127 
00128         // protected constructors
00129         // no copy
00130         // no conversion constructors
00131 
00132         virtual ~PacketInterpreterBase();
00133 
00134         static                             factory_t no_factory();
00135 
00136         ptr clone();
00137 
00138         //\}
00139         //-////////////////////////////////////////////////////////////////////////
00140 
00142         //\{
00143 
00144         ptr next();
00145         ptr prev();
00146         ptr first();
00147         ptr last();
00148 
00149         template <class Type> typename PacketInterpreter<Type>::ptr parseNextAs();
00150                                        ptr                          parseNextAs(factory_t factory);
00151         template <class Type>          bool                         is();
00152         template <class Type> typename PacketInterpreter<Type>::ptr as();
00153 
00154         ptr append(ptr packet);
00155 
00156         void reparse();
00157 
00158         //\}
00159 
00161         //\{
00162 
00163         using PacketData::valid;
00164         PacketData & data();
00165 
00166         //\}
00167 
00169         //\{
00170 
00171         template <class Annotation>
00172         Annotation & annotation();
00173 
00174         void clearAnnotations();
00175 
00176         //\}
00177 
00179         //\{
00180 
00181         optional_range nextPacketRange();
00182         void finalizeThis();
00183         void finalizeTo(ptr other);
00184         void dump(std::ostream & os);
00185         TypeIdValue typeId();
00186         factory_t factory();
00187         factory_t nextPacketType();
00188 
00189         //\}
00190 
00191     protected:
00192         // protected structors
00193 
00194         PacketInterpreterBase(detail::PacketImpl * impl, iterator b, iterator e, Append_t);
00195         PacketInterpreterBase(detail::PacketImpl * impl, iterator b, iterator e, Prepend_t);
00196         PacketInterpreterBase(detail::PacketImpl * impl, iterator b, iterator e, ptr before);
00197 
00198         ptr appendClone(detail::PacketImpl * impl, iterator base, iterator new_base);
00199         ptr appendClone(detail::PacketImpl * impl, range r);
00200 
00201     public:
00202         // Need this for g++ < 4.0. Since PacketInterpreter is not publicly visible, it should not
00203         // be a real problem to make impl() public here
00204         using PacketData::impl;
00205 
00206     private:
00207         // abstract packet type interface
00208 
00209         virtual optional_range v_nextPacketRange() = 0;
00210         virtual ptr v_appendClone(detail::PacketImpl * impl, iterator base, iterator new_base) = 0;
00211         virtual ptr v_appendClone(detail::PacketImpl * impl, range r) =0;
00212         virtual void v_finalize() = 0;
00213         virtual void v_dump(std::ostream & os) = 0;
00214         virtual TypeIdValue v_type() = 0;
00215         virtual factory_t v_factory() = 0;
00216         virtual factory_t v_nextPacketType() = 0;
00217 
00218         // reference/memory management. Only to be called by intrusive_refcount_t.
00219 
00220         void add_ref();
00221         void release();
00222 
00223         // containment management. Only to be called by PacketImpl.
00224 
00225         void assignImpl(detail::PacketImpl *);
00226         void releaseImpl();
00227 
00228         friend class detail::PacketImpl;
00229         friend class intrusive_refcount_base;
00230         template <class PacketType> friend class PacketInterpreter;
00231         friend class detail::packet::test::TestDriver;
00232         friend class PacketParserBase;
00233 
00234         friend void senf::intrusive_ptr_add_ref(PacketInterpreterBase const *);
00235         friend void senf::intrusive_ptr_release(PacketInterpreterBase const *);
00236     };
00237 
00238     void intrusive_ptr_add_ref(PacketInterpreterBase const * p);
00239     void intrusive_ptr_release(PacketInterpreterBase const * p);
00240 
00251     template <class PacketType>
00252     class PacketInterpreter
00253         : public PacketInterpreterBase,
00254           public pool_alloc_mixin< PacketInterpreter<PacketType> >
00255     {
00256     public:
00257         //-////////////////////////////////////////////////////////////////////////
00258         // Types
00259 
00260         typedef typename senf::detail::packet::smart_pointer<
00261             PacketInterpreter>::ptr_t ptr;
00262         typedef PacketType type;
00263         typedef typename type::parser parser;
00264 
00265         //-////////////////////////////////////////////////////////////////////////
00267         //\{
00268 
00269         // private constructors
00270         // no copy
00271         // no conversion constructors
00272 
00273         static factory_t factory();
00274 
00275         // Create completely new packet
00276 
00277         static ptr create();
00278         static ptr create(senf::NoInit_t);
00279         static ptr create(size_type size);
00280         static ptr create(size_type size, senf::NoInit_t);
00281         template <class ForwardReadableRange>
00282         static ptr create(ForwardReadableRange const & range);
00283 
00284         // Create packet as new packet after a given packet
00285 
00286         static ptr createAfter(PacketInterpreterBase::ptr packet);
00287         static ptr createAfter(PacketInterpreterBase::ptr packet, senf::NoInit_t);
00288         static ptr createAfter(PacketInterpreterBase::ptr packet, size_type size);
00289         static ptr createAfter(PacketInterpreterBase::ptr packet, size_type size, senf::NoInit_t);
00290         template <class ForwardReadableRange>
00291         static ptr createAfter(PacketInterpreterBase::ptr packet,
00292                                ForwardReadableRange const & range);
00293 
00294         // Create packet as new packet (header) before a given packet
00295 
00296         static ptr createBefore(PacketInterpreterBase::ptr packet);
00297         static ptr createBefore(PacketInterpreterBase::ptr packet, senf::NoInit_t);
00298 
00299         static ptr createInsertBefore(PacketInterpreterBase::ptr packet);
00300         static ptr createInsertBefore(PacketInterpreterBase::ptr packet, senf::NoInit_t);
00301 
00302         // Create a clone of the current packet
00303 
00304         ptr clone();
00305 
00306         //\}
00307         //-////////////////////////////////////////////////////////////////////////
00308 
00309         // Packet field access
00310 
00311         parser fields();
00312 
00313         // PacketType access
00314 
00315         static size_type initSize();
00316         static size_type initHeadSize();
00317 
00318     protected:
00319 
00320     private:
00321         // Private structors
00322 
00323         PacketInterpreter(detail::PacketImpl * impl, iterator b, iterator e, Append_t);
00324         PacketInterpreter(detail::PacketImpl * impl, iterator b, iterator e, Prepend_t);
00325         PacketInterpreter(detail::PacketImpl * impl, iterator b, iterator e,
00326                           PacketInterpreterBase::ptr before);
00327 
00328         static ptr create(detail::PacketImpl * impl, iterator b, iterator e, Append_t);
00329         static ptr create(detail::PacketImpl * impl, iterator b, iterator e, Prepend_t);
00330         static ptr create(detail::PacketImpl * impl, iterator b, iterator e,
00331                           PacketInterpreterBase::ptr before);
00332 
00333         // PacketType access
00334 
00335         void init();
00336 
00337         // virtual interface
00338 
00339         virtual optional_range v_nextPacketRange();
00340         virtual PacketInterpreterBase::ptr v_appendClone(detail::PacketImpl * impl,
00341                                                          iterator base, iterator new_base);
00342         virtual PacketInterpreterBase::ptr v_appendClone(detail::PacketImpl * impl, range r);
00343         virtual void v_finalize();
00344         virtual void v_dump(std::ostream & os);
00345         virtual TypeIdValue v_type();
00346         virtual factory_t v_factory();
00347         virtual factory_t v_nextPacketType();
00348 
00349         // factory
00350 
00357         struct FactoryImpl : public Factory {
00358             // Create completely new packet
00359 
00360             virtual PacketInterpreterBase::ptr create() const;
00361             virtual PacketInterpreterBase::ptr create(senf::NoInit_t) const;
00362             virtual PacketInterpreterBase::ptr create(size_type size) const;
00363             virtual PacketInterpreterBase::ptr create(size_type size,senf::NoInit_t) const;
00364 
00365             // Create packet as new packet after a given packet
00366 
00367             virtual PacketInterpreterBase::ptr createAfter(PacketInterpreterBase::ptr packet)
00368                 const;
00369             virtual PacketInterpreterBase::ptr createAfter(PacketInterpreterBase::ptr packet,
00370                                                            senf::NoInit_t) const;
00371             virtual PacketInterpreterBase::ptr createAfter(PacketInterpreterBase::ptr packet,
00372                                                            size_type size) const;
00373             virtual PacketInterpreterBase::ptr createAfter(PacketInterpreterBase::ptr packet,
00374                                                            size_type size, senf::NoInit_t) const;
00375 
00376             // Create packet as new packet (header) before a given packet
00377 
00378             virtual PacketInterpreterBase::ptr createBefore(PacketInterpreterBase::ptr packet)
00379                 const;
00380             virtual PacketInterpreterBase::ptr createBefore(PacketInterpreterBase::ptr packet,
00381                                                             senf::NoInit_t)
00382                 const;
00383 
00384             virtual PacketInterpreterBase::ptr createInsertBefore(PacketInterpreterBase::ptr packet)
00385                 const;
00386             virtual PacketInterpreterBase::ptr createInsertBefore(PacketInterpreterBase::ptr packet,
00387                                                                   senf::NoInit_t)
00388                 const;
00389 
00390             // Parse next packet in chain
00391 
00392             virtual PacketInterpreterBase::ptr parseNext(PacketInterpreterBase::ptr packet)
00393                 const;
00394         };
00395 
00396         static const FactoryImpl factory_;
00397 
00398         friend class detail::packet::test::TestDriver;
00399         friend class PacketInterpreterBase;
00400         friend class FactoryImpl;
00401     };
00402 
00408     struct InvalidPacketChainException : public senf::Exception
00409     { InvalidPacketChainException() : senf::Exception("invalid packet chain") {} };
00410 
00411 }
00412 
00413 //-/////////////////////////////////////////////////////////////////////////////////////////////////
00414 #endif
00415 #if !defined(HH_SENF_Packets_Packets__decls_) && !defined(HH_SENF_Packets_PacketInterpreter_i_)
00416 #define HH_SENF_Packets_PacketInterpreter_i_
00417 #include "PacketInterpreter.cci"
00418 #include "PacketInterpreter.ct"
00419 #include "PacketInterpreter.cti"
00420 #endif
00421 
00422 
00423 // Local Variables:
00424 // mode: c++
00425 // fill-column: 100
00426 // c-file-style: "senf"
00427 // indent-tabs-mode: nil
00428 // ispell-local-dictionary: "american"
00429 // compile-command: "scons -u test"
00430 // comment-column: 40
00431 // End:

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