diff --git a/libbitdht/src/udp/udpbitdht.cc b/libbitdht/src/udp/udpbitdht.cc index 17832a6a8..da1942526 100644 --- a/libbitdht/src/udp/udpbitdht.cc +++ b/libbitdht/src/udp/udpbitdht.cc @@ -72,10 +72,11 @@ UdpBitDht::UdpBitDht(UdpPublisher *pub, bdNodeId *id, std::string appVersion, st usedVersion = ""; /* blank it */ #endif + clearDataTransferred(); + /* setup nodeManager */ bdStackMutex stack(dhtMtx); /********** MUTEX LOCKED *************/ mBitDhtManager = new bdNodeManager(id, usedVersion, bootstrapfile, fns); - } @@ -271,6 +272,7 @@ int UdpBitDht::recvPkt(void *data, int size, struct sockaddr_in &from) if (mBitDhtManager->isBitDhtPacket((char *) data, size, from)) { + mReadBytes += size; mBitDhtManager->incomingMsg(&from, (char *) data, size); return 1; } @@ -284,6 +286,29 @@ int UdpBitDht::status(std::ostream &out) return 1; } +void UdpBitDht::clearDataTransferred() +{ + /* pass onto bitdht */ + bdStackMutex stack(dhtMtx); /********** MUTEX LOCKED *************/ + + mReadBytes = 0; + mWriteBytes = 0; +} + + +void UdpBitDht::getDataTransferred(uint32_t &read, uint32_t &write) +{ + { + /* pass onto bitdht */ + bdStackMutex stack(dhtMtx); /********** MUTEX LOCKED *************/ + + read = mReadBytes; + write = mWriteBytes; + } + clearDataTransferred(); +} + + /*** Overloaded from iThread ***/ #define MAX_MSG_PER_TICK 100 #define TICK_PAUSE_USEC 20000 /* 20ms secs .. max messages = 50 x 100 = 5000 */ @@ -323,6 +348,7 @@ int UdpBitDht::tick() std::cerr << std::endl; #endif + mWriteBytes += size; sendPkt(data, size, toAddr, BITDHT_TTL); // iterate diff --git a/libbitdht/src/udp/udpbitdht.h b/libbitdht/src/udp/udpbitdht.h index 52742b912..a72f90d40 100644 --- a/libbitdht/src/udp/udpbitdht.h +++ b/libbitdht/src/udp/udpbitdht.h @@ -95,6 +95,8 @@ virtual uint32_t statsNetworkSize(); virtual uint32_t statsBDVersionSize(); virtual uint32_t setDhtMode(uint32_t dhtFlags); +void getDataTransferred(uint32_t &read, uint32_t &write); + /******************* Internals *************************/ /***** Iteration / Loop Management *****/ @@ -111,9 +113,17 @@ virtual void run(); private: +void clearDataTransferred(); + bdMutex dhtMtx; /* for all class data (below) */ bdNodeManager *mBitDhtManager; bdDhtFunctions *mFns; + + + uint32_t mReadBytes; + uint32_t mWriteBytes; + + }; diff --git a/libbitdht/src/udp/udplayer.cc b/libbitdht/src/udp/udplayer.cc index ac46391b4..c618a8a9b 100644 --- a/libbitdht/src/udp/udplayer.cc +++ b/libbitdht/src/udp/udplayer.cc @@ -394,6 +394,7 @@ int UdpLayer::openSocket() std::cerr << "Setting TTL to " << UDP_DEF_TTL << std::endl; #endif setTTL(UDP_DEF_TTL); + clearDataTransferred(); // clear statistics. // start up our thread. { @@ -464,6 +465,28 @@ int UdpLayer::tick() return 1; } +void UdpLayer::getDataTransferred(uint32_t &read, uint32_t &write) +{ + sockMtx.lock(); /********** LOCK MUTEX *********/ + + read = readBytes; + write = writeBytes; + + sockMtx.unlock(); /******** UNLOCK MUTEX *********/ + + clearDataTransferred(); +} + +void UdpLayer::clearDataTransferred() +{ + sockMtx.lock(); /********** LOCK MUTEX *********/ + + readBytes = 0; + writeBytes = 0; + + sockMtx.unlock(); /******** UNLOCK MUTEX *********/ +} + /******************* Internals *************************************/ int UdpLayer::receiveUdpPacket(void *data, int *size, struct sockaddr_in &from) @@ -477,6 +500,11 @@ int UdpLayer::receiveUdpPacket(void *data, int *size, struct sockaddr_in &from) insize = bdnet_recvfrom(sockfd,data,insize,0, (struct sockaddr*)&fromaddr,&fromsize); + if (0 < insize) + { + readBytes += insize; + } + sockMtx.unlock(); /******** UNLOCK MUTEX *********/ if (0 < insize) @@ -508,6 +536,8 @@ int UdpLayer::sendUdpPacket(const void *data, int size, const struct sockaddr_in (struct sockaddr *) &(toaddr), sizeof(toaddr)); + writeBytes += size; + sockMtx.unlock(); /******** UNLOCK MUTEX *********/ return 1; } diff --git a/libbitdht/src/udp/udplayer.h b/libbitdht/src/udp/udplayer.h index 40ba10a91..8f2551cb0 100644 --- a/libbitdht/src/udp/udplayer.h +++ b/libbitdht/src/udp/udplayer.h @@ -72,6 +72,7 @@ class UdpLayer: public bdThread virtual ~UdpLayer() { return; } int reset(struct sockaddr_in &local); /* calls join, close, openSocket */ +void getDataTransferred(uint32_t &read, uint32_t &write); int status(std::ostream &out); @@ -106,10 +107,15 @@ virtual int sendUdpPacket(const void *data, int size, const struct sockaddr_in & /* low level */ private: +void clearDataTransferred(); + UdpReceiver *recv; struct sockaddr_in laddr; /* local addr */ + uint32_t readBytes; + uint32_t writeBytes; + int errorState; int sockfd; int ttl;