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
00032
00033
00034
00035
00036
00037
00038
00039 #include "PathloadMeasurement.h"
00040
00041 namespace ariba {
00042 namespace utility {
00043
00044 use_logging_cpp(PathloadMeasurement);
00045
00046 PathloadMeasurement::PathloadMeasurement(BaseOverlay* _overlay)
00047 : running( false ), resultNode( NodeID::UNSPECIFIED ),
00048 listener( NULL), serverpid( -1 ) {
00049
00050 if( _overlay != NULL )
00051 baseoverlay = _overlay;
00052
00053
00054
00055
00056
00057
00058
00059
00060 const char* argv[4];
00061 argv[0] = "pathload_snd";
00062 argv[1] = "-q";
00063 argv[2] = "-i";
00064 argv[3] = 0;
00065
00066 if( (serverpid = vfork()) == 0 ){
00067
00068
00069 execvp( "pathload_snd", (char* const*)argv );
00070
00071
00072
00073 logging_warn( "no bandwidth measurement module found " <<
00074 "(executing pathload_snd for bandwidth measurement server failed)" );
00075 }
00076 }
00077
00078 PathloadMeasurement::~PathloadMeasurement(){
00079
00080
00081 if( serverpid != -1 )
00082 kill( serverpid, SIGQUIT );
00083 }
00084
00085 void PathloadMeasurement::measure(const NodeID& destnode, PathloadMeasurementListener* _listener){
00086
00087 if( running ){
00088 logging_warn( "measurement already running" );
00089 return;
00090 }
00091
00092 logging_info( "starting new measurement for " << destnode.toString() );
00093
00094 listener = _listener;
00095 resultNode = destnode;
00096 resultMbps = -1;
00097
00098 runBlockingMethod();
00099 }
00100
00101 void PathloadMeasurement::dispatchFunction(){
00102 if( listener != NULL )
00103 listener->onMeasurement( resultNode, resultMbps );
00104 }
00105
00106 void PathloadMeasurement::blockingFunction(){
00107
00108
00109 const EndpointDescriptor& endp = baseoverlay->getEndpointDescriptor( resultNode );
00110 if( endp == EndpointDescriptor::UNSPECIFIED ){
00111 logging_warn( "can not measure node " << resultNode.toString() << ": can't resolve endpoint" );
00112 return;
00113 }
00114
00115
00116 if( endp == baseoverlay->getEndpointDescriptor() ){
00117 logging_debug( "don't perform measurement on local machine" );
00118 resultMbps = -1;
00119 dispatch();
00120 return;
00121 }
00122
00123
00124
00125 string endpoint = endp.toString();
00126 string::size_type p = endpoint.find(':');
00127 if( p != string::npos ) endpoint = endpoint.substr( 0, p );
00128
00129 logging_info( "measuring node " << resultNode.toString() <<
00130 " on endpoint " << endpoint );
00131
00132 string cmdline = "pathload_rcv -s " + endpoint;
00133
00134
00135 FILE* stream = popen( cmdline.c_str(), "r" );
00136 if( stream == NULL || stream <= 0){
00137 logging_warn( "no bandwidth measurement module found " <<
00138 "(executing pathload_rvc for bandwidth measurement failed)" );
00139 return;
00140 }
00141
00142 char buf[128];
00143 string content = "";
00144 bool failed = true;
00145
00146 while( fgets(buf, 100, stream) != NULL ){
00147 content += buf;
00148 }
00149
00150 logging_debug("pathload measurment output:\n" << content );
00151
00152
00153
00154
00155
00156 if( content.find("Connection refused") != string::npos ){
00157 logging_warn( "bandwidth measurement failed due to connection error" );
00158 return;
00159 }
00160
00161 if( content.find("Measurements terminated") != string::npos ){
00162 logging_warn( "bandwidth measurement failed" );
00163 return;
00164 }
00165
00166 if( content.find("Measurements finished") == string::npos ){
00167 logging_warn( "bandwidth measurement failed" );
00168 return;
00169 }
00170
00171 try {
00172 content = content.substr( content.find("Available bandwidth range") );
00173 content = content.substr( content.find("-")+2 );
00174 content = content.erase( content.find("(")-1, string::npos );
00175
00176 resultMbps = strtod(content.c_str(), NULL);
00177 logging_info( "measurement for node " << resultNode.toString() <<
00178 " on endpoint " << endp.toString() << " ended with: " << resultMbps );
00179 } catch(...) {
00180 logging_warn( "bandwidth measurement failed" );
00181 }
00182
00183
00184
00185 dispatch();
00186 }
00187
00188 }}