ctkEventBusImpl.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /*=============================================================================
  2. Library: CTK
  3. Copyright (c) German Cancer Research Center,
  4. Division of Medical and Biological Informatics
  5. Licensed under the Apache License, Version 2.0 (the "License");
  6. you may not use this file except in compliance with the License.
  7. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. =============================================================================*/
  15. #include "ctkEventBusImpl_p.h"
  16. #include <QSetIterator>
  17. #include "ctkEventHandlerWrapper_p.h"
  18. #include "ctkBusEvent.h"
  19. #include "ctkEventDefinitions.h"
  20. #define ctkEventArgument(type,data) QArgument<type >(#type, data)
  21. ctkEventBusImpl::ctkEventBusImpl()
  22. {
  23. m_EventBusManager = ctkEventBus::ctkEventBusManager::instance();
  24. }
  25. void ctkEventBusImpl::postEvent(const ::ctkEvent& event)
  26. {
  27. dispatchEvent(event, true);
  28. }
  29. void ctkEventBusImpl::sendEvent(const ::ctkEvent& event)
  30. {
  31. dispatchEvent(event, false);
  32. }
  33. void ctkEventBusImpl::publishSignal(const QObject* publisher, const char* signal, const QString& topic,
  34. Qt::ConnectionType type)
  35. {
  36. Q_UNUSED(type);
  37. ctkBusEvent *mesbEvent = new ctkBusEvent(topic, ctkEventBus::ctkEventTypeLocal, ctkEventBus::ctkSignatureTypeSignal, const_cast<QObject *>(publisher), signal);
  38. m_EventBusManager->addEventProperty(*mesbEvent);
  39. }
  40. void ctkEventBusImpl::unpublishSignal(const QObject *publisher, const char *signal, const QString &topic)
  41. {
  42. Q_UNUSED(publisher)
  43. Q_UNUSED(signal)
  44. Q_UNUSED(topic)
  45. //TODO implement
  46. }
  47. qlonglong ctkEventBusImpl::subscribeSlot(const QObject* subscriber, const char* member,
  48. const ctkDictionary& properties, Qt::ConnectionType type)
  49. {
  50. Q_UNUSED(type)
  51. ctkDictionary toSend(properties);
  52. QString topic = properties.value(TOPIC).toString();
  53. toSend.insert(TOPIC, topic);
  54. toSend.insert(TYPE, ctkEventBus::ctkEventTypeLocal);
  55. toSend.insert(SIGTYPE, ctkEventBus::ctkSignatureTypeCallback);
  56. QVariant var;
  57. var.setValue(const_cast<QObject *>(subscriber));
  58. toSend.insert(OBJECT, var);
  59. toSend.insert(SIGNATURE,member);
  60. ctkBusEvent *mesbEvent = new ctkBusEvent(topic, toSend);
  61. m_EventBusManager->addEventProperty(*mesbEvent);
  62. return topic.toLongLong();
  63. }
  64. void ctkEventBusImpl::unsubscribeSlot(qlonglong subscriptionId) {
  65. Q_UNUSED(subscriptionId)
  66. // @@@@to be implemented
  67. }
  68. bool ctkEventBusImpl::updateProperties(qlonglong subscriptionId, const ctkDictionary& properties)
  69. {
  70. Q_UNUSED(subscriptionId)
  71. Q_UNUSED(properties)
  72. // @@@@to be implemented
  73. return false;
  74. }
  75. void ctkEventBusImpl::dispatchEvent(const ctkEvent& event, bool isAsync)
  76. {
  77. Q_UNUSED(isAsync)
  78. ctkBusEvent *mebEvent = new ctkBusEvent("",ctkEventBus::ctkEventTypeRemote,ctkEventBus::ctkSignatureTypeSignal, this, "no");
  79. //cycle for all other elements
  80. QStringList keyList = event.getPropertyNames();
  81. QStringList::const_iterator constIterator;
  82. for (constIterator = keyList.constBegin(); constIterator != keyList.constEnd(); ++constIterator) {
  83. QVariant value = event.getProperty((*constIterator));
  84. //qDebug() << (*constIterator) << " " << value.toString();
  85. (*mebEvent)[(*constIterator)] = event.getProperty((*constIterator));
  86. }
  87. typedef QList<QGenericArgument> ctkEventArgumentList;
  88. ctkEventArgumentList list;
  89. list.append(Q_ARG(QVariantList,event.getProperty("localEvent").toList()));
  90. list.append(Q_ARG(QVariantList,event.getProperty("localData").toList()));
  91. m_EventBusManager->notifyEvent(*mebEvent, &list);
  92. }
  93. bool ctkEventBusImpl::createServer(const QString &communication_protocol, unsigned int listen_port) {
  94. return m_EventBusManager->createServer(communication_protocol,listen_port);
  95. }
  96. void ctkEventBusImpl::startListen() {
  97. m_EventBusManager->startListen();
  98. }
  99. bool ctkEventBusImpl::createClient(const QString &communication_protocol, const QString &server_host, unsigned int port) {
  100. return m_EventBusManager->createClient(communication_protocol,server_host,port);
  101. }