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 "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 }