Packets/80221Bundle: MIHFIdTLVParser API clean up
tho [Tue, 10 Nov 2009 14:20:08 +0000 (14:20 +0000)]
git-svn-id: https://svn.berlios.de/svnroot/repos/senf/trunk@1518 270642c3-0616-0410-b53a-bc976706d245

senf/Packets/80221Bundle/MIHPacket.test.cc
senf/Packets/80221Bundle/TLVParser.cc
senf/Packets/80221Bundle/TLVParser.cci
senf/Packets/80221Bundle/TLVParser.cti [new file with mode: 0644]
senf/Packets/80221Bundle/TLVParser.hh

index 64d2ff1..444f576 100644 (file)
@@ -73,8 +73,8 @@ BOOST_AUTO_UNIT_TEST(MIHPacket_create_string)
     // set some fields
     mihPacket->fragmentNr() = 42;
     mihPacket->transactionId() = 21;
-    mihPacket->src_mihfId().setString( "senf@berlios.de");
-    mihPacket->dst_mihfId().setString( "test");
+    mihPacket->src_mihfId().value( "senf@berlios.de");
+    mihPacket->dst_mihfId().value( "test");
     mihPacket.finalizeThis();
 
     unsigned char data[] = {
@@ -88,15 +88,15 @@ BOOST_AUTO_UNIT_TEST(MIHPacket_create_string)
             0x02, 0x04, 0x74, 0x65, 0x73, 0x74
     };
     BOOST_CHECK(equal( mihPacket.data().begin(), mihPacket.data().end(), data ));
-    BOOST_CHECK_EQUAL( mihPacket->src_mihfId().asString(), "senf@berlios.de");
-    BOOST_CHECK_EQUAL( mihPacket->dst_mihfId().asString(), "test");
+    BOOST_CHECK_EQUAL( mihPacket->src_mihfId().valueAsString(), "senf@berlios.de");
+    BOOST_CHECK_EQUAL( mihPacket->dst_mihfId().valueAsString(), "test");
 
     // the maximum length of a MIHF_ID is 253 octets
-    BOOST_CHECK_THROW( mihPacket->dst_mihfId().setString( std::string(254, 'x')), std::length_error);
+    BOOST_CHECK_THROW( mihPacket->dst_mihfId().value( std::string(254, 'x')), std::length_error);
 
     // now expand a MIHF_ID
     mihPacket->dst_mihfId().maxLengthValue(253);
-    mihPacket->dst_mihfId().setString( std::string(200, 'x'));
+    mihPacket->dst_mihfId().value( std::string(200, 'x'));
     mihPacket.finalizeThis();
 
     BOOST_CHECK_EQUAL( mihPacket.size(), unsigned(8 + 17 + 203));
@@ -111,12 +111,14 @@ BOOST_AUTO_UNIT_TEST(MIHPacket_create_string)
 
 BOOST_AUTO_UNIT_TEST(MIHPacket_create_mac)
 {
+    MACAddress srcMac ( MACAddress::from_string("01:02:03:04:05:06"));
+    MACAddress dstMac ( MACAddress::from_string("07:08:09:0a:0b:0c"));
     MIHPacket mihPacket (MIHPacket::create());
     // set some fields
     mihPacket->fragmentNr() = 42;
     mihPacket->transactionId() = 21;
-    mihPacket->src_mihfId().setMACAddress( MACAddress::from_string("01:02:03:04:05:06"));
-    mihPacket->dst_mihfId().setMACAddress( MACAddress::from_string("07:08:09:0a:0b:0c"));
+    mihPacket->src_mihfId().value( srcMac);
+    mihPacket->dst_mihfId().value( dstMac);
     mihPacket.finalizeThis();
 
     unsigned char data[] = {
@@ -131,12 +133,10 @@ BOOST_AUTO_UNIT_TEST(MIHPacket_create_mac)
     };
     SENF_CHECK_EQUAL_COLLECTIONS( data, data+sizeof(data),
             mihPacket.data().begin(), mihPacket.data().end() );
-    BOOST_CHECK_EQUAL(
-            mihPacket->src_mihfId().asMACAddress(),
-            MACAddress::from_string("01:02:03:04:05:06"));
-    BOOST_CHECK_EQUAL(
-            mihPacket->dst_mihfId().asMACAddress(),
-            MACAddress::from_string("07:08:09:0a:0b:0c"));
+    BOOST_CHECK_EQUAL( mihPacket->src_mihfId().valueAsMACAddress(), srcMac);
+    BOOST_CHECK_EQUAL( mihPacket->dst_mihfId().valueAsMACAddress(), dstMac);
+    BOOST_CHECK_EQUAL( mihPacket->dst_mihfId().valueAs( MIHFId::MACAddress), MIHFId(dstMac) );
+    BOOST_CHECK( mihPacket->src_mihfId().valueEquals(srcMac));
 }
 
 
@@ -146,8 +146,8 @@ BOOST_AUTO_UNIT_TEST(MIHPacket_create_inet4)
     // set some fields
     mihPacket->fragmentNr() = 42;
     mihPacket->transactionId() = 21;
-    mihPacket->src_mihfId().setINet4Address( INet4Address::from_string("128.129.130.131"));
-    mihPacket->dst_mihfId().setINet4Address( INet4Address::from_string("132.133.134.135"));
+    mihPacket->src_mihfId().value( INet4Address::from_string("128.129.130.131"));
+    mihPacket->dst_mihfId().value( INet4Address::from_string("132.133.134.135"));
     mihPacket.finalizeThis();
 
     unsigned char data[] = {
@@ -163,10 +163,10 @@ BOOST_AUTO_UNIT_TEST(MIHPacket_create_inet4)
     SENF_CHECK_EQUAL_COLLECTIONS( data, data+sizeof(data),
             mihPacket.data().begin(), mihPacket.data().end() );
     BOOST_CHECK_EQUAL(
-            mihPacket->src_mihfId().asINet4Address(),
+            mihPacket->src_mihfId().valueAsINet4Address(),
             INet4Address::from_string("128.129.130.131"));
     BOOST_CHECK_EQUAL(
-            mihPacket->dst_mihfId().asINet4Address(),
+            mihPacket->dst_mihfId().valueAsINet4Address(),
             INet4Address::from_string("132.133.134.135"));
 }
 
@@ -177,8 +177,8 @@ BOOST_AUTO_UNIT_TEST(MIHPacket_create_inet6)
     // set some fields
     mihPacket->fragmentNr() = 42;
     mihPacket->transactionId() = 21;
-    mihPacket->src_mihfId().setINet6Address( INet6Address::from_string("::ffff:1.2.3.4"));
-    mihPacket->dst_mihfId().setINet6Address( INet6Address::from_string("::ffff:5.6.7.8"));
+    mihPacket->src_mihfId().value( INet6Address::from_string("::ffff:1.2.3.4"));
+    mihPacket->dst_mihfId().value( INet6Address::from_string("::ffff:5.6.7.8"));
     mihPacket.finalizeThis();
 
     unsigned char data[] = {
@@ -202,10 +202,10 @@ BOOST_AUTO_UNIT_TEST(MIHPacket_create_inet6)
     SENF_CHECK_EQUAL_COLLECTIONS( data, data+sizeof(data),
             mihPacket.data().begin(), mihPacket.data().end() );
     BOOST_CHECK_EQUAL(
-            mihPacket->src_mihfId().asINet6Address(),
+            mihPacket->src_mihfId().valueAsINet6Address(),
             INet6Address::from_string("::ffff:1.2.3.4"));
     BOOST_CHECK_EQUAL(
-            mihPacket->dst_mihfId().asINet6Address(),
+            mihPacket->dst_mihfId().valueAsINet6Address(),
             INet6Address::from_string("::ffff:5.6.7.8") );
 }
 
@@ -264,8 +264,8 @@ BOOST_AUTO_UNIT_TEST(MIHPayload_create)
     MIHPacket mihPacket (MIHPacket::create());
     mihPacket->fragmentNr() = 42;
     mihPacket->transactionId() = 21;
-    mihPacket->src_mihfId().setString( "senf@berlios.de");
-    mihPacket->dst_mihfId().setString( "test");
+    mihPacket->src_mihfId().value( "senf@berlios.de");
+    mihPacket->dst_mihfId().value( "test");
 
     MIHGenericPayloadPacket mihPayload (MIHGenericPayloadPacket::createAfter(mihPacket));
     MIHGenericPayloadPacket::Parser::tlvList_t::container tlvListContainer (
index 45622fb..80d2ce7 100644 (file)
@@ -49,11 +49,11 @@ prefix_ void senf::MIHFIdTLVParser::dump(std::ostream & os)
     os << indent << "type:   " << unsigned (type()) << std::endl
        << indent << "length: " << unsigned (length()) << std::endl
        << indent << "value:\n";
-    std::string src_mihfId (asString());
+    std::string src_mihfId (valueAsString());
     hexdump(src_mihfId.begin(), src_mihfId.end(), os);
 }
 
-prefix_ void senf::MIHFIdTLVParser::setString(std::string const &id)
+prefix_ void senf::MIHFIdTLVParser::value(std::string const & id)
 {
     size_type str_size (id.size());
     // the maximum length of a MIHF_ID is 253 octets (see F.3.11 in 802.21)
@@ -63,30 +63,35 @@ prefix_ void senf::MIHFIdTLVParser::setString(std::string const &id)
     std::copy( id.begin(), id.end(), si);
 }
 
-prefix_ void senf::MIHFIdTLVParser::setMACAddress(senf::MACAddress const &mac)
+prefix_ void senf::MIHFIdTLVParser::value(senf::MACAddress const & addr)
 {
     safe_data_iterator si = resizeValueField(12);
-    std::copy( mac.begin(), mac.end(), getNAIEncodedOutputIterator(si));
+    std::copy( addr.begin(), addr.end(), getNAIEncodedOutputIterator(si));
 }
 
-prefix_ void senf::MIHFIdTLVParser::setINet4Address(senf::INet4Address const &addr)
+prefix_ void senf::MIHFIdTLVParser::value(senf::INet4Address const & addr)
 {
     safe_data_iterator si = resizeValueField(8);
     std::copy( addr.begin(), addr.end(), getNAIEncodedOutputIterator(si));
 }
 
-prefix_ void senf::MIHFIdTLVParser::setINet6Address(senf::INet6Address const &addr)
+prefix_ void senf::MIHFIdTLVParser::value(senf::INet6Address const & addr)
 {
     safe_data_iterator si = resizeValueField(32);
     std::copy( addr.begin(), addr.end(), getNAIEncodedOutputIterator(si));
 }
 
-prefix_ void senf::MIHFIdTLVParser::setEUI64(senf::EUI64 const &addr)
+prefix_ void senf::MIHFIdTLVParser::value(senf::EUI64 const & addr)
 {
     safe_data_iterator si = resizeValueField(16);
     std::copy( addr.begin(), addr.end(), getNAIEncodedOutputIterator(si));
 }
 
+prefix_ void senf::MIHFIdTLVParser::value( MIHFId const & id)
+{
+    boost::apply_visitor( ValueSetterVisitor(*this), id);
+}
+
 prefix_ senf::MIHFId senf::MIHFIdTLVParser::valueAs(MIHFId::Type type)
     const
 {
@@ -95,15 +100,15 @@ prefix_ senf::MIHFId senf::MIHFIdTLVParser::valueAs(MIHFId::Type type)
     case MIHFId::Empty:
         return MIHFId();
     case MIHFId::MACAddress:
-        return MIHFId( asMACAddress());
+        return MIHFId( valueAsMACAddress());
     case MIHFId::INet4Address:
-        return MIHFId( asINet4Address());
+        return MIHFId( valueAsINet4Address());
     case MIHFId::INet6Address:
-        return MIHFId( asINet6Address());
+        return MIHFId( valueAsINet6Address());
     case MIHFId::String:
-        return MIHFId( asINet6Address());
+        return MIHFId( valueAsString());
     case MIHFId::EUI64:
-        return MIHFId( asINet6Address());
+        return MIHFId( valueAsEUI64());
     }
     return MIHFId();
 }
index be724df..6a893be 100644 (file)
@@ -39,42 +39,93 @@ prefix_ senf::MIHTLVLengthParser::size_type senf::MIHTLVLengthParser::bytes() co
 }
 
 ///////////////////////////////////////////////////////////////////////////
+// MIHFIdTLVParser::binaryNAIDecoder
+
+prefix_ senf::MIHFIdTLVParser::binaryNAIDecoder::binaryNAIDecoder()
+    : readNextByte_( true)
+{}
+
+prefix_ bool senf::MIHFIdTLVParser::binaryNAIDecoder::operator()(boost::uint8_t v)
+{
+    readNextByte_ = readNextByte_ ? false : true;
+    return readNextByte_;
+}
+
+///////////////////////////////////////////////////////////////////////////
 // MIHFIdTLVParser
 
-prefix_ std::string senf::MIHFIdTLVParser::asString()
+prefix_ std::string senf::MIHFIdTLVParser::valueAsString()
     const
 {
     return std::string( i(1+length_().bytes()), i(1+length_().bytes()+length()) );
 }
 
-prefix_ senf::MACAddress senf::MIHFIdTLVParser::asMACAddress()
+prefix_ senf::MACAddress senf::MIHFIdTLVParser::valueAsMACAddress()
     const
 {
     return MACAddress::from_data( 
             getNAIDecodedIterator( i(1+length_().bytes()), i(1+length_().bytes()+12) ));
 }
 
-prefix_ senf::INet4Address senf::MIHFIdTLVParser::asINet4Address()
+prefix_ senf::INet4Address senf::MIHFIdTLVParser::valueAsINet4Address()
     const
 {
     return INet4Address::from_data( 
             getNAIDecodedIterator( i(1+length_().bytes()), i(1+length_().bytes()+8) ));
 }
 
-prefix_ senf::INet6Address senf::MIHFIdTLVParser::asINet6Address()
+prefix_ senf::INet6Address senf::MIHFIdTLVParser::valueAsINet6Address()
     const
 {
     return INet6Address::from_data( 
             getNAIDecodedIterator( i(1+length_().bytes()), i(1+length_().bytes()+32) ));
 }
 
-prefix_ senf::EUI64 senf::MIHFIdTLVParser::asEUI64()
+prefix_ senf::EUI64 senf::MIHFIdTLVParser::valueAsEUI64()
     const
 {
     return EUI64::from_data( 
             getNAIDecodedIterator( i(1+length_().bytes()), i(1+length_().bytes()+16) ));
 }
 
+prefix_ bool senf::MIHFIdTLVParser::valueEquals( std::string const &id) 
+    const
+{
+    return id == valueAsString();
+}
+
+prefix_ bool senf::MIHFIdTLVParser::valueEquals( senf::MACAddress const & addr)
+    const
+{
+    return length()==12 && addr==valueAsMACAddress();
+}
+
+prefix_ bool senf::MIHFIdTLVParser::valueEquals( senf::INet4Address const & addr)
+    const
+{
+    return length()==8 && addr==valueAsINet4Address();
+}
+
+prefix_ bool senf::MIHFIdTLVParser::valueEquals( senf::INet6Address const & addr)
+    const
+{
+    return length()==32 && addr==valueAsINet6Address();
+}
+
+prefix_ bool senf::MIHFIdTLVParser::valueEquals( senf::EUI64 const & addr)
+    const
+{
+    return length()==16 && addr==valueAsEUI64();
+}
+
+prefix_ bool senf::MIHFIdTLVParser::valueEquals( MIHFId const & id)
+    const
+{
+    return boost::apply_visitor( ValueEqualsVisitor(*this), id);
+}
+
+
+
 ///////////////////////////////cci.e////////////////////////////////////////
 #undef prefix_
 
diff --git a/senf/Packets/80221Bundle/TLVParser.cti b/senf/Packets/80221Bundle/TLVParser.cti
new file mode 100644 (file)
index 0000000..cd5c69e
--- /dev/null
@@ -0,0 +1,76 @@
+// $Id$
+//
+// Copyright (C) 2009
+// Fraunhofer Institute for Open Communication Systems (FOKUS)
+// Competence Center NETwork research (NET), St. Augustin, GERMANY
+//     Thorsten Horstmann <tho@berlios.de>
+//
+// This program is free software; you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation; either version 2 of the License, or
+// (at your option) any later version.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with this program; if not, write to the
+// Free Software Foundation, Inc.,
+// 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
+
+/** \file
+    \brief TLVParser inline template implementation */
+
+// Custom includes
+
+#define prefix_ inline
+///////////////////////////////cti.p///////////////////////////////////////
+
+///////////////////////////////////////////////////////////////////////////
+// senf::MIHFIdTLVParser::binaryNAIEncoder<OutputIterator>
+
+template <class OutputIterator>
+prefix_ senf::MIHFIdTLVParser::binaryNAIEncoder<OutputIterator>::binaryNAIEncoder(OutputIterator & i)
+    : i_(i)
+{}
+
+template <class OutputIterator>
+prefix_ void senf::MIHFIdTLVParser::binaryNAIEncoder<OutputIterator>::operator()(boost::uint8_t v) 
+{
+    *i_++ = '\\';
+    *i_++ = v;
+}
+
+///////////////////////////////////////////////////////////////////////////
+// senf::MIHFIdTLVParser
+
+template <class OutputIterator>
+prefix_ boost::function_output_iterator<senf::MIHFIdTLVParser::binaryNAIEncoder<OutputIterator> > 
+senf::MIHFIdTLVParser::getNAIEncodedOutputIterator(OutputIterator i) 
+{
+    return boost::make_function_output_iterator(binaryNAIEncoder<OutputIterator>(i));
+}
+
+template <class Iterator>
+prefix_ boost::filter_iterator<senf::MIHFIdTLVParser::binaryNAIDecoder, Iterator> 
+senf::MIHFIdTLVParser::getNAIDecodedIterator(Iterator begin, Iterator end) 
+{
+    return boost::make_filter_iterator<binaryNAIDecoder>(begin, end);
+}
+
+            
+///////////////////////////////cti.e///////////////////////////////////////
+#undef prefix_
+
+\f
+// Local Variables:
+// mode: c++
+// fill-column: 100
+// comment-column: 40
+// c-file-style: "senf"
+// indent-tabs-mode: nil
+// ispell-local-dictionary: "american"
+// compile-command: "scons -u test"
+// End:
index c8b49ad..2df7440 100644 (file)
@@ -171,52 +171,86 @@ namespace senf {
         SENF_PARSER_FINALIZE ( MIHFIdTLVParser  );
         
     public:
-        std::string asString() const;
-        void setString(std::string const &id);
-
-        senf::MACAddress asMACAddress() const;
-        void setMACAddress(senf::MACAddress const &mac);
-
-        senf::INet4Address asINet4Address() const;
-        void setINet4Address(senf::INet4Address const &addr);
-
-        senf::INet6Address asINet6Address() const;
-        void setINet6Address(senf::INet6Address const &addr);
+        ///\name value setters
+        ///@{
+        void value( MIHFId const & id);
         
-        senf::EUI64 asEUI64() const;
-        void setEUI64(senf::EUI64 const &addr);
+        void value( std::string        const & id  );
+        void value( senf::MACAddress   const & addr);
+        void value( senf::INet4Address const & addr);
+        void value( senf::INet6Address const & addr);
+        void value( senf::EUI64        const & addr);    
+        ///@}
 
-        MIHFId valueAs(MIHFId::Type type) const;
+        ///\name value getters
+        ///@{
+        MIHFId valueAs( MIHFId::Type type) const;
+        
+        std::string        valueAsString()       const;
+        senf::MACAddress   valueAsMACAddress()   const;
+        senf::INet4Address valueAsINet4Address() const;
+        senf::INet6Address valueAsINet6Address() const;
+        senf::EUI64        valueAsEUI64()        const;
+        ///@}
+        
+        ///\name value comparisons
+        ///@{
+        bool valueEquals( MIHFId const & id) const;
+        
+        bool valueEquals( std::string        const & id  ) const;
+        bool valueEquals( senf::MACAddress   const & addr) const;
+        bool valueEquals( senf::INet4Address const & addr) const;
+        bool valueEquals( senf::INet6Address const & addr) const;
+        bool valueEquals( senf::EUI64        const & addr) const;
+        ///@}
         
         void dump(std::ostream & os) const;
 
     private:
         template <class OutputIterator>
         struct binaryNAIEncoder {
-            binaryNAIEncoder(OutputIterator &i) : i_(i) {}
-            void operator()(const boost::uint8_t &v) const {
-                *i_++ = '\\';
-                *i_++ = v;
-            }
-            OutputIterator &i_;
+            binaryNAIEncoder(OutputIterator & i);
+            void operator()(boost::uint8_t v);
+            OutputIterator & i_;
         };
+        
         template <class OutputIterator>
-        static boost::function_output_iterator<binaryNAIEncoder<OutputIterator> > getNAIEncodedOutputIterator(OutputIterator i) {
-            return boost::make_function_output_iterator(binaryNAIEncoder<OutputIterator>(i));
-        }
+        static boost::function_output_iterator<binaryNAIEncoder<OutputIterator> > 
+        getNAIEncodedOutputIterator(OutputIterator i);
 
         struct binaryNAIDecoder {
-            binaryNAIDecoder() : readNextByte_(true) {}
-            bool operator()(const boost::uint8_t &v) {
-                readNextByte_ = readNextByte_ ? false : true;
-                return readNextByte_;
-            }
+            binaryNAIDecoder();
+            bool operator()(boost::uint8_t v);
             bool readNextByte_;
         };
+        
         template <class Iterator>
-        static boost::filter_iterator<binaryNAIDecoder, Iterator> getNAIDecodedIterator(Iterator begin, Iterator end) {
-            return boost::make_filter_iterator<binaryNAIDecoder>(begin, end);
-        }
+        static boost::filter_iterator<binaryNAIDecoder, Iterator> 
+        getNAIDecodedIterator(Iterator begin, Iterator end);
+        
+        struct ValueSetterVisitor : public boost::static_visitor<> {
+            MIHFIdTLVParser & parser;
+            ValueSetterVisitor( MIHFIdTLVParser & p) : parser(p) {}
+            void operator()( boost::blank ) const {
+                parser.value( std::string());
+            }
+            template <typename MIHFIdType>
+            void operator()( MIHFIdType const & id ) const {
+                parser.value( id);
+            }
+        };
+        
+        struct ValueEqualsVisitor : public boost::static_visitor<bool> {
+            MIHFIdTLVParser const & parser;
+            ValueEqualsVisitor( MIHFIdTLVParser const & p) : parser(p) {}
+            bool operator()( boost::blank ) const {
+                return parser.length() == 0;
+            }
+            template <typename MIHFIdType>
+            bool operator()( MIHFIdType const & id ) const {
+                return parser.valueEquals( id);
+            }
+        };
     };
 
     struct MIHFSrcIdTLVParser : public MIHFIdTLVParser
@@ -228,7 +262,6 @@ namespace senf {
             type() << typeId+0;
         }
         static type_t::value_type const typeId = 1;
-        
         void dump(std::ostream & os) const;
     };
     
@@ -241,7 +274,6 @@ namespace senf {
             type() << typeId+0;
         }
         static type_t::value_type const typeId = 2;
-        
         void dump(std::ostream & os) const;
     };
     
@@ -253,6 +285,7 @@ namespace senf {
         SENF_PARSER_INHERIT ( MIHBaseTLVParser   );
         SENF_PARSER_FIELD   ( value, UInt8Parser );
         SENF_PARSER_FINALIZE( MIHStatusTLVParser );
+        
         SENF_PARSER_INIT() {
             defaultInit();
             type() << typeId+0;
@@ -305,7 +338,7 @@ namespace senf {
 ///////////////////////////////hh.e////////////////////////////////////////
 #include "TLVParser.cci"
 //#include "TLVParser.ct"
-//#include "TLVParser.cti"
+#include "TLVParser.cti"
 #endif
 
 \f