3 #include <QReadWriteLock>
5 #include <QWriteLocker>
6 #include <QMutexLocker>
7 #include <QWaitCondition>
9 #include <QCoreApplication>
10 #include <QNetworkProxy>
19 #include "mythconfig.h"
25 #define LOC QString("MythSocketManager: ")
27 #define PRT_TIMEOUT 10
38 virtual void run(
void)
59 m_server(NULL), m_threadPool(
"MythSocketManager")
69 QMap<QString, SocketRequestHandler*>::iterator i;
96 LOG(VB_GENERAL, LOG_ERR, QString(
"Failed to bind port %1.").arg(port));
100 #if (QT_VERSION >= 0x050000)
123 LOG(VB_GENERAL, LOG_WARNING,
LOC + name +
124 " has already been registered.");
129 LOG(VB_GENERAL, LOG_INFO,
LOC +
130 "Registering socket command handler " + name);
161 "ServiceRequest", PRT_TIMEOUT);
172 QMap<QString, SocketRequestHandler*>::const_iterator i;
174 (*i)->connectionClosed(sock);
200 QStringList listline;
204 QString line = listline[0].simplified();
205 QStringList tokens = line.split(
' ', QString::SkipEmptyParts);
206 QString command = tokens[0];
208 bool handled =
false;
211 if (command ==
"DONE")
221 if (command ==
"MYTH_PROTO_VERSION")
227 LOG(VB_SOCKET, LOG_ERR,
LOC +
228 "Use of socket attempted before protocol validation.");
230 listline <<
"ERROR" <<
"socket has not been validated";
240 if (command ==
"ANN")
244 QMap<QString, SocketRequestHandler*>::const_iterator i
248 LOG(VB_SOCKET, LOG_DEBUG,
LOC +
249 QString(
"Attempting to handle annouce with: %1")
250 .arg((*i)->GetHandlerName()));
251 handled = (*i)->HandleAnnounce(sock, tokens, listline);
258 LOG(VB_SOCKET, LOG_DEBUG,
LOC +
259 QString(
"Socket announce handled by: %1")
260 .arg((*i)->GetHandlerName()));
263 (*i)->connectionAnnounced(sock, tokens, listline);
267 LOG(VB_SOCKET, LOG_ERR,
LOC +
"Socket announce unhandled.");
269 listline <<
"ERROR" <<
"unhandled announce";
277 LOG(VB_SOCKET, LOG_ERR,
LOC +
278 "Use of socket attempted before announcement.");
280 listline <<
"ERROR" <<
"socket has not been announced";
286 if (command ==
"ANN")
288 LOG(VB_SOCKET, LOG_ERR,
LOC +
"ANN sent out of sequence.");
290 listline <<
"ERROR" <<
"socket has already been announced";
302 LOG(VB_SOCKET, LOG_ERR,
LOC +
"No handler found for socket.");
304 listline <<
"ERROR" <<
"socket handler cannot be found";
312 QMap<QString, SocketRequestHandler*>::const_iterator i
316 handled = (*i)->HandleQuery(handler, tokens, listline);
321 LOG(VB_SOCKET, LOG_DEBUG,
LOC + QString(
"Query handled by: %1")
322 .arg((*--i)->GetHandlerName()));
327 if (command ==
"BACKEND_MESSAGE")
333 listline <<
"ERROR" <<
"unknown command";
339 const QStringList slist)
343 if (version != MYTH_PROTO_VERSION)
345 LOG(VB_GENERAL, LOG_ERR,
LOC +
346 "Client speaks protocol version " + version +
347 " but we speak " + MYTH_PROTO_VERSION +
'!');
348 retlist <<
"REJECT" << MYTH_PROTO_VERSION;
354 if (slist.size() < 3)
356 LOG(VB_GENERAL, LOG_ERR,
LOC +
"Client did not pass protocol "
357 "token. Refusing connection!");
358 retlist <<
"REJECT" << MYTH_PROTO_VERSION;
364 QString token = slist[2];
365 if (token != MYTH_PROTO_TOKEN)
367 LOG(VB_GENERAL, LOG_ERR,
LOC +
"Client sent incorrect protocol token "
368 "for protocol version. Refusing connection!");
369 retlist <<
"REJECT" << MYTH_PROTO_VERSION;
375 LOG(VB_SOCKET, LOG_DEBUG,
LOC +
"Client validated");
376 retlist <<
"ACCEPT" << MYTH_PROTO_VERSION;