/
mythudplistener.cpp
132 lines (113 loc) · 3.32 KB
/
mythudplistener.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
#include <QCoreApplication>
#include <QUdpSocket>
#include <QDomDocument>
#include <QList>
#include <QHostAddress>
#include "mythcorecontext.h"
#include "mythlogging.h"
#include "mythmainwindow.h"
#include "mythudplistener.h"
#define LOC QString("UDPListener: ")
MythUDPListener::MythUDPListener()
{
Enable();
}
void MythUDPListener::deleteLater(void)
{
Disable();
disconnect();
QObject::deleteLater();
}
void MythUDPListener::Enable(void)
{
if (m_socketPool)
return;
LOG(VB_GENERAL, LOG_INFO, LOC + "Enabling");
m_socketPool = new ServerPool(this);
connect(m_socketPool, SIGNAL(newDatagram(QByteArray, QHostAddress,
quint16)),
this, SLOT(Process(const QByteArray, QHostAddress,
quint16)));
QList<QHostAddress> addrs = ServerPool::DefaultListen();
addrs << ServerPool::DefaultBroadcast();
if (!m_socketPool->bind(addrs,
gCoreContext->GetNumSetting("UDPNotifyPort", 0), false))
{
delete m_socketPool;
m_socketPool = NULL;
}
}
void MythUDPListener::Disable(void)
{
if (!m_socketPool)
return;
LOG(VB_GENERAL, LOG_INFO, LOC + "Disabling");
m_socketPool->close();
delete m_socketPool;
m_socketPool = NULL;
}
void MythUDPListener::Process(const QByteArray &buf, QHostAddress sender,
quint16 senderPort)
{
QString errorMsg;
int errorLine = 0;
int errorColumn = 0;
QDomDocument doc;
if (!doc.setContent(buf, false, &errorMsg, &errorLine, &errorColumn))
{
LOG(VB_GENERAL, LOG_ERR, LOC +
QString("Parsing xml:\n\t\t\t at line: %1 column: %2\n\t\t\t%3")
.arg(errorLine).arg(errorColumn).arg(errorMsg));
return;
}
QDomElement docElem = doc.documentElement();
if (!docElem.isNull())
{
if (docElem.tagName() != "mythmessage")
{
LOG(VB_GENERAL, LOG_ERR, LOC +
"Unknown UDP packet (not <mythmessage> XML)");
return;
}
QString version = docElem.attribute("version", "");
if (version.isEmpty())
{
LOG(VB_GENERAL, LOG_ERR, LOC +
"<mythmessage> missing 'version' attribute");
return;
}
}
QString msg = QString("");
uint timeout = 0;
QDomNode n = docElem.firstChild();
while (!n.isNull())
{
QDomElement e = n.toElement();
if (!e.isNull())
{
if (e.tagName() == "text")
msg = e.text();
else if (e.tagName() == "timeout")
timeout = e.text().toUInt();
else
{
LOG(VB_GENERAL, LOG_ERR, LOC + QString("Unknown element: %1")
.arg(e.tagName()));
return;
}
}
n = n.nextSibling();
}
if (!msg.isEmpty())
{
if (timeout > 1000)
timeout = 0;
LOG(VB_GENERAL, LOG_INFO, QString("Received message '%1', timeout %2")
.arg(msg).arg(timeout));
QStringList args;
args << QString::number(timeout);
MythMainWindow *window = GetMythMainWindow();
MythEvent* me = new MythEvent(MythEvent::MythUserMessage, msg, args);
qApp->postEvent(window, me);
}
}