Created a Response object for the success/message pair

This commit is contained in:
Joe Ferris 2011-05-05 17:45:44 -04:00
parent 7a8cd4cc2f
commit cc74875e4b
17 changed files with 79 additions and 52 deletions

View File

@ -6,17 +6,17 @@ require 'capybara_webkit_builder'
desc "Generate a Makefile using qmake" desc "Generate a Makefile using qmake"
file 'Makefile' do file 'Makefile' do
CapybaraWebkitBuilder.makefile CapybaraWebkitBuilder.makefile or exit(1)
end end
desc "Regenerate dependencies using qmake" desc "Regenerate dependencies using qmake"
task :qmake => 'Makefile' do task :qmake => 'Makefile' do
CapybaraWebkitBuilder.qmake CapybaraWebkitBuilder.qmake or exit(1)
end end
desc "Build the webkit server" desc "Build the webkit server"
task :build => :qmake do task :build => :qmake do
CapybaraWebkitBuilder.build CapybaraWebkitBuilder.build or exit(1)
end end
file 'bin/webkit_server' => :build file 'bin/webkit_server' => :build

View File

@ -12,7 +12,7 @@ module CapybaraWebkitBuilder
end end
def build def build
system("make") system("make") or return false
FileUtils.mkdir("bin") unless File.directory?("bin") FileUtils.mkdir("bin") unless File.directory?("bin")
@ -24,8 +24,8 @@ module CapybaraWebkitBuilder
end end
def build_all def build_all
makefile makefile &&
qmake qmake &&
build build
end end
end end

View File

@ -3,6 +3,7 @@
#include <QObject> #include <QObject>
#include <QStringList> #include <QStringList>
#include "Response.h"
class WebPage; class WebPage;
@ -14,7 +15,7 @@ class Command : public QObject {
virtual void start(QStringList &arguments); virtual void start(QStringList &arguments);
signals: signals:
void finished(bool success, QString &response); void finished(Response *response);
protected: protected:
WebPage *page(); WebPage *page();

View File

@ -85,13 +85,13 @@ void Connection::startCommand() {
m_command = createCommand(m_commandName.toAscii().constData()); m_command = createCommand(m_commandName.toAscii().constData());
if (m_command) { if (m_command) {
connect(m_command, connect(m_command,
SIGNAL(finished(bool, QString &)), SIGNAL(finished(Response *)),
this, this,
SLOT(finishCommand(bool, QString &))); SLOT(finishCommand(Response *)));
m_command->start(m_arguments); m_command->start(m_arguments);
} else { } else {
QString failure = QString("Unknown command: ") + m_commandName + "\n"; QString failure = QString("Unknown command: ") + m_commandName + "\n";
writeResponse(false, failure); writeResponse(new Response(false, failure));
} }
m_commandName = QString(); m_commandName = QString();
} }
@ -106,27 +106,29 @@ void Connection::pendingLoadFinished(bool success) {
if (success) { if (success) {
startCommand(); startCommand();
} else { } else {
QString response = m_page->failureString(); QString message = m_page->failureString();
writeResponse(false, response); writeResponse(new Response(false, message));
} }
} }
void Connection::finishCommand(bool success, QString &response) { void Connection::finishCommand(Response *response) {
m_command->deleteLater(); m_command->deleteLater();
m_command = NULL; m_command = NULL;
writeResponse(success, response); writeResponse(response);
} }
void Connection::writeResponse(bool success, QString &response) { void Connection::writeResponse(Response *response) {
if (success) if (response->isSuccess())
m_socket->write("ok\n"); m_socket->write("ok\n");
else else
m_socket->write("failure\n"); m_socket->write("failure\n");
QByteArray response_utf8 = response.toUtf8(); QByteArray messageUtf8 = response->message().toUtf8();
QString responseLength = QString::number(response_utf8.size()) + "\n"; QString messageLength = QString::number(messageUtf8.size()) + "\n";
m_socket->write(responseLength.toAscii()); m_socket->write(messageLength.toAscii());
m_socket->write(response_utf8); m_socket->write(messageUtf8);
delete response;
m_arguments.clear(); m_arguments.clear();
m_commandName = QString(); m_commandName = QString();
m_argumentsExpected = -1; m_argumentsExpected = -1;

View File

@ -4,6 +4,7 @@
class QTcpSocket; class QTcpSocket;
class WebPage; class WebPage;
class Command; class Command;
class Response;
class Connection : public QObject { class Connection : public QObject {
Q_OBJECT Q_OBJECT
@ -13,7 +14,7 @@ class Connection : public QObject {
public slots: public slots:
void checkNext(); void checkNext();
void finishCommand(bool success, QString &response); void finishCommand(Response *response);
void pendingLoadFinished(bool success); void pendingLoadFinished(bool success);
private: private:
@ -23,7 +24,7 @@ class Connection : public QObject {
Command *createCommand(const char *name); Command *createCommand(const char *name);
void processArgument(const char *line); void processArgument(const char *line);
void startCommand(); void startCommand();
void writeResponse(bool success, QString &response); void writeResponse(Response *response);
QTcpSocket *m_socket; QTcpSocket *m_socket;
QString m_commandName; QString m_commandName;

View File

@ -9,7 +9,7 @@ Evaluate::Evaluate(WebPage *page, QObject *parent) : Command(page, parent) {
void Evaluate::start(QStringList &arguments) { void Evaluate::start(QStringList &arguments) {
QVariant result = page()->currentFrame()->evaluateJavaScript(arguments[0]); QVariant result = page()->currentFrame()->evaluateJavaScript(arguments[0]);
addVariant(result); addVariant(result);
emit finished(true, m_buffer); emit finished(new Response(true, m_buffer));
} }
void Evaluate::addVariant(QVariant &object) { void Evaluate::addVariant(QVariant &object) {

View File

@ -7,12 +7,10 @@ Execute::Execute(WebPage *page, QObject *parent) : Command(page, parent) {
void Execute::start(QStringList &arguments) { void Execute::start(QStringList &arguments) {
QString script = arguments[0] + QString("; 'success'"); QString script = arguments[0] + QString("; 'success'");
QVariant result = page()->currentFrame()->evaluateJavaScript(script); QVariant result = page()->currentFrame()->evaluateJavaScript(script);
QString response;
if (result.isValid()) { if (result.isValid()) {
emit finished(true, response); emit finished(new Response(true));
} else { } else {
response = "Javascript failed to execute"; emit finished(new Response(false, "Javascript failed to execute"));
emit finished(false, response);
} }
} }

View File

@ -6,15 +6,14 @@ Find::Find(WebPage *page, QObject *parent) : Command(page, parent) {
} }
void Find::start(QStringList &arguments) { void Find::start(QStringList &arguments) {
QString response; QString message;
QVariant result = page()->invokeCapybaraFunction("find", arguments); QVariant result = page()->invokeCapybaraFunction("find", arguments);
if (result.isValid()) { if (result.isValid()) {
response = result.toString(); message = result.toString();
emit finished(true, response); emit finished(new Response(true, message));
} else { } else {
response = "Invalid XPath expression"; emit finished(new Response(false, "Invalid XPath expression"));
emit finished(false, response);
} }
} }

View File

@ -50,8 +50,7 @@ void FrameFocus::focusId(QString name) {
void FrameFocus::focusParent() { void FrameFocus::focusParent() {
if (page()->currentFrame()->parentFrame() == 0) { if (page()->currentFrame()->parentFrame() == 0) {
QString response = "Already at parent frame."; emit finished(new Response(false, "Already at parent frame."));
emit finished(false, response);
} else { } else {
page()->currentFrame()->parentFrame()->setFocus(); page()->currentFrame()->parentFrame()->setFocus();
success(); success();
@ -59,11 +58,9 @@ void FrameFocus::focusParent() {
} }
void FrameFocus::frameNotFound() { void FrameFocus::frameNotFound() {
QString response = "Unable to locate frame. "; emit finished(new Response(false, "Unable to locate frame. "));
emit finished(false, response);
} }
void FrameFocus::success() { void FrameFocus::success() {
QString response; emit finished(new Response(true));
emit finished(true, response);
} }

View File

@ -9,6 +9,6 @@ void Node::start(QStringList &arguments) {
QString functionName = functionArguments.takeFirst(); QString functionName = functionArguments.takeFirst();
QVariant result = page()->invokeCapybaraFunction(functionName, functionArguments); QVariant result = page()->invokeCapybaraFunction(functionName, functionArguments);
QString attributeValue = result.toString(); QString attributeValue = result.toString();
emit finished(true, attributeValue); emit finished(new Response(true, attributeValue));
} }

View File

@ -10,7 +10,6 @@ void Reset::start(QStringList &arguments) {
page()->triggerAction(QWebPage::Stop); page()->triggerAction(QWebPage::Stop);
page()->currentFrame()->setHtml("<html><body></body></html>"); page()->currentFrame()->setHtml("<html><body></body></html>");
page()->networkAccessManager()->setCookieJar(new QNetworkCookieJar()); page()->networkAccessManager()->setCookieJar(new QNetworkCookieJar());
QString response = ""; emit finished(new Response(true));
emit finished(true, response);
} }

19
src/Response.cpp Normal file
View File

@ -0,0 +1,19 @@
#include "Response.h"
#include <iostream>
Response::Response(bool success, QString message) {
m_success = success;
m_message = message;
}
Response::Response(bool success) {
Response(success, QString());
}
bool Response::isSuccess() const {
return m_success;
}
QString Response::message() const {
return m_message;
}

13
src/Response.h Normal file
View File

@ -0,0 +1,13 @@
#include <QString>
class Response {
public:
Response(bool success, QString message);
Response(bool success);
bool isSuccess() const;
QString message() const;
private:
bool m_success;
QString m_message;
};

View File

@ -7,8 +7,7 @@ Source::Source(WebPage *page, QObject *parent) : Command(page, parent) {
void Source::start(QStringList &arguments) { void Source::start(QStringList &arguments) {
Q_UNUSED(arguments) Q_UNUSED(arguments)
QString response = page()->currentFrame()->toHtml(); QString result = page()->currentFrame()->toHtml();
emit finished(new Response(true, result));
emit finished(true, response);
} }

View File

@ -9,8 +9,7 @@ void Url::start(QStringList &argments) {
QUrl humanUrl = page()->currentFrame()->url(); QUrl humanUrl = page()->currentFrame()->url();
QByteArray encodedBytes = humanUrl.toEncoded(); QByteArray encodedBytes = humanUrl.toEncoded();
QString response = QString(encodedBytes); QString urlString = QString(encodedBytes);
emit finished(new Response(true, urlString));
emit finished(true, response);
} }

View File

@ -11,10 +11,10 @@ void Visit::start(QStringList &arguments) {
} }
void Visit::loadFinished(bool success) { void Visit::loadFinished(bool success) {
QString response; QString message;
if (!success) if (!success)
response = page()->failureString(); message = page()->failureString();
emit finished(success, response); emit finished(new Response(success, message));
} }

View File

@ -1,8 +1,8 @@
TEMPLATE = app TEMPLATE = app
TARGET = webkit_server TARGET = webkit_server
DESTDIR = . DESTDIR = .
HEADERS = WebPage.h Server.h Connection.h Command.h Visit.h Find.h Reset.h Node.h JavascriptInvocation.h Url.h Source.h Evaluate.h Execute.h FrameFocus.h HEADERS = WebPage.h Server.h Connection.h Command.h Visit.h Find.h Reset.h Node.h JavascriptInvocation.h Url.h Source.h Evaluate.h Execute.h FrameFocus.h Response.h
SOURCES = main.cpp WebPage.cpp Server.cpp Connection.cpp Command.cpp Visit.cpp Find.cpp Reset.cpp Node.cpp JavascriptInvocation.cpp Url.cpp Source.cpp Evaluate.cpp Execute.cpp FrameFocus.cpp SOURCES = main.cpp WebPage.cpp Server.cpp Connection.cpp Command.cpp Visit.cpp Find.cpp Reset.cpp Node.cpp JavascriptInvocation.cpp Url.cpp Source.cpp Evaluate.cpp Execute.cpp FrameFocus.cpp Response.cpp
RESOURCES = webkit_server.qrc RESOURCES = webkit_server.qrc
QT += network webkit QT += network webkit
CONFIG += console CONFIG += console