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 "config.h"
00032
00033 #include <iostream>
00034
00035 #include "RoutedForwarding.h"
00036 #include "Node.h"
00037 #include "Link.h"
00038 #include "Bundle.h"
00039 #include "BundlePointer.h"
00040
00041 using namespace DTN;
00042
00043 void
00044 RoutedForwarding::cache( BundlePointer& p )
00045 {
00046
00047
00048 try
00049 {
00050 const Link& l = target( *(p.repr()->bundle()) );
00051 m_cache[ &l ].push_back( p );
00052 }
00053 catch ( ... )
00054 {
00055
00056 }
00057 }
00058
00059 bool
00060 RoutedForwarding::forward( Bundle* b )
00061 {
00062 if ( 0 == m_owner ) return false;
00063
00064
00065
00066
00067
00068 try
00069 {
00070 Link& l = target( *b );
00071 if ( l.available() )
00072 {
00073 m_owner->send( b, l );
00074 return true;
00075 }
00076 }
00077 catch ( ... )
00078 {
00079 return false;
00080 }
00081 return false;
00082 }
00083
00084 Bundle*
00085 RoutedForwarding::forwardOn( const Link& l )
00086 {
00087 if ( 0 == m_owner )
00088 {
00089 std::cerr << "Object of type RoutedForwarding has no owner" << std::endl;
00090 throw Exception( __FILE__ , __LINE__ );
00091 }
00092
00093 BundleCache::iterator itr = m_cache.find( &l );
00094 if ( m_cache.end() == itr )
00095 {
00096 return 0;
00097 }
00098 while ( 0 != itr->second.size() )
00099 {
00100 BundlePointer p = itr->second.front();
00101 itr->second.pop_front();
00102 if ( m_owner->validate(p) )
00103 {
00104 Bundle* b = p.repr()->bundle();
00105 m_owner->remove(b,false);
00106 return b;
00107 }
00108 }
00109 return 0;
00110 }
00111
00112 void
00113 RoutedForwarding::addRouting( RoutingTable* table )
00114 {
00115 if ( 0 != table )
00116 m_routing = table;
00117 }
00118
00119 Link&
00120 RoutedForwarding::target( const Bundle& b )
00121 {
00122 if ( 0 != m_routing )
00123 {
00124 return m_routing->target( b );
00125 }
00126 else
00127 {
00128 throw Exception( __FILE__ , __LINE__ );
00129 }
00130 }
00131
00132 void
00133 RoutedForwarding::protected_set()
00134 {
00135 m_cache.clear();
00136 }