Statistics.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 "Statistics.h"
00032 #include "dtn/Bundle.h"
00033 #include "dtn/Node.h"
00034 #include <math.h>
00035 #include <netinet/in.h>
00036 
00037 double
00038 Stat::mean() const
00039 {
00040    return ( 1.0 * m_s ) / m_n;
00041 }
00042 
00043 double
00044 Stat::var() const
00045 {
00046    double m = mean();
00047    double v = ( 1.0 * m_w ) / m_n;
00048    return v - m*m;
00049 }
00050 
00051 double
00052 Stat::sigma() const
00053 {
00054    return sqrt(var());
00055 }
00056 
00057 void
00058 Stat::add( unsigned long val )
00059 {
00060    ++m_n;
00061    m_s += val;
00062    m_w += val*val;
00063 }
00064 
00065 void
00066 Stat::reset()
00067 {
00068    m_n = 0;
00069    m_s = 0;
00070    m_w = 0;
00071 }
00072 
00073 UniqueStat::UniqueStat()
00074 {
00075 }
00076 
00077 UniqueStat::~UniqueStat()
00078 {
00079 }
00080 
00081 void
00082 UniqueStat::add( const DTN::Bundle& b )
00083 {
00084    unsigned long int length = b.size();
00085    if ( m_seen[b.source()].add( ntohl(b.seqNum()) ) )
00086    {
00087       m_unique.add(length);
00088    }
00089    else
00090    {
00091       m_duplicate.add(length);
00092    }
00093 }
00094 
00095 void
00096 UniqueStat::reset()
00097 {
00098    m_unique.reset();
00099    m_duplicate.reset();
00100 }
00101 
00102 void
00103 StatCollection::addOriginated( const DTN::Bundle& b )
00104 {
00105    m_originated.add(b);
00106 }
00107 
00108 void
00109 StatCollection::addSent( unsigned long int val )
00110 {
00111    m_sent.add(val);
00112 }
00113 
00114 void
00115 StatCollection::addReceived( unsigned long int val )
00116 {
00117    m_received.add(val);
00118 }
00119 
00120 void
00121 StatCollection::addDelivered( const DTN::Bundle& b )
00122 {
00123    m_delivered.add(b);
00124    m_deliverHops.add(b.hopCount());
00125 }
00126 
00127 void
00128 StatCollection::addDropped( const DTN::Bundle& b )
00129 {
00130    m_dropped.add(b.size());
00131    m_dropHops.add(b.hopCount());
00132 }
00133 
00134 void
00135 StatCollection::reset()
00136 {
00137    m_originated.reset();
00138    m_sent.reset();
00139    m_received.reset();
00140    m_delivered.reset();
00141    m_dropped.reset();
00142    m_exhausted = false;
00143 }
00144 
00145 Statistics::Statistics( DTN::Node* owner ) :
00146    DTN::Consumer( owner )
00147 {
00148 }
00149 
00150 Statistics::~Statistics()
00151 {
00152 }
00153 
00154 void
00155 Statistics::operator()( const DTN::Bundle& b )
00156 {
00157    if ( DTN::Bundle::kACK & b.type() )
00158    {
00159       return;
00160    }
00161 
00162    if ( DTN::Bundle::kData & b.type() )
00163    {
00164       m_since.addDelivered( b );
00165       m_total.addDelivered( b );
00166    }
00167 }
00168 
00169 void
00170 Statistics::drop( const DTN::Bundle& b, const DTN::DropCause& c )
00171 {
00173    m_since.addDropped( b );
00174    m_total.addDropped( b );
00175 }
00176 
00177 void
00178 Statistics::custody( const DTN::Bundle& b )
00179 {
00180 }
00181 
00182 void
00183 Statistics::persistentStore( const DTN::Bundle& b )
00184 {
00185    if ( 0 == m_owner ) return;
00186 
00187    unsigned long int cap  = m_owner->persistentCap();
00188    unsigned long int used = m_owner->usedPersistentCap();
00189    if ( used >= cap )
00190    {
00191       m_since.setExhausted();
00192       m_total.setExhausted();
00193    }
00194 }
00195 
00196 void
00197 Statistics::testOrigin( const DTN::Bundle& b )
00198 {
00199    bool origin = false;
00200    if ( !( DTN::Bundle::kACK & b.type()) )
00201    {
00202       origin = b.send() == b.source();
00203    }
00204 
00205    if ( origin )
00206    {
00207       m_since.addOriginated( b );
00208       m_total.addOriginated( b );
00209    }
00210 }
00211 
00212 void
00213 Statistics::persistentRemove( const DTN::Bundle& b, bool cleanup )
00214 {
00215 }
00216 
00217 void
00218 Statistics::send( const DTN::Bundle& b )
00219 {
00220    unsigned long int length = b.size();
00221    m_since.addSent( length );
00222    m_total.addSent( length );
00223 
00224    testOrigin(b);
00225 }
00226 
00227 void
00228 Statistics::recv( const DTN::Bundle& b )
00229 {
00230    unsigned long int length = b.size();
00231    m_since.addReceived( length );
00232    m_total.addReceived( length );
00233 }
00234 
00235 void
00236 Statistics::exhausted( const DTN::Bundle& b )
00237 {
00238    m_since.setExhausted();
00239    m_total.setExhausted();
00240 }
00241 
00242 unsigned long int
00243 Statistics::used() const
00244 {
00245    if ( 0 == m_owner ) return 0;
00246    return m_owner->usedPersistentCap();
00247 }
00248 
00249 unsigned long int
00250 Statistics::capacity() const
00251 {
00252    if ( 0 == m_owner ) return 0;
00253    return m_owner->persistentCap();
00254 }

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