File: userscript.cpp

package info (click to toggle)
ostinato 1.3.0-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 4,840 kB
  • sloc: cpp: 46,226; makefile: 8
file content (545 lines) | stat: -rw-r--r-- 14,505 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
/*
Copyright (C) 2010, 2014 Srivats P.

This file is part of "Ostinato"

This 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 <http://www.gnu.org/licenses/>
*/

#include "userscript.h"

//
// -------------------- UserScriptProtocol --------------------
//

UserScriptProtocol::UserScriptProtocol(StreamBase *stream, AbstractProtocol *parent)
    : AbstractProtocol(stream, parent),
        userProtocol_(this)
{
    isScriptValid_ = false;
    errorLineNumber_ = 0;

    userProtocolScriptValue_ = engine_.newQObject(&userProtocol_);
    engine_.globalObject().setProperty("protocol", userProtocolScriptValue_);

    QScriptValue meta = engine_.newQMetaObject(userProtocol_.metaObject());
    engine_.globalObject().setProperty("Protocol", meta);
}

UserScriptProtocol::~UserScriptProtocol()
{
}

AbstractProtocol* UserScriptProtocol::createInstance(StreamBase *stream,
    AbstractProtocol *parent)
{
    return new UserScriptProtocol(stream, parent);
}

quint32 UserScriptProtocol::protocolNumber() const
{
    return OstProto::Protocol::kUserScriptFieldNumber;
}

void UserScriptProtocol::protoDataCopyInto(OstProto::Protocol &protocol) const
{
    protocol.MutableExtension(OstProto::userScript)->CopyFrom(data);
    protocol.mutable_protocol_id()->set_id(protocolNumber());
}

void UserScriptProtocol::protoDataCopyFrom(const OstProto::Protocol &protocol)
{
    if (protocol.protocol_id().id() == protocolNumber() &&
            protocol.HasExtension(OstProto::userScript))
        data.MergeFrom(protocol.GetExtension(OstProto::userScript));

    evaluateUserScript();
}

QString UserScriptProtocol::name() const
{
    return QString("%1:{UserScript} [EXPERIMENTAL]").arg(userProtocol_.name());
}

QString UserScriptProtocol::shortName() const
{
    return QString("%1:{Script} [EXPERIMENTAL]").arg(userProtocol_.name());
}

quint32 UserScriptProtocol::protocolId(ProtocolIdType type) const
{
    QScriptValue userFunction;
    QScriptValue userValue;

    if (!isScriptValid_)
        goto _do_default;

    userFunction = userProtocolScriptValue_.property("protocolId");

    if (!userFunction.isValid())
        goto _do_default;

    Q_ASSERT(userFunction.isFunction());

    userValue = userFunction.call(QScriptValue(),
        QScriptValueList() << QScriptValue(&engine_, type));

    Q_ASSERT(userValue.isValid());
    Q_ASSERT(userValue.isNumber());

    return userValue.toUInt32();

_do_default:
    return AbstractProtocol::protocolId(type);
}

int UserScriptProtocol::fieldCount() const
{
    return userScript_fieldCount;
}

QVariant UserScriptProtocol::fieldData(int index, FieldAttrib attrib,
        int streamIndex) const
{
    switch (index)
    {
    case userScript_program:

        switch(attrib)
        {
        case FieldName:            
            return QString("UserProtocol");

        case FieldValue:
        case FieldTextValue:
            return QString().fromStdString(data.program());

        case FieldFrameValue:
        {
            if (!isScriptValid_)
                return QByteArray();

            QScriptValue userFunction = userProtocolScriptValue_.property(
                    "protocolFrameValue");

            Q_ASSERT(userFunction.isValid());
            Q_ASSERT(userFunction.isFunction());

            QScriptValue userValue = userFunction.call(QScriptValue(),
                QScriptValueList() << QScriptValue(&engine_, streamIndex));

            Q_ASSERT(userValue.isValid());
            Q_ASSERT(userValue.isArray());

            QByteArray fv;
            QList<int> pktBuf;
               
            qScriptValueToSequence(userValue, pktBuf); 

            fv.resize(pktBuf.size());
            for (int i = 0; i < pktBuf.size(); i++)
                fv[i] = pktBuf.at(i) & 0xFF;

            return fv;
        }
        default:
            break;
        }
        break;

    default:
        qFatal("%s: unimplemented case %d in switch", __PRETTY_FUNCTION__,
            index);
        break;
    }

    return AbstractProtocol::fieldData(index, attrib, streamIndex);
}

bool UserScriptProtocol::setFieldData(int index, const QVariant &value, 
        FieldAttrib attrib)
{
    bool isOk = false;

    if (attrib != FieldValue)
        goto _exit;

    switch (index)
    {
        case userScript_program:
        {
            data.set_program(value.toString().toStdString());
            evaluateUserScript();
            break;
        }
        default:
            qFatal("%s: unimplemented case %d in switch", __PRETTY_FUNCTION__,
                index);
            break;
    }

_exit:
    return isOk;
}

int UserScriptProtocol::protocolFrameSize(int streamIndex) const
{
    if (!isScriptValid_)
        return 0;

    QScriptValue userFunction = userProtocolScriptValue_.property(
            "protocolFrameSize");

    Q_ASSERT(userFunction.isValid());
    Q_ASSERT(userFunction.isFunction());

    QScriptValue userValue = userFunction.call(QScriptValue(), 
            QScriptValueList() << QScriptValue(&engine_, streamIndex));

    Q_ASSERT(userValue.isNumber());

    return userValue.toInt32();
}

bool UserScriptProtocol::isProtocolFrameSizeVariable() const
{
    return userProtocol_.isProtocolFrameSizeVariable();
}

int UserScriptProtocol::protocolFrameVariableCount() const
{
    return AbstractProtocol::lcm(
            AbstractProtocol::protocolFrameVariableCount(),
            userProtocol_.protocolFrameVariableCount());
}

quint32 UserScriptProtocol::protocolFrameCksum(int streamIndex,
        CksumType cksumType, CksumFlags cksumFlags) const
{
    QScriptValue userFunction;
    QScriptValue userValue;

    if (!isScriptValid_)
        goto _do_default;

    userFunction = userProtocolScriptValue_.property("protocolFrameCksum");

    qDebug("userscript protoFrameCksum(): isValid:%d/isFunc:%d",
        userFunction.isValid(), userFunction.isFunction());

    if (!userFunction.isValid())
        goto _do_default;

    Q_ASSERT(userFunction.isFunction());

    userValue = userFunction.call(
                    QScriptValue(),
                    QScriptValueList()
                        << QScriptValue(&engine_, streamIndex)
                        << QScriptValue(&engine_, cksumType)
                        << QScriptValue(&engine_, cksumFlags));

    Q_ASSERT(userValue.isValid());
    Q_ASSERT(userValue.isNumber());

    return userValue.toUInt32();

_do_default:
    return AbstractProtocol::protocolFrameCksum(
                                streamIndex, cksumType, cksumFlags);
}

void UserScriptProtocol::evaluateUserScript() const
{
    QScriptValue    userFunction;
    QScriptValue    userValue;
    QString         property;

    isScriptValid_ = false;
    errorLineNumber_ = userScriptLineCount();

    // Reset all properties including the dynamic ones
    userProtocol_.reset();
    userProtocolScriptValue_.setProperty("protocolFrameValue", QScriptValue());
    userProtocolScriptValue_.setProperty("protocolFrameSize", QScriptValue());
    userProtocolScriptValue_.setProperty("protocolFrameCksum", QScriptValue());
    userProtocolScriptValue_.setProperty("protocolId", QScriptValue());

    engine_.evaluate(fieldData(userScript_program, FieldValue).toString());
    if (engine_.hasUncaughtException())
        goto _error_exception;

    // Validate protocolFrameValue()
    property = QString("protocolFrameValue");
    userFunction = userProtocolScriptValue_.property(property);

    qDebug("userscript property %s: isValid:%d/isFunc:%d", 
            qPrintable(property),
            userFunction.isValid(), userFunction.isFunction());

    if (!userFunction.isValid())
    {
        errorText_ = property + QString(" not set");
        goto _error_exit;
    }

    if (!userFunction.isFunction())
    {
        errorText_ = property + QString(" is not a function");
        goto _error_exit;
    }

    userValue = userFunction.call();
    if (engine_.hasUncaughtException())
        goto _error_exception;

    qDebug("userscript property %s return value: isValid:%d/isArray:%d",
            qPrintable(property),
            userValue.isValid(), userValue.isArray());

    if (!userValue.isArray())
    {
        errorText_ = property + QString(" does not return an array");
        goto _error_exit;
    }

    // Validate protocolFrameSize()
    property = QString("protocolFrameSize");
    userFunction = userProtocolScriptValue_.property(property);

    qDebug("userscript property %s: isValid:%d/isFunc:%d", 
            qPrintable(property),
            userFunction.isValid(), userFunction.isFunction());

    if (!userFunction.isValid())
    {
        errorText_ = property + QString(" not set");
        goto _error_exit;
    }

    if (!userFunction.isFunction())
    {
        errorText_ = property + QString(" is not a function");
        goto _error_exit;
    }

    userValue = userFunction.call();
    if (engine_.hasUncaughtException())
        goto _error_exception;

    qDebug("userscript property %s return value: isValid:%d/isNumber:%d",
            qPrintable(property),
            userValue.isValid(), userValue.isNumber());

    if (!userValue.isNumber())
    {
        errorText_ = property + QString(" does not return a number");
        goto _error_exit;
    }

    // Validate protocolFrameCksum() [optional]
    property = QString("protocolFrameCksum");
    userFunction = userProtocolScriptValue_.property(property);

    qDebug("userscript property %s: isValid:%d/isFunc:%d", 
            qPrintable(property),
            userFunction.isValid(), userFunction.isFunction());

    if (!userFunction.isValid())
        goto _skip_cksum;

    if (!userFunction.isFunction())
    {
        errorText_ = property + QString(" is not a function");
        goto _error_exit;
    }

    userValue = userFunction.call();
    if (engine_.hasUncaughtException())
        goto _error_exception;

    qDebug("userscript property %s return value: isValid:%d/isNumber:%d",
            qPrintable(property),
            userValue.isValid(), userValue.isNumber());

    if (!userValue.isNumber())
    {
        errorText_ = property + QString(" does not return a number");
        goto _error_exit;
    }


_skip_cksum:
    // Validate protocolId() [optional]
    property = QString("protocolId");
    userFunction = userProtocolScriptValue_.property(property);

    qDebug("userscript property %s: isValid:%d/isFunc:%d", 
            qPrintable(property),
            userFunction.isValid(), userFunction.isFunction());

    if (!userFunction.isValid())
        goto _skip_protocol_id;

    if (!userFunction.isFunction())
    {
        errorText_ = property + QString(" is not a function");
        goto _error_exit;
    }

    userValue = userFunction.call();
    if (engine_.hasUncaughtException())
        goto _error_exception;

    qDebug("userscript property %s return value: isValid:%d/isNumber:%d",
            qPrintable(property),
            userValue.isValid(), userValue.isNumber());

    if (!userValue.isNumber())
    {
        errorText_ = property + QString(" does not return a number");
        goto _error_exit;
    }


_skip_protocol_id:
    errorText_ = QString("");
    isScriptValid_ = true;
    return;

_error_exception:
    errorLineNumber_ = engine_.uncaughtExceptionLineNumber();
    errorText_ = engine_.uncaughtException().toString();

_error_exit:
    userProtocol_.reset();
    return;
}

bool UserScriptProtocol::isScriptValid() const
{
    return isScriptValid_;
}

int UserScriptProtocol::userScriptErrorLineNumber() const
{
    return errorLineNumber_;
}

QString UserScriptProtocol::userScriptErrorText() const
{
    return errorText_;
}

int UserScriptProtocol::userScriptLineCount() const
{
    return fieldData(userScript_program, FieldValue).toString().count(
            QChar('\n')) + 1;
}

//
// -------------------- UserProtocol --------------------
//

UserProtocol::UserProtocol(AbstractProtocol *parent)
    : parent_ (parent)
{
    reset();
}

void UserProtocol::reset()
{
    name_ = QString();
    protocolFrameSizeVariable_ = false;
    protocolFrameVariableCount_ = 1;
}

QString UserProtocol::name() const
{
    return name_;
}

void UserProtocol::setName(QString &name)
{
    name_ = name;
}

bool UserProtocol::isProtocolFrameSizeVariable() const
{
    return protocolFrameSizeVariable_;
}

void UserProtocol::setProtocolFrameSizeVariable(bool variable)
{
    protocolFrameSizeVariable_ = variable;
}

int UserProtocol::protocolFrameVariableCount() const
{
    return protocolFrameVariableCount_;
}

void UserProtocol::setProtocolFrameVariableCount(int count)
{
    protocolFrameVariableCount_ = count;
}

quint32 UserProtocol::payloadProtocolId(UserProtocol::ProtocolIdType type) const
{
    return parent_->payloadProtocolId(
            static_cast<AbstractProtocol::ProtocolIdType>(type));
}

int UserProtocol::protocolFrameOffset(int streamIndex) const
{
    return parent_->protocolFrameOffset(streamIndex);
}

int UserProtocol::protocolFramePayloadSize(int streamIndex) const
{
    return parent_->protocolFramePayloadSize(streamIndex);
}

bool UserProtocol::isProtocolFramePayloadValueVariable() const
{
    return parent_->isProtocolFramePayloadValueVariable();
}

bool UserProtocol::isProtocolFramePayloadSizeVariable() const
{
    return parent_->isProtocolFramePayloadSizeVariable();
}

int UserProtocol::protocolFramePayloadVariableCount() const
{
    return parent_->protocolFramePayloadVariableCount();
}

quint32 UserProtocol::protocolFrameHeaderCksum(int streamIndex,
    AbstractProtocol::CksumType cksumType) const
{
    return parent_->protocolFrameHeaderCksum(streamIndex, cksumType);
}

quint32 UserProtocol::protocolFramePayloadCksum(int streamIndex,
    AbstractProtocol::CksumType cksumType) const
{
    quint32 cksum;

    cksum = parent_->protocolFramePayloadCksum(streamIndex, cksumType);
    qDebug("UserProto:%s = %d", __FUNCTION__, cksum);
    return cksum;
}

/* vim: set shiftwidth=4 tabstop=8 softtabstop=4 expandtab: */