FlowTracer.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 "FlowTracer.h"
00034 #include "pydtn/WrapNode.h"
00035 #include "pydtn/WrapLink.h"
00036 #include "simlpy/Clock.h"
00037 #include <iostream>
00038 #include <ctype.h>
00039 #include <netinet/in.h>
00040 
00041 FlowTracer::FlowTracer( std::ostream& s ) :
00042    m_stream( s )
00043 {
00044    m_stream << std::fixed;
00045 }
00046 
00047 FlowTracer::~FlowTracer()
00048 {
00049 }
00050 
00051 void
00052 FlowTracer::node( const WrapNode& wn )
00053 {
00054 }
00055 
00056 void
00057 FlowTracer::link( const WrapLink& wl )
00058 {
00059 }
00060 
00061 void
00062 FlowTracer::enqueue( const DTN::Bundle& b, const DTN::Node* n )
00063 {
00064 }
00065 
00066 void
00067 FlowTracer::dequeue( const DTN::Bundle& b, const DTN::Node* n )
00068 {
00069 }
00070 
00071 void
00072 FlowTracer::send( const DTN::Bundle& b, const DTN::Node* n )
00073 {
00074    if ( 0 == n ) return;
00075    if ( n->addr() != b.source() ) return;
00076    traceBundle(b,"inject",*n);
00077 }
00078 
00079 void
00080 FlowTracer::receive( const DTN::Bundle& b, const DTN::Node* n )
00081 {
00082    if ( 0 == n ) return;
00083    if ( n->addr() != b.destination() ) return;
00084    traceBundle(b,"deliver",*n);
00085 }
00086 
00087 void
00088 FlowTracer::drop( const DTN::Bundle& b,
00089                   const DTN::DropCause& c,
00090                   const DTN::Node* n )
00091 {
00092    if ( 0 == n ) return;
00093    std::string cause = "unknown";
00094    if ( DTN::BitErrorDrop::inst.isType(c) )
00095    {
00096       cause = "ber";
00097    }
00098    else if ( DTN::CustodyPolicyDrop::inst.isType(c) )
00099    {
00100       cause = "custody";
00101    }
00102    else if ( DTN::ExpiryDrop::inst.isType(c) )
00103    {
00104       cause = "expiry";
00105    }
00106    else if ( DTN::FullQueueDrop::inst.isType(c) )
00107    {
00108       cause = "fullq";
00109    }
00110    else if ( DTN::LinkFailureDrop::inst.isType(c) )
00111    {
00112       cause = "linkfail";
00113    }
00114    else if ( DTN::NoRouteDrop::inst.isType(c) )
00115    {
00116       cause = "noroute";
00117    }
00118    traceBundle(b,"drop",*n,cause);
00119 }
00120 
00121 void
00122 FlowTracer::traceBundle( const DTN::Bundle& b,
00123                          const std::string& func,
00124                          const DTN::Node& n,
00125                          const std::string& xtra )
00126 {
00127    if ( b.type() & DTN::Bundle::kACK )
00128    {
00129       return;
00130    }
00131    Time t = Clock::time();
00132    m_stream << t.tv.tv_sec << "," << t.tv.tv_usec << " "
00133             << stringify(b.source()) << " "
00134             << stringify(b.destination()) << " "
00135             << stringify(b.app()) << " "
00136             << ntohl(b.seqNum()) << " "
00137             << func << " "
00138             << b.size() << " "
00139             << b.hopCount() << " "
00140             << stringify(n.addr()) << " "
00141             << xtra
00142             << std::endl;
00143 }
00144 
00145 std::string
00146 FlowTracer::stringify( const DTN::ByteString& b ) const
00147 {
00148    if ( 0 == b.length() ) return "-";
00149    std::string s;
00150    bool printable = true;
00151    for ( unsigned int i = 0 ; i < b.length() ; ++i )
00152    {
00153       if ( !isprint(b[i]) ) printable = false;
00154       if ( isspace(b[i]) ) printable = false;
00155    }
00156    if ( ! printable )
00157    {
00158       s += "0x";
00159    }
00160    for ( unsigned int i = 0 ; i < b.length() ; ++i )
00161    {
00162       if ( printable )
00163       {
00164          s += b[i];
00165       }
00166       else
00167       {
00168          char tmp[3];
00169          ::sprintf(tmp,"%02X",b[i]);
00170          s += tmp[0];
00171          s += tmp[1];
00172       }
00173    }
00174 
00175    return s;
00176 }

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