| 1 | /**************************************************************************** |
| 2 | ** |
| 3 | ** Copyright (C) 2018 The Qt Company Ltd and/or its subsidiary(-ies). |
| 4 | ** Contact: http://www.qt-project.org/legal |
| 5 | ** |
| 6 | ** This file is part of the QtSystems module of the Qt Toolkit. |
| 7 | ** |
| 8 | ** $QT_BEGIN_LICENSE:LGPL$ |
| 9 | ** Commercial License Usage |
| 10 | ** Licensees holding valid commercial Qt licenses may use this file in |
| 11 | ** accordance with the commercial license agreement provided with the |
| 12 | ** Software or, alternatively, in accordance with the terms contained in |
| 13 | ** a written agreement between you and The Qt Company. For licensing terms |
| 14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
| 15 | ** information use the contact form at https://www.qt.io/contact-us. |
| 16 | ** |
| 17 | ** GNU Lesser General Public License Usage |
| 18 | ** Alternatively, this file may be used under the terms of the GNU Lesser |
| 19 | ** General Public License version 3 as published by the Free Software |
| 20 | ** Foundation and appearing in the file LICENSE.LGPL3 included in the |
| 21 | ** packaging of this file. Please review the following information to |
| 22 | ** ensure the GNU Lesser General Public License version 3 requirements |
| 23 | ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. |
| 24 | ** |
| 25 | ** GNU General Public License Usage |
| 26 | ** Alternatively, this file may be used under the terms of the GNU |
| 27 | ** General Public License version 2.0 or (at your option) the GNU General |
| 28 | ** Public license version 3 or any later version approved by the KDE Free |
| 29 | ** Qt Foundation. The licenses are as published by the Free Software |
| 30 | ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 |
| 31 | ** included in the packaging of this file. Please review the following |
| 32 | ** information to ensure the GNU General Public License requirements will |
| 33 | ** be met: https://www.gnu.org/licenses/gpl-2.0.html and |
| 34 | ** https://www.gnu.org/licenses/gpl-3.0.html. |
| 35 | ** |
| 36 | ** $QT_END_LICENSE$ |
| 37 | ** |
| 38 | ****************************************************************************/ |
| 39 | |
| 40 | #include "objectendpoint_p.h" |
| 41 | #include "instancemanager_p.h" |
| 42 | #include <private/qmetaobjectbuilder_p.h> |
| 43 | #include "proxyobject_p.h" |
| 44 | #include "qsignalintercepter_p.h" |
| 45 | #include "qserviceclientcredentials.h" |
| 46 | #include "qserviceclientcredentials_p.h" |
| 47 | #include <QTimer> |
| 48 | #include <QEvent> |
| 49 | #include <QVarLengthArray> |
| 50 | #include <QTime> |
| 51 | #include <QCoreApplication> |
| 52 | #include <QFile> |
| 53 | #include <QStringList> |
| 54 | |
| 55 | #include "qservicedebuglog_p.h" |
| 56 | |
| 57 | #ifdef Q_OS_LINUX |
| 58 | #include <execinfo.h> |
| 59 | #endif |
| 60 | |
| 61 | QT_BEGIN_NAMESPACE |
| 62 | |
| 63 | class Response |
| 64 | { |
| 65 | public: |
| 66 | Response() : isFinished(false), result(0) |
| 67 | { } |
| 68 | |
| 69 | ~Response() |
| 70 | { } |
| 71 | |
| 72 | bool isFinished; |
| 73 | void* result; |
| 74 | QString error; |
| 75 | }; |
| 76 | |
| 77 | class ServiceSignalIntercepter : public QSignalIntercepter |
| 78 | { |
| 79 | //Do not put Q_OBJECT here |
| 80 | public: |
| 81 | ServiceSignalIntercepter(QObject* sender, const QByteArray& signal, |
| 82 | ObjectEndPoint* parent) |
| 83 | : QSignalIntercepter(sender, signal, parent), endPoint(parent) |
| 84 | { |
| 85 | this->signal = signal; |
| 86 | } |
| 87 | |
| 88 | void setMetaIndex(int index) |
| 89 | { |
| 90 | metaIndex = index; |
| 91 | } |
| 92 | |
| 93 | protected: |
| 94 | void activated( const QList<QVariant>& args ) |
| 95 | { |
| 96 | qServiceLog() << "class" << "servicesignalinter" |
| 97 | << "event" << "signal" |
| 98 | << "endpoint" << endPoint->objectName() |
| 99 | << "metaidx" << metaIndex |
| 100 | << "signal" << QString::fromLatin1(signal) |
| 101 | << "args" << args.count(); |
| 102 | |
| 103 | endPoint->invokeRemote(metaIndex, args, QMetaType::Void); |
| 104 | } |
| 105 | private: |
| 106 | ObjectEndPoint* endPoint; |
| 107 | int metaIndex; |
| 108 | QByteArray signal; |
| 109 | |
| 110 | }; |
| 111 | |
| 112 | class ObjectEndPointPrivate |
| 113 | { |
| 114 | public: |
| 115 | //used on client and service side |
| 116 | ObjectEndPoint::Type endPointType; |
| 117 | ObjectEndPoint* parent; |
| 118 | |
| 119 | //used on service side |
| 120 | QRemoteServiceRegister::Entry entry; |
| 121 | QUuid serviceInstanceId; |
| 122 | |
| 123 | // user on the client side |
| 124 | bool functionReturned; |
| 125 | QUuid waitingOnReturnUuid; |
| 126 | |
| 127 | ObjectEndPointPrivate() : |
| 128 | endPointType(ObjectEndPoint::Service), |
| 129 | parent(0), |
| 130 | functionReturned(false) |
| 131 | { |
| 132 | waitingOnReturnUuid = QUuid(); |
| 133 | } |
| 134 | |
| 135 | ~ObjectEndPointPrivate() |
| 136 | { |
| 137 | } |
| 138 | |
| 139 | //service side |
| 140 | void setupSignalIntercepters(QObject * service) |
| 141 | { |
| 142 | Q_ASSERT(endPointType == ObjectEndPoint::Service); |
| 143 | |
| 144 | //create a signal intercepter for each signal |
| 145 | //offered by service |
| 146 | //exclude QObject signals |
| 147 | const QMetaObject* mo = service->metaObject(); |
| 148 | while (mo && strcmp(mo->className(), "QObject")) |
| 149 | { |
| 150 | for (int i = mo->methodOffset(); i < mo->methodCount(); ++i) { |
| 151 | const QMetaMethod method = mo->method(i); |
| 152 | if (method.methodType() == QMetaMethod::Signal) { |
| 153 | QByteArray signal = method.methodSignature(); |
| 154 | //add '2' for signal - see QSIGNAL_CODE |
| 155 | ServiceSignalIntercepter* intercept = |
| 156 | new ServiceSignalIntercepter(service, "2"+signal, parent ); |
| 157 | intercept->setMetaIndex(i); |
| 158 | } |
| 159 | } |
| 160 | mo = mo->superClass(); |
| 161 | } |
| 162 | } |
| 163 | |
| 164 | /*! |
| 165 | Activate slots connected to given signal. Unfortunately we can only do this |
| 166 | using the signal index relative to the meta object defining the signal. |
| 167 | */ |
| 168 | int triggerConnectedSlots(QObject* service, const QMetaObject* meta, int id, void **args) |
| 169 | { |
| 170 | Q_ASSERT(endPointType == ObjectEndPoint::Client); |
| 171 | |
| 172 | const QMetaObject* parentMeta = meta->superClass(); |
| 173 | if (parentMeta) |
| 174 | id = triggerConnectedSlots(service, parentMeta, id, args); |
| 175 | |
| 176 | if (id < 0) |
| 177 | return id; |
| 178 | |
| 179 | const int methodsThisType = meta->methodCount() - meta->methodOffset(); |
| 180 | if (id >= 0 && id < methodsThisType) |
| 181 | QMetaObject::activate(service, meta, id, args); |
| 182 | |
| 183 | id -= methodsThisType; |
| 184 | return id; |
| 185 | } |
| 186 | }; |
| 187 | |
| 188 | ObjectEndPoint::ObjectEndPoint(Type type, QServiceIpcEndPoint* comm, QObject* parent) |
| 189 | : QObject(parent), dispatch(comm), service(0), localToRemote(0), |
| 190 | remoteToLocal(0) |
| 191 | { |
| 192 | Q_ASSERT(dispatch); |
| 193 | d = new ObjectEndPointPrivate; |
| 194 | d->parent = this; |
| 195 | d->endPointType = type; |
| 196 | |
| 197 | dispatch->setParent(this); |
| 198 | #ifdef SFW_USE_UNIX_BACKEND |
| 199 | connect(dispatch, SIGNAL(readyRead()), this, SLOT(newPackageReady()), Qt::DirectConnection); |
| 200 | connect(dispatch, SIGNAL(disconnected()), this, SLOT(disconnected()), Qt::DirectConnection); |
| 201 | #else |
| 202 | connect(dispatch, SIGNAL(readyRead()), this, SLOT(newPackageReady()), Qt::QueuedConnection); |
| 203 | connect(dispatch, SIGNAL(disconnected()), this, SLOT(disconnected()), Qt::QueuedConnection); |
| 204 | #endif |
| 205 | if (type == Client) { |
| 206 | return; //we are waiting for conctructProxy() call |
| 207 | } else { |
| 208 | if (dispatch->packageAvailable()) |
| 209 | QTimer::singleShot(0, this, SLOT(newPackageReady())); |
| 210 | } |
| 211 | } |
| 212 | |
| 213 | ObjectEndPoint::~ObjectEndPoint() |
| 214 | { |
| 215 | qServiceLog() << "class" << "objectendpoint" |
| 216 | << "event" << "delete" |
| 217 | << "name" << objectName(); |
| 218 | delete d; |
| 219 | } |
| 220 | |
| 221 | void ObjectEndPoint::disconnected() |
| 222 | { |
| 223 | qServiceLog() << "class" << "objectendpoint" |
| 224 | << "event" << "disconnected" |
| 225 | << "name" << objectName(); |
| 226 | if (d->endPointType == Service) { |
| 227 | InstanceManager::instance()->removeObjectInstance(d->entry, d->serviceInstanceId); |
| 228 | deleteLater(); |
| 229 | } |
| 230 | foreach (Response *r, openRequests) { |
| 231 | r->error = QLatin1Literal("end point disconnected"); |
| 232 | r->isFinished = true; |
| 233 | dispatch->waitingDone(); |
| 234 | } |
| 235 | } |
| 236 | |
| 237 | /* |
| 238 | Client requests proxy object. The proxy is owned by calling |
| 239 | code and this object must clean itself up upon destruction of |
| 240 | proxy. |
| 241 | */ |
| 242 | QObject* ObjectEndPoint::constructProxy(const QRemoteServiceRegister::Entry & entry) |
| 243 | { |
| 244 | //client side |
| 245 | Q_ASSERT(d->endPointType == ObjectEndPoint::Client); |
| 246 | |
| 247 | //ask for serialized meta object |
| 248 | //get proxy based on meta object |
| 249 | //return meta object |
| 250 | QServicePackage p; |
| 251 | p.d = new QServicePackagePrivate(); |
| 252 | p.d->messageId = QUuid::createUuid(); |
| 253 | p.d->entry = entry; |
| 254 | |
| 255 | Response* response = new Response(); |
| 256 | openRequests.insert(p.d->messageId, response); |
| 257 | |
| 258 | dispatch->writePackage(p); |
| 259 | qServiceLog() << "class" << "objectendpoint" |
| 260 | << "event" << "constructProxy" |
| 261 | << "progress" << "wait for result"; |
| 262 | waitForResponse(p.d->messageId); |
| 263 | qServiceLog() << "class" << "objectendpoint" |
| 264 | << "event" << "constructProxy" |
| 265 | << "progress" << "got result"; |
| 266 | |
| 267 | |
| 268 | if (response->isFinished) { |
| 269 | if (response->result == 0) |
| 270 | qWarning() << "Request for remote service failed"; |
| 271 | else |
| 272 | service = reinterpret_cast<QServiceProxy* >(response->result); |
| 273 | } else { |
| 274 | qDebug() << Q_FUNC_INFO << "response passed but not finished"; |
| 275 | return 0; |
| 276 | } |
| 277 | |
| 278 | openRequests.take(p.d->messageId); |
| 279 | delete response; |
| 280 | |
| 281 | return service; |
| 282 | } |
| 283 | |
| 284 | void ObjectEndPoint::newPackageReady() |
| 285 | { |
| 286 | //client and service side |
| 287 | |
| 288 | while (dispatch->packageAvailable() && !d->functionReturned) |
| 289 | { |
| 290 | QServicePackage p = dispatch->nextPackage(); |
| 291 | if (!p.isValid()) |
| 292 | continue; |
| 293 | |
| 294 | switch (p.d->packageType) { |
| 295 | case QServicePackage::ObjectCreation: |
| 296 | objectRequest(p); |
| 297 | break; |
| 298 | case QServicePackage::MethodCall: |
| 299 | methodCall(p); |
| 300 | break; |
| 301 | case QServicePackage::PropertyCall: |
| 302 | propertyCall(p); |
| 303 | break; |
| 304 | default: |
| 305 | qWarning() << "Unknown package type received."; |
| 306 | } |
| 307 | } |
| 308 | } |
| 309 | |
| 310 | void ObjectEndPoint::propertyCall(const QServicePackage& p) |
| 311 | { |
| 312 | if (p.d->responseType == QServicePackage::NotAResponse) { |
| 313 | //service side |
| 314 | Q_ASSERT(d->endPointType == ObjectEndPoint::Service); |
| 315 | |
| 316 | if (!service) { // ingore everything until the service is created |
| 317 | qWarning() << Q_FUNC_INFO << "dropping a property call since the object hasn't completed construction"; |
| 318 | return; |
| 319 | } |
| 320 | |
| 321 | QByteArray data = p.d->payload.toByteArray(); |
| 322 | QDataStream stream(&data, QIODevice::ReadOnly); |
| 323 | int metaIndex = -1; |
| 324 | QVariant arg; |
| 325 | int callType; |
| 326 | stream >> metaIndex; |
| 327 | stream >> arg; |
| 328 | stream >> callType; |
| 329 | const QMetaObject::Call c = (QMetaObject::Call) callType; |
| 330 | |
| 331 | QVariant result; |
| 332 | QMetaProperty property = service->metaObject()->property(metaIndex); |
| 333 | if (property.isValid()) { |
| 334 | switch (c) { |
| 335 | case QMetaObject::ReadProperty: |
| 336 | result = property.read(service); |
| 337 | break; |
| 338 | case QMetaObject::WriteProperty: |
| 339 | property.write(service, arg); |
| 340 | break; |
| 341 | case QMetaObject::ResetProperty: |
| 342 | property.reset(service); |
| 343 | break; |
| 344 | default: |
| 345 | break; |
| 346 | |
| 347 | } |
| 348 | } |
| 349 | |
| 350 | if (c == QMetaObject::ReadProperty) { |
| 351 | QServicePackage response = p.createResponse(); |
| 352 | if (property.isValid()) { |
| 353 | response.d->responseType = QServicePackage::Success; |
| 354 | response.d->payload = result; |
| 355 | } else { |
| 356 | response.d->responseType = QServicePackage::Failed; |
| 357 | } |
| 358 | dispatch->writePackage(response); |
| 359 | } |
| 360 | } else { |
| 361 | //client side |
| 362 | Q_ASSERT(d->endPointType == ObjectEndPoint::Client); |
| 363 | static QQueue<QUuid> lastId; |
| 364 | Response* response = openRequests.value(p.d->messageId); |
| 365 | if (response) { |
| 366 | lastId.enqueue(p.d->messageId); |
| 367 | while (lastId.count() > 10) |
| 368 | lastId.dequeue(); |
| 369 | response->isFinished = true; |
| 370 | if (p.d->responseType == QServicePackage::Failed) { |
| 371 | response->result = 0; |
| 372 | response->error = QLatin1Literal("QServicePackaged::Failed"); |
| 373 | response->isFinished = true; |
| 374 | dispatch->waitingDone(); |
| 375 | qWarning() << "Service method call failed"; |
| 376 | return; |
| 377 | } |
| 378 | QVariant* variant = new QVariant(p.d->payload); |
| 379 | response->result = reinterpret_cast<void *>(variant); |
| 380 | |
| 381 | dispatch->waitingDone(); |
| 382 | } |
| 383 | else { |
| 384 | qWarning() << "**** FAILED TO FIND MESSAGE ID!!! ****"; |
| 385 | qWarning() << "Current id" << p.d->messageId.toString(); |
| 386 | foreach (const QUuid &last, lastId) |
| 387 | qWarning() << "last ids" << last.toString(); |
| 388 | qWarning() << p; |
| 389 | } |
| 390 | } |
| 391 | } |
| 392 | |
| 393 | void ObjectEndPoint::objectRequest(const QServicePackage& p) |
| 394 | { |
| 395 | if (p.d->responseType != QServicePackage::NotAResponse ) { |
| 396 | //client side |
| 397 | Q_ASSERT(d->endPointType == ObjectEndPoint::Client); |
| 398 | |
| 399 | Response* response = openRequests.value(p.d->messageId); |
| 400 | if (p.d->responseType == QServicePackage::Failed) { |
| 401 | response->result = 0; |
| 402 | response->error = QLatin1Literal("objectRequest QServicePackage::Failed"); |
| 403 | response->isFinished = true; |
| 404 | dispatch->waitingDone(); |
| 405 | qWarning() << "Service instantiation failed"; |
| 406 | return; |
| 407 | } |
| 408 | //deserialize meta object and |
| 409 | //create proxy object |
| 410 | QServiceProxy* proxy = new QServiceProxy(p.d->payload.toByteArray(), this); |
| 411 | response->result = reinterpret_cast<void *>(proxy); |
| 412 | response->isFinished = true; |
| 413 | |
| 414 | //wake up waiting code |
| 415 | dispatch->waitingDone(); |
| 416 | |
| 417 | } else { |
| 418 | //service side |
| 419 | Q_ASSERT(d->endPointType == ObjectEndPoint::Service); |
| 420 | |
| 421 | QServicePackage response = p.createResponse(); |
| 422 | InstanceManager* m = InstanceManager::instance(); |
| 423 | |
| 424 | //get meta object from type register |
| 425 | const QMetaObject* meta = m->metaObject(p.d->entry); |
| 426 | if (!meta) { |
| 427 | qDebug() << "Unknown type" << p.d->entry; |
| 428 | dispatch->writePackage(response); |
| 429 | return; |
| 430 | } |
| 431 | |
| 432 | setObjectName(p.d->entry.interfaceName() + QLatin1Char(' ') + dispatch->objectName()); |
| 433 | dispatch->setObjectName(objectName()); |
| 434 | |
| 435 | //serialize meta object |
| 436 | QByteArray data; |
| 437 | QDataStream stream( &data, QIODevice::WriteOnly | QIODevice::Append ); |
| 438 | QMetaObjectBuilder builder(meta); |
| 439 | builder.serialize(stream); |
| 440 | |
| 441 | QServiceClientCredentials creds; |
| 442 | dispatch->getSecurityCredentials(creds); |
| 443 | |
| 444 | // not supported on windows at the moment |
| 445 | if (!creds.isValid()) { |
| 446 | qWarning() << "SFW Unable to get socket credentials client asking for" << p.d->entry.interfaceName() << p.d->entry.serviceName() << "this may fail in the future"; |
| 447 | disconnected(); |
| 448 | return; |
| 449 | } |
| 450 | |
| 451 | //instantiate service object from type register |
| 452 | service = m->createObjectInstance(p.d->entry, d->serviceInstanceId, creds); |
| 453 | if (!service) { |
| 454 | qWarning() << "Cannot instanciate service object"; |
| 455 | dispatch->writePackage(response); |
| 456 | return; |
| 457 | } |
| 458 | |
| 459 | if (!creds.isClientAccepted()) { |
| 460 | qWarning() << "SFW Security failure by" << |
| 461 | creds.getProcessIdentifier() << |
| 462 | creds.getUserIdentifier() << |
| 463 | creds.getGroupIdentifier() << |
| 464 | "requesting" << p.d->entry.interfaceName() << p.d->entry.serviceName(); |
| 465 | m->removeObjectInstance(p.d->entry, d->serviceInstanceId); |
| 466 | disconnected(); |
| 467 | dispatch->terminateConnection(); |
| 468 | return; |
| 469 | } |
| 470 | |
| 471 | d->setupSignalIntercepters(service); |
| 472 | |
| 473 | //send meta object |
| 474 | d->entry = p.d->entry; |
| 475 | response.d->entry = p.d->entry; |
| 476 | response.d->responseType = QServicePackage::Success; |
| 477 | response.d->payload = QVariant(data); |
| 478 | dispatch->writePackage(response); |
| 479 | } |
| 480 | } |
| 481 | |
| 482 | void ObjectEndPoint::methodCall(const QServicePackage& p) |
| 483 | { |
| 484 | if (!service) // ingore everything until the service is created |
| 485 | { |
| 486 | qWarning() << Q_FUNC_INFO << "dropping a method call or signal since the object hasn't completed construction"; |
| 487 | return; |
| 488 | } |
| 489 | |
| 490 | if (p.d->responseType == QServicePackage::NotAResponse ) { |
| 491 | //service side if slot invocation |
| 492 | //client side if signal emission (isSignal==true) |
| 493 | |
| 494 | QByteArray data = p.d->payload.toByteArray(); |
| 495 | QDataStream stream(&data, QIODevice::ReadOnly); |
| 496 | int metaIndex = -1; |
| 497 | QVariantList args; |
| 498 | stream >> metaIndex; |
| 499 | stream >> args; |
| 500 | |
| 501 | if (remoteToLocal) |
| 502 | metaIndex = remoteToLocal[metaIndex]; |
| 503 | |
| 504 | QMetaMethod method = service->metaObject()->method(metaIndex); |
| 505 | const bool isSignal = (method.methodType() == QMetaMethod::Signal); |
| 506 | const int returnType = method.returnType(); |
| 507 | |
| 508 | if (isSignal) { |
| 509 | Q_ASSERT(d->endPointType == ObjectEndPoint::Client); |
| 510 | // Construct the raw argument list. |
| 511 | /* we ignore a possible return type of the signal. The value is |
| 512 | not deterministic and it can actually create memory leaks |
| 513 | in moc generated code. |
| 514 | */ |
| 515 | const int numArgs = args.size(); |
| 516 | QVarLengthArray<void *, 32> a( numArgs+1 ); |
| 517 | a[0] = 0; |
| 518 | |
| 519 | const QList<QByteArray> pTypes = method.parameterTypes(); |
| 520 | for ( int arg = 0; arg < numArgs; ++arg ) { |
| 521 | if (pTypes.at(arg) == "QVariant") |
| 522 | a[arg+1] = (void *)&( args[arg] ); |
| 523 | else |
| 524 | a[arg+1] = (void *)( args[arg].data() ); |
| 525 | } |
| 526 | |
| 527 | d->triggerConnectedSlots(service, service->metaObject(), metaIndex, a.data()); |
| 528 | return; |
| 529 | } |
| 530 | //service side |
| 531 | if (d->endPointType != ObjectEndPoint::Service) { |
| 532 | |
| 533 | qWarning() << "SFW FATAL ERROR. Client got a method call that wasn't a signal" << objectName(); |
| 534 | |
| 535 | qServiceLog() << "error" << "non-signal method call on client" |
| 536 | << "fatal" << 1; |
| 537 | |
| 538 | #ifdef Q_OS_LINUX |
| 539 | void *symbols[128]; |
| 540 | char **it; |
| 541 | size_t symbol_count; |
| 542 | size_t i; |
| 543 | symbol_count = backtrace(symbols, 128); |
| 544 | it = backtrace_symbols(symbols, symbol_count); |
| 545 | for (i = 0; i < symbol_count; ++i) |
| 546 | printf("TRACE:\t%s\n", it[i]); |
| 547 | printf("\n"); |
| 548 | #endif |
| 549 | return; |
| 550 | } |
| 551 | |
| 552 | const char* typenames[] = {0,0,0,0,0,0,0,0,0,0}; |
| 553 | const void* param[] = {0,0,0,0,0,0,0,0,0,0}; |
| 554 | |
| 555 | for (int i=0; i<args.size(); i++) { |
| 556 | if (args[i].isValid()) { |
| 557 | typenames[i] = args[i].typeName(); |
| 558 | } else { |
| 559 | if (method.parameterTypes().at(i) == "QVariant") |
| 560 | typenames[i] = "QVariant"; |
| 561 | } |
| 562 | param[i] = args[i].constData(); |
| 563 | } |
| 564 | |
| 565 | |
| 566 | bool result = false; |
| 567 | if (returnType == QMetaType::Void) { |
| 568 | result = method.invoke(service, |
| 569 | QGenericArgument(typenames[0], param[0]), |
| 570 | QGenericArgument(typenames[1], param[1]), |
| 571 | QGenericArgument(typenames[2], param[2]), |
| 572 | QGenericArgument(typenames[3], param[3]), |
| 573 | QGenericArgument(typenames[4], param[4]), |
| 574 | QGenericArgument(typenames[5], param[5]), |
| 575 | QGenericArgument(typenames[6], param[6]), |
| 576 | QGenericArgument(typenames[7], param[7]), |
| 577 | QGenericArgument(typenames[8], param[8]), |
| 578 | QGenericArgument(typenames[9], param[9])); |
| 579 | } else { |
| 580 | //result buffer |
| 581 | QVariant returnValue; |
| 582 | //ignore whether QVariant is a declared meta type or not |
| 583 | if (returnType != QMetaType::QVariant) { |
| 584 | returnValue = QVariant(returnType, (const void*) 0); |
| 585 | } |
| 586 | |
| 587 | QGenericReturnArgument ret(method.typeName(), returnValue.data()); |
| 588 | result = method.invoke(service, ret, |
| 589 | QGenericArgument(typenames[0], param[0]), |
| 590 | QGenericArgument(typenames[1], param[1]), |
| 591 | QGenericArgument(typenames[2], param[2]), |
| 592 | QGenericArgument(typenames[3], param[3]), |
| 593 | QGenericArgument(typenames[4], param[4]), |
| 594 | QGenericArgument(typenames[5], param[5]), |
| 595 | QGenericArgument(typenames[6], param[6]), |
| 596 | QGenericArgument(typenames[7], param[7]), |
| 597 | QGenericArgument(typenames[8], param[8]), |
| 598 | QGenericArgument(typenames[9], param[9])); |
| 599 | |
| 600 | QServicePackage response = p.createResponse(); |
| 601 | |
| 602 | if (result) { |
| 603 | response.d->responseType = QServicePackage::Success; |
| 604 | response.d->payload = returnValue; |
| 605 | } else { |
| 606 | response.d->responseType = QServicePackage::Failed; |
| 607 | } |
| 608 | dispatch->writePackage(response); |
| 609 | |
| 610 | } |
| 611 | if (!result) |
| 612 | qWarning( "%s::%s cannot be called.", service->metaObject()->className(), method.methodSignature().constData()); |
| 613 | } else { |
| 614 | //client side |
| 615 | Q_ASSERT(d->endPointType == ObjectEndPoint::Client); |
| 616 | |
| 617 | if (d->waitingOnReturnUuid == p.d->messageId) { |
| 618 | d->functionReturned = true; |
| 619 | } |
| 620 | |
| 621 | Response* response = openRequests.value(p.d->messageId); |
| 622 | if (response){ |
| 623 | response->isFinished = true; |
| 624 | if (p.d->responseType == QServicePackage::Failed) { |
| 625 | response->result = 0; |
| 626 | response->error = QLatin1Literal("methodCall QServicePakcage::Failed"); |
| 627 | response->isFinished = true; |
| 628 | dispatch->waitingDone(); |
| 629 | return; |
| 630 | } |
| 631 | QVariant* variant = new QVariant(p.d->payload); |
| 632 | response->result = reinterpret_cast<void *>(variant); |
| 633 | |
| 634 | dispatch->waitingDone(); |
| 635 | } |
| 636 | } |
| 637 | } |
| 638 | |
| 639 | /* |
| 640 | Will block if return value expected |
| 641 | Handles property calls |
| 642 | */ |
| 643 | QVariant ObjectEndPoint::invokeRemoteProperty(int metaIndex, const QVariant& arg, int /*returnType*/, QMetaObject::Call c ) |
| 644 | { |
| 645 | //client and service side |
| 646 | Q_ASSERT(d->endPointType == ObjectEndPoint::Client |
| 647 | || d->endPointType == ObjectEndPoint::Service); |
| 648 | |
| 649 | QServicePackage p; |
| 650 | p.d = new QServicePackagePrivate(); |
| 651 | p.d->packageType = QServicePackage::PropertyCall; |
| 652 | p.d->messageId = QUuid::createUuid(); |
| 653 | |
| 654 | QByteArray data; |
| 655 | QDataStream stream(&data, QIODevice::WriteOnly|QIODevice::Append); |
| 656 | stream << metaIndex << arg << c; |
| 657 | p.d->payload = data; |
| 658 | |
| 659 | if (c == QMetaObject::ReadProperty) { |
| 660 | //create response and block for answer |
| 661 | Response* response = new Response(); |
| 662 | openRequests.insert(p.d->messageId, response); |
| 663 | |
| 664 | dispatch->writePackage(p); |
| 665 | qServiceLog() << "class" << "objectendpoint" |
| 666 | << "event" << "invokeRemoteProperty" |
| 667 | << "progress" << "wait for result"; |
| 668 | waitForResponse(p.d->messageId); |
| 669 | |
| 670 | QVariant result; |
| 671 | if (response->isFinished) { |
| 672 | if (response->result == 0) { |
| 673 | qWarning() << "Service property call failed"; |
| 674 | } else { |
| 675 | QVariant* resultPointer = reinterpret_cast<QVariant* >(response->result); |
| 676 | result = (*resultPointer); |
| 677 | delete resultPointer; |
| 678 | } |
| 679 | } else { |
| 680 | qDebug() << Q_FUNC_INFO << "response passed but not finished"; |
| 681 | } |
| 682 | |
| 683 | openRequests.take(p.d->messageId); |
| 684 | delete response; |
| 685 | |
| 686 | return result; |
| 687 | } else { |
| 688 | dispatch->writePackage(p); |
| 689 | } |
| 690 | |
| 691 | return QVariant(); |
| 692 | } |
| 693 | |
| 694 | /* |
| 695 | Will block if return value expected |
| 696 | Handles signal/slots |
| 697 | */ |
| 698 | QVariant ObjectEndPoint::invokeRemote(int metaIndex, const QVariantList& args, int returnType) |
| 699 | { |
| 700 | //client side |
| 701 | //Q_ASSERT(d->endPointType == ObjectEndPoint::Client); |
| 702 | |
| 703 | QServicePackage p; |
| 704 | p.d = new QServicePackagePrivate(); |
| 705 | p.d->packageType = QServicePackage::MethodCall; |
| 706 | p.d->messageId = QUuid::createUuid(); |
| 707 | |
| 708 | QByteArray data; |
| 709 | QDataStream stream(&data, QIODevice::WriteOnly|QIODevice::Append); |
| 710 | stream << metaIndex << args; |
| 711 | p.d->payload = data; |
| 712 | |
| 713 | if (returnType == QMetaType::Void) { |
| 714 | dispatch->writePackage(p); |
| 715 | } else { |
| 716 | //create response and block for answer |
| 717 | Response* response = new Response(); |
| 718 | openRequests.insert(p.d->messageId, response); |
| 719 | |
| 720 | d->waitingOnReturnUuid = p.d->messageId; |
| 721 | |
| 722 | dispatch->writePackage(p); |
| 723 | qServiceLog() << "class" << "objectendpoint" |
| 724 | << "event" << "invokeRemote" |
| 725 | << "progress" << "wait for result"; |
| 726 | waitForResponse(p.d->messageId); |
| 727 | |
| 728 | d->waitingOnReturnUuid = QUuid(); |
| 729 | |
| 730 | QVariant result; |
| 731 | if (response->isFinished) { |
| 732 | if (response->result == 0) { |
| 733 | qWarning() << "Remote function call failed"; |
| 734 | } else { |
| 735 | QVariant* resultPointer = reinterpret_cast<QVariant* >(response->result); |
| 736 | result = (*resultPointer); |
| 737 | delete resultPointer; |
| 738 | } |
| 739 | } else { |
| 740 | qDebug() << Q_FUNC_INFO << "response passed but not finished"; |
| 741 | } |
| 742 | |
| 743 | openRequests.take(p.d->messageId); |
| 744 | delete response; |
| 745 | |
| 746 | return result; |
| 747 | |
| 748 | } |
| 749 | |
| 750 | return QVariant(); |
| 751 | } |
| 752 | |
| 753 | void ObjectEndPoint::waitForResponse(const QUuid& requestId) |
| 754 | { |
| 755 | Q_ASSERT(d->endPointType == ObjectEndPoint::Client); |
| 756 | if (openRequests.contains(requestId) ) { |
| 757 | Response *r = openRequests.value(requestId); |
| 758 | QTime elapsed; |
| 759 | elapsed.start(); |
| 760 | while (r->isFinished == false && (elapsed.elapsed() < 15000)) { |
| 761 | qServiceLog() << "class" << "objectendpoint" |
| 762 | << "event" << "waiting" |
| 763 | << "elapsed" << elapsed.elapsed() |
| 764 | << "name" << objectName() |
| 765 | << "uuid" << requestId.toString(); |
| 766 | int ret = dispatch->waitForData(); |
| 767 | if (ret != 0) { |
| 768 | qWarning() << this << "SFW ipc error" << r->error; |
| 769 | break; |
| 770 | } |
| 771 | } |
| 772 | |
| 773 | qServiceLog() << "class" << "objectendpoint" |
| 774 | << "event" << "waiting done" |
| 775 | << "elapsed" << elapsed.elapsed() |
| 776 | << "name" << objectName(); |
| 777 | |
| 778 | if (r->isFinished == false) { |
| 779 | qWarning() << "SFW IPC failure, remote end failed to respond to blocking IPC call in" << elapsed.elapsed()/1000 << "seconds." << objectName(); |
| 780 | } |
| 781 | if (d->functionReturned) { |
| 782 | d->functionReturned = false; |
| 783 | QMetaObject::invokeMethod(this, "newPackageReady", Qt::QueuedConnection); |
| 784 | } |
| 785 | } |
| 786 | } |
| 787 | |
| 788 | void ObjectEndPoint::setLookupTable(int *local, int *remote) |
| 789 | { |
| 790 | localToRemote = local; |
| 791 | remoteToLocal = remote; |
| 792 | } |
| 793 | |
| 794 | #include "moc_objectendpoint_p.cpp" |
| 795 | |
| 796 | QT_END_NAMESPACE |