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

PacketParser.hh

Go to the documentation of this file.
00001 // $Id: PacketParser.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.be>
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 
00153 #ifndef HH_SENF_Packets_PacketParser_
00154 #define HH_SENF_Packets_PacketParser_ 1
00155 
00156 // Custom includes
00157 #include <boost/utility/enable_if.hpp>
00158 #include <boost/type_traits.hpp>
00159 #include <boost/optional.hpp>
00160 #include <senf/Utils/safe_bool.hh>
00161 #include "PacketTypes.hh"
00162 #include "PacketData.hh"
00163 #include "ParseHelpers.hh"
00164 #include "SafeIterator.hh"
00165 
00166 //#include "PacketParser.mpp"
00167 //-/////////////////////////////////////////////////////////////////////////////////////////////////
00168 
00169 namespace senf {
00170 
00171     class Packet;
00172 
00238     class PacketParserBase
00239     {
00240     public:
00241         //-////////////////////////////////////////////////////////////////////////
00242         // Types
00243 
00244         typedef detail::packet::iterator data_iterator; 
00245         typedef detail::packet::size_type size_type; 
00246         typedef detail::packet::difference_type difference_type; 
00247         typedef detail::packet::byte byte; 
00248         typedef PacketData * state_type; 
00249         typedef PacketParserBase parser_base_type; 
00250 
00251         //-////////////////////////////////////////////////////////////////////////
00253         //\{
00254 
00255         // no default constructor
00256         // default copy
00257         // default destructor
00258         // no conversion constructors
00259 
00260         //\}
00261         //-////////////////////////////////////////////////////////////////////////
00262 
00263         data_iterator i() const;        
00264 
00269         data_iterator i(size_type offset) const; 
00270 
00280         state_type state() const;       
00281 
00285         PacketData & data() const;      
00286 
00289         void init() const;              
00290 
00294     private:
00295         struct ParserProtector {
00296             senf::safe_data_iterator safe_i_;
00297             mutable PacketParserBase const * parser_;
00298 
00299             ParserProtector(PacketParserBase const * parser);
00300             ParserProtector(ParserProtector const & other_);
00301             ~ParserProtector();
00302         };
00303     protected:
00304         ParserProtector protect() const;
00305 
00306         PacketParserBase(data_iterator i, state_type s); 
00307 
00311         PacketParserBase(data_iterator i, state_type s, size_type size);
00313 
00329         bool check(size_type size) const; 
00330 
00333         void validate(size_type size) const; 
00334 
00338         template <class Parser> Parser parse(data_iterator i) const; 
00339 
00343         template <class Parser, class Arg> Parser parse(Arg const & arg, data_iterator i) const;
00345 
00349         template <class Parser> Parser parse(size_type n) const; 
00350 
00354         template <class Parser, class Arg> Parser parse(Arg const & arg, size_type n) const;
00356 
00359         void defaultInit() const;       
00360 
00364         Packet packet() const;          
00365 
00369         void resize(size_type oldSize, size_type newSize); 
00370 
00379     private:
00380         data_iterator end() const;
00381 
00382         data_iterator i_;
00383         PacketData * data_;
00384 
00385         template <class Parser> friend class SafePacketParserWrapper;
00386         friend class ParserProtector;
00387     };
00388 
00403     template <class Parser>
00404     PacketParserBase::size_type bytes(Parser p);
00405 
00406     namespace detail { template <class Parser> class ParserInitBytes; }
00407     namespace detail { template <class Parser> class ParserIsFixed; }
00408 
00424     template <class Parser>
00425     struct init_bytes : public detail::ParserInitBytes<Parser>
00426     {};
00427 
00442     template <class Parser>
00443     struct is_fixed : public detail::ParserIsFixed<Parser>
00444     {};
00445 
00446 #   ifndef DOXYGEN
00447     template <class Parser>
00448     typename boost::enable_if<
00449         boost::is_base_of<PacketParserBase, Parser>,
00450         Parser >::type
00451     operator<<(Parser target, Parser source);
00452 #   else
00453 
00466     template <class Parser>
00467     Parser operator<<(Parser target, Parser source);
00468 #   endif
00469 
00470 #   ifndef DOXYGEN
00471     template <class Parser, class Value>
00472     typename boost::enable_if_c <
00473         boost::is_base_of<PacketParserBase, Parser>::value
00474             && ! boost::is_base_of<PacketParserBase, Value>::value,
00475         Parser >::type
00476     operator<<(Parser target, Value const & value);
00477 #   else
00478 
00486     template <class Parser, class Value>
00487     Parser operator<<(Parser target, Value const & value);
00488 #   endif
00489 
00490 #   ifndef DOXYGEN
00491     template <class Parser, class Value>
00492     typename boost::enable_if_c <
00493         boost::is_base_of<PacketParserBase, Parser>::value
00494             && ! boost::is_base_of<PacketParserBase, Value>::value,
00495         Parser >::type
00496     operator<<(Parser target, boost::optional<Value> const & value);
00497 #   else
00498 
00508     template <class Parser, class Value>
00509     Parser operator<<(Parser target, boost::optional<Value> const & value);
00510 #   endif
00511 
00514     struct VoidPacketParser
00515         : public PacketParserBase
00516     {
00517 #       include SENF_FIXED_PARSER()
00518         SENF_PARSER_FINALIZE(VoidPacketParser);
00519     };
00520 
00521 
00522     template <class Parser, typename ValueType, PacketParserBase::size_type FixedBytes>
00523     struct ValueParserBase : public PacketParserBase
00524     {
00525         typedef ValueParserBase<Parser, ValueType, FixedBytes> Base;
00526 
00527         static size_type const fixed_bytes = FixedBytes;
00528         typedef ValueType value_type;
00529 
00530         ValueParserBase(data_iterator i, state_type s);
00531 
00532         operator value_type () const;
00533         byte & operator[](size_type index);
00534         Parser const & operator= (value_type const & other);
00535     };
00536 
00537 }
00538 
00539 //-/////////////////////////////////////////////////////////////////////////////////////////////////
00540 #endif
00541 #if !defined(HH_SENF_Packets_Packets__decls_) && !defined(HH_SENF_Packets_PacketParser_i_)
00542 #define HH_SENF_Packets_PacketParser_i_
00543 #include "PacketParser.cci"
00544 #include "PacketParser.ct"
00545 #include "PacketParser.cti"
00546 #endif
00547 
00548 
00549 // Local Variables:
00550 // mode: c++
00551 // fill-column: 100
00552 // c-file-style: "senf"
00553 // indent-tabs-mode: nil
00554 // ispell-local-dictionary: "american"
00555 // compile-command: "scons -u test"
00556 // comment-column: 40
00557 // End:
00558 

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