NOX: UserScript - separated protocol and widget as per new framework

This commit is contained in:
Srivats P. 2014-04-25 06:58:38 +05:30
parent 88ec853d09
commit 7d3caad43c
8 changed files with 189 additions and 126 deletions

View File

@ -70,10 +70,10 @@ HEADERS += \
tcp.h \
udp.h \
textproto.h \
hexdump.h
hexdump.h \
userscript.h
HEADERS1 += \
userscript.h \
sample.h
SOURCES = \
@ -103,10 +103,10 @@ SOURCES += \
tcp.cpp \
udp.cpp \
textproto.cpp \
hexdump.cpp
hexdump.cpp \
userscript.cpp
SOURCES1 += \
userscript.cpp \
sample.cpp
QMAKE_DISTCLEAN += object_script.*

View File

@ -1,6 +1,6 @@
TEMPLATE = lib
CONFIG += qt staticlib
QT += network xml
QT += network xml script
INCLUDEPATH += "../extra/qhexedit2/src"
LIBS += \
-lprotobuf
@ -24,10 +24,10 @@ FORMS += \
tcp.ui \
udp.ui \
textproto.ui \
hexdump.ui
hexdump.ui \
userscript.ui
FORMS1 += \
userscript.ui \
sample.ui
PROTOS = \
@ -72,7 +72,8 @@ HEADERS += \
tcpconfig.h \
udpconfig.h \
textprotoconfig.h \
hexdumpconfig.h
hexdumpconfig.h \
userscriptconfig.h
SOURCES += \
ostprotolib.cpp \
@ -103,7 +104,8 @@ SOURCES += \
tcpconfig.cpp \
udpconfig.cpp \
textprotoconfig.cpp \
hexdumpconfig.cpp
hexdumpconfig.cpp \
userscriptconfig.cpp
QMAKE_DISTCLEAN += object_script.*

View File

@ -22,7 +22,6 @@ along with this program. If not, see <http://www.gnu.org/licenses/>
#include "protocol.pb.h"
#if 0
#include "userscript.h"
#include "sample.h"
#else
#include "mac.h"
@ -54,6 +53,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>
#include "textproto.h"
// Special Protos
#include "hexdump.h"
#include "userscript.h"
#endif
ProtocolManager *OstProtocolManager;
@ -64,8 +64,6 @@ ProtocolManager::ProtocolManager()
themselves (once this is done remove the #includes for all the protocols)
*/
#if 0
registerProtocol(OstProto::Protocol::kUserScriptFieldNumber,
(void*) UserScriptProtocol::createInstance);
registerProtocol(OstProto::Protocol::kSampleFieldNumber,
(void*) SampleProtocol::createInstance);
#else
@ -130,6 +128,8 @@ ProtocolManager::ProtocolManager()
// Special Protocols
registerProtocol(OstProto::Protocol::kHexDumpFieldNumber,
(void*) HexDumpProtocol::createInstance);
registerProtocol(OstProto::Protocol::kUserScriptFieldNumber,
(void*) UserScriptProtocol::createInstance);
#endif
populateNeighbourProtocols();

View File

@ -48,6 +48,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>
#include "textprotoconfig.h"
// Special Protocol Widgets
#include "hexdumpconfig.h"
#include "userscriptconfig.h"
ProtocolWidgetFactory *OstProtocolWidgetFactory;
QMap<int, void*> ProtocolWidgetFactory::configWidgetFactory;
@ -144,6 +145,9 @@ ProtocolWidgetFactory::ProtocolWidgetFactory()
OstProtocolWidgetFactory->registerProtocolConfigWidget(
OstProto::Protocol::kHexDumpFieldNumber,
(void*) HexDumpConfigForm::createInstance);
OstProtocolWidgetFactory->registerProtocolConfigWidget(
OstProto::Protocol::kUserScriptFieldNumber,
(void*) UserScriptConfigForm::createInstance);
}
ProtocolWidgetFactory::~ProtocolWidgetFactory()

View File

@ -1,5 +1,5 @@
/*
Copyright (C) 2010 Srivats P.
Copyright (C) 2010, 2014 Srivats P.
This file is part of "Ostinato"
@ -19,54 +19,6 @@ along with this program. If not, see <http://www.gnu.org/licenses/>
#include "userscript.h"
#include <QMessageBox>
//
// -------------------- UserScriptConfigForm --------------------
//
UserScriptConfigForm::UserScriptConfigForm(UserScriptProtocol *protocol,
QWidget *parent) : QWidget(parent), protocol_(protocol)
{
setupUi(this);
updateStatus();
}
void UserScriptConfigForm::updateStatus()
{
if (protocol_->isScriptValid())
{
statusLabel->setText(QString("<font color=\"green\">Success</font>"));
compileButton->setDisabled(true);
}
else
{
statusLabel->setText(
QString("<font color=\"red\">Error: %1: %2</font>").arg(
protocol_->userScriptErrorLineNumber()).arg(
protocol_->userScriptErrorText()));
compileButton->setEnabled(true);
}
}
void UserScriptConfigForm::on_programEdit_textChanged()
{
compileButton->setEnabled(true);
}
void UserScriptConfigForm::on_compileButton_clicked(bool /*checked*/)
{
protocol_->storeConfigWidget();
if (!protocol_->isScriptValid())
{
QMessageBox::critical(this, "Error",
QString("%1: %2").arg(
protocol_->userScriptErrorLineNumber()).arg(
protocol_->userScriptErrorText()));
}
updateStatus();
}
//
// -------------------- UserScriptProtocol --------------------
//
@ -75,7 +27,6 @@ UserScriptProtocol::UserScriptProtocol(StreamBase *stream, AbstractProtocol *par
: AbstractProtocol(stream, parent),
userProtocol_(this)
{
configForm = NULL;
isScriptValid_ = false;
errorLineNumber_ = 0;
@ -88,7 +39,6 @@ UserScriptProtocol::UserScriptProtocol(StreamBase *stream, AbstractProtocol *par
UserScriptProtocol::~UserScriptProtocol()
{
delete configForm;
}
AbstractProtocol* UserScriptProtocol::createInstance(StreamBase *stream,
@ -230,6 +180,7 @@ bool UserScriptProtocol::setFieldData(int index, const QVariant &value,
case userScript_program:
{
data.set_program(value.toString().toStdString());
evaluateUserScript();
break;
}
default:
@ -308,32 +259,6 @@ _do_default:
return AbstractProtocol::protocolFrameCksum(streamIndex, cksumType);
}
QWidget* UserScriptProtocol::configWidget()
{
if (configForm == NULL)
{
configForm = new UserScriptConfigForm(this);
loadConfigWidget();
}
return configForm;
}
void UserScriptProtocol::loadConfigWidget()
{
configWidget();
configForm->programEdit->setPlainText(
fieldData(userScript_program, FieldValue).toString());
}
void UserScriptProtocol::storeConfigWidget()
{
configWidget();
setFieldData(userScript_program, configForm->programEdit->toPlainText());
evaluateUserScript();
}
void UserScriptProtocol::evaluateUserScript() const
{
QScriptValue userFunction;

View File

@ -1,5 +1,5 @@
/*
Copyright (C) 2010 Srivats P.
Copyright (C) 2010, 2014 Srivats P.
This file is part of "Ostinato"
@ -22,7 +22,6 @@ along with this program. If not, see <http://www.gnu.org/licenses/>
#include "abstractprotocol.h"
#include "userscript.pb.h"
#include "ui_userscript.h"
#include <QScriptEngine>
#include <QScriptValue>
@ -99,30 +98,17 @@ private:
int protocolFrameVariableCount_;
};
class UserScriptConfigForm : public QWidget, public Ui::UserScript
{
Q_OBJECT
public:
UserScriptConfigForm(UserScriptProtocol *protocol, QWidget *parent = 0);
private:
void updateStatus();
UserScriptProtocol *protocol_;
private slots:
void on_programEdit_textChanged();
void on_compileButton_clicked(bool checked = false);
};
class UserScriptProtocol : public AbstractProtocol
{
public:
enum userScriptfield
{
// Frame Fields
userScript_program = 0,
userScript_fieldCount
};
UserScriptProtocol(StreamBase *stream, AbstractProtocol *parent = 0);
virtual ~UserScriptProtocol();
@ -154,10 +140,6 @@ public:
virtual quint32 protocolFrameCksum(int streamIndex = 0,
CksumType cksumType = CksumIp) const;
virtual QWidget* configWidget();
virtual void loadConfigWidget();
virtual void storeConfigWidget();
void evaluateUserScript() const;
bool isScriptValid() const;
int userScriptErrorLineNumber() const;
@ -166,15 +148,7 @@ public:
private:
int userScriptLineCount() const;
enum userScriptfield
{
// Frame Fields
userScript_program = 0,
userScript_fieldCount
};
OstProto::UserScript data;
UserScriptConfigForm *configForm;
mutable QScriptEngine engine_;
mutable UserProtocol userProtocol_;
@ -186,5 +160,3 @@ private:
};
#endif
/* vim: set shiftwidth=4 tabstop=8 softtabstop=4 expandtab: */

109
common/userscriptconfig.cpp Normal file
View File

@ -0,0 +1,109 @@
/*
Copyright (C) 2010, 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 "userscriptconfig.h"
#include "userscript.h"
#include <QMessageBox>
UserScriptConfigForm::UserScriptConfigForm(QWidget *parent)
: AbstractProtocolConfigForm(parent)
{
setupUi(this);
// The protocol_ (UserScriptProtocol) is a dummy protocol internal
// to UserScriptConfigForm whose sole purpose is to "compile" the script
// so that the configForm widget can display the compilation result.
// It is *not* used for actual packet contents at any time
protocol_ = new UserScriptProtocol(NULL);
compileScript();
}
UserScriptConfigForm::~UserScriptConfigForm()
{
delete protocol_;
}
UserScriptConfigForm* UserScriptConfigForm::createInstance()
{
return new UserScriptConfigForm;
}
void UserScriptConfigForm::loadWidget(AbstractProtocol *proto)
{
programEdit->setPlainText(
proto->fieldData(
UserScriptProtocol::userScript_program,
AbstractProtocol::FieldValue
).toString());
compileScript();
}
void UserScriptConfigForm::storeWidget(AbstractProtocol *proto)
{
proto->setFieldData(
UserScriptProtocol::userScript_program,
programEdit->toPlainText());
}
//
// ----- private methods
//
void UserScriptConfigForm::compileScript()
{
// storeWidget() will save the updated userscript into
// the protocol_ which in turn triggers the protocol_ to
// compile it
storeWidget(protocol_);
if (protocol_->isScriptValid())
{
statusLabel->setText(QString("<font color=\"green\">Success</font>"));
compileButton->setDisabled(true);
}
else
{
statusLabel->setText(
QString("<font color=\"red\">Error: %1: %2</font>").arg(
protocol_->userScriptErrorLineNumber()).arg(
protocol_->userScriptErrorText()));
compileButton->setEnabled(true);
}
}
//
// ----- private slots
//
void UserScriptConfigForm::on_programEdit_textChanged()
{
compileButton->setEnabled(true);
}
void UserScriptConfigForm::on_compileButton_clicked(bool /*checked*/)
{
compileScript();
if (!protocol_->isScriptValid())
{
QMessageBox::critical(this, "Error",
QString("%1: %2").arg(
protocol_->userScriptErrorLineNumber()).arg(
protocol_->userScriptErrorText()));
}
}

51
common/userscriptconfig.h Normal file
View File

@ -0,0 +1,51 @@
/*
Copyright (C) 2010, 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/>
*/
#ifndef _USER_SCRIPT_CONFIG_H
#define _USER_SCRIPT_CONFIG_H
#include "abstractprotocolconfig.h"
#include "ui_userscript.h"
class UserScriptProtocol;
class UserScriptConfigForm :
public AbstractProtocolConfigForm,
private Ui::UserScript
{
Q_OBJECT
public:
UserScriptConfigForm(QWidget *parent = 0);
virtual ~UserScriptConfigForm();
static UserScriptConfigForm* createInstance();
virtual void loadWidget(AbstractProtocol *proto);
virtual void storeWidget(AbstractProtocol *proto);
private:
void compileScript();
UserScriptProtocol *protocol_;
private slots:
void on_programEdit_textChanged();
void on_compileButton_clicked(bool checked = false);
};
#endif