00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031 #include "PrefixRoutingTable.h"
00032 #include "PrefixRoute.h"
00033 #include "Link.h"
00034 #include "Bundle.h"
00035 #include "Exception.h"
00036
00037 using namespace DTN;
00038
00039 PrefixRoutingTable::PrefixRoutingTable()
00040 {
00041 }
00042
00043 PrefixRoutingTable::~PrefixRoutingTable()
00044 {
00045 RouteMap::iterator end = m_routes.end();
00046 RouteMap::iterator itr = m_routes.begin();
00047 for ( ; itr != end ; ++itr )
00048 {
00049 if ( 0 != itr->second )
00050 {
00051 delete itr->second;
00052 }
00053 }
00054 }
00055
00056 Link&
00057 PrefixRoutingTable::target( const Bundle& b ) const
00058 {
00059 unsigned int destLength = b.destination().length();
00060
00061 for ( int length = destLength ; length >= 0 ; --length )
00062 {
00063 ByteString substring = b.destination().substr(0,length);
00064 RouteMap::iterator rmItr = m_routes.find( substring );
00065 if ( m_routes.end() != rmItr )
00066 {
00067 if ( 0 == rmItr->second )
00068 {
00069 throw Exception( __FILE__ , __LINE__ );
00070 }
00071 if ( ! rmItr->second->matches( b ) )
00072 {
00073 throw Exception( __FILE__ , __LINE__ );
00074 }
00075 return rmItr->second->link();
00076 }
00077 }
00078
00079 throw NoRoute( __FILE__ , __LINE__ );
00080 }
00081
00082 void
00083 PrefixRoutingTable::addRoute( PrefixRoute* r )
00084 {
00085 if ( 0 != r )
00086 {
00087 m_routes.insert( RouteMap::value_type( r->prefix(), r ) );
00088 }
00089 }
00090
00091 void
00092 PrefixRoutingTable::delRoute( const ByteString& pfix )
00093 {
00094 m_routes.erase( pfix );
00095 }