forked from cculianu/SpikeGL
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStimGL_SpikeGL_Integration.cpp
More file actions
265 lines (239 loc) · 10.1 KB
/
StimGL_SpikeGL_Integration.cpp
File metadata and controls
265 lines (239 loc) · 10.1 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
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
#include "StimGL_SpikeGL_Integration.h"
#include "Util.h"
#include "SockUtil.h"
#include <QApplication>
#include <QTcpSocket>
#include <QHostAddress>
#include <QSharedMemory>
#include <QMessageBox>
#define GREETING_STRING "HELLO I'M SPIKEGL"
#define PLUGIN_START_STRING "PLUGIN START"
#define PLUGIN_END_STRING "PLUGIN END"
#define PLUGIN_PARAMS_STRING "PLUGIN PARAMS"
#define PLUGIN_PARAMS_END_STRING "END PLUGIN PARAMS"
#define OK_STRING "OK"
typedef QMap<QString, QVariant> PM;
Q_DECLARE_METATYPE(PM);
namespace StimGL_SpikeGL_Integration
{
static bool doNotify (bool isStart,
bool isEnd,
const QString & pname,
const QMap<QString, QVariant> &pparms,
QString *errStr_out,
const QString & host,
unsigned short port,
int timeout_msecs)
{
Debug() << "Notifying SpikeGL of `" << pname << "' " << (isStart ? "start" : (isEnd ? "end" : "params")) << "...";
SockUtil::Context ctx("Notify SpikeGL");
QTcpSocket sock;
sock.connectToHost(host, port);
if (!sock.waitForConnected(timeout_msecs) || !sock.isValid()) {
QString estr = SockUtil::errorToString(sock.error());
if (errStr_out) *errStr_out = estr;
QString logoutput = SockUtil::contextName() + " failed to notify; " + estr;
if (sock.error() == QAbstractSocket::ConnectionRefusedError)
Debug() << logoutput;
else
Error() << logoutput;
return false;
}
QString line = SockUtil::readLine(sock, timeout_msecs, errStr_out);
if (line.isNull()) return false;
if (isStart || isEnd) {
if (!SockUtil::send(sock, QString(isStart ? PLUGIN_START_STRING : PLUGIN_END_STRING) + " " + pname.trimmed() + "\n", timeout_msecs, errStr_out))
return false;
}
if (!SockUtil::send(sock, QString(PLUGIN_PARAMS_STRING) + "\n", timeout_msecs, errStr_out)) return false;
for (QMap<QString, QVariant> ::const_iterator it = pparms.begin();
it != pparms.end();
++it) {
if (!SockUtil::send(sock, QString("%1 = %2\n").arg(it.key()).arg(it.value().toString()), timeout_msecs, errStr_out)) return false;
}
if (!SockUtil::send(sock, QString("%1\n").arg(PLUGIN_PARAMS_END_STRING), timeout_msecs, errStr_out)) return false;
line = SockUtil::readLine(sock, timeout_msecs, errStr_out);
if (line.isNull()) return false;
if (!line.startsWith(OK_STRING)) {
QString estr = QString("Did not read OK from SpikeGL after sending plugin ") + (isStart ? "start" : "end") + " msg!";
if (errStr_out) *errStr_out = estr;
Error() << "Notify SpikeGL failed: " << estr;
return false;
}
Debug() << "SpikeGL notified of plugin `" << pname << "' " << (isStart ? "start" : "end") << " via socket!";
return true;
}
bool Notify_PluginStart( const QString & pname,
const QMap<QString, QVariant> &pparms,
QString *errStr_out,
const QString & host,
unsigned short port,
int timeout_msecs)
{
return doNotify(true, false, pname, pparms, errStr_out, host, port, timeout_msecs);
}
bool Notify_PluginEnd (const QString & pname,
const QMap<QString, QVariant> &pparms,
QString *errStr_out,
const QString & host,
unsigned short port,
int timeout_msecs)
{
return doNotify(false, true, pname, pparms, errStr_out, host, port, timeout_msecs);
}
bool Notify_PluginParams (const QString & pname,
const QMap<QString, QVariant> &pparms,
QString *errStr_out,
const QString & host,
unsigned short port,
int timeout_msecs)
{
return doNotify(false, false, pname, pparms, errStr_out, host, port, timeout_msecs);
}
void NotifyServer::processConnection(QTcpSocket & sock)
{
QString line;
SockUtil::Context ctx("NotifyServerThread");
line = QString(GREETING_STRING) + "\n";
bool isStart = false, isEnd = false, isParams = false;
if (!SockUtil::send(sock, line, timeout_msecs)) return;
if ((line = SockUtil::readLine(sock, timeout_msecs)).isNull()) return;
if ( !(isStart = line.startsWith(PLUGIN_START_STRING))
&& !(isEnd = line.startsWith(PLUGIN_END_STRING))
&& !(isParams = line.startsWith(PLUGIN_PARAMS_STRING)) ) {
Error() << SockUtil::contextName() << " parse error expected: {" << PLUGIN_START_STRING << "|" << PLUGIN_END_STRING << "} got: " << line;
return;
}
if (isStart)
line = line.mid(QString(PLUGIN_START_STRING).length());
else if (isEnd)
line = line.mid(QString(PLUGIN_END_STRING).length());
line = line.trimmed();
QString pluginName = "unknown";
if (isStart || isEnd) {
pluginName = line;
if ((line = SockUtil::readLine(sock, timeout_msecs)).isNull()) return;
if (!line.startsWith(PLUGIN_PARAMS_STRING)) {
Error() << SockUtil::contextName() << " parse error expected: " << PLUGIN_PARAMS_STRING << "got: " << line;
return;
}
}
// at this point all modes read params ...
QMap<QString, QVariant> params;
while ( !(line = SockUtil::readLine(sock, timeout_msecs)).startsWith(PLUGIN_PARAMS_END_STRING)) {
QStringList l = line.trimmed().split("=");
if (l.count() < 2) { Debug() << "skipping params line that does not contain '=': " << line; continue; }
QString n = l.front().trimmed();
l.pop_front();
QString v = l.join("=").trimmed();
params[n] = v;
}
line = QString(OK_STRING) + "\n";
if (!SockUtil::send(sock, line, timeout_msecs)) return;
emitGotPluginNotification(isStart, isEnd, pluginName, params);
//Log() << "Received plugin " << (isStart ? "start" : "end") << " notificaton from StimGL for plugin " << pluginName;
}
void NotifyServer::gotNewConnection()
{
QTcpSocket *sock = srv.nextPendingConnection();
processConnection(*sock);
sock->close();
delete sock;
}
static bool mapMetaTypeRegistered = false;
NotifyServer::NotifyServer(QObject *parent)
: QObject(parent)
{
if (!mapMetaTypeRegistered) {
qRegisterMetaType<QMap<QString, QVariant> >();
mapMetaTypeRegistered = true;
}
Connect(&srv, SIGNAL(newConnection()), this, SLOT(gotNewConnection()));
}
NotifyServer::~NotifyServer()
{
}
bool NotifyServer::beginListening(const QString & iface, unsigned short port, int tout) {
timeout_msecs = tout;
QHostAddress haddr;
if (iface == "0.0.0.0") haddr = QHostAddress(QHostAddress::Any);
else if (iface == "localhost" || iface == "127.0.0.1") haddr = QHostAddress(QHostAddress::LocalHost);
else haddr = iface;
if (!srv.listen(haddr, port)) {
Error() << "NotifyServer could not listen: " << srv.errorString();
return false;
}
Log() << "StimGL notification server listening on " << haddr.toString() << ":" << port;
return true;
}
void NotifyServer::emitGotPluginNotification(bool isStart, bool isEnd, const QString &p, const QMap<QString, QVariant> &pp) {
if (isStart)
emit gotPluginStartNotification(p, pp);
else if (isEnd)
emit gotPluginEndNotification(p, pp);
else
emit gotPluginParamsNotification(p, pp);
}
FrameShare::FrameShare()
{
qsm = new QSharedMemory(QString("%1").arg(FRAME_SHARE_SHM_MAGIC), qApp);
shm = 0;
createdByThisInstance = false;
if (!qsm->isAttached() && !qsm->attach() && qsm->create(FRAME_SHARE_SHM_SIZE)) {
if (lock()) {
shm = (volatile FrameShareShm *)qsm->data();
memset((void *)shm, 0, FRAME_SHARE_SHM_SIZE);
shm->magic = FRAME_SHARE_SHM_MAGIC;
unlock();
createdByThisInstance = true;
} else {
Error() << "INTERNAL ERROR: Could not lock frame share shm after create!";
}
}
if (!qsm->isAttached())
Error() << "INTERNAL ERROR: 'frame share' shared memory segment cannot be attached/created due to the following reason: " << qsm->errorString();
else {
shm = const_cast<volatile FrameShareShm *>(reinterpret_cast<FrameShareShm *>(qsm->data()));
if (qsm->size() != FRAME_SHARE_SHM_SIZE) {
Error() << "INTERNAL ERROR: 'frame share' shared memory segment attached correctly but it has an incorrect size. Detaching.";
detach();
shm = 0;
} else if (shm->magic != FRAME_SHARE_SHM_MAGIC) {
Error() << "INTERNAL ERROR: 'frame share' shared memory segment attached correctly but it appears corrupted. Detaching.";
detach();
shm = 0;
}
}
}
FrameShare::~FrameShare()
{
detach();
delete qsm, qsm = 0;
}
void FrameShare::detach()
{
if (qsm->isAttached()) qsm->detach();
shm = 0;
}
bool FrameShare::lock()
{
return qsm->lock();
}
bool FrameShare::unlock()
{
return qsm->unlock();
}
int FrameShare::size() const { return qsm->size(); }
bool FrameShare::warnUserAlreadyRunning() const
{
QMessageBox::StandardButton but = QMessageBox::critical
(0,
"Duplicate Instance Running",
QString("After attaching to the 'frame share shm' segment, it appears another "
"instance of %1 may already be attached to this segment (or if "
"not, it quit uncleanly).\n\n"
"Do you wish to continue using the frame share mechanism anyway?").arg(qApp->applicationName()),
QMessageBox::Yes|QMessageBox::No, QMessageBox::No);
return (but == QMessageBox::Yes);
}
}