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 "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
00055
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
00089
00090
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 }