tests/auto/qtipc/lackey/main.cpp
changeset 7 f7bc934e204c
equal deleted inserted replaced
3:41300fa6a67c 7:f7bc934e204c
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
       
     4 ** All rights reserved.
       
     5 ** Contact: Nokia Corporation (qt-info@nokia.com)
       
     6 **
       
     7 ** This file is part of the test suite of the Qt Toolkit.
       
     8 **
       
     9 ** $QT_BEGIN_LICENSE:LGPL$
       
    10 ** No Commercial Usage
       
    11 ** This file contains pre-release code and may not be distributed.
       
    12 ** You may use this file in accordance with the terms and conditions
       
    13 ** contained in the Technology Preview License Agreement accompanying
       
    14 ** this package.
       
    15 **
       
    16 ** GNU Lesser General Public License Usage
       
    17 ** Alternatively, this file may be used under the terms of the GNU Lesser
       
    18 ** General Public License version 2.1 as published by the Free Software
       
    19 ** Foundation and appearing in the file LICENSE.LGPL included in the
       
    20 ** packaging of this file.  Please review the following information to
       
    21 ** ensure the GNU Lesser General Public License version 2.1 requirements
       
    22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
       
    23 **
       
    24 ** In addition, as a special exception, Nokia gives you certain additional
       
    25 ** rights.  These rights are described in the Nokia Qt LGPL Exception
       
    26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
       
    27 **
       
    28 ** If you have questions regarding the use of this file, please contact
       
    29 ** Nokia at qt-info@nokia.com.
       
    30 **
       
    31 **
       
    32 **
       
    33 **
       
    34 **
       
    35 **
       
    36 **
       
    37 **
       
    38 ** $QT_END_LICENSE$
       
    39 **
       
    40 ****************************************************************************/
       
    41 
       
    42 
       
    43 
       
    44 #include <qscriptengine.h>
       
    45 
       
    46 #include <QtCore/QFile>
       
    47 #include <QtCore/QTextStream>
       
    48 #include <QTest>
       
    49 
       
    50 #include <qstringlist.h>
       
    51 #include <stdlib.h>
       
    52 #include <qsharedmemory.h>
       
    53 #include <qsystemsemaphore.h>
       
    54 #include <qsystemlock.h>
       
    55 
       
    56 class ScriptSystemSemaphore : public QObject
       
    57 {
       
    58     Q_OBJECT
       
    59 
       
    60 public:
       
    61     ScriptSystemSemaphore(QObject *parent = 0) : QObject(parent), ss(QString())
       
    62     {
       
    63     }
       
    64 
       
    65 public slots:
       
    66     bool acquire()
       
    67     {
       
    68         return ss.acquire();
       
    69     };
       
    70 
       
    71     bool release(int n = 1)
       
    72     {
       
    73         return ss.release(n);
       
    74     };
       
    75 
       
    76     void setKey(const QString &key, int n = 0)
       
    77     {
       
    78         ss.setKey(key, n);
       
    79     };
       
    80 
       
    81     QString key() const
       
    82     {
       
    83         return ss.key();
       
    84     }
       
    85 
       
    86 private:
       
    87     QSystemSemaphore ss;
       
    88 };
       
    89 
       
    90 class ScriptSystemLock : public QObject
       
    91 {
       
    92     Q_OBJECT
       
    93     Q_PROPERTY(QString key WRITE setKey READ key)
       
    94 
       
    95 public:
       
    96     ScriptSystemLock(QObject *parent = 0) : QObject(parent), sl(QString())
       
    97     {
       
    98     }
       
    99 
       
   100 public slots:
       
   101 
       
   102     bool lockReadOnly()
       
   103     {
       
   104         return sl.lock(QSystemLock::ReadOnly);
       
   105     }
       
   106 
       
   107     bool lock()
       
   108     {
       
   109         return sl.lock();
       
   110     };
       
   111 
       
   112     bool unlock()
       
   113     {
       
   114         return sl.unlock();
       
   115     };
       
   116 
       
   117     void setKey(const QString &key)
       
   118     {
       
   119         sl.setKey(key);
       
   120     };
       
   121 
       
   122     QString key() const
       
   123     {
       
   124         return sl.key();
       
   125     }
       
   126 
       
   127 private:
       
   128     QSystemLock sl;
       
   129 };
       
   130 
       
   131 class ScriptSharedMemory : public QObject
       
   132 {
       
   133     Q_OBJECT
       
   134     Q_PROPERTY(bool attached READ isAttached)
       
   135     Q_PROPERTY(QString key WRITE setKey READ key)
       
   136 
       
   137 public:
       
   138     enum SharedMemoryError
       
   139     {
       
   140         NoError = 0,
       
   141         PermissionDenied = 1,
       
   142         InvalidSize = 2,
       
   143         KeyError = 3,
       
   144         AlreadyExists = 4,
       
   145         NotFound = 5,
       
   146         LockError = 6,
       
   147         OutOfResources = 7,
       
   148         UnknownError = 8
       
   149     };
       
   150 
       
   151     ScriptSharedMemory(QObject *parent = 0) : QObject(parent)
       
   152     {
       
   153     }
       
   154 
       
   155 public slots:
       
   156     void sleep(int x) const
       
   157     {
       
   158         QTest::qSleep(x);
       
   159     }
       
   160 
       
   161     bool create(int size)
       
   162     {
       
   163         return sm.create(size);
       
   164     };
       
   165 
       
   166     bool createReadOnly(int size)
       
   167     {
       
   168         return sm.create(size, QSharedMemory::ReadOnly);
       
   169     };
       
   170 
       
   171     int size() const
       
   172     {
       
   173         return sm.size();
       
   174     };
       
   175 
       
   176     bool attach()
       
   177     {
       
   178         return sm.attach();
       
   179     };
       
   180 
       
   181     bool attachReadOnly()
       
   182     {
       
   183         return sm.attach(QSharedMemory::ReadOnly);
       
   184     };
       
   185 
       
   186     bool isAttached() const
       
   187     {
       
   188         return sm.isAttached();
       
   189     };
       
   190 
       
   191     bool detach()
       
   192     {
       
   193         return sm.detach();
       
   194     };
       
   195 
       
   196     int error() const
       
   197     {
       
   198         return (int)sm.error();
       
   199     };
       
   200 
       
   201     QString errorString() const
       
   202     {
       
   203         return sm.errorString();
       
   204     };
       
   205 
       
   206     void set(int i, QChar value)
       
   207     {
       
   208         ((char*)sm.data())[i] = value.toLatin1();
       
   209     }
       
   210 
       
   211     QString get(int i)
       
   212     {
       
   213         return QChar::fromLatin1(((char*)sm.data())[i]);
       
   214     }
       
   215 
       
   216     char *data() const
       
   217     {
       
   218         return (char*)sm.data();
       
   219     };
       
   220 
       
   221     void setKey(const QString &key)
       
   222     {
       
   223         sm.setKey(key);
       
   224     };
       
   225 
       
   226     QString key() const
       
   227     {
       
   228         return sm.key();
       
   229     }
       
   230 
       
   231     bool lock()
       
   232     {
       
   233         return sm.lock();
       
   234     }
       
   235 
       
   236     bool unlock()
       
   237     {
       
   238         return sm.unlock();
       
   239     }
       
   240 
       
   241 private:
       
   242     QSharedMemory sm;
       
   243 };
       
   244 
       
   245 QT_BEGIN_NAMESPACE
       
   246 Q_SCRIPT_DECLARE_QMETAOBJECT(ScriptSharedMemory, QObject*);
       
   247 Q_SCRIPT_DECLARE_QMETAOBJECT(ScriptSystemLock, QObject*);
       
   248 Q_SCRIPT_DECLARE_QMETAOBJECT(ScriptSystemSemaphore, QObject*);
       
   249 QT_END_NAMESPACE
       
   250 
       
   251 static void interactive(QScriptEngine &eng)
       
   252 {
       
   253 #ifdef Q_OS_WINCE
       
   254     fprintf(stderr, "Interactive mode not supported on Windows CE\n");
       
   255     return;
       
   256 #endif
       
   257     QTextStream qin(stdin, QFile::ReadOnly);
       
   258 
       
   259     const char *qscript_prompt = "qs> ";
       
   260     const char *dot_prompt = ".... ";
       
   261     const char *prompt = qscript_prompt;
       
   262 
       
   263     QString code;
       
   264 
       
   265     forever {
       
   266         QString line;
       
   267 
       
   268         printf("%s", prompt);
       
   269         fflush(stdout);
       
   270 
       
   271         line = qin.readLine();
       
   272         if (line.isNull())
       
   273         break;
       
   274 
       
   275         code += line;
       
   276         code += QLatin1Char('\n');
       
   277 
       
   278         if (line.trimmed().isEmpty()) {
       
   279             continue;
       
   280 
       
   281         } else if (! eng.canEvaluate(code)) {
       
   282             prompt = dot_prompt;
       
   283 
       
   284         } else {
       
   285             QScriptValue result = eng.evaluate(code);
       
   286             code.clear();
       
   287             prompt = qscript_prompt;
       
   288             if (!result.isUndefined())
       
   289                 fprintf(stderr, "%s\n", qPrintable(result.toString()));
       
   290         }
       
   291     }
       
   292 }
       
   293 
       
   294 int main(int argc, char *argv[])
       
   295 {
       
   296     QCoreApplication app(argc, argv);
       
   297 
       
   298     QScriptEngine eng;
       
   299     QScriptValue globalObject = eng.globalObject();
       
   300 
       
   301     QScriptValue sm = qScriptValueFromQMetaObject<ScriptSharedMemory>(&eng);
       
   302     eng.globalObject().setProperty("ScriptSharedMemory", sm);
       
   303 
       
   304     QScriptValue sl = qScriptValueFromQMetaObject<ScriptSystemLock>(&eng);
       
   305     eng.globalObject().setProperty("ScriptSystemLock", sl);
       
   306 
       
   307     QScriptValue ss = qScriptValueFromQMetaObject<ScriptSystemSemaphore>(&eng);
       
   308     eng.globalObject().setProperty("ScriptSystemSemaphore", ss);
       
   309 
       
   310 
       
   311     if (! *++argv) {
       
   312         interactive(eng);
       
   313         return EXIT_SUCCESS;
       
   314     }
       
   315 
       
   316     QStringList arguments = app.arguments();
       
   317     arguments.takeFirst();
       
   318 
       
   319     while (!arguments.isEmpty()) {
       
   320         QString fn = arguments.takeFirst();
       
   321 
       
   322         if (fn == QLatin1String("-i")) {
       
   323             interactive(eng);
       
   324             break;
       
   325         }
       
   326 
       
   327         QString contents;
       
   328 
       
   329         if (fn == QLatin1String("-")) {
       
   330             QTextStream stream(stdin, QFile::ReadOnly);
       
   331             contents = stream.readAll();
       
   332         } else {
       
   333             QFile file(fn);
       
   334 	    if (!file.exists()) {
       
   335                 fprintf(stderr, "%s doesn't exists\n", qPrintable(fn));
       
   336 	        return EXIT_FAILURE;
       
   337 	    }
       
   338             if (file.open(QFile::ReadOnly)) {
       
   339                 QTextStream stream(&file);
       
   340                 contents = stream.readAll();
       
   341                 file.close();
       
   342             }
       
   343         }
       
   344 
       
   345         if (contents.isEmpty())
       
   346             continue;
       
   347 
       
   348         if (contents[0] == '#') {
       
   349             contents.prepend("//");
       
   350             QScriptValue args = eng.newArray();
       
   351             args.setProperty("0", QScriptValue(&eng, fn));
       
   352             int i = 1;
       
   353             while (!arguments.isEmpty())
       
   354                 args.setProperty(i++, QScriptValue(&eng, arguments.takeFirst()));
       
   355             eng.currentContext()->activationObject().setProperty("args", args);
       
   356         }
       
   357         QScriptValue r = eng.evaluate(contents);
       
   358         if (eng.hasUncaughtException()) {
       
   359             int line = eng.uncaughtExceptionLineNumber();
       
   360             fprintf(stderr, "%d: %s\n\t%s\n\n", line, qPrintable(fn), qPrintable(r.toString()));
       
   361             return EXIT_FAILURE;
       
   362         }
       
   363         if (r.isNumber())
       
   364             return r.toInt32();
       
   365     }
       
   366 
       
   367     return EXIT_SUCCESS;
       
   368 }
       
   369 
       
   370 #include "main.moc"