23
23
24
24
#include " libtorrent/alert_types.hpp"
25
25
#include " libtorrent/bencode.hpp" // for bencode()
26
- #include < libtorrent/hex.hpp> // for to_hex
26
+ #include " libtorrent/hex.hpp" // for to_hex
27
27
#include " libtorrent/kademlia/ed25519.hpp"
28
- #include < libtorrent/kademlia/item.hpp> // for sign_mutable_item
28
+ #include " libtorrent/kademlia/item.hpp" // for sign_mutable_item
29
+ #include " libtorrent/session_stats.hpp"
29
30
#include " libtorrent/span.hpp"
30
31
32
+ #include < boost/algorithm/string.hpp>
31
33
#include < boost/filesystem.hpp>
32
34
#include < boost/thread/thread.hpp>
33
35
@@ -53,6 +55,12 @@ static std::shared_ptr<std::thread> pDHTTorrentThread;
53
55
static std::shared_ptr<boost::thread> pReannounceThread = nullptr ;
54
56
static std::map<HashRecordKey, uint32_t > mPutCommands ;
55
57
static uint64_t nPutRecords = 0 ;
58
+ static uint64_t nPutPieces = 0 ;
59
+ static uint64_t nPutBytes = 0 ;
60
+ static uint64_t nGetRecords = 0 ;
61
+ static uint64_t nGetPieces = 0 ;
62
+ static uint64_t nGetBytes = 0 ;
63
+ static uint64_t nGetErrors = 0 ;
56
64
57
65
static bool fStarted ;
58
66
static bool fReannounceStarted = false ;
@@ -160,13 +168,15 @@ void StartEventListener(std::shared_ptr<CHashTableSession> dhtSession)
160
168
dhtSession->AddToDHTGetEventMap (infoHash, event);
161
169
}
162
170
}
163
- }
164
- else if (iAlertType == DHT_STATS_ALERT_TYPE_CODE) {
165
- // TODO (dht): handle stats
166
- // dht_stats_alert* pAlert = alert_cast<dht_stats_alert>((*iAlert));
167
- LogPrintf (" %s -- DHT Alert Message: AlertType = %s\n " , __func__, strAlertTypeName);
168
- }
169
- else {
171
+ } else if (iAlertType == DHT_STATS_ALERT_TYPE_CODE) {
172
+ LogPrintf (" %s -- DHT Status Alert Message: AlertType = %s\n " , __func__, strAlertTypeName);
173
+ dht_stats_alert* pAlert = alert_cast<dht_stats_alert>((*iAlert));
174
+ dhtSession->DHTStats = pAlert;
175
+ } else if (iAlertType == STATS_ALERT_TYPE_CODE) {
176
+ LogPrintf (" %s -- Status Alert Message: AlertType = %s\n " , __func__, strAlertTypeName);
177
+ session_stats_alert* pAlert = alert_cast<session_stats_alert>((*iAlert));
178
+ dhtSession->SessionStats = pAlert;
179
+ } else {
170
180
const CEvent event (strAlertMessage, iAlertType, iAlertCategory, strAlertTypeName);
171
181
dhtSession->AddToEventMap (iAlertType, event);
172
182
}
@@ -452,38 +462,6 @@ void StopTorrentDHTNetwork()
452
462
LogPrintf (" %s --Finished stopping all DHT session threads.\n " , __func__);
453
463
}
454
464
455
- void CHashTableSession::GetDHTStats (session_status& stats, std::vector<dht_lookup>& vchDHTLookup, std::vector<dht_routing_bucket>& vchDHTBuckets)
456
- {
457
- LogPrint (" dht" , " DHTTorrentNetwork -- GetDHTStats started.\n " );
458
-
459
- if (!Session) {
460
- return ;
461
- }
462
-
463
- if (!Session->is_dht_running ()) {
464
- return ;
465
- // LogPrint("dht", "DHTTorrentNetwork -- GetDHTStats Restarting DHT.\n");
466
- // if (!LoadSessionState(Session)) {
467
- // LogPrint("dht", "DHTTorrentNetwork -- GetDHTStats Couldn't load previous settings. Trying to bootstrap again.\n");
468
- // Bootstrap();
469
- // }
470
- // else {
471
- // LogPrint("dht", "DHTTorrentNetwork -- GetDHTStats setting loaded from file.\n");
472
- // }
473
- }
474
- else {
475
- LogPrint (" dht" , " DHTTorrentNetwork -- GetDHTStats DHT already running. Bootstrap not needed.\n " );
476
- }
477
-
478
- Session->post_dht_stats ();
479
- // get alert from map
480
- // alert* dhtAlert = WaitForResponse(Session, dht_stats_alert::alert_type);
481
- // dht_stats_alert* dhtStatsAlert = alert_cast<dht_stats_alert>(dhtAlert);
482
- // vchDHTLookup = dhtStatsAlert->active_requests;
483
- // vchDHTBuckets = dhtStatsAlert->routing_table;
484
- stats = Session->status ();
485
- }
486
-
487
465
void CleanUpPutCommandMap ()
488
466
{
489
467
int64_t nCurrentTime = GetTime ();
@@ -614,8 +592,11 @@ bool CHashTableSession::SubmitGetRecord(const std::array<char, 32>& public_key,
614
592
CDataRecord getRecord (strOperationType, nTotalSlots, header, vChunks, Array32ToVector (private_seed));
615
593
if (record.HasError ()) {
616
594
strErrorMessage = strprintf (" Record has errors: %s\n " , __func__, getRecord.ErrorMessage ());
595
+ nGetErrors++;
617
596
return false ;
618
597
}
598
+ nGetPieces += header.nChunks + 1 ;
599
+ nGetBytes += header.nDataSize ;
619
600
record = getRecord;
620
601
return true ;
621
602
}
@@ -886,11 +867,13 @@ bool SubmitPut(const std::array<char, 32> public_key, const std::array<char, 64>
886
867
return false ;
887
868
}
888
869
arraySessions[nCounter].second ->SubmitPut (public_key, private_key, lastSequence, pair.first , pair.second );
889
- LogPrintf (" %s -- salt: %s, value: %s\n " , __func__, pair.first , pair.second .to_string ());
870
+ LogPrintf (" %s -- thread: %d, salt: %s, value: %s\n " , __func__, nCounter , pair.first , pair.second .to_string ());
890
871
if (fMultiThreads )
891
872
nCounter++;
892
873
}
893
874
nPutRecords++;
875
+ nPutPieces += record.GetHeader ().nChunks + 1 ;
876
+ nPutBytes += record.GetHeader ().nDataSize + record.GetHeader ().HexValue ().size ();
894
877
895
878
if (nPutRecords % 32 == 0 )
896
879
CleanUpPutCommandMap ();
@@ -933,6 +916,7 @@ bool SubmitGetRecord(const size_t nSessionThread, const std::array<char, 32>& pu
933
916
if (!arraySessions[nSessionThread].second )
934
917
return false ;
935
918
919
+ nGetRecords++;
936
920
return arraySessions[nSessionThread].second ->SubmitGetRecord (public_key, private_seed, strOperationType, iSequence, record);
937
921
}
938
922
@@ -969,15 +953,52 @@ bool GetAllDHTGetEvents(const size_t nSessionThread, std::vector<CMutableGetEven
969
953
return arraySessions[nSessionThread].second ->GetAllDHTGetEvents (vchGetEvents);
970
954
}
971
955
972
- void GetDHTStats (const size_t nSessionThread, libtorrent::session_status & stats, std::vector<libtorrent::dht_lookup>& vchDHTLookup, std::vector<libtorrent::dht_routing_bucket>& vchDHTBuckets )
956
+ void GetDHTStats (CSessionStats & stats)
973
957
{
974
- if (nSessionThread >= nThreads)
975
- return ;
958
+ CSessionStats newStats;
959
+ size_t nRunningThreads = fMultiThreads ? nThreads : 1 ;
960
+ for (unsigned int i = 0 ; i < nRunningThreads; i++) {
961
+ if (!arraySessions[i].second || !arraySessions[i].second ->Session )
962
+ return ;
963
+ // arraySessions[i].second->Session->post_dht_stats();
964
+ arraySessions[i].second ->Session ->post_session_stats ();
965
+ }
966
+ // test get
967
+ MilliSleep (333 );
976
968
977
- if (!arraySessions[nSessionThread].second )
978
- return ;
969
+ std::vector<libtorrent::stats_metric> vStats = session_stats_metrics ();
970
+ newStats.nSessions = nRunningThreads;
971
+ for (unsigned int i = 0 ; i < nRunningThreads; i++) {
972
+ libtorrent::session_stats_alert* statsAlert = arraySessions[i].second ->SessionStats ;
973
+ if (statsAlert) {
974
+ std::string strMessage = statsAlert->message ();
975
+ std::vector<std::string> vSplit1;
976
+ boost::split (vSplit1, strMessage, boost::is_any_of (" :" ));
977
+ if (vSplit1.size () > 1 ) {
978
+ unsigned int x = 0 ;
979
+ std::vector<std::string> vSplit2;
980
+ boost::split (vSplit2, vSplit1[1 ], boost::is_any_of (" ," ));
981
+ for (const std::string& strValue : vSplit2) {
982
+ if (std::string (vStats[x].name ).find (" dht." ) == 0 ) {
983
+ std::string strThreadName = " thread[" + std::to_string (i + 1 ) + " ]" ;
984
+ newStats.vMessages .push_back (std::make_pair (strThreadName + std::string (vStats[x].name ), strValue));
985
+ }
986
+ x++;
987
+ }
988
+ }
989
+ }
990
+ }
991
+
992
+ newStats.nPutRecords = nPutRecords;
993
+ newStats.nPutPieces = nPutPieces;
994
+ newStats.nPutBytes = nPutBytes;
995
+ newStats.nGetRecords = nGetRecords;
996
+ newStats.nGetPieces = nGetPieces;
997
+ newStats.nGetBytes = nGetBytes;
998
+ newStats.nGetErrors = nGetErrors;
979
999
980
- arraySessions[nSessionThread].second ->GetDHTStats (stats, vchDHTLookup, vchDHTBuckets);
1000
+ // get dht_global_nodes
1001
+ stats = newStats;
981
1002
}
982
1003
983
1004
} // end DHT namespace
0 commit comments