Skip to content

Commit

Permalink
Created a Response object for the success/message pair
Browse files Browse the repository at this point in the history
  • Loading branch information
jferris committed May 5, 2011
1 parent 7a8cd4c commit cc74875
Show file tree
Hide file tree
Showing 17 changed files with 79 additions and 52 deletions.
6 changes: 3 additions & 3 deletions Rakefile
Expand Up @@ -6,17 +6,17 @@ require 'capybara_webkit_builder'

desc "Generate a Makefile using qmake"
file 'Makefile' do
CapybaraWebkitBuilder.makefile
CapybaraWebkitBuilder.makefile or exit(1)
end

desc "Regenerate dependencies using qmake"
task :qmake => 'Makefile' do
CapybaraWebkitBuilder.qmake
CapybaraWebkitBuilder.qmake or exit(1)
end

desc "Build the webkit server"
task :build => :qmake do
CapybaraWebkitBuilder.build
CapybaraWebkitBuilder.build or exit(1)
end

file 'bin/webkit_server' => :build
Expand Down
6 changes: 3 additions & 3 deletions lib/capybara_webkit_builder.rb
Expand Up @@ -12,7 +12,7 @@ def qmake
end

def build
system("make")
system("make") or return false

FileUtils.mkdir("bin") unless File.directory?("bin")

Expand All @@ -24,8 +24,8 @@ def build
end

def build_all
makefile
qmake
makefile &&
qmake &&
build
end
end
3 changes: 2 additions & 1 deletion src/Command.h
Expand Up @@ -3,6 +3,7 @@

#include <QObject>
#include <QStringList>
#include "Response.h"

class WebPage;

Expand All @@ -14,7 +15,7 @@ class Command : public QObject {
virtual void start(QStringList &arguments);

signals:
void finished(bool success, QString &response);
void finished(Response *response);

protected:
WebPage *page();
Expand Down
28 changes: 15 additions & 13 deletions src/Connection.cpp
Expand Up @@ -85,13 +85,13 @@ void Connection::startCommand() {
m_command = createCommand(m_commandName.toAscii().constData());
if (m_command) {
connect(m_command,
SIGNAL(finished(bool, QString &)),
SIGNAL(finished(Response *)),
this,
SLOT(finishCommand(bool, QString &)));
SLOT(finishCommand(Response *)));
m_command->start(m_arguments);
} else {
QString failure = QString("Unknown command: ") + m_commandName + "\n";
writeResponse(false, failure);
writeResponse(new Response(false, failure));
}
m_commandName = QString();
}
Expand All @@ -106,27 +106,29 @@ void Connection::pendingLoadFinished(bool success) {
if (success) {
startCommand();
} else {
QString response = m_page->failureString();
writeResponse(false, response);
QString message = m_page->failureString();
writeResponse(new Response(false, message));
}
}

void Connection::finishCommand(bool success, QString &response) {
void Connection::finishCommand(Response *response) {
m_command->deleteLater();
m_command = NULL;
writeResponse(success, response);
writeResponse(response);
}

void Connection::writeResponse(bool success, QString &response) {
if (success)
void Connection::writeResponse(Response *response) {
if (response->isSuccess())
m_socket->write("ok\n");
else
m_socket->write("failure\n");

QByteArray response_utf8 = response.toUtf8();
QString responseLength = QString::number(response_utf8.size()) + "\n";
m_socket->write(responseLength.toAscii());
m_socket->write(response_utf8);
QByteArray messageUtf8 = response->message().toUtf8();
QString messageLength = QString::number(messageUtf8.size()) + "\n";
m_socket->write(messageLength.toAscii());
m_socket->write(messageUtf8);
delete response;

m_arguments.clear();
m_commandName = QString();
m_argumentsExpected = -1;
Expand Down
5 changes: 3 additions & 2 deletions src/Connection.h
Expand Up @@ -4,6 +4,7 @@
class QTcpSocket;
class WebPage;
class Command;
class Response;

class Connection : public QObject {
Q_OBJECT
Expand All @@ -13,7 +14,7 @@ class Connection : public QObject {

public slots:
void checkNext();
void finishCommand(bool success, QString &response);
void finishCommand(Response *response);
void pendingLoadFinished(bool success);

private:
Expand All @@ -23,7 +24,7 @@ class Connection : public QObject {
Command *createCommand(const char *name);
void processArgument(const char *line);
void startCommand();
void writeResponse(bool success, QString &response);
void writeResponse(Response *response);

QTcpSocket *m_socket;
QString m_commandName;
Expand Down
2 changes: 1 addition & 1 deletion src/Evaluate.cpp
Expand Up @@ -9,7 +9,7 @@ Evaluate::Evaluate(WebPage *page, QObject *parent) : Command(page, parent) {
void Evaluate::start(QStringList &arguments) {
QVariant result = page()->currentFrame()->evaluateJavaScript(arguments[0]);
addVariant(result);
emit finished(true, m_buffer);
emit finished(new Response(true, m_buffer));
}

void Evaluate::addVariant(QVariant &object) {
Expand Down
6 changes: 2 additions & 4 deletions src/Execute.cpp
Expand Up @@ -7,12 +7,10 @@ Execute::Execute(WebPage *page, QObject *parent) : Command(page, parent) {
void Execute::start(QStringList &arguments) {
QString script = arguments[0] + QString("; 'success'");
QVariant result = page()->currentFrame()->evaluateJavaScript(script);
QString response;
if (result.isValid()) {
emit finished(true, response);
emit finished(new Response(true));
} else {
response = "Javascript failed to execute";
emit finished(false, response);
emit finished(new Response(false, "Javascript failed to execute"));
}
}

9 changes: 4 additions & 5 deletions src/Find.cpp
Expand Up @@ -6,15 +6,14 @@ Find::Find(WebPage *page, QObject *parent) : Command(page, parent) {
}

void Find::start(QStringList &arguments) {
QString response;
QString message;
QVariant result = page()->invokeCapybaraFunction("find", arguments);

if (result.isValid()) {
response = result.toString();
emit finished(true, response);
message = result.toString();
emit finished(new Response(true, message));
} else {
response = "Invalid XPath expression";
emit finished(false, response);
emit finished(new Response(false, "Invalid XPath expression"));
}
}

9 changes: 3 additions & 6 deletions src/FrameFocus.cpp
Expand Up @@ -50,20 +50,17 @@ void FrameFocus::focusId(QString name) {

void FrameFocus::focusParent() {
if (page()->currentFrame()->parentFrame() == 0) {
QString response = "Already at parent frame.";
emit finished(false, response);
emit finished(new Response(false, "Already at parent frame."));
} else {
page()->currentFrame()->parentFrame()->setFocus();
success();
}
}

void FrameFocus::frameNotFound() {
QString response = "Unable to locate frame. ";
emit finished(false, response);
emit finished(new Response(false, "Unable to locate frame. "));
}

void FrameFocus::success() {
QString response;
emit finished(true, response);
emit finished(new Response(true));
}
2 changes: 1 addition & 1 deletion src/Node.cpp
Expand Up @@ -9,6 +9,6 @@ void Node::start(QStringList &arguments) {
QString functionName = functionArguments.takeFirst();
QVariant result = page()->invokeCapybaraFunction(functionName, functionArguments);
QString attributeValue = result.toString();
emit finished(true, attributeValue);
emit finished(new Response(true, attributeValue));
}

3 changes: 1 addition & 2 deletions src/Reset.cpp
Expand Up @@ -10,7 +10,6 @@ void Reset::start(QStringList &arguments) {
page()->triggerAction(QWebPage::Stop);
page()->currentFrame()->setHtml("<html><body></body></html>");
page()->networkAccessManager()->setCookieJar(new QNetworkCookieJar());
QString response = "";
emit finished(true, response);
emit finished(new Response(true));
}

19 changes: 19 additions & 0 deletions src/Response.cpp
@@ -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 changes: 13 additions & 0 deletions src/Response.h
@@ -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;
};
5 changes: 2 additions & 3 deletions src/Source.cpp
Expand Up @@ -7,8 +7,7 @@ Source::Source(WebPage *page, QObject *parent) : Command(page, parent) {
void Source::start(QStringList &arguments) {
Q_UNUSED(arguments)

QString response = page()->currentFrame()->toHtml();

emit finished(true, response);
QString result = page()->currentFrame()->toHtml();
emit finished(new Response(true, result));
}

5 changes: 2 additions & 3 deletions src/Url.cpp
Expand Up @@ -9,8 +9,7 @@ void Url::start(QStringList &argments) {

QUrl humanUrl = page()->currentFrame()->url();
QByteArray encodedBytes = humanUrl.toEncoded();
QString response = QString(encodedBytes);

emit finished(true, response);
QString urlString = QString(encodedBytes);
emit finished(new Response(true, urlString));
}

6 changes: 3 additions & 3 deletions src/Visit.cpp
Expand Up @@ -11,10 +11,10 @@ void Visit::start(QStringList &arguments) {
}

void Visit::loadFinished(bool success) {
QString response;
QString message;
if (!success)
response = page()->failureString();
message = page()->failureString();

emit finished(success, response);
emit finished(new Response(success, message));
}

4 changes: 2 additions & 2 deletions src/webkit_server.pro
@@ -1,8 +1,8 @@
TEMPLATE = app
TARGET = webkit_server
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
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
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 Response.cpp
RESOURCES = webkit_server.qrc
QT += network webkit
CONFIG += console
Expand Down

0 comments on commit cc74875

Please sign in to comment.