Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a new MythUDPListener class to libmythui.
To test:- mythtvosd --template=alert --alert_text='Hello world!' This replaces the old udpnotify class in libmythtv and the new object is owned by MythMainWindow. The UDP port setting is reinstated and moved from the Playback screen to the General settings pages. I've retained the XML functionality to ensure we don't pick up stray UDP broadcast messages and to make the most of the eXtensibility once we've settled on a general framework and UI for messaging (e.g. to add timeout settings). For the time being, mythtvosd will work as above but I will replace it with a new command line utility (mythmessage?) with the template handling removed.
- Loading branch information
Mark Kendall
committed
Dec 23, 2010
1 parent
200d3bf
commit 7b4f59a
Showing
5 changed files
with
175 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
#include <QCoreApplication> | ||
#include <QUdpSocket> | ||
#include <QDomDocument> | ||
|
||
#include "mythcorecontext.h" | ||
#include "mythverbose.h" | ||
#include "mythmainwindow.h" | ||
#include "mythudplistener.h" | ||
|
||
#define LOC QString("UDPListener: ") | ||
#define ERR QString("UPDListener Error: ") | ||
|
||
MythUDPListener::MythUDPListener() | ||
{ | ||
uint udp_port = gCoreContext->GetNumSetting("UDPNotifyPort", 0); | ||
m_socket = new QUdpSocket(this); | ||
connect(m_socket, SIGNAL(readyRead()), | ||
this, SLOT(ReadPending())); | ||
if (m_socket->bind(udp_port)) | ||
{ | ||
VERBOSE(VB_GENERAL, LOC + QString("bound to port %1").arg(udp_port)); | ||
} | ||
else | ||
{ | ||
VERBOSE(VB_GENERAL, LOC + | ||
QString("failed to bind to port %1").arg(udp_port)); | ||
} | ||
} | ||
|
||
void MythUDPListener::deleteLater(void) | ||
{ | ||
TeardownAll(); | ||
disconnect(); | ||
QObject::deleteLater(); | ||
} | ||
|
||
void MythUDPListener::TeardownAll(void) | ||
{ | ||
if (!m_socket) | ||
return; | ||
|
||
VERBOSE(VB_GENERAL, LOC + "Disconnecting"); | ||
|
||
m_socket->disconnect(); | ||
m_socket->close(); | ||
m_socket->deleteLater(); | ||
m_socket = NULL; | ||
} | ||
|
||
void MythUDPListener::ReadPending(void) | ||
{ | ||
QByteArray buf; | ||
while (m_socket->hasPendingDatagrams()) | ||
{ | ||
buf.resize(m_socket->pendingDatagramSize()); | ||
QHostAddress sender; | ||
quint16 senderPort; | ||
|
||
m_socket->readDatagram(buf.data(), buf.size(), | ||
&sender, &senderPort); | ||
|
||
Process(buf); | ||
} | ||
} | ||
|
||
void MythUDPListener::Process(const QByteArray &buf) | ||
{ | ||
QString errorMsg; | ||
int errorLine = 0; | ||
int errorColumn = 0; | ||
QDomDocument doc; | ||
if (!doc.setContent(buf, false, &errorMsg, &errorLine, &errorColumn)) | ||
{ | ||
VERBOSE(VB_IMPORTANT, ERR + | ||
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() != "mythnotify") | ||
{ | ||
VERBOSE(VB_IMPORTANT, ERR + | ||
"Unknown UDP packet (not <mythnotify> XML)"); | ||
return; | ||
} | ||
|
||
QString version = docElem.attribute("version", ""); | ||
if (version.isEmpty()) | ||
{ | ||
VERBOSE(VB_IMPORTANT, ERR + | ||
"<mythnotify> missing 'version' attribute"); | ||
return; | ||
} | ||
} | ||
|
||
QDomNode n = docElem.firstChild(); | ||
while (!n.isNull()) | ||
{ | ||
QDomElement e = n.toElement(); | ||
if (!e.isNull()) | ||
{ | ||
if (e.tagName() == "container") | ||
{ | ||
QString msg = e.text(); | ||
VERBOSE(VB_GENERAL, LOC + msg); | ||
MythMainWindow *window = GetMythMainWindow(); | ||
MythEvent* me = new MythEvent(MythEvent::MythUserMessage, msg); | ||
qApp->postEvent(window, me); | ||
} | ||
else | ||
{ | ||
VERBOSE(VB_IMPORTANT, ERR + QString("Unknown element: %1") | ||
.arg(e.tagName())); | ||
return; | ||
} | ||
} | ||
n = n.nextSibling(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
#ifndef MYTHUDPLISTENER_H | ||
#define MYTHUDPLISTENER_H | ||
|
||
#include <QObject> | ||
|
||
class QByteArray; | ||
class QUdpSocket; | ||
class QDomElement; | ||
|
||
class MythUDPListener : public QObject | ||
{ | ||
Q_OBJECT | ||
|
||
public: | ||
MythUDPListener(); | ||
|
||
public slots: | ||
virtual void deleteLater(void); | ||
|
||
private slots: | ||
void ReadPending(void); | ||
|
||
private: | ||
~MythUDPListener(void) { TeardownAll(); } | ||
|
||
void Process(const QByteArray &buf); | ||
void TeardownAll(void); | ||
|
||
private: | ||
QUdpSocket *m_socket; | ||
}; | ||
|
||
#endif // MYTHUDPLISTENER_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters