sign: refactored tx stats into a new PcapTxStats class

This commit is contained in:
Srivats P 2016-10-19 18:52:35 +05:30
parent 2f87618fea
commit 31c2cd2dcb
8 changed files with 201 additions and 16 deletions

View File

@ -41,6 +41,7 @@ SOURCES += \
abstractport.cpp \
pcapport.cpp \
pcaptransmitter.cpp \
pcaptxstats.cpp \
pcaptxthread.cpp \
bsdport.cpp \
linuxport.cpp \

View File

@ -22,6 +22,16 @@ along with this program. If not, see <http://www.gnu.org/licenses/>
PcapTransmitter::PcapTransmitter(const char *device)
: txThread_(device)
{
memset(&stats_, 0, sizeof(stats_));
txStats_.setTxThreadStats(&stats_);
txStats_.start(); // TODO: alongwith user transmit start
txThread_.setStats(&stats_);
}
PcapTransmitter::~PcapTransmitter()
{
txStats_.stop(); // TODO: alongwith user transmit stop
}
bool PcapTransmitter::setRateAccuracy(
@ -65,7 +75,7 @@ void PcapTransmitter::setPacketListLoopMode(
void PcapTransmitter::useExternalStats(AbstractPort::PortStats *stats)
{
txThread_.useExternalStats(stats);
txStats_.useExternalStats(stats);
}
void PcapTransmitter::start()

View File

@ -21,12 +21,15 @@ along with this program. If not, see <http://www.gnu.org/licenses/>
#define _PCAP_TRANSMITTER_H
#include "abstractport.h"
#include "pcaptxstats.h"
#include "pcaptxthread.h"
#include "statstuple.h"
class PcapTransmitter
{
public:
PcapTransmitter(const char *device);
~PcapTransmitter();
bool setRateAccuracy(AbstractPort::Accuracy accuracy);
@ -46,6 +49,8 @@ public:
private:
PcapTxThread txThread_;
PcapTxStats txStats_;
StatsTuple stats_;
};
#endif

85
server/pcaptxstats.cpp Normal file
View File

@ -0,0 +1,85 @@
/*
Copyright (C) 2016 Srivats P.
This file is part of "Ostinato"
This is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>
*/
#include "pcaptxstats.h"
#include "statstuple.h"
PcapTxStats::PcapTxStats()
{
txThreadStats_ = NULL;
stats_ = new AbstractPort::PortStats;
usingInternalStats_ = true;
stop_ = false;
}
PcapTxStats::~PcapTxStats()
{
if (usingInternalStats_)
delete stats_;
}
void PcapTxStats::setTxThreadStats(StatsTuple *stats)
{
txThreadStats_ = stats;
}
void PcapTxStats::useExternalStats(AbstractPort::PortStats *stats)
{
if (usingInternalStats_)
delete stats_;
stats_ = stats;
usingInternalStats_ = false;
}
void PcapTxStats::start()
{
QThread::start();
while (!isRunning())
QThread::msleep(10);
}
void PcapTxStats::stop()
{
stop_ = true;
while (isRunning())
QThread::msleep(10);
}
void PcapTxStats::run()
{
Q_ASSERT(txThreadStats_);
qDebug("txStats: collection start");
while (1) {
stats_->txPkts = txThreadStats_->pkts;
stats_->txBytes = txThreadStats_->bytes;
if (stop_)
break;
QThread::msleep(1000);
}
stop_ = false;
qDebug("txStats: collection end");
}

53
server/pcaptxstats.h Normal file
View File

@ -0,0 +1,53 @@
/*
Copyright (C) 2016 Srivats P.
This file is part of "Ostinato"
This is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>
*/
#ifndef _PCAP_TX_STATS_H
#define _PCAP_TX_STATS_H
#include "abstractport.h"
#include <QThread>
class StatsTuple;
class PcapTxStats : public QThread
{
public:
PcapTxStats();
~PcapTxStats();
void setTxThreadStats(StatsTuple *stats);
void useExternalStats(AbstractPort::PortStats *stats);
void start();
void stop();
private:
void run();
StatsTuple *txThreadStats_;
bool usingInternalStats_;
AbstractPort::PortStats *stats_;
volatile bool stop_;
};
#endif

View File

@ -19,6 +19,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>
#include "pcaptransmitter.h"
#include "statstuple.h"
#include "timestamp.h"
PcapTxThread::PcapTxThread(const char *device)
@ -37,8 +38,6 @@ PcapTxThread::PcapTxThread(const char *device)
returnToQIdx_ = -1;
loopDelay_ = 0;
stop_ = false;
stats_ = new AbstractPort::PortStats;
usingInternalStats_ = true;
handle_ = pcap_open_live(device, 64 /* FIXME */, 0, 1000 /* ms */, errbuf);
if (handle_ == NULL)
@ -46,6 +45,8 @@ PcapTxThread::PcapTxThread(const char *device)
usingInternalHandle_ = true;
stats_ = NULL;
return;
_open_error:
@ -55,8 +56,6 @@ _open_error:
PcapTxThread::~PcapTxThread()
{
if (usingInternalStats_)
delete stats_;
if (usingInternalHandle_)
pcap_close(handle_);
}
@ -199,12 +198,9 @@ void PcapTxThread::setHandle(pcap_t *handle)
usingInternalHandle_ = false;
}
void PcapTxThread::useExternalStats(AbstractPort::PortStats *stats)
void PcapTxThread::setStats(StatsTuple *stats)
{
if (usingInternalStats_)
delete stats_;
stats_ = stats;
usingInternalStats_ = false;
}
void PcapTxThread::run()
@ -266,8 +262,8 @@ _restart:
seq->sendQueue_, kSyncTransmit);
if (ret >= 0)
{
stats_->txPkts += seq->packets_;
stats_->txBytes += seq->bytes_;
stats_->pkts += seq->packets_;
stats_->bytes += seq->bytes_;
getTimeStamp(&ovrEnd);
overHead += seq->usecDuration_
@ -407,8 +403,8 @@ int PcapTxThread::sendQueueTransmit(pcap_t *p,
Q_ASSERT(pktLen > 0);
pcap_sendpacket(p, pkt, pktLen);
stats_->txPkts++;
stats_->txBytes += pktLen;
stats_->pkts++;
stats_->bytes += pktLen;
// Step to the next packet in the buffer
hdr = (struct pcap_pkthdr*) (pkt + pktLen);

View File

@ -26,6 +26,8 @@ along with this program. If not, see <http://www.gnu.org/licenses/>
#include <QThread>
#include <pcap.h>
class StatsTuple;
class PcapTxThread: public QThread
{
public:
@ -42,13 +44,15 @@ public:
void setPacketListLoopMode(bool loop, quint64 secDelay, quint64 nsecDelay);
void setHandle(pcap_t *handle);
void useExternalStats(AbstractPort::PortStats *stats);
void setStats(StatsTuple *stats);
void run();
void start();
void stop();
bool isRunning();
private:
enum State
{
@ -72,12 +76,12 @@ private:
void (*udelayFn_)(unsigned long);
bool usingInternalStats_;
AbstractPort::PortStats *stats_;
bool usingInternalHandle_;
pcap_t *handle_;
volatile bool stop_;
volatile State state_;
StatsTuple *stats_;
};
#endif

31
server/statstuple.h Normal file
View File

@ -0,0 +1,31 @@
/*
Copyright (C) 2016 Srivats P.
This file is part of "Ostinato"
This is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>
*/
#ifndef _STATS_TUPLE_H
#define _STATS_TUPLE_H
#include <QtGlobal>
struct StatsTuple
{
quint64 pkts;
quint64 bytes;
};
#endif