EpidemicForwarding.cc

00001 // Copyright 2008 Michael Marsh, University of Maryland.
00002 //
00003 // This file is part of pydtn.
00004 //
00005 // pydtn is free software: you can redistribute it and/or modify
00006 // it under the terms of the GNU General Public License as published by
00007 // the Free Software Foundation, either version 3 of the License, or
00008 // (at your option) any later version.
00009 //
00010 // pydtn is distributed in the hope that it will be useful,
00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013 // GNU General Public License for more details.
00014 //
00015 // You should have received a copy of the GNU General Public License
00016 // along with pydtn.  If not, see <http://www.gnu.org/licenses/>.
00017 //
00018 // The views and conclusions contained in the software and documentation
00019 // are those of the authors and should not be interpreted as representing
00020 // official policies, either expressed or implied, of the University
00021 // of Maryland.
00022 //
00023 // pydtn extends and embeds the Python interpreter, which is
00024 // Copyright 2001-2006 Python Software Foundation, All Rights Reserved,
00025 // and is released under the PSF License Agreement.
00026 //
00027 // RANLUX random number generation uses the Boost library,
00028 // Copyright 1994-2006 by various authors (details in individual files),
00029 // which is released under the Boost Software License, Version 1.0.
00030 
00031 #include "EpidemicForwarding.h"
00032 #include "EpidemicApp.h"
00033 
00034 #include "dtn/Bundle.h"
00035 #include "dtn/Node.h"
00036 
00037 using namespace Epidemic;
00038 
00039 EpidemicForwarding::EpidemicForwarding( DTN::Node* owner,
00040                                         DTN::Link* link,
00041                                         EpidemicApp* app ):
00042    DTN::ForwardingPolicy(owner),
00043    m_link(link),
00044    m_app(app)
00045 {
00046 }
00047 
00048 EpidemicForwarding::~EpidemicForwarding()
00049 {
00050 }
00051 
00052 bool
00053 EpidemicForwarding::forward( DTN::Bundle* b )
00054 {
00055    if ( 0 == b ) return false;
00056    if ( 0 == m_owner ) return false;
00057    if ( 0 == m_link ) return false;
00058    if ( 0 == m_app ) return false;
00059 
00060    if ( m_link->available() )
00061    {
00062       try
00063       {
00064          if ( DTN::Bundle::kBcast & b->type() )
00065          {
00066             m_owner->send( b , *m_link );
00067             return true;
00068          }
00069          DTN::Link* l = m_app->getLink( b->recv() );
00070          if ( 0 != l )
00071          {
00072             m_owner->send( b , *l );
00073             return true;
00074          }
00075       }
00076       catch ( ... )
00077       {
00078       }
00079    }
00080 
00081    return false;
00082 }
00083 
00084 DTN::Bundle*
00085 EpidemicForwarding::forwardOn( const DTN::Link& l )
00086 {
00087    if ( 0 == m_owner ) return 0;
00088 
00089    DTN::Bundle* retval = 0;
00090 
00091    DTN::BundlePointer p =  m_owner->cachedVolatile();
00092    DTN::BundlePointer prev;
00093    while ( ! p.isNull() )
00094    {
00095       DTN::Bundle* b = p.repr()->bundle();
00096       if ( ( !(DTN::Bundle::kBcast & b->type()) ) &&
00097            ( 0 == m_app->getLink( b->recv() ) ) )
00098       {
00099          m_owner->remove(b,true,DTN::NoRouteDrop::inst);
00100          p = prev;
00101       }
00102       else if ( 0 == retval )
00103       {
00104          retval = b;
00105       }
00106       prev = p;
00107       if ( p.isNull() )
00108       {
00109          p = m_owner->cachedVolatile();
00110       }
00111       else
00112       {
00113          p = p.next();
00114       }
00115    }
00116 
00117    if ( 0 != retval )
00118    {
00119       m_owner->remove(retval,false);
00120    }
00121    return retval;
00122 }

Generated on Mon Mar 24 11:15:45 2008 for Pydtn Simulator by  doxygen 1.5.4