BeaconConsumer.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 "simlpy/interpreter_defs.h"
00032 
00033 #include "BeaconConsumer.h"
00034 #include "MobileNode.h"
00035 #include "simlpy/Clock.h"
00036 
00037 #include <vector>
00038 
00039 using namespace Mobility;
00040 
00041 BeaconConsumer::BeaconConsumer( MobileNode* owner ) :
00042    Consumer( &(owner->node()) ),
00043    m_mobileOwner( owner )
00044 {
00045 }
00046 
00047 BeaconConsumer::~BeaconConsumer()
00048 {
00049 }
00050 
00051 void
00052 BeaconConsumer::operator()( const DTN::Bundle& b )
00053 {
00054    // recv() takes care of everything we need to do.  The base class
00055    // requires that this be defined, though.
00056 }
00057 
00058 void
00059 BeaconConsumer::send( const DTN::Bundle& b )
00060 {
00061    if ( b.type() & DTN::Bundle::kBcast )
00062    {
00063       m_lastSend = Clock::time();
00064    }
00065 }
00066 
00067 void
00068 BeaconConsumer::recv( const DTN::Bundle& b )
00069 {
00070    const DTN::ByteString& addr = b.send();
00071    const Time& t = Clock::time();
00072    if ( m_neighbors.end() == m_neighbors.find(addr) )
00073    {
00074       m_mobileOwner->notifyApps(addr);
00075    }
00076    m_neighbors[addr] = t;
00077 }
00078 
00079 void
00080 BeaconConsumer::setTimeout( const Time& t )
00081 {
00082    m_timeout = t;
00083 }
00084 
00085 void
00086 BeaconConsumer::clean()
00087 {
00088    // Apparently, backing up in a map doesn't preserve the goodness
00089    // of an iterator when an erasure occurs.  So, we keep a to-erase
00090    // list separately.
00091    typedef std::vector< DTN::ByteString >  ErasureList;
00092    ErasureList  timedOut;
00093    Time lastGood = Clock::time() - m_timeout;
00094    NodeMap::iterator itr = m_neighbors.begin();
00095    for ( ; itr != m_neighbors.end() ; ++itr )
00096    {
00097       if ( itr->second < lastGood )
00098       {
00099          timedOut.push_back( itr->first );
00100       }
00101    }
00102 
00103    ErasureList::iterator eitr = timedOut.begin();
00104    ErasureList::iterator eend = timedOut.end();
00105    for ( ; eitr != eend ; ++eitr )
00106    {
00107       m_neighbors.erase( *eitr );
00108    }
00109 }
00110 
00111 bool
00112 BeaconConsumer::visible( const DTN::ByteString& addr )
00113 {
00114    NodeMap::iterator itr = m_neighbors.find( addr );
00115    if ( m_neighbors.end() == itr )
00116    {
00117       return false;
00118    }
00119    if ( itr->second + m_timeout < Clock::time() )
00120    {
00121       m_neighbors.erase(itr);
00122       return false;
00123    }
00124    return true;
00125 }
00126 
00127 InterpreterItem
00128 BeaconConsumer::visible_list()
00129 {
00130    clean();
00131    Entity::ArgList alist;
00132    NodeMap::iterator itr = m_neighbors.begin();
00133    for ( ; itr != m_neighbors.end() ; ++itr )
00134    {
00135       alist.push_back(
00136          PyString_FromStringAndSize( (const char*)(itr->first.data()),
00137                                      itr->first.length() ) );
00138    }
00139    return construct_list( alist );
00140 }

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