NOX: Mac - Separated protocol and widget as per new framework

This commit is contained in:
Srivats P. 2014-03-13 06:08:43 +05:30
parent cabd6ad096
commit 37ca28ca94
4 changed files with 312 additions and 121 deletions

View File

@ -17,66 +17,15 @@ 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 <qendian.h>
#include <QHostAddress>
#include "mac.h"
MacConfigForm::MacConfigForm(QWidget *parent)
: QWidget(parent)
{
QRegExp reMac("([0-9,a-f,A-F]{2,2}[:-]){5,5}[0-9,a-f,A-F]{2,2}");
setupUi(this);
leDstMac->setValidator(new QRegExpValidator(reMac, this));
leSrcMac->setValidator(new QRegExpValidator(reMac, this));
leDstMacCount->setValidator(new QIntValidator(1, MAX_MAC_ITER_COUNT, this));
leSrcMacCount->setValidator(new QIntValidator(1, MAX_MAC_ITER_COUNT, this));
}
MacConfigForm::~MacConfigForm()
{
qDebug("In MacConfigForm destructor");
}
void MacConfigForm::on_cmbDstMacMode_currentIndexChanged(int index)
{
if (index == OstProto::Mac::e_mm_fixed)
{
leDstMacCount->setEnabled(false);
leDstMacStep->setEnabled(false);
}
else
{
leDstMacCount->setEnabled(true);
leDstMacStep->setEnabled(true);
}
}
void MacConfigForm::on_cmbSrcMacMode_currentIndexChanged(int index)
{
if (index == OstProto::Mac::e_mm_fixed)
{
leSrcMacCount->setEnabled(false);
leSrcMacStep->setEnabled(false);
}
else
{
leSrcMacCount->setEnabled(true);
leSrcMacStep->setEnabled(true);
}
}
MacProtocol::MacProtocol(StreamBase *stream, AbstractProtocol *parent)
: AbstractProtocol(stream, parent)
{
configForm = NULL;
}
MacProtocol::~MacProtocol()
{
delete configForm;
}
AbstractProtocol* MacProtocol::createInstance(StreamBase *stream
@ -239,13 +188,50 @@ QVariant MacProtocol::fieldData(int index, FieldAttrib attrib,
}
break;
}
// Meta fields
case mac_dstMacMode:
switch(attrib)
{
case FieldValue: return data.dst_mac_mode();
default: break;
}
break;
case mac_dstMacCount:
switch(attrib)
{
case FieldValue: return data.dst_mac_count();
default: break;
}
break;
case mac_dstMacStep:
switch(attrib)
{
case FieldValue: return data.dst_mac_step();
default: break;
}
break;
case mac_srcMacMode:
switch(attrib)
{
case FieldValue: return data.src_mac_mode();
default: break;
}
break;
case mac_srcMacCount:
switch(attrib)
{
case FieldValue: return data.src_mac_count();
default: break;
}
break;
case mac_srcMacStep:
switch(attrib)
{
case FieldValue: return data.src_mac_step();
default: break;
}
break;
default:
break;
}
@ -253,10 +239,86 @@ QVariant MacProtocol::fieldData(int index, FieldAttrib attrib,
return AbstractProtocol::fieldData(index, attrib, streamIndex);
}
bool MacProtocol::setFieldData(int /*index*/, const QVariant& /*value*/,
FieldAttrib /*attrib*/)
bool MacProtocol::setFieldData(int index, const QVariant &value,
FieldAttrib attrib)
{
return false;
bool isOk = false;
if (attrib != FieldValue)
goto _exit;
switch (index)
{
case mac_dstAddr:
{
quint64 mac = value.toString().toULongLong(&isOk, BASE_HEX);
if (isOk)
data.set_dst_mac(mac);
break;
}
case mac_srcAddr:
{
quint64 mac = value.toString().toULongLong(&isOk, BASE_HEX);
if (isOk)
data.set_src_mac(mac);
break;
}
// Meta-Fields
case mac_dstMacMode:
{
uint mode = value.toUInt(&isOk);
if (isOk && data.MacAddrMode_IsValid(mode))
data.set_dst_mac_mode((OstProto::Mac::MacAddrMode) mode);
else
isOk = false;
break;
}
case mac_dstMacCount:
{
uint count = value.toUInt(&isOk);
if (isOk)
data.set_dst_mac_count(count);
break;
}
case mac_dstMacStep:
{
uint step = value.toUInt(&isOk);
if (isOk)
data.set_dst_mac_step(step);
break;
}
case mac_srcMacMode:
{
uint mode = value.toUInt(&isOk);
if (isOk && data.MacAddrMode_IsValid(mode))
data.set_src_mac_mode((OstProto::Mac::MacAddrMode) mode);
else
isOk = false;
break;
}
case mac_srcMacCount:
{
uint count = value.toUInt(&isOk);
if (isOk)
data.set_src_mac_count(count);
break;
}
case mac_srcMacStep:
{
uint step = value.toUInt(&isOk);
if (isOk)
data.set_src_mac_step(step);
break;
}
default:
qFatal("%s: unimplemented case %d in switch", __PRETTY_FUNCTION__,
index);
break;
}
_exit:
return isOk;
}
bool MacProtocol::isProtocolFrameValueVariable() const
@ -281,49 +343,3 @@ int MacProtocol::protocolFrameVariableCount() const
return count;
}
QWidget* MacProtocol::configWidget()
{
if (configForm == NULL)
{
configForm = new MacConfigForm;
loadConfigWidget();
}
return configForm;
}
void MacProtocol::loadConfigWidget()
{
configWidget();
configForm->leDstMac->setText(uintToHexStr(data.dst_mac(), 6));
configForm->cmbDstMacMode->setCurrentIndex(data.dst_mac_mode());
configForm->leDstMacCount->setText(QString().setNum(data.dst_mac_count()));
configForm->leDstMacStep->setText(QString().setNum(data.dst_mac_step()));
configForm->leSrcMac->setText(uintToHexStr(data.src_mac(), 6));
configForm->cmbSrcMacMode->setCurrentIndex(data.src_mac_mode());
configForm->leSrcMacCount->setText(QString().setNum(data.src_mac_count()));
configForm->leSrcMacStep->setText(QString().setNum(data.src_mac_step()));
}
void MacProtocol::storeConfigWidget()
{
bool isOk;
configWidget();
data.set_dst_mac(configForm->leDstMac->text().remove(QChar(' ')).
toULongLong(&isOk, 16));
data.set_dst_mac_mode((OstProto::Mac::MacAddrMode) configForm->
cmbDstMacMode->currentIndex());
data.set_dst_mac_count(configForm->leDstMacCount->text().toULong(&isOk));
data.set_dst_mac_step(configForm->leDstMacStep->text().toULong(&isOk));
data.set_src_mac(configForm->leSrcMac->text().remove(QChar(' ')).
toULongLong(&isOk, 16));
data.set_src_mac_mode((OstProto::Mac::MacAddrMode) configForm->
cmbSrcMacMode->currentIndex());
data.set_src_mac_count(configForm->leSrcMacCount->text().toULong(&isOk));
data.set_src_mac_step(configForm->leSrcMacStep->text().toULong(&isOk));
}

View File

@ -23,26 +23,10 @@ along with this program. If not, see <http://www.gnu.org/licenses/>
#include "abstractprotocol.h"
#include "mac.pb.h"
#include "ui_mac.h"
#define MAX_MAC_ITER_COUNT 256
class MacConfigForm : public QWidget, public Ui::mac
{
Q_OBJECT
public:
MacConfigForm(QWidget *parent = 0);
virtual ~MacConfigForm();
private slots:
void on_cmbDstMacMode_currentIndexChanged(int index);
void on_cmbSrcMacMode_currentIndexChanged(int index);
};
class MacProtocol : public AbstractProtocol
{
private:
OstProto::Mac data;
MacConfigForm *configForm;
public:
enum macfield
{
mac_dstAddr = 0,
@ -58,7 +42,6 @@ private:
mac_fieldCount
};
public:
MacProtocol(StreamBase *stream, AbstractProtocol *parent = 0);
virtual ~MacProtocol();
@ -83,9 +66,8 @@ public:
virtual bool isProtocolFrameValueVariable() const;
virtual int protocolFrameVariableCount() const;
virtual QWidget* configWidget();
virtual void loadConfigWidget();
virtual void storeConfigWidget();
private:
OstProto::Mac data;
};
#endif

148
common/macconfig.cpp Normal file
View File

@ -0,0 +1,148 @@
/*
Copyright (C) 2010,2013-2014 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 "macconfig.h"
#include "mac.h"
#define MAX_MAC_ITER_COUNT 256
MacConfigForm::MacConfigForm(QWidget *parent)
: AbstractProtocolConfigForm(parent)
{
QRegExp reMac("([0-9,a-f,A-F]{2,2}[:-]){5,5}[0-9,a-f,A-F]{2,2}");
setupUi(this);
leDstMac->setValidator(new QRegExpValidator(reMac, this));
leSrcMac->setValidator(new QRegExpValidator(reMac, this));
leDstMacCount->setValidator(new QIntValidator(1, MAX_MAC_ITER_COUNT, this));
leSrcMacCount->setValidator(new QIntValidator(1, MAX_MAC_ITER_COUNT, this));
}
MacConfigForm::~MacConfigForm()
{
}
MacConfigForm* MacConfigForm::createInstance()
{
MacConfigForm *f = new MacConfigForm;
return f;
}
void MacConfigForm::on_cmbDstMacMode_currentIndexChanged(int index)
{
if (index == OstProto::Mac::e_mm_fixed)
{
leDstMacCount->setEnabled(false);
leDstMacStep->setEnabled(false);
}
else
{
leDstMacCount->setEnabled(true);
leDstMacStep->setEnabled(true);
}
}
void MacConfigForm::on_cmbSrcMacMode_currentIndexChanged(int index)
{
if (index == OstProto::Mac::e_mm_fixed)
{
leSrcMacCount->setEnabled(false);
leSrcMacStep->setEnabled(false);
}
else
{
leSrcMacCount->setEnabled(true);
leSrcMacStep->setEnabled(true);
}
}
void MacConfigForm::loadWidget(AbstractProtocol *proto)
{
leDstMac->setText(
proto->fieldData(
MacProtocol::mac_dstAddr,
AbstractProtocol::FieldTextValue
).toString());
cmbDstMacMode->setCurrentIndex(
proto->fieldData(
MacProtocol::mac_dstMacMode,
AbstractProtocol::FieldValue
).toUInt());
leDstMacCount->setText(
proto->fieldData(
MacProtocol::mac_dstMacCount,
AbstractProtocol::FieldValue
).toString());
leDstMacStep->setText(
proto->fieldData(
MacProtocol::mac_dstMacStep,
AbstractProtocol::FieldValue
).toString());
leSrcMac->setText(
proto->fieldData(
MacProtocol::mac_srcAddr,
AbstractProtocol::FieldTextValue
).toString());
cmbSrcMacMode->setCurrentIndex(
proto->fieldData(
MacProtocol::mac_srcMacMode,
AbstractProtocol::FieldValue
).toUInt());
leSrcMacCount->setText(
proto->fieldData(
MacProtocol::mac_srcMacCount,
AbstractProtocol::FieldValue
).toString());
leSrcMacStep->setText(
proto->fieldData(
MacProtocol::mac_srcMacStep,
AbstractProtocol::FieldValue
).toString());
}
void MacConfigForm::storeWidget(AbstractProtocol *proto)
{
proto->setFieldData(
MacProtocol::mac_dstAddr,
leDstMac->text().remove(QChar(' ')));
proto->setFieldData(
MacProtocol::mac_dstMacMode,
cmbDstMacMode->currentIndex());
proto->setFieldData(
MacProtocol::mac_dstMacCount,
leDstMacCount->text());
proto->setFieldData(
MacProtocol::mac_dstMacStep,
leDstMacStep->text());
proto->setFieldData(
MacProtocol::mac_srcAddr,
leSrcMac->text().remove(QChar(' ')));
proto->setFieldData(
MacProtocol::mac_srcMacMode,
cmbSrcMacMode->currentIndex());
proto->setFieldData(
MacProtocol::mac_srcMacCount,
leSrcMacCount->text());
proto->setFieldData(
MacProtocol::mac_srcMacStep,
leSrcMacStep->text());
}

45
common/macconfig.h Normal file
View File

@ -0,0 +1,45 @@
/*
Copyright (C) 2010-2012 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 _MAC_CONFIG_H
#define _MAC_CONFIG_H
#include "abstractprotocolconfig.h"
#include "ui_mac.h"
class MacConfigForm :
public AbstractProtocolConfigForm,
private Ui::mac
{
Q_OBJECT
public:
MacConfigForm(QWidget *parent = 0);
virtual ~MacConfigForm();
static MacConfigForm* createInstance();
virtual void loadWidget(AbstractProtocol *proto);
virtual void storeWidget(AbstractProtocol *proto);
private slots:
void on_cmbDstMacMode_currentIndexChanged(int index);
void on_cmbSrcMacMode_currentIndexChanged(int index);
};
#endif