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 "DFPolicy.h"
00032 #include "DFApp.h"
00033 #include "dtn/Bundle.h"
00034 #include "dtn/Node.h"
00035
00036 using namespace DifferentialForwarding;
00037
00038 DFPolicy::DFPolicy( DTN::Node* owner, DTN::Link* link, DFApp* app ) :
00039 DTN::ForwardingPolicy( owner ),
00040 m_link( link ),
00041 m_app( app )
00042 {
00043 }
00044
00045 DFPolicy::~DFPolicy()
00046 {
00047 }
00048
00049 bool
00050 DFPolicy::forward( DTN::Bundle* b )
00051 {
00052 if ( 0 == m_owner ) return false;
00053 if ( 0 == m_link ) return false;
00054 if ( 0 == m_app ) return false;
00055 if ( 0 == b ) return false;
00056
00057
00058
00059 if ( ( DTN::Bundle::kBcast & b->type() ) ||
00060 ( DTN::Bundle::kACK & b->type() ) )
00061 {
00062 if ( m_link->available() )
00063 {
00064 m_owner->send( b, *m_link );
00065 return true;
00066 }
00067 return false;
00068 }
00069
00070
00071 DTN::Link* dl = m_app->linked( b->destination() );
00072 if ( 0 == dl ) return false;
00073 if ( m_link->available() )
00074 {
00075 m_owner->send( b, *dl );
00076 return true;
00077 }
00078
00079 return false;
00080 }
00081
00082 DTN::Bundle*
00083 DFPolicy::forwardOn( const DTN::Link& l )
00084 {
00085 if ( 0 == m_owner ) return 0;
00086 if ( 0 == m_link ) return 0;
00087 if ( 0 == m_app ) return 0;
00088
00089 DTN::BundlePointer firstFallback;
00090 DTN::BundlePointer p = m_owner->cachedVolatile();
00091
00092
00093
00094
00095 while ( ! p.isNull() )
00096 {
00097 DTN::Bundle* b = p.repr()->bundle();
00098 if ( 0 != m_app->linked( b->destination() ) )
00099 {
00100 m_owner->remove(b,false);
00101 return b;
00102 }
00103 if ( firstFallback.isNull() )
00104 {
00105 if ( ( DTN::Bundle::kBcast & b->type() ) ||
00106 ( DTN::Bundle::kACK & b->type() ) )
00107 {
00108 firstFallback = p;
00109 }
00110 }
00111 p = p.next();
00112 }
00113
00114
00115
00116 if ( ! firstFallback.isNull() )
00117 {
00118 DTN::Bundle* b = firstFallback.repr()->bundle();
00119 m_owner->remove(b,false);
00120 return b;
00121 }
00122
00123
00124 return 0;
00125 }