DoodleManager.cpp
5.51 KB
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
#include "DoodlePch.h"
#include "../mainapp.h"
namespace JusDoodle {
const double Version = 1.5;
DoodleManager *DoodleManager::_pDoodleManager = NULL;
DoodleManager::DoodleManager(QObject *parent)
: QObject(parent)
, _SessMap()
{
_pDoodleManager = this;
}
DoodleManager::~DoodleManager()
{
}
DoodleManager *DoodleManager::Instance()
{
if (!_pDoodleManager)
_pDoodleManager = new DoodleManager;
return _pDoodleManager;
}
void DoodleManager::onServiceEvent(const QString &name, size_t cookie, const Notification &info)
{
if (onCallEvent(name, cookie, info))
return;
if (onClientEvent(name, cookie, info))
return;
}
bool DoodleManager::init()
{
return connect((MainApp *)qApp, SIGNAL(serviceEvent(const QString &, size_t, const Notification &)),
SLOT(onServiceEvent(const QString &, size_t, const Notification &)));
}
bool DoodleManager::startSess(SESSID sessId, int pageCount)
{
DoodleSession *pSess = findSess(sessId);
if (!pSess) return false;
// start session
if (!pSess->start(pageCount))
{
DOODLE_LOG_ERROR("start session %d.", sessId);
return false;
}
DOODLE_LOG_INFO("session %d.", (SESSID)sessId);
return true;
}
void DoodleManager::stopSess(SESSID sessId)
{
DoodleSession *pSess = findSess(sessId);
if (!pSess) return;
DOODLE_LOG_INFO("sess<%lld>.", sessId);
pSess->stop();
_SessMap.erase((SESSID)pSess);
delete pSess;
}
void DoodleManager::setBackgroundImages(SESSID sessId, const QStringList &imagePaths)
{
DoodleSession *pSess = findSess(sessId);
if (!pSess) return;
DOODLE_LOG_INFO("sess<%lld>.", sessId);
pSess->setBackgroundImages(imagePaths);
}
void DoodleManager::onRecvData(SESSID sessId, const char *name, const char *data)
{
DoodleSession *pSess = findSess(sessId);
if (!pSess) return;
DOODLE_LOG_INFO("sess<%lld>.", sessId);
pSess->recvData(name, data);
}
DoodleSession *DoodleManager::findSess(SESSID sessId)
{
std::map<SESSID, DoodleSession *>::iterator it = _SessMap.find(sessId);
if (it != _SessMap.end())
return it->second;
return NULL;
}
bool DoodleManager::onCallEvent(const QString &name, size_t, const Notification &_info)
{
Notification info = (Notification &)_info;
DoodleSession *sess;
int sessId;
if (name == MtcCallConnectingNotification)
{
sessId = info.getIntValue(MtcCallIdKey);
stopSess(sessId);
// create session
sess = new DoodleSession(sessId, this);
if (!sess)
{
DOODLE_LOG_ERROR("allocate session %d.", sessId);
return true;
}
// init session
if (!sess->init())
{
DOODLE_LOG_ERROR("init session %d.", sessId);
return true;
}
// add to map
_SessMap[sessId] = sess;
DOODLE_LOG_INFO("allocate session %d.", sessId);
emit sessCreated(sessId);
return true;
}
else if (name == MtcCallTermedNotification
|| name == MtcCallDidTermNotification)
{
// delete session
// return true;
sessId = info.getIntValue(MtcCallIdKey);
sess = findSess(sessId);
// stopSess(sessId);
if (sess)
{
// _SessMap.erase(sessId);
DOODLE_LOG_INFO("free session %d.", sessId);
// delete sess;
}
return true;
}
else if (name == MtcCallStreamDataReceivedNotification)
{
// process doodle data
sessId = info.getIntValue(MtcCallIdKey);
sess = findSess(sessId);
qDebug()<<"dataToInt = "<<MtcCallStreamDataReceivedNotification<<"\n";
if (sess)
{
QString dataName = info.getStringValue(MtcCallDataNameKey);
QString data = info.getStringValue(MtcCallDataValueKey);
qDebug("CMQ dataName = %s data = %s\n",dataName.toLatin1().data(),data.toLatin1().data());
// qDebug()<<"dataToInt = "<<data.toInt()<<"\n";
sess->recvData(dataName, data);
}
return true;
}
else if (name == MtcCallStreamFileReceivedNotification)
{
// process doodle file
sessId = info.getIntValue(MtcCallIdKey);
sess = findSess(sessId);
if (sess)
{
QString data = info.getStringValue(MtcCallUserDataKey);
Json::Value imageInfo;
Json::Reader reader;
reader.parse(data.toUtf8().data(), imageInfo, false);
int pageId = imageInfo.get(MtcDoodlePageIdKey, -1).asInt();
if (pageId >= 0)
{
QString fileName = info.getStringValue(MtcCallFileNameKey);
QString filePath = info.getStringValue(MtcCallFilePathKey);
sess->recvFile(fileName, filePath, pageId);
qDebug("CMQ fileName = %s filePath = %s",fileName.toLatin1().data(),filePath.toLatin1().data());
}
}
return true;
}
return false;
}
bool DoodleManager::onClientEvent(const QString &name, size_t, const Notification &)
{
if (name == MtcCliServerDidLogoutNotification
|| name == MtcCliServerLogoutedNotification)
{
// delete all sessions
std::map<SESSID, DoodleSession *>::iterator it = _SessMap.begin();
while (it != _SessMap.end())
{
delete it->second;
_SessMap.erase(it);
it = _SessMap.begin();
}
DOODLE_LOG_INFO("free all session.", NULL);
return true;
}
return false;
}
} // namespace JusDoodle