1 /*
2 * UploadSpeedTest.cpp
3 * OpenLieroX
4 *
5 * Created by Karel Petranek on 28.1.09
6 * code under LGPL
7 *
8 */
9
10 #include <time.h>
11 #include <stdlib.h>
12
13 #include "Options.h"
14 #include "UploadSpeedTest.h"
15 #include "FindFile.h"
16 #include "Debug.h"
17 #include "LieroX.h"
18
19
20 #define TEST_DATA_SIZE (300 * 1024) // In bytes
21
22 /////////////////////
23 // Constructor
UploadSpeedTest()24 UploadSpeedTest::UploadSpeedTest()
25 : m_finished(false)
26 {
27 m_http.onFinished.handler() = getEventHandler(this, &UploadSpeedTest::Http_onFinished);
28
29 // Read the URL from disk
30 FILE *fp = OpenGameFile(std::string("cfg/") + UPLOAD_TEST_SERVERS, "rt");
31 if (fp) {
32 m_url = ReadUntil(fp);
33 fclose(fp);
34 }
35 }
36
37 /////////////////////
38 // Constructor
UploadSpeedTest(const std::string & test_url)39 UploadSpeedTest::UploadSpeedTest(const std::string &test_url)
40 : m_url(test_url), m_finished(false)
41 {
42 m_http.onFinished.handler() = getEventHandler(this, &UploadSpeedTest::Http_onFinished);
43 }
44
45 ////////////////////
46 // Destructor
~UploadSpeedTest()47 UploadSpeedTest::~UploadSpeedTest()
48 {
49 m_http.CancelProcessing();
50 m_http.onFinished.handler() = null;
51 }
52
53 /////////////////////
54 // Generates random data for the test
generateRandomData(size_t size,std::string & result)55 void UploadSpeedTest::generateRandomData(size_t size, std::string& result)
56 {
57 // Just fill the string with an uninitialized memory
58 char *buf = new char[size];
59 for( size_t i=0; i<size/2; i++ )
60 ((Uint16 *)buf)[i] = GetRandomInt(65536);
61 result.append(buf, size);
62 delete[] buf;
63 }
64
65 /////////////////////
66 // Starts the bandwidth test
startTest()67 void UploadSpeedTest::startTest()
68 {
69 // Get some random data
70 std::string random_data;
71 generateRandomData(TEST_DATA_SIZE, random_data);
72 m_finished = false;
73
74 notes << "Starting an upload speed test to host " << m_url << endl;
75
76 // Start uploading
77 std::list<HTTPPostField> data;
78 data.push_back(HTTPPostField(random_data, "binary/random", "data", ""));
79 m_http.SendData(data, m_url, tLXOptions->sHttpProxy);
80 m_startTime = tLX->currentTime;
81 }
82
83 /////////////////////
84 // Cancels the test
cancelTest()85 void UploadSpeedTest::cancelTest()
86 {
87 m_finished = true;
88 m_http.CancelProcessing();
89 }
90
91 /////////////////////
92 // Called by the HTTP class when the upload finishes
Http_onFinished(CHttp::HttpEventData d)93 void UploadSpeedTest::Http_onFinished(CHttp::HttpEventData d)
94 {
95 m_finished = true;
96 TimeDiff testTime = tLX->currentTime - m_startTime;
97 if( testTime.seconds() <= 0.01f )
98 testTime = 0.01f;
99 m_rate = (float)TEST_DATA_SIZE / testTime.seconds();
100 hints << "UploadSpeedTest: testTime " << testTime.seconds() << " data size " << TEST_DATA_SIZE << " rate " << m_rate << endl;
101 // Delegate the event
102 onFinished.occurred(TestData(this, d.bSucceeded, m_rate));
103 }
104
105 ////////////////////
106 // Returns progress of the test
getProgress() const107 int UploadSpeedTest::getProgress() const
108 {
109 if (m_finished)
110 return 100;
111
112 if (m_http.GetDataToSendLength() == 0)
113 return 100;
114
115 return (int)(m_http.GetSentDataLen() * 100 / TEST_DATA_SIZE);
116 }
117
118