From 593ae4b4998824f3f7e9acd5c47bf44feb547d89 Mon Sep 17 00:00:00 2001 From: tkawata Date: Sat, 3 Mar 2012 01:09:48 +0900 Subject: [PATCH] alpha1 dev (Qt) continues.. --- Source/DNContainerBuilder.cpp | 6 +- Source/DNDirectory.cpp | 6 ++ Source/DNDirectory.h | 3 +- Source/DNDirectoryImpl.h | 2 + Source/DNEngine.cpp | 71 +++++++++++------ Source/DNEngine.h | 11 ++- Source/DNFileList.cpp | 5 +- Source/DNGlobal.cpp | 131 +++++++++++++++++++++++++++++++ Source/DNGlobal.h | 59 ++++++++++++++ Source/DNHTTPServer.cpp | 6 ++ Source/DNHTTPServer.h | 1 + Source/DNHTTPServerImpl.h | 1 + Source/DNThread.cpp | 47 +++++++++++ Source/DNThread.h | 42 ++++++++++ Source/DNThreadImpl.h | 37 +++++++++ Source/DNXMLImpl.h | 1 + Source/QtDennco/mainwindow.cpp | 33 +++++++- Source/QtDennco/mainwindow.h | 3 + Source/QtScript/dnqsbasiccell.cpp | 2 +- Source/QtScript/dnqscellcodeinstance.cpp | 2 +- Source/QtScript/dnqscontainer.cpp | 3 +- Source/TKCell.cpp | 7 +- Source/TKContainer.h | 2 +- Source/TKDebug.h | 6 +- Source/platform/qt/QtTKConsole.cpp | 36 +++++++++ Source/platform/qt/qtdndirectoryimpl.cpp | 17 +++- Source/platform/qt/qtdnthreadimpl.cpp | 80 +++++++++++++++++++ Source/platform/qt/qtdnthreadimpl.h | 52 ++++++++++++ Source/platform/qt/qtdnxmlimpl.cpp | 61 +++++++++----- Source/platform/qt/qtdnxmlimpl.h | 14 ++-- dennco.pro | 12 ++- 31 files changed, 684 insertions(+), 75 deletions(-) create mode 100644 Source/DNGlobal.cpp create mode 100644 Source/DNGlobal.h create mode 100644 Source/DNThread.cpp create mode 100644 Source/DNThread.h create mode 100644 Source/DNThreadImpl.h create mode 100644 Source/platform/qt/QtTKConsole.cpp create mode 100644 Source/platform/qt/qtdnthreadimpl.cpp create mode 100644 Source/platform/qt/qtdnthreadimpl.h diff --git a/Source/DNContainerBuilder.cpp b/Source/DNContainerBuilder.cpp index 4eb5e6d..a9b93d5 100644 --- a/Source/DNContainerBuilder.cpp +++ b/Source/DNContainerBuilder.cpp @@ -278,8 +278,8 @@ void DNContainerBuilder::parseXHTML(const char *docRoot, const char *path) if (classElement && (element == NULL || element->depth <= classElement->depth)) { std::string _cellCodeName = getFQNString(path,className.c_str()); - std::string _cellCodeAPIType = classAPIElement->text; - std::string _cellCodeScript = classScriptElement->text; + std::string _cellCodeAPIType = classAPIElement ? classAPIElement->text : ""; + std::string _cellCodeScript = classScriptElement ? classScriptElement->text : ""; DEBUG_TRACE("\n===== Define cell code class ===== \nClass:%s\nAPI:%s\n%s\n", _cellCodeName.c_str(), _cellCodeAPIType.c_str(), _cellCodeScript.c_str()); @@ -327,4 +327,4 @@ void DNContainerBuilder::parseXHTML(const char *docRoot, const char *path) } } -} \ No newline at end of file +} diff --git a/Source/DNDirectory.cpp b/Source/DNDirectory.cpp index 2a891fb..27b96f5 100644 --- a/Source/DNDirectory.cpp +++ b/Source/DNDirectory.cpp @@ -25,6 +25,12 @@ DNDirectory::DNDirectory(const char *dir) impl = DNDirectoryImpl::create(dir); } +DNDirectory::~DNDirectory() +{ + if (impl) + delete impl; +} + const DNFileList* DNDirectory::getFileList(const char *filter) { return impl->getFileList(filter); diff --git a/Source/DNDirectory.h b/Source/DNDirectory.h index 210bf03..301ec4a 100644 --- a/Source/DNDirectory.h +++ b/Source/DNDirectory.h @@ -27,7 +27,8 @@ class DNDirectory { public: DNDirectory(const char *dir); - + ~DNDirectory(); + const DNFileList* getFileList(const char *filter); private: diff --git a/Source/DNDirectoryImpl.h b/Source/DNDirectoryImpl.h index 00495f2..43e73e0 100644 --- a/Source/DNDirectoryImpl.h +++ b/Source/DNDirectoryImpl.h @@ -28,6 +28,8 @@ public: static DNDirectoryImpl* create(const char *dir); virtual const DNFileList* getFileList(const char *filter) = 0; + + virtual ~DNDirectoryImpl() {} }; #endif diff --git a/Source/DNEngine.cpp b/Source/DNEngine.cpp index 58f43ad..2e122e2 100644 --- a/Source/DNEngine.cpp +++ b/Source/DNEngine.cpp @@ -28,32 +28,50 @@ #include "DNEngine.h" #include "DNAlert.h" #include "DNXML.h" +#include "DNGlobal.h" #include DNEngine::DNEngine(const char *contentPath) : -mContainer(NULL),mIsRunning(false),mPortNumber(9080),mHTTPServer(NULL),mValid(false) +mContainer(NULL),mPortNumber(9080),mHTTPServer(NULL),mValid(false) { mTimeKeeper = new DNTimeKeeper(); mContainer = TKContainer::createContainer(); + dnGlobal()->updateRunningStatus(DNGlobal::STOPPED); + std::string basePath(contentPath); std::string containerRoot = basePath.append("/Container"); bool succeeded = false; succeeded = parseSettingFile(contentPath); - if (!succeeded) + if (!succeeded || !dnGlobal()->isErrorStatusNormal()) { - DNAlert::show("Initialization failed", "failed to parse setting file /property.xml"); + if (dnGlobal()->updateErrorStatus(DNGlobal::ERROR)) + { + dnGlobal()->setMessage1("property.xml parse error"); + dnGlobal()->setMessage2("failed to parse setting file /property.xml"); + } + else + { + dnGlobal()->setMessage1("property.xml parse error"); + } return; } succeeded = parseContainerFile(containerRoot.c_str()); if (!succeeded) { - DNAlert::show("Initialization failed", "failed to parse container file"); - return; + if (dnGlobal()->updateErrorStatus(DNGlobal::ERROR)) + { + dnGlobal()->setMessage1("Initialization failed"); + dnGlobal()->setMessage2("Failed to parse container file"); + } + else + { + dnGlobal()->setMessage1("Initialization failed"); + } } - + mValid = true; } @@ -86,9 +104,10 @@ bool DNEngine::parseSettingFile(const char *contentRoot) DNXML *xml = DNXML::createXMLFromFile(contentRoot, "property.xml"); if (xml) { - + //TODO + delete xml; } - return false; + return true; } bool DNEngine::parseContainerFile(const char *containerRoot) @@ -131,28 +150,34 @@ void DNEngine::setTickIntervalSec(float interval) mTimeKeeper->setIntevalSec(interval); } -void DNEngine::doTickThread() +bool DNEngine::startEngine() +{ + mDoTickThread = DNThread::createThread(DNEngine::doTickThread, this); + mDoTickThread->start(); +} + +bool DNEngine::stopEngine() { - mTickThreadLock.lock(); - if (mIsRunning) + dnGlobal()->updateRunningStatus(DNGlobal::STOPPING); + return mDoTickThread->waitForExit(5000); +} + +void DNEngine::doTickThread(void *self) +{ + if (!dnGlobal()->updateRunningStatus(DNGlobal::RUNNIING)) { - //doTickThread is already running. - mTickThreadLock.unlock(); return; } - mIsRunning = true; - mTickThreadLock.unlock(); - - - DNTimeKeeper *timeKeeper = new DNTimeKeeper(); + + DNEngine *engine = (DNEngine*)self; - while(mIsRunning) + while(dnGlobal()->getRunningStatus() == DNGlobal::RUNNIING) { - mContainer->doTick(timeKeeper->getTickTime()); - timeKeeper->sleepUntilNextInterval(); + engine->mContainer->doTick(engine->mTimeKeeper->getTickTime()); + engine->mTimeKeeper->sleepUntilNextInterval(); } - - delete timeKeeper; + + dnGlobal()->updateRunningStatus(DNGlobal::STOPPED); } float DNEngine::doClientGetRequest(const char* path) diff --git a/Source/DNEngine.h b/Source/DNEngine.h index 253ce7d..27500d3 100644 --- a/Source/DNEngine.h +++ b/Source/DNEngine.h @@ -25,6 +25,7 @@ class DNTimeKeeper; class DNHTTPServer; #include "TKLock.h" +#include "DNThread.h" #include @@ -33,13 +34,16 @@ class DNEngine public: DNEngine(const char *contentPath); virtual ~DNEngine(); + + bool startEngine(); + bool stopEngine(); bool startHTTPServer(int portNumber); void stopServer(); void setTickIntervalSec(float interval); - void doTickThread(); + static void doTickThread(void *self); float doClientGetRequest(const char* path); bool doClientSetRequest(const char* path, const char* value); @@ -54,12 +58,11 @@ private: TKContainer *mContainer; int mPortNumber; - TKLock mTickThreadLock; DNHTTPServer *mHTTPServer; - bool mIsRunning; DNTimeKeeper *mTimeKeeper; bool mValid; - + + DNThread *mDoTickThread; }; diff --git a/Source/DNFileList.cpp b/Source/DNFileList.cpp index e1875f3..b8adb29 100644 --- a/Source/DNFileList.cpp +++ b/Source/DNFileList.cpp @@ -28,8 +28,9 @@ DNFileList::~DNFileList() { if (prev) { - prev->next = this->next; + prev->next = next; } - this->next->prev = prev; + if (next) + next->prev = prev; } diff --git a/Source/DNGlobal.cpp b/Source/DNGlobal.cpp new file mode 100644 index 0000000..ffb630a --- /dev/null +++ b/Source/DNGlobal.cpp @@ -0,0 +1,131 @@ +// Copyright (c) 2012 Dennco Project +// +// This program 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 . + +// +// Created by tkawata on Mar-1/2012. +// +#include "DNGlobal.h" + +DNGlobal* dnGlobal() +{ + return DNGlobal::instance(); +} + +//static +DNGlobal *DNGlobal::instance() +{ + static DNGlobal *_instance = 0; + if (!_instance) + { + _instance = new DNGlobal(); + } + + return _instance; +} + +DNGlobal::DNGlobal() : mEStatus(NORMAL), mRStatus(STOPPED) +{ +} + +DNGlobal::~DNGlobal() +{ +} + +bool DNGlobal::updateErrorStatus(E_Status status) +{ + bool isStatusUpdated = false; + mLock.lock(); + if (mEStatus < status) + { + mEStatus = status; + isStatusUpdated = true; + } + mLock.unlock(); + return isStatusUpdated; +} + +void DNGlobal::setMessage1(std::string message1) +{ + mLock.lock(); + mMessage1 = message1; + mLock.unlock(); +} + +void DNGlobal::setMessage2(std::string message2) +{ + mLock.lock(); + mMessage2 = message2; + mLock.unlock(); +} + +DNGlobal::E_Status DNGlobal::getErrorStatus() +{ + return mEStatus; +} + +bool DNGlobal::isErrorStatusNormal() +{ + return mEStatus == NORMAL; +} + +std::string DNGlobal::getMessage1() +{ + if (mEStatus > NORMAL) + { + return mMessage1; + } + else + { + return ""; + } +} + +std::string DNGlobal::getMessage2() +{ + if (mEStatus > NORMAL) + { + return mMessage2; + } + else + { + return ""; + } +} + +void DNGlobal::resetErrorStatus() +{ + mEStatus = NORMAL; + mMessage1 = ""; + mMessage2 = ""; +} + +DNGlobal::R_Status DNGlobal::getRunningStatus() +{ + return mRStatus; +} + +bool DNGlobal::updateRunningStatus(R_Status status) +{ + bool updated = false; + mLock.lock(); + if (mRStatus != status) + { + updated = true; + mRStatus = status; + } + mLock.unlock(); + + return updated; +} diff --git a/Source/DNGlobal.h b/Source/DNGlobal.h new file mode 100644 index 0000000..c81096c --- /dev/null +++ b/Source/DNGlobal.h @@ -0,0 +1,59 @@ +// Copyright (c) 2012 Dennco Project +// +// This program 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 . + +// +// Created by tkawata on Mar-1/2012. +// +#ifndef DNGLOBAL_H +#define DNGLOBAL_H + +#include +#include "TKLock.h" + +class DNGlobal +{ +public: + static DNGlobal* instance(); + ~DNGlobal(); + + enum E_Status {NORMAL,WARNING,ERROR,FATAL_ERROR}; + enum R_Status {STOPPED,RUNNIING,STOPPING}; + + bool updateErrorStatus(E_Status status); + E_Status getErrorStatus(); + + bool isErrorStatusNormal(); + void setMessage1(std::string message); + void setMessage2(std::string message); + std::string getMessage1(); + std::string getMessage2(); + void resetErrorStatus(); + + bool updateRunningStatus(R_Status running); + R_Status getRunningStatus(); + + +private: + DNGlobal(); + TKLock mLock; + std::string mMessage1; + std::string mMessage2; + E_Status mEStatus; + R_Status mRStatus; +}; + +DNGlobal* dnGlobal(); + +#endif // DNGLOBAL_H diff --git a/Source/DNHTTPServer.cpp b/Source/DNHTTPServer.cpp index 6e8aa45..13a4dcc 100644 --- a/Source/DNHTTPServer.cpp +++ b/Source/DNHTTPServer.cpp @@ -34,6 +34,12 @@ DNHTTPServer::DNHTTPServer(DNEngine* engine) : DNServer(engine) impl = DNHTTPServerImpl::create(this, DNHTTPServer::clientRequestHandler); } +DNHTTPServer::~DNHTTPServer() +{ + if (impl) + delete impl; +} + bool DNHTTPServer::isRunning() { return impl->isRunning(); diff --git a/Source/DNHTTPServer.h b/Source/DNHTTPServer.h index 9de07a5..bb0c633 100644 --- a/Source/DNHTTPServer.h +++ b/Source/DNHTTPServer.h @@ -39,6 +39,7 @@ class DNHTTPServer : public DNServer { public: DNHTTPServer(DNEngine *engine); + ~DNHTTPServer(); virtual bool isRunning(); virtual void start(); diff --git a/Source/DNHTTPServerImpl.h b/Source/DNHTTPServerImpl.h index 3b14aa1..26259de 100644 --- a/Source/DNHTTPServerImpl.h +++ b/Source/DNHTTPServerImpl.h @@ -37,6 +37,7 @@ public: virtual void replyWithStatusCode(int code, const char* message) = 0; virtual void replyWithFile(const char* filePath) = 0; + virtual ~DNHTTPServerImpl() {}; }; #endif diff --git a/Source/DNThread.cpp b/Source/DNThread.cpp new file mode 100644 index 0000000..a9468a1 --- /dev/null +++ b/Source/DNThread.cpp @@ -0,0 +1,47 @@ +// Copyright (c) 2012 Dennco Project +// +// This program 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 . + +// +// Created by tkawata on Mar-1/2012. +// +#include "DNThread.h" +#include "DNThreadImpl.h" + +DNThread::DNThread(DNThreadImpl *_impl) : impl(_impl) +{ +} + +DNThread::~DNThread() +{ + if (impl) + delete impl; +} + + +//static +DNThread* DNThread::createThread(DNThreadFunc threadFunc, void *data) +{ + return DNThreadImpl::createThread(threadFunc, data); +} + +bool DNThread::start() +{ + return impl->start(); +} + +bool DNThread::waitForExit(int timeout) +{ + return impl->waitForExit(timeout); +} diff --git a/Source/DNThread.h b/Source/DNThread.h new file mode 100644 index 0000000..58b166f --- /dev/null +++ b/Source/DNThread.h @@ -0,0 +1,42 @@ +// Copyright (c) 2012 Dennco Project +// +// This program 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 . + +// +// Created by tkawata on Mar-1/2012. +// +#ifndef DNTHREAD_H +#define DNTHREAD_H + +typedef void (*DNThreadFunc)(void* data); + +class DNThreadImpl; + +class DNThread +{ +public: + DNThread(DNThreadImpl *impl); + ~DNThread(); + + bool waitForExit(int timeout); + bool start(); + + static DNThread* createThread(DNThreadFunc threadFunc, void *data); + +private: + + DNThreadImpl *impl; +}; + +#endif // DNTHREAD_H diff --git a/Source/DNThreadImpl.h b/Source/DNThreadImpl.h new file mode 100644 index 0000000..f0a9e29 --- /dev/null +++ b/Source/DNThreadImpl.h @@ -0,0 +1,37 @@ +// Copyright (c) 2012 Dennco Project +// +// This program 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 . + +// +// Created by tkawata on Mar-1/2012. +// +#ifndef DNTHREADIMPL_H +#define DNTHREADIMPL_H + +#include "DNThread.h" + +class DNThreadImpl +{ +public: + static DNThread* createThread(DNThreadFunc threadFunc, void *data); + + virtual bool start() = 0; + virtual bool waitForExit(int timeout) = 0; + + DNThreadImpl() {} + virtual ~DNThreadImpl() {} + +}; + +#endif // DNTHREADIMPL_H diff --git a/Source/DNXMLImpl.h b/Source/DNXMLImpl.h index 7c390cb..75a8dc2 100644 --- a/Source/DNXMLImpl.h +++ b/Source/DNXMLImpl.h @@ -29,6 +29,7 @@ public: static DNXMLImpl* createXMLFromFileImpl(const char *docRoot, const char *path); virtual DNXMLElement* getRoot() = 0; + virtual ~DNXMLImpl() {} }; #endif diff --git a/Source/QtDennco/mainwindow.cpp b/Source/QtDennco/mainwindow.cpp index 003e1c5..60b461b 100644 --- a/Source/QtDennco/mainwindow.cpp +++ b/Source/QtDennco/mainwindow.cpp @@ -5,6 +5,8 @@ #include #include #include "TKLog.h" +#include "DNGlobal.h" +#include "DNAlert.h" MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), @@ -43,16 +45,39 @@ void MainWindow::on_startButton_clicked() { delete mEngine; } - QString path = ui->filePath->text(); - mEngine = new DNEngine(ui->filePath->text().toLocal8Bit().data()); - if (mEngine->isValid()) + + if (startEngine()) { ui->startButton->setText("Stop"); } - } else { ui->startButton->setText("Start"); } } + +bool MainWindow::startEngine() +{ + bool initializationFailed = false; + + mEngine = new DNEngine(ui->filePath->text().toLocal8Bit().data()); + if (!dnGlobal()->isErrorStatusNormal()) + { + DNAlert::show(dnGlobal()->getMessage1(), dnGlobal()->getMessage2()); + dnGlobal()->resetErrorStatus(); + initializationFailed = true; + } + else if (!mEngine->isValid()) + { + DNAlert::show("DNEngine initialize error", "ERROR. DNEngine not configured properly"); + initializationFailed = true; + } + + if (!initializationFailed) + { + mEngine->startEngine(); + } + return !initializationFailed; +} + diff --git a/Source/QtDennco/mainwindow.h b/Source/QtDennco/mainwindow.h index e19a202..98e2170 100644 --- a/Source/QtDennco/mainwindow.h +++ b/Source/QtDennco/mainwindow.h @@ -17,12 +17,15 @@ class MainWindow : public QMainWindow public: explicit MainWindow(QWidget *parent = 0); ~MainWindow(); + private slots: void on_chooseDirButton_clicked(); void on_startButton_clicked(); private: + bool startEngine(); + Ui::MainWindow *ui; DNEngine *mEngine; TKConsole *mConsole; diff --git a/Source/QtScript/dnqsbasiccell.cpp b/Source/QtScript/dnqsbasiccell.cpp index b6a126e..6715a22 100644 --- a/Source/QtScript/dnqsbasiccell.cpp +++ b/Source/QtScript/dnqsbasiccell.cpp @@ -46,7 +46,7 @@ DNQSBasicCell::~DNQSBasicCell() bool DNQSBasicCell::doTick(float time) { - if (mCellCodeInstance) return false; + if (!mCellCodeInstance) return false; //update receptor values for ( TKReceptorMap::iterator it = mReceptors.begin(); it != mReceptors.end(); ++it ) { diff --git a/Source/QtScript/dnqscellcodeinstance.cpp b/Source/QtScript/dnqscellcodeinstance.cpp index 0146208..f942c0a 100644 --- a/Source/QtScript/dnqscellcodeinstance.cpp +++ b/Source/QtScript/dnqscellcodeinstance.cpp @@ -66,7 +66,7 @@ bool DNQSCellCodeInstance::doTick(float time) { QScriptValueList args; args << time; - mQSDoInit.call(mQSCellCodeInstance, args); + mQSDoTick.call(mQSCellCodeInstance, args); return true; } else diff --git a/Source/QtScript/dnqscontainer.cpp b/Source/QtScript/dnqscontainer.cpp index 6f9dba4..7139a6e 100644 --- a/Source/QtScript/dnqscontainer.cpp +++ b/Source/QtScript/dnqscontainer.cpp @@ -35,8 +35,7 @@ static QScriptValue scriptPrint(QScriptContext *context, QScriptEngine *engine) if (context->argumentCount() >= 1) { QString value = context->argument(0).toString(); - const char *str = value.toUtf8().constData(); - TKLog::printf("%s", str); + TKLog::printf("%s", value.toLocal8Bit().data()); } return engine->nullValue(); } diff --git a/Source/TKCell.cpp b/Source/TKCell.cpp index 0f807ea..f794f3a 100644 --- a/Source/TKCell.cpp +++ b/Source/TKCell.cpp @@ -70,9 +70,10 @@ void TKCell::setAxonValue(float value) bool TKCell::setCellCode(TKCellCode *code, const void *data) { mCellCodeInstance = code->createCellCodeInstance(this, data); - mCellCodeInstance->doInit(); - - return true; + if (mCellCodeInstance) + return mCellCodeInstance->doInit(); + else + return false; } bool TKCell::connectTo(std::string connectionName, TKCell *targetCell) diff --git a/Source/TKContainer.h b/Source/TKContainer.h index 3505de1..18fe130 100644 --- a/Source/TKContainer.h +++ b/Source/TKContainer.h @@ -53,7 +53,7 @@ public: inline virtual float getValue(std::string key) = 0; protected: - TKContainer() {}; + TKContainer() {} TKCellMap mCells; TKCellMap mInterfaceCells; TKCellCodeMap mCellCodes; diff --git a/Source/TKDebug.h b/Source/TKDebug.h index 85c1de8..be7c36a 100644 --- a/Source/TKDebug.h +++ b/Source/TKDebug.h @@ -30,9 +30,9 @@ #endif #ifdef DEBUG -#define TKASSERT(b) { if (!b) {TKLog::debugPrintf("ASSERT ERROR! %s(%d)",__PRETTY_FUNCTION__, __LINE__);exit(-1);}} + #define TKASSERT(b) { if (!b) {TKLog::debugPrintf("ASSERT ERROR! %s(%d)",__PRETTY_FUNCTION__, __LINE__);/*TODO*/}} #else -#define TKASSERT(b) + #define TKASSERT(b) #endif -#endif \ No newline at end of file +#endif diff --git a/Source/platform/qt/QtTKConsole.cpp b/Source/platform/qt/QtTKConsole.cpp new file mode 100644 index 0000000..860cc0e --- /dev/null +++ b/Source/platform/qt/QtTKConsole.cpp @@ -0,0 +1,36 @@ +// Copyright (c) 2012 Dennco Project +// +// This program 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 . + +// +// Created by tkawata on 2/27/2012. +// + +#include "TKConsole.h" + +#include + +void TKConsole::vprintf(const char *fmt, va_list ap) +{ + QString msg = QString().vsprintf(fmt,ap); + qDebug() << msg << endl; +} + +void TKConsole::vDebugPrintf(const char *fmt, va_list ap) +{ +#ifdef DEBUG + QString msg = QString().vsprintf(fmt,ap); + qDebug() << "DEBUG:" << msg << endl; +#endif +} diff --git a/Source/platform/qt/qtdndirectoryimpl.cpp b/Source/platform/qt/qtdndirectoryimpl.cpp index d726086..953efa3 100644 --- a/Source/platform/qt/qtdndirectoryimpl.cpp +++ b/Source/platform/qt/qtdndirectoryimpl.cpp @@ -39,7 +39,22 @@ const DNFileList* QtDNDirectoryImpl::getFileList(const char *filter) { cleanFileList(); - QDir dir(mRootDir,QString::fromLocal8Bit(filter)); + QDir dir(mRootDir); + QStringList filters; + QString filterString = QString::fromLocal8Bit(filter); + if (filterString.length()>0) + { + int idx = 0; + while( (idx = filterString.indexOf("|")) != -1 ) + { + QString pre("*."); + filters << pre.append(filterString.left(idx)); + filterString = filterString.right(filterString.length()-idx-1); + } + QString pre("*."); + filters << pre.append(filterString); + dir.setNameFilters (filters); + } QStringList list = dir.entryList(QDir::Files); mFileList = 0; DNFileList *prev = 0; diff --git a/Source/platform/qt/qtdnthreadimpl.cpp b/Source/platform/qt/qtdnthreadimpl.cpp new file mode 100644 index 0000000..df77449 --- /dev/null +++ b/Source/platform/qt/qtdnthreadimpl.cpp @@ -0,0 +1,80 @@ +// Copyright (c) 2012 Dennco Project +// +// This program 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 . + +// +// Created by tkawata on Mar-1/2012. +// +#include "qtdnthreadimpl.h" + +#include "TKLog.h" + +//static +DNThread* DNThreadImpl::createThread(DNThreadFunc threadFunc, void *data) +{ + return new DNThread(new QtDNThreadImpl(threadFunc,data)); +} + +QtDNThreadImpl::QtDNThreadImpl(DNThreadFunc threadFunc, void *data): mThreadFunc(threadFunc), mData(data) +{ + mThread = new QThread; +} + +QtDNThreadImpl::~QtDNThreadImpl() +{ + mLock.lock(); + if (mThread) + { + QThread *t = mThread; + mThread = NULL; + mLock.unlock(); + if (!t->wait(5000)) + { + TKLog::printf("!WARNING! DNThread %p hasn't finished in 5sec. execute terminate to quit forcedly ", this); + t->terminate(); + } + delete t; + } + else + { + mLock.unlock(); + } +} + +bool QtDNThreadImpl::start() +{ + bool started = false; + mLock.lock(); + if (!mThread->isRunning()) + { + moveToThread(mThread); + mThread->start(); + QMetaObject::invokeMethod(this, "doWork", Qt::QueuedConnection); + started = true; + } + mLock.unlock(); + return started; +} + +bool QtDNThreadImpl::waitForExit(int timeout) +{ + return mThread->wait(timeout); +} + +void QtDNThreadImpl::doWork() +{ + mThreadFunc(mData); +} + + diff --git a/Source/platform/qt/qtdnthreadimpl.h b/Source/platform/qt/qtdnthreadimpl.h new file mode 100644 index 0000000..a29e0fa --- /dev/null +++ b/Source/platform/qt/qtdnthreadimpl.h @@ -0,0 +1,52 @@ +// Copyright (c) 2012 Dennco Project +// +// This program 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 . + +// +// Created by tkawata on Mar-1/2012. +// +#ifndef QTDNTHREADIMPL_H +#define QTDNTHREADIMPL_H + +#include "DNThreadImpl.h" + +#include "TKLock.h" +#include "DNThread.h" + +#include +#include + +class QtDNThreadImpl : public QObject, public DNThreadImpl +{ + Q_OBJECT + +public: + QtDNThreadImpl(DNThreadFunc threadFunc, void *data); + virtual ~QtDNThreadImpl(); + + virtual bool start(); + virtual bool waitForExit(int timeout); + + +public slots: + void doWork(); + +private: + DNThreadFunc mThreadFunc; + void *mData; + TKLock mLock; + QThread *mThread; +}; + +#endif // QTDNTHREADIMPL_H diff --git a/Source/platform/qt/qtdnxmlimpl.cpp b/Source/platform/qt/qtdnxmlimpl.cpp index 3cfcfa6..647be24 100644 --- a/Source/platform/qt/qtdnxmlimpl.cpp +++ b/Source/platform/qt/qtdnxmlimpl.cpp @@ -20,6 +20,7 @@ #include "DNXMLElement.h" #include "TKLog.h" +#include "DNGlobal.h" DNXMLImpl *DNXMLImpl::createXMLFromFileImpl(const char *docRoot, const char *docPath) { @@ -33,28 +34,35 @@ DNXMLImpl *DNXMLImpl::createXMLFromFileImpl(const char *docRoot, const char *doc QString filePath = qDocRoot; filePath.append(qDocPath); + return new QtDNXMLImpl(filePath); +} + +QtDNXMLImpl::QtDNXMLImpl(QString filePath) : mValid(false), mFilePath(filePath) +{ QFile file(filePath); if (file.open(QFile::ReadOnly | QFile::Text)) { QXmlInputSource xmlInputSource(&file); - return new QtDNXMLImpl(&xmlInputSource); + mHandler = new QtXMLHandler(filePath); + QXmlSimpleReader reader; + reader.setContentHandler(mHandler); + reader.setErrorHandler(mHandler); + + mValid = reader.parse(xmlInputSource); } - else + + if (!mValid) { - TKLog::printf("ERROR failed to load file.%s/%s", docRoot,docPath); - return NULL; + TKLog::printf("ERROR failed to load file.%s", filePath.toLocal8Bit().data()); + std::string message = "failed to load file:"; + message.append(filePath.toStdString()); + if (dnGlobal()->updateErrorStatus(DNGlobal::ERROR)) + { + dnGlobal()->setMessage1("Error at loading XML file"); + dnGlobal()->setMessage2(message); + } } } -QtDNXMLImpl::QtDNXMLImpl(QXmlInputSource *input) -{ - mHandler = new QtXMLHandler(); - QXmlSimpleReader reader; - reader.setContentHandler(mHandler); - reader.setErrorHandler(mHandler); - - reader.parse(input,true); -} - QtDNXMLImpl::~QtDNXMLImpl() { if (mHandler) @@ -63,7 +71,7 @@ QtDNXMLImpl::~QtDNXMLImpl() DNXMLElement* QtDNXMLImpl::getRoot() { - if (mHandler) + if (mHandler && mValid) { return mHandler->getRoot(); } @@ -73,7 +81,7 @@ DNXMLElement* QtDNXMLImpl::getRoot() } } -QtXMLHandler::QtXMLHandler() : mRootElement(NULL), mCurrentElement(NULL) +QtXMLHandler::QtXMLHandler(QString filePath) : mRootElement(NULL), mCurrentElement(NULL), mFilePath(filePath) { } @@ -107,15 +115,16 @@ bool QtXMLHandler::startElement(const QString & namespaceURI, const QString & lo { if (!mCurrentElement->inner) { - mCurrentElement->inner = new DNXMLElement(qName.toStdString()); DNXMLElement *outer = mCurrentElement; + mCurrentElement->inner = new DNXMLElement(qName.toStdString()); mCurrentElement = mCurrentElement->inner; mCurrentElement->outer = outer; - mCurrentElement->depth = outer->depth++; + mCurrentElement->depth = outer->depth + 1; } else { DNXMLElement *prev = mCurrentElement->inner; + while(prev->next) prev = prev->next; prev->next = new DNXMLElement(qName.toStdString()); mCurrentElement = prev->next; mCurrentElement->outer = prev->outer; @@ -144,7 +153,7 @@ bool QtXMLHandler::endElement (const QString & namespaceURI, const QString & loc bool QtXMLHandler::characters (const QString & ch ) { - mCurrentElement->text = ch.toStdString(); + mCurrentElement->text.append(ch.toStdString()); return true; } @@ -155,3 +164,17 @@ bool QtXMLHandler::error (const QXmlParseException & exception ) return true; } +bool QtXMLHandler::fatalError(const QXmlParseException & exception ) +{ + QString message = QString().sprintf("ERROR while reading XML file!! file.%s (line:%d) \nReason:%s", + mFilePath.toLocal8Bit().data(), exception.lineNumber(), exception.message().toLocal8Bit().data()); + TKLog::printf("%s", message.toLocal8Bit().data()); + if (dnGlobal()->updateErrorStatus(DNGlobal::ERROR)) + { + dnGlobal()->setMessage1("Error at loading XML file"); + dnGlobal()->setMessage2(message.toStdString()); + } + return true; +} + + diff --git a/Source/platform/qt/qtdnxmlimpl.h b/Source/platform/qt/qtdnxmlimpl.h index 8d45150..aea2b2b 100644 --- a/Source/platform/qt/qtdnxmlimpl.h +++ b/Source/platform/qt/qtdnxmlimpl.h @@ -25,7 +25,7 @@ class QtXMLHandler : public QXmlDefaultHandler { public: - QtXMLHandler(); + QtXMLHandler(QString filePath); virtual ~QtXMLHandler(); DNXMLElement *getRoot() { return mRootElement; } @@ -37,22 +37,26 @@ public: virtual bool endElement (const QString & namespaceURI, const QString & localName, const QString & qName ); virtual bool characters (const QString & ch ); virtual bool error (const QXmlParseException & exception ); + virtual bool fatalError(const QXmlParseException & exception ); private: - DNXMLElement *mRootElement; - DNXMLElement *mCurrentElement; + DNXMLElement *mRootElement; + DNXMLElement *mCurrentElement; + QString mFilePath; }; class QtDNXMLImpl : public DNXMLImpl { public: - QtDNXMLImpl(QXmlInputSource *input); + QtDNXMLImpl(QString filePath); virtual ~QtDNXMLImpl(); virtual DNXMLElement *getRoot(); private: - QtXMLHandler *mHandler; + QtXMLHandler *mHandler; + bool mValid; + QString mFilePath; }; diff --git a/dennco.pro b/dennco.pro index 25a5bdf..1ba82b3 100644 --- a/dennco.pro +++ b/dennco.pro @@ -46,7 +46,10 @@ SOURCES += Source/QtDennco/mainwindow.cpp \ Source/platform/qt/qtsimplehttpserverimpl.cpp \ Source/platform/qt/qtdntimekeeperimpl.cpp \ Source/platform/qt/qtdnxmlimpl.cpp \ - Source/platform/qt/QtTKConsole.cpp + Source/platform/qt/QtTKConsole.cpp \ + Source/DNGlobal.cpp \ + Source/DNThread.cpp \ + Source/platform/qt/qtdnthreadimpl.cpp HEADERS += Source/QtDennco/mainwindow.h \ Source/TKUICell.h \ @@ -90,6 +93,11 @@ HEADERS += Source/QtDennco/mainwindow.h \ Source/platform/qt/qtdnalertimpl.h \ Source/platform/qt/qtsimplehttpserverimpl.h \ Source/platform/qt/qtdntimekeeperimpl.h \ - Source/platform/qt/qtdnxmlimpl.h + Source/platform/qt/qtdnxmlimpl.h \ + Source/DNGlobal.h \ + Source/DNThread.h \ + Source/DNThreadImpl.h \ + Source/platform/qt/qtdnthreadimpl.h FORMS += Source/QtDennco/mainwindow.ui +Debug:DEFINES+=DEBUG -- 2.11.0