From: tho Date: Tue, 11 Jan 2011 15:17:37 +0000 (+0000) Subject: Packets/80221Bundle: revised signature of validate function X-Git-Url: http://g0dil.de/git?a=commitdiff_plain;h=bd52d6b1c3b258c030c7302f5e926aeb5b5b0715;p=senf.git Packets/80221Bundle: revised signature of validate function git-svn-id: https://svn.berlios.de/svnroot/repos/senf/trunk@1760 270642c3-0616-0410-b53a-bc976706d245 --- diff --git a/senf/Packets/80221Bundle/MIHMessageRegistry.cc b/senf/Packets/80221Bundle/MIHMessageRegistry.cc index 61fcb1e..fbec742 100644 --- a/senf/Packets/80221Bundle/MIHMessageRegistry.cc +++ b/senf/Packets/80221Bundle/MIHMessageRegistry.cc @@ -38,12 +38,11 @@ # define PTRMAP_GET_CONTENTS(v) (*(v).second) #endif -prefix_ std::pair senf::MIHMessageRegistry::validate(key_t messageId, senf::Packet message) +prefix_ void senf::MIHMessageRegistry::validate(key_t messageId, senf::Packet message) { Map::const_iterator i (map_.find( messageId)); if (i != map_.end()) - return PTRMAP_GET_CONTENTS(*i).validate( message); - return std::make_pair(true, ""); + PTRMAP_GET_CONTENTS(*i).validate( message); } #undef PTRMAP_GET_CONTENTS diff --git a/senf/Packets/80221Bundle/MIHMessageRegistry.hh b/senf/Packets/80221Bundle/MIHMessageRegistry.hh index cf125d6..4daabb7 100644 --- a/senf/Packets/80221Bundle/MIHMessageRegistry.hh +++ b/senf/Packets/80221Bundle/MIHMessageRegistry.hh @@ -55,23 +55,21 @@ namespace senf { struct MIHMessageRegistry_EntryBase { virtual ~MIHMessageRegistry_EntryBase() {} - virtual std::pair validate(senf::Packet message) const = 0; + virtual void validate(senf::Packet message) const = 0; }; template (MIHPacket)>::value> + bool use_validate_member = has_static_validate_member::value> struct MIHMessageRegistryEntry : MIHMessageRegistry_EntryBase { - virtual std::pair validate(senf::Packet message) const { - return std::make_pair(true, ""); - } + virtual void validate(senf::Packet message) const {} }; template struct MIHMessageRegistryEntry : MIHMessageRegistry_EntryBase { - virtual std::pair validate(senf::Packet message) const { - return MIHPacket::type::validate(message.as()); + virtual void validate(senf::Packet message) const { + MIHPacket::type::validate(message.as()); } }; } @@ -94,7 +92,7 @@ namespace senf { template void registerMessageType(); - std::pair validate(key_t messageId, senf::Packet message); + void validate(key_t messageId, senf::Packet message); private: typedef boost::ptr_map Map; diff --git a/senf/Packets/80221Bundle/MIHMessageRegistry.test.cc b/senf/Packets/80221Bundle/MIHMessageRegistry.test.cc index 15eda8b..2c06fd3 100644 --- a/senf/Packets/80221Bundle/MIHMessageRegistry.test.cc +++ b/senf/Packets/80221Bundle/MIHMessageRegistry.test.cc @@ -80,7 +80,7 @@ namespace test { static const boost::uint16_t MESSAGE_ID; - static std::pair validate(packet message) { + static void validate(packet message) { return message->registerRequestCodeTLV().validate(); } }; @@ -106,23 +106,23 @@ SENF_AUTO_UNIT_TEST(MIHMessageRegistry_validate) mihPacket->src_mihfId().value( "senf@berlios.de"); mihPacket->dst_mihfId().value( "test"); - BOOST_CHECK(! MIHPacketType::validate( mihPacket).first); + BOOST_CHECK_THROW( MIHPacketType::validate( mihPacket), InvalidMIHPacketException); mihPacket.finalizeThis(); - BOOST_CHECK( MIHPacketType::validate( mihPacket).first); + BOOST_CHECK_NO_THROW( MIHPacketType::validate( mihPacket)); { test::TestMessagePacket testMessage (test::TestMessagePacket::createAfter(mihPacket)); mihPacket.finalizeAll(); - BOOST_CHECK( MIHPacketType::validate( mihPacket).first); + BOOST_CHECK_NO_THROW( MIHPacketType::validate( mihPacket)); } { test::ValidatedTestMessagePacket testMessage (test::ValidatedTestMessagePacket::createAfter(mihPacket)); mihPacket.finalizeAll(); testMessage->registerRequestCodeTLV().value() << 3; - BOOST_CHECK(! MIHPacketType::validate( mihPacket).first); + BOOST_CHECK_THROW( MIHPacketType::validate( mihPacket), InvalidMIHPacketException); testMessage->registerRequestCodeTLV().value() << 1; - BOOST_CHECK( MIHPacketType::validate( mihPacket).first); + BOOST_CHECK_NO_THROW( MIHPacketType::validate( mihPacket)); } } diff --git a/senf/Packets/80221Bundle/MIHPacket.cc b/senf/Packets/80221Bundle/MIHPacket.cc index a33bc65..50b4439 100644 --- a/senf/Packets/80221Bundle/MIHPacket.cc +++ b/senf/Packets/80221Bundle/MIHPacket.cc @@ -78,21 +78,20 @@ prefix_ senf::PacketInterpreterBase::factory_t senf::MIHPacketType::nextPacketTy return e ? e->factory() : MIHGenericPayloadPacket::factory(); } -prefix_ std::pair senf::MIHPacketType::validate(packet p) +prefix_ void senf::MIHPacketType::validate(packet p) { try { if (p.data().size() < initSize()) - return std::make_pair(false, "truncated MIH message"); + throw InvalidMIHPacketException("truncated MIH message"); if (p->version() != 1) - return std::make_pair(false, "invalid MIH version: " + senf::str(p->version()) ); + throw InvalidMIHPacketException("invalid MIH version: ") << senf::str(p->version()); if (p->payloadLength() != p.size()-8) - return std::make_pair(false, "wrong MIH length: " + senf::str(p->payloadLength()) ); + throw InvalidMIHPacketException("wrong MIH length: ") << senf::str(p->payloadLength()); if (p.next(senf::nothrow)) - return MIHMessageRegistry::instance().validate( p->messageId(), p.next()); + MIHMessageRegistry::instance().validate( p->messageId(), p.next()); } catch (senf::TruncatedPacketException e) { - return std::make_pair(false, "truncated MIH message"); + throw InvalidMIHPacketException("truncated MIH message"); } - return std::make_pair(true, ""); } //-///////////////////////////////////////////////////////////////////////////////////////////////// diff --git a/senf/Packets/80221Bundle/MIHPacket.hh b/senf/Packets/80221Bundle/MIHPacket.hh index 385b2ff..8b54535 100644 --- a/senf/Packets/80221Bundle/MIHPacket.hh +++ b/senf/Packets/80221Bundle/MIHPacket.hh @@ -109,8 +109,7 @@ namespace senf { static void dump(packet p, std::ostream &os); static void finalize(packet p); static factory_t nextPacketType(packet p); - - static std::pair validate(packet p); + static void validate(packet p); }; /** \brief MIH packet typedef diff --git a/senf/Packets/80221Bundle/TLVParser.cc b/senf/Packets/80221Bundle/TLVParser.cc index 5d6a20c..5f35f25 100644 --- a/senf/Packets/80221Bundle/TLVParser.cc +++ b/senf/Packets/80221Bundle/TLVParser.cc @@ -42,16 +42,15 @@ SENF_PACKET_TLV_REGISTRY_REGISTER( senf::MIHValidTimeIntervalTLVParser ); //-///////////////////////////////////////////////////////////////////////////////////////////////// // MIHBaseTLVParser -prefix_ std::pair senf::MIHBaseTLVParser::validateTL(boost::uint8_t expectedType, MIHTLVLengthParser::value_type expectedLength) +prefix_ void senf::MIHBaseTLVParser::validateTL(boost::uint8_t expectedType, MIHTLVLengthParser::value_type expectedLength) const { if (! check( 1 + senf::bytes(length_()) + length()) ) - return std::make_pair(false, "truncated TLV. Type: " + senf::str(type())); + throw InvalidMIHPacketException("truncated TLV.") << " Type: " << senf::str(type()); if (type() != expectedType) - return std::make_pair(false, "invalid TLV type: " + senf::str(type())); + throw InvalidMIHPacketException("invalid TLV type") << " Type: " << senf::str(type()); if (length() != expectedLength) - return std::make_pair(false, "invalid length in TLV. Type: " + senf::str(type())); - return std::make_pair(true, ""); + throw InvalidMIHPacketException("invalid length in TLV.") << " Type: " << senf::str(type()); } //-///////////////////////////////////////////////////////////////////////////////////////////////// @@ -183,14 +182,13 @@ prefix_ void senf::MIHFSrcIdTLVParser::dump(std::ostream & os) MIHFIdTLVParser::dump(os); } -prefix_ std::pair senf::MIHFSrcIdTLVParser::validate() +prefix_ void senf::MIHFSrcIdTLVParser::validate() const { if (! check( 1 + senf::bytes(length_()) + length()) ) - return std::make_pair(false, "truncated TLV. Type: " + senf::str(type())); + throw InvalidMIHPacketException("truncated TLV.") << " Type: " << senf::str(type()); if (type() != typeId) - return std::make_pair(false, "invalid TLV type: " + senf::str(type())); - return std::make_pair(true, ""); + throw InvalidMIHPacketException("invalid TLV type: ") << senf::str(type()); } @@ -205,14 +203,13 @@ prefix_ void senf::MIHFDstIdTLVParser::dump(std::ostream & os) MIHFIdTLVParser::dump(os); } -prefix_ std::pair senf::MIHFDstIdTLVParser::validate() +prefix_ void senf::MIHFDstIdTLVParser::validate() const { if (! check( 1 + senf::bytes(length_()) + length()) ) - return std::make_pair(false, "truncated TLV. Type: " + senf::str(type())); + throw InvalidMIHPacketException("truncated TLV.") << " Type: " << senf::str(type()); if (type() != typeId) - return std::make_pair(false, "invalid TLV type: " + senf::str(type())); - return std::make_pair(true, ""); + throw InvalidMIHPacketException("invalid TLV type: ") << senf::str(type()); } //-///////////////////////////////////////////////////////////////////////////////////////////////// @@ -247,14 +244,12 @@ prefix_ void senf::MIHStatusTLVParser::dump(std::ostream & os) os << " (???; invalid value!)" << std::endl; } -prefix_ std::pair senf::MIHStatusTLVParser::validate() +prefix_ void senf::MIHStatusTLVParser::validate() const { - if (! validateTL( typeId, 1).first) - return validateTL( typeId, 1); + validateTL( typeId, 1); if (value() >= 4) - return std::make_pair(false, "invalid value in MIHStatusTLV " + senf::str(value())); - return std::make_pair(true, ""); + throw InvalidMIHPacketException("invalid value in MIHStatusTLV ") << senf::str(value()); } //-///////////////////////////////////////////////////////////////////////////////////////////////// @@ -280,14 +275,12 @@ prefix_ void senf::MIHRegisterReqCodeTLVParser::dump(std::ostream & os) os << " (???; invalid value!)" << std::endl; } -prefix_ std::pair senf::MIHRegisterReqCodeTLVParser::validate() +prefix_ void senf::MIHRegisterReqCodeTLVParser::validate() const { - if (! validateTL( typeId, 1).first) - return validateTL( typeId, 1); + validateTL( typeId, 1); if (value() >= 2) - return std::make_pair(false, "invalid value in MIHRegisterReqCodeTLV " + senf::str(value())); - return std::make_pair(true, ""); + throw InvalidMIHPacketException("invalid value in MIHRegisterReqCodeTLV ") << senf::str(value()); } //-///////////////////////////////////////////////////////////////////////////////////////////////// @@ -305,11 +298,10 @@ prefix_ void senf::MIHValidTimeIntervalTLVParser::dump(std::ostream & os) << ( value()==0 ? " (infinite)" : " seconds") << std::endl; } -prefix_ std::pair senf::MIHValidTimeIntervalTLVParser::validate() +prefix_ void senf::MIHValidTimeIntervalTLVParser::validate() const { - if (! validateTL( typeId, 4).first) return validateTL( typeId, 4); - return std::make_pair(true, ""); + validateTL( typeId, 4); } //-///////////////////////////////////////////////////////////////////////////////////////////////// diff --git a/senf/Packets/80221Bundle/TLVParser.hh b/senf/Packets/80221Bundle/TLVParser.hh index da6a96f..ce40481 100644 --- a/senf/Packets/80221Bundle/TLVParser.hh +++ b/senf/Packets/80221Bundle/TLVParser.hh @@ -41,6 +41,11 @@ namespace senf { : senf::Exception("MIHTLVLengthException") {} }; + struct InvalidMIHPacketException : public senf::Exception { + InvalidMIHPacketException(std::string const & description) + : senf::Exception("Invalid MIH message: ") { append(description); } + }; + class MIHTLVLengthParser : public detail::packet::IntParserOps, @@ -128,7 +133,7 @@ namespace senf { */ void maxLength(MIHTLVLengthParser::value_type maxl) const; - std::pair validateTL(boost::uint8_t type, MIHTLVLengthParser::value_type length) const; + void validateTL(boost::uint8_t type, MIHTLVLengthParser::value_type length) const; }; @@ -294,7 +299,7 @@ namespace senf { } static type_t::value_type const typeId = 1; void dump(std::ostream & os) const; - std::pair validate() const; + void validate() const; }; /** \brief Parser for 802.21 destination MIHF_ID TLV @@ -309,7 +314,7 @@ namespace senf { } static type_t::value_type const typeId = 2; void dump(std::ostream & os) const; - std::pair validate() const; + void validate() const; }; /** \brief Parser for 802.21 Status TLV @@ -328,7 +333,7 @@ namespace senf { } static type_t::value_type const typeId = 3; void dump(std::ostream & os) const; ///< dump string representation to given stream - std::pair validate() const; + void validate() const; enum StatusCode { Success, UnspecifiedFailure, Rejected, AuthorizationFailure, NetworkError }; @@ -348,7 +353,7 @@ namespace senf { } static type_t::value_type const typeId = 11; void dump(std::ostream & os) const; ///< dump string representation to given stream - std::pair validate() const; + void validate() const; enum RequestCode { Registration, ReRegistration }; }; @@ -367,7 +372,7 @@ namespace senf { } static type_t::value_type const typeId = 12; void dump(std::ostream & os) const; ///< dump string representation to given stream - std::pair validate() const; + void validate() const; }; }