Permalink
Browse files

Created a Response object for the success/message pair

  • Loading branch information...
jferris committed May 5, 2011
1 parent 7a8cd4c commit cc74875e4baee2879dbb6d13e018925dc34f1b0d
Showing with 79 additions and 52 deletions.
  1. +3 −3 Rakefile
  2. +3 −3 lib/capybara_webkit_builder.rb
  3. +2 −1 src/Command.h
  4. +15 −13 src/Connection.cpp
  5. +3 −2 src/Connection.h
  6. +1 −1 src/Evaluate.cpp
  7. +2 −4 src/Execute.cpp
  8. +4 −5 src/Find.cpp
  9. +3 −6 src/FrameFocus.cpp
  10. +1 −1 src/Node.cpp
  11. +1 −2 src/Reset.cpp
  12. +19 −0 src/Response.cpp
  13. +13 −0 src/Response.h
  14. +2 −3 src/Source.cpp
  15. +2 −3 src/Url.cpp
  16. +3 −3 src/Visit.cpp
  17. +2 −2 src/webkit_server.pro
View
@@ -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
@@ -12,7 +12,7 @@ def qmake
end
def build
- system("make")
+ system("make") or return false
FileUtils.mkdir("bin") unless File.directory?("bin")
@@ -24,8 +24,8 @@ def build
end
def build_all
- makefile
- qmake
+ makefile &&
+ qmake &&
build
end
end
View
@@ -3,6 +3,7 @@
#include <QObject>
#include <QStringList>
+#include "Response.h"
class WebPage;
@@ -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();
View
@@ -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();
}
@@ -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;
View
@@ -4,6 +4,7 @@
class QTcpSocket;
class WebPage;
class Command;
+class Response;
class Connection : public QObject {
Q_OBJECT
@@ -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:
@@ -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;
View
@@ -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) {
View
@@ -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"));
}
}
View
@@ -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"));
}
}
View
@@ -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));
}
View
@@ -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));
}
View
@@ -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));
}
View
@@ -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;
+}
View
@@ -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
@@ -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));
}
View
@@ -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));
}
View
@@ -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));
}
View
@@ -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

0 comments on commit cc74875

Please sign in to comment.