そこで、私はGoogleを掘り下げることから作業のこの部分を開始し、1つの興味深いリンクを見つけました-http: //speedtest.net/speedtest-config.php、かなり大きなXMLコードをロードします。
<?xml version = "1.0" encoding = "UTF-8" ?> <settings > <client ip = "*.*.*.*" lat = "60.0000" lon = "100.0000" isp = "Samara Telecom, JSC" isprating = "3.3" rating = "5" ispdlavg = "4449" ispulavg = "1394" /> <licensekey > 9c1687ea58e5e770-1df5b7cd427370f7-4b62a84526ea1f56 </licensekey > <customer > speedtest </customer > <servers > <server url = "http://speedtest.pronea.no/speedtest/upload.php" lat = "69.6828" lon = "18.9428" name = "Tromso" country = "Norway" countrycode = "NO" sponsor = "Pronea AS" sponsorurl = "http://www.pronea.no/" id = "1327" gid = "0" bigsamples = "1" /> <server url = "http://test.nax.no/speedtest/upload.php" lat = "65.8333" lon = "13.2000" name = "Helgeland" country = "Norway" countrycode = "NO" sponsor = "Noraxess Broadband" sponsorurl = "http://www.nax.no" id = "1362" gid = "0" url2 = "http://85.221.66.41/speedtest/upload.php" bigsamples = "1" />
<?xml version = "1.0" encoding = "UTF-8" ?> <settings > <client ip = "*.*.*.*" lat = "60.0000" lon = "100.0000" isp = "Samara Telecom, JSC" isprating = "3.3" rating = "5" ispdlavg = "4449" ispulavg = "1394" /> <licensekey > 9c1687ea58e5e770-1df5b7cd427370f7-4b62a84526ea1f56 </licensekey > <customer > speedtest </customer > <servers > <server url = "http://speedtest.pronea.no/speedtest/upload.php" lat = "69.6828" lon = "18.9428" name = "Tromso" country = "Norway" countrycode = "NO" sponsor = "Pronea AS" sponsorurl = "http://www.pronea.no/" id = "1327" gid = "0" bigsamples = "1" /> <server url = "http://test.nax.no/speedtest/upload.php" lat = "65.8333" lon = "13.2000" name = "Helgeland" country = "Norway" countrycode = "NO" sponsor = "Noraxess Broadband" sponsorurl = "http://www.nax.no" id = "1362" gid = "0" url2 = "http://85.221.66.41/speedtest/upload.php" bigsamples = "1" />
<?xml version = "1.0" encoding = "UTF-8" ?> <settings > <client ip = "*.*.*.*" lat = "60.0000" lon = "100.0000" isp = "Samara Telecom, JSC" isprating = "3.3" rating = "5" ispdlavg = "4449" ispulavg = "1394" /> <licensekey > 9c1687ea58e5e770-1df5b7cd427370f7-4b62a84526ea1f56 </licensekey > <customer > speedtest </customer > <servers > <server url = "http://speedtest.pronea.no/speedtest/upload.php" lat = "69.6828" lon = "18.9428" name = "Tromso" country = "Norway" countrycode = "NO" sponsor = "Pronea AS" sponsorurl = "http://www.pronea.no/" id = "1327" gid = "0" bigsamples = "1" /> <server url = "http://test.nax.no/speedtest/upload.php" lat = "65.8333" lon = "13.2000" name = "Helgeland" country = "Norway" countrycode = "NO" sponsor = "Noraxess Broadband" sponsorurl = "http://www.nax.no" id = "1362" gid = "0" url2 = "http://85.221.66.41/speedtest/upload.php" bigsamples = "1" />
<?xml version = "1.0" encoding = "UTF-8" ?> <settings > <client ip = "*.*.*.*" lat = "60.0000" lon = "100.0000" isp = "Samara Telecom, JSC" isprating = "3.3" rating = "5" ispdlavg = "4449" ispulavg = "1394" /> <licensekey > 9c1687ea58e5e770-1df5b7cd427370f7-4b62a84526ea1f56 </licensekey > <customer > speedtest </customer > <servers > <server url = "http://speedtest.pronea.no/speedtest/upload.php" lat = "69.6828" lon = "18.9428" name = "Tromso" country = "Norway" countrycode = "NO" sponsor = "Pronea AS" sponsorurl = "http://www.pronea.no/" id = "1327" gid = "0" bigsamples = "1" /> <server url = "http://test.nax.no/speedtest/upload.php" lat = "65.8333" lon = "13.2000" name = "Helgeland" country = "Norway" countrycode = "NO" sponsor = "Noraxess Broadband" sponsorurl = "http://www.nax.no" id = "1362" gid = "0" url2 = "http://85.221.66.41/speedtest/upload.php" bigsamples = "1" />
<?xml version = "1.0" encoding = "UTF-8" ?> <settings > <client ip = "*.*.*.*" lat = "60.0000" lon = "100.0000" isp = "Samara Telecom, JSC" isprating = "3.3" rating = "5" ispdlavg = "4449" ispulavg = "1394" /> <licensekey > 9c1687ea58e5e770-1df5b7cd427370f7-4b62a84526ea1f56 </licensekey > <customer > speedtest </customer > <servers > <server url = "http://speedtest.pronea.no/speedtest/upload.php" lat = "69.6828" lon = "18.9428" name = "Tromso" country = "Norway" countrycode = "NO" sponsor = "Pronea AS" sponsorurl = "http://www.pronea.no/" id = "1327" gid = "0" bigsamples = "1" /> <server url = "http://test.nax.no/speedtest/upload.php" lat = "65.8333" lon = "13.2000" name = "Helgeland" country = "Norway" countrycode = "NO" sponsor = "Noraxess Broadband" sponsorurl = "http://www.nax.no" id = "1362" gid = "0" url2 = "http://85.221.66.41/speedtest/upload.php" bigsamples = "1" />
<?xml version = "1.0" encoding = "UTF-8" ?> <settings > <client ip = "*.*.*.*" lat = "60.0000" lon = "100.0000" isp = "Samara Telecom, JSC" isprating = "3.3" rating = "5" ispdlavg = "4449" ispulavg = "1394" /> <licensekey > 9c1687ea58e5e770-1df5b7cd427370f7-4b62a84526ea1f56 </licensekey > <customer > speedtest </customer > <servers > <server url = "http://speedtest.pronea.no/speedtest/upload.php" lat = "69.6828" lon = "18.9428" name = "Tromso" country = "Norway" countrycode = "NO" sponsor = "Pronea AS" sponsorurl = "http://www.pronea.no/" id = "1327" gid = "0" bigsamples = "1" /> <server url = "http://test.nax.no/speedtest/upload.php" lat = "65.8333" lon = "13.2000" name = "Helgeland" country = "Norway" countrycode = "NO" sponsor = "Noraxess Broadband" sponsorurl = "http://www.nax.no" id = "1362" gid = "0" url2 = "http://85.221.66.41/speedtest/upload.php" bigsamples = "1" />
<?xml version = "1.0" encoding = "UTF-8" ?> <settings > <client ip = "*.*.*.*" lat = "60.0000" lon = "100.0000" isp = "Samara Telecom, JSC" isprating = "3.3" rating = "5" ispdlavg = "4449" ispulavg = "1394" /> <licensekey > 9c1687ea58e5e770-1df5b7cd427370f7-4b62a84526ea1f56 </licensekey > <customer > speedtest </customer > <servers > <server url = "http://speedtest.pronea.no/speedtest/upload.php" lat = "69.6828" lon = "18.9428" name = "Tromso" country = "Norway" countrycode = "NO" sponsor = "Pronea AS" sponsorurl = "http://www.pronea.no/" id = "1327" gid = "0" bigsamples = "1" /> <server url = "http://test.nax.no/speedtest/upload.php" lat = "65.8333" lon = "13.2000" name = "Helgeland" country = "Norway" countrycode = "NO" sponsor = "Noraxess Broadband" sponsorurl = "http://www.nax.no" id = "1362" gid = "0" url2 = "http://85.221.66.41/speedtest/upload.php" bigsamples = "1" />
さらに、すべてのxmlは、speedtestサーバーコンポーネントがインストールされているサーバーのリストです。 緯度と経度のパラメータは緯度と経度、つまり緯度と経度にすぎないことに気づいたとき、手で自分に最も近いサーバーを見つけなければなりませんでした-私は冷や汗をかきました。 「グライム」と思ったので、大きなC ++の魔法が始まりました。
_sendPacket関数は、httpパッケージの単純な構造であり、それ自体に科学的な関心を持たないため、許可を得てスキップします。 _getConfigに直行しましょう。
- void _getConfig ( )
- {
- std :: stringstream netspeedConfigStream = _sendPacket ( _SpeedtestServer、_SpeedtestConfigUrl ) ;
- //空のプロパティツリーオブジェクトを作成します
- boost :: property_tree :: ptreeを使用し ます。
- ptree PropertyTree ;
- // XMLファイルを読み込み、その内容をプロパティツリーに配置します。
- // Koenigのため、名前空間の修飾は必要ありません
- // 2番目の引数を検索します。 読み取りに失敗した場合、例外
- //スローされます。
- read_xml ( netspeedConfigStream、PropertyTree ) ;
- ptree設定= PropertyTree。 get_child ( "settings" ) ;
- //クライアントの場所を構築します
- _ServerLocation * _ClientLocation ;
- //サーバーの場所のリストを宣言します
- std :: vector < _ServerLocation > * _ServerLocationList ;
- _ClientLocation = new _ServerLocation ( Settings。Begin ( ) - > second、 false ) ;
- _ServerLocationList = new std :: vector < _ServerLocation > ;
- // XMLからリストを構築します
- BOOST_FOREACH ( ptree :: value_type & v、PropertyTree。Get_child ( "settings.servers" ) )
- _ServerLocationList- > push_back ( _ServerLocation ( v。Second、 true ) ) ;
- std :: sort ( _ServerLocationList- > begin ( ) 、_ ServerLocationList- > end ( ) 、_ ServerLocationSortFunctor ( _ClientLocation ) )) ;
- //最も近いサーバーのURL
- std :: vector < std :: string > SplitUrl ;
- boost :: split ( SplitUrl、_ServerLocationList- > begin ( ) - > GetUrl ( ) 、boost :: is_any_of ( "/" ) ) ;
- _ClientLocationを削除します。
- _ServerLocationListを削除します。
- _NearestServer = SplitUrl [ 2 ] ;
- for ( int i = 3 ; i < SplitUrl。size ( ) - 1 ; ++ i ) // TODO:イテレータで書き換える
- {
- _NearestServerUrl。 追加 ( "/" ) ;
- _NearestServerUrl。 append ( SplitUrl [ i ] ) ;
- }
- 帰る
- }
コードからわかるように、XMLを受信してから解析し、_ServerLocation構造体のベクターに入力しています。 自転車の暴力に抵抗しないように、最も近いサーバーの検索は、STL-ev sortを呼び出すことによって行われました。このために、ソートファンクターを作成する必要がありました。
- struct _ServerLocationSortFunctor
- {
- 公開 :
- _ServerLocationSortFunctor ( _ServerLocation * ClientLocation ) : _ClientLocation ( ClientLocation ) { }
- bool演算子( ) ( _ServerLocation & _First、_ServerLocation & _Second )
- {
- return _ServerDist ( _ClientLocation、 & _First ) < _ServerDist ( _ClientLocation、 & _Second ) ;
- }
- プライベート :
- _ServerLocation * _ClientLocation ;
- インライン double deg2rad ( double ServerAplpha )
- {
- ServerAplpha * PI / 180を 返し ます。
- }
- インライン 二重 hypot3 ( 二重 dx、 二重 dy、 二重 dz )
- {
- return sqrt ( dx * dx + dy * dy + dz * dz ) ;
- }
- double _ServerDist ( _ServerLocation * _First、_ServerLocation * _Second )
- {
- double phi1 = deg2rad ( _First- > GetLon ( ) ) ;
- double psi1 = deg2rad ( _First- > GetLat ( ) ) ;
- double phi2 = deg2rad ( _Second- > GetLon ( ) ) ;
- double psi2 = deg2rad ( _Second- > GetLat ( ) ) ;
- double cos_psi1 = cos ( psi1 ) ;
- double x1 = EarthRadius * cos ( phi1 ) * cos_psi1 ;
- double y1 = EarthRadius * sin ( phi1 ) * cos_psi1 ;
- double z1 = EarthRadius * sin ( psi1 ) ;
- double cos_psi2 = cos ( psi2 ) ;
- double x2 = EarthRadius * cos ( phi2 ) * cos_psi2 ;
- double y2 = EarthRadius * sin ( phi2 ) * cos_psi2 ;
- double z2 = EarthRadius * sin ( psi2 ) ;
- double d = hypot3 ( x2 - x1、y2 - y1、z2 - z1 ) ;
- EarthRadius * acos ( 1- ( d * d ) / ( 2 * EarthRadius * EarthRadius ) ) ;
- }
- } ;
学校のコースは地理または幾何学のいずれかであり、最も近いサーバーが見つかりました。
さらに、速度測定コード自体は簡単なので、それほど面白くなりません。
- void _testLatency ( )
- {
- std :: string Url ( _NearestServerUrl ) ;
- URL append ( _LatencyUrl ) ;
- boost :: タイマー LatencyTimer ;
- _sendPacket ( _NearestServer、Url ) ;
- _LatencyTime = LatencyTimer。 経過 ( ) ;
- std :: cout << "PING:" << _LatencyTime << std :: endl ;
- 帰る
- }
- void _testDownload ( )
- {
- std :: string Url ( _NearestServerUrl ) ;
- URL append ( _DownloadUrl ) ;
- boost :: タイマー DownloadTimer ;
- std :: stringstream DownloadPacket = _sendPacket ( _NearestServer、Url ) ;
- double DownloadTime = DownloadTimer。 経過 ( ) ;
- double DownloadSpeed = ( DownloadPacket.str ( ) 。 length ( ) / ( DownloadTime - _LatencyTime ) )) * 8/1024 ;
- std :: cout << "DOWNLOAD:" << DownloadSpeed << std :: endl ;
- 帰る
- }
- void _testUpload ( )
- {
- std :: string Url ( _NearestServerUrl ) ;
- URL append ( _UploadUrl ) ;
- std :: string UploadDataValue ( _UploadBytes、 'a' ) ;
- std :: string UploadData ( "content0 =" ) ;
- UploadData + = UploadDataValue ;
- boost :: タイマー UploadTimer ;
- _sendPacket ( _NearestServer、Url、 true 、UploadData ) ;
- double UploadTime = UploadTimer。 経過 ( ) ;
- double UploadSpeed = ( _UploadBytes / ( UploadTime - _LatencyTime ) ) * 8/1024 ;
- std :: cout << "UPLOAD:" << UploadSpeed << std :: endl ;
- }
たぶん誰かが役に立つでしょう。 コードはVisual Studio 2010でテストされました(プロジェクトの残りの部分はWindowsで強化されました)。
私は、STLとBoostのドキュメントに加えて、自習用にインターネットで見つけたソースコードの一部に感謝したいと思います。
このopusの完全なコードは、 http: //paste.pocoo.org/show/215424/で入手できます。
______________________