summaryrefslogtreecommitdiffstats
path: root/examples/debuggee/main.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'examples/debuggee/main.cpp')
-rw-r--r--examples/debuggee/main.cpp98
1 files changed, 98 insertions, 0 deletions
diff --git a/examples/debuggee/main.cpp b/examples/debuggee/main.cpp
new file mode 100644
index 0000000..9167f54
--- /dev/null
+++ b/examples/debuggee/main.cpp
@@ -0,0 +1,98 @@
+#include <QtScript>
+#include <qscriptdebuggerconnector.h>
+
+class MyObject : public QObject
+{
+ Q_OBJECT
+public:
+ MyObject(const QHostAddress &addr, quint16 port, bool connect, QObject *parent = 0);
+private slots:
+ void connected();
+ void disconnected();
+private:
+ QScriptEngine *m_engine;
+ QScriptDebuggerConnector *m_connector;
+};
+
+MyObject::MyObject(const QHostAddress &addr, quint16 port, bool connect, QObject *parent)
+ : QObject(parent)
+{
+ m_engine = new QScriptEngine(this);
+ m_connector = new QScriptDebuggerConnector(this);
+ QObject::connect(m_connector, SIGNAL(connected()), this, SLOT(connected()), Qt::QueuedConnection);
+ QObject::connect(m_connector, SIGNAL(disconnected()), this, SLOT(disconnected()), Qt::QueuedConnection);
+ m_connector->setEngine(m_engine);
+ if (connect) {
+ qDebug("attempting to connect to debugger at %s:%d", qPrintable(addr.toString()), port);
+ m_connector->connectToDebugger(addr, port);
+ } else {
+ if (m_connector->listen(addr, port))
+ qDebug("waiting for debugger to connect at %s:%d", qPrintable(addr.toString()), port);
+ else {
+ qWarning("Failed to listen!");
+ QCoreApplication::quit();
+ }
+ }
+}
+
+void MyObject::connected()
+{
+ qDebug("connected!!");
+ QStringList fileNames;
+ QString path = QDir::currentPath();
+ fileNames << (path + QDir::separator() + "foo.qs")
+ << (path + QDir::separator() + "example.qs");
+ for (int i = 0; i < fileNames.size(); ++i) {
+ QString fn = fileNames.at(i);
+ QFile file(fn);
+ file.open(QIODevice::ReadOnly);
+ QString program = file.readAll();
+ qDebug("calling evaluate");
+ m_engine->evaluate(program, fn);
+ qDebug("evaluate done");
+ }
+ m_connector->disconnectFromDebugger();
+}
+
+void MyObject::disconnected()
+{
+ qDebug("disconnected");
+ QCoreApplication::quit();
+}
+
+int main(int argc, char **argv)
+{
+ QCoreApplication app(argc, argv);
+ QHostAddress addr(QHostAddress::LocalHost);
+ quint16 port = 2000;
+ bool connect = false;
+ for (int i = 1; i < argc; ++i) {
+ QString arg(argv[i]);
+ arg = arg.trimmed();
+ if(arg.startsWith("--")) {
+ QString opt;
+ QString val;
+ int split = arg.indexOf("=");
+ if(split > 0) {
+ opt = arg.mid(2).left(split-2);
+ val = arg.mid(split + 1).trimmed();
+ } else {
+ opt = arg.mid(2);
+ }
+ if (opt == QLatin1String("address"))
+ addr.setAddress(val);
+ else if (opt == QLatin1String("port"))
+ port = val.toUShort();
+ else if (opt == QLatin1String("connect"))
+ connect = true;
+ else if (opt == QLatin1String("help")) {
+ fprintf(stdout, "Usage: debuggee --address=ADDR --port=NUM [--connect]\n");
+ return(0);
+ }
+ }
+ }
+ MyObject obj(addr, port, connect);
+ return app.exec();
+}
+
+#include "main.moc"