RoutedForwarding.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 "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    // We know the BundlePointer is valid when this method is called,
00047    // so we can forego testing it.
00048    try
00049    {
00050       const Link& l = target( *(p.repr()->bundle()) );
00051       m_cache[ &l ].push_back( p ); // FIFO
00052    }
00053    catch ( ... )
00054    {
00055       // do nothing
00056    }
00057 }
00058 
00059 bool
00060 RoutedForwarding::forward( Bundle* b )
00061 {
00062    if ( 0 == m_owner ) return false;
00063 
00064    // This is FIFO.  Why?  Because a Link that's available clearly
00065    // didn't have any other Bundles waiting when it went from
00066    // unavailable to available.  If there are any corner cases in
00067    // which this turns out not to be true, I'm willing to accept that.
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); // The Link will take ownership
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 }

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