-
Notifications
You must be signed in to change notification settings - Fork 8
/
mafEventBusManager.cpp
executable file
·190 lines (163 loc) · 6.49 KB
/
mafEventBusManager.cpp
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
/*
* mafEventBusManager.cpp
* mafEventBus
*
* Created by Paolo Quadrani on 27/03/09.
* Copyright 2009 B3C. All rights reserved.
*
* See Licence at: http://tiny.cc/QXJ4D
*
*/
#include "mafEventBusManager.h"
#include "mafNetworkConnectorQtSoap.h"
#include "mafNetworkConnectorQXMLRPC.h"
using namespace mafEventBus;
mafEventBusManager::mafEventBusManager() : m_EnableEventLogging(false), m_LogEventTopic("*") {
// Create local event dispatcher.
m_LocalDispatcher = new mafEventDispatcherLocal();
m_LocalDispatcher->setObjectName("Local Event Dispatcher");
// Create the remote event dispatcher.
m_RemoteDispatcher = new mafEventDispatcherRemote();
m_RemoteDispatcher->setObjectName("Remote Event Dispatcher");
}
mafEventBusManager::~mafEventBusManager() {
mafNetworkConnectorHash::iterator i = m_NetworkConnectorHash.begin();
while(i != m_NetworkConnectorHash.end()) {
delete i.value();
++i;
}
if(m_LocalDispatcher) delete m_LocalDispatcher;
if(m_RemoteDispatcher) delete m_RemoteDispatcher;
}
mafEventBusManager* mafEventBusManager::instance() {
static mafEventBusManager instanceEventBus;
return &instanceEventBus;
}
void mafEventBusManager::shutdown() {
}
void mafEventBusManager::initializeNetworkConnectors() {
plugNetworkConnector("SOAP", new mafNetworkConnectorQtSoap());
plugNetworkConnector("XMLRPC", new mafNetworkConnectorQXMLRPC());
}
bool mafEventBusManager::addEventProperty(const mafEvent &props) const {
// Design by contract condition.
//REQUIRE(!props[TOPIC].toString().isEmpty());
if(props[TYPE].toInt() == mafEventTypeLocal) {
// Local event dispatching.
if(props[SIGTYPE].toInt() == mafSignatureTypeCallback) {
return m_LocalDispatcher->addObserver(props);
} else {
return m_LocalDispatcher->registerSignal(props);
}
} else {
// Remote event dispatching.
if(props[SIGTYPE].toInt() == mafSignatureTypeCallback) {
mafMsgWarning("%s", mafTr("Local Observer can't register in Remote dispatcher").toAscii().data());
return false;
} else {
return m_RemoteDispatcher->registerSignal(props);
}
}
return false;
}
bool mafEventBusManager::removeEventProperty(const mafEvent &props) const {
// Design by contract condition.
//REQUIRE(!props.eventTopic().isEmpty());
if(props.eventType() == mafEventTypeLocal) {
// Local event dispatching.
if(props[SIGTYPE].toInt() == mafSignatureTypeCallback) {
return m_LocalDispatcher->removeObserver(props);
} else {
return m_LocalDispatcher->removeSignal(props);
}
} else {
// Remote event dispatching.
if(props[SIGTYPE].toInt() == mafSignatureTypeCallback) {
return m_RemoteDispatcher->removeObserver(props);
} else {
return m_RemoteDispatcher->removeSignal(props);
}
}
return false;
}
/*void mafEventBusManager::notifyEvent(const mafString id, mafEventType ev_type, mafEventArgumentsList *argList, mafGenericReturnArgument *returnArg) const {
mafId numeric_id = mafIdProvider::instance()->idValue(id);
if(numeric_id != -1) {
notifyEvent(numeric_id, ev_type, argList, returnArg);
} else {
mafMsgWarning("%s", mafTr("ID named '%1' is not mapped into the mafIdProvider!!").arg(id).toAscii().data());
}
}*/
void mafEventBusManager::notifyEvent(const mafString topic, mafEventType ev_type, mafEventArgumentsList *argList, mafGenericReturnArgument *returnArg) const {
// Design by contract condition.
//REQUIRE(!topic.isEmpty());
if(m_EnableEventLogging) {
if(m_LogEventTopic == "*" || m_LogEventTopic == topic) {
mafMsgDebug() << mafTr("Event notification for ID: ") << topic << mafTr(" named: ") << topic << "\n";
}
}
//event dispatched in local channel
mafEvent *event_dic = new mafEventBus::mafEvent;
(*event_dic)[TOPIC] = topic;
(*event_dic)[TYPE] = static_cast<int>(ev_type);
notifyEvent(*event_dic, argList, returnArg);
delete event_dic;
}
void mafEventBusManager::notifyEvent(const mafEvent &event_dictionary, mafEventArgumentsList *argList, mafGenericReturnArgument *returnArg) const {
//event dispatched in remote channel
if(event_dictionary[TYPE].toInt() == mafEventTypeLocal) {
m_LocalDispatcher->notifyEvent(event_dictionary, argList, returnArg);
} else {
m_RemoteDispatcher->notifyEvent(event_dictionary, argList);
}
}
void mafEventBusManager::enableEventLogging(bool enable) {
m_EnableEventLogging = enable;
}
void mafEventBusManager::logEventTopic(const mafString topic) {
// Design by contract condition.
//REQUIRE(!topic.isEmpty());
m_LogEventTopic = topic;
}
void mafEventBusManager::logAllEvents() {
m_LogEventTopic = "*";
}
bool mafEventBusManager::createServer(const mafString &communication_protocol, unsigned int listen_port) {
if(m_NetworkConnectorHash.count() == 0) {
initializeNetworkConnectors();
}
bool res(m_NetworkConnectorHash.contains(communication_protocol));
if(res) {
mafNetworkConnector *connector = m_NetworkConnectorHash.value(communication_protocol);
m_RemoteDispatcher->setNetworkConnectorServer(connector);
//mafNetworkConnector *connector = m_RemoteDispatcher->networkConnectorServer();
res = connector != NULL;
if(res) {
m_RemoteDispatcher->networkConnectorServer()->createServer(listen_port);
}
}
return res;
}
void mafEventBusManager::startListen() {
mafNetworkConnector *connector = m_RemoteDispatcher->networkConnectorServer();
if(connector) {
connector->startListen();
} else {
mafMsgWarning("%s", mafTr("Server can not start. Create it first, then call startListen again!!").toAscii().data());
}
}
bool mafEventBusManager::createClient(const mafString &communication_protocol, const mafString &server_host, unsigned int port) {
if(m_NetworkConnectorHash.count() == 0) {
initializeNetworkConnectors();
}
bool res(m_NetworkConnectorHash.contains(communication_protocol));
if(res) {
m_RemoteDispatcher->setNetworkConnectorClient(m_NetworkConnectorHash.value(communication_protocol));
mafNetworkConnector *connector = m_RemoteDispatcher->networkConnectorClient();
res = connector != NULL;
if(res) {
m_RemoteDispatcher->networkConnectorClient()->createClient(server_host, port);
}
}
return res;
}