Skip to content
Permalink
Browse files

Add mythmessage, a replacement for mythtvosd.

mythmessage --message_text='Hello world' now replaces the temporary
mythtvosd --template=alert --alert_text='Hello world!'.

While there is no longer any custom xml or templating functionality,
internally mythmessage still uses a simple xml message structure to
allow adding additional message related data at a later data.
  • Loading branch information
Mark Kendall
Mark Kendall committed Jan 30, 2011
1 parent 793b2a9 commit bd469cf06576877789b174b8f17f6c0bcd058d75
@@ -126,6 +126,13 @@
#define TV_EXIT_NO_TV GENERIC_EXIT_START+2
#define TV_EXIT_START GENERIC_EXIT_START+2

// mythmessage
#define MESSAGE_EXIT_OK GENERIC_EXIT_OK
#define MESSAGE_EXIT_INVALID_CMDLINE GENERIC_EXIT_INVALID_CMDLINE
#define MESSAGE_EXIT_SOCKET_ERROR GENERIC_EXIT_SOCKET_ERROR
#define MESSAGE_EXIT_NO_TEMPLATE GENERIC_EXIT_START+1
#define MESSAGE_EXIT_START GENERIC_EXIT_START+1

// mythtvosd
#define TVOSD_EXIT_OK GENERIC_EXIT_OK
#define TVOSD_EXIT_INVALID_CMDLINE GENERIC_EXIT_INVALID_CMDLINE
@@ -81,18 +81,18 @@ void MythUDPListener::Process(const QByteArray &buf)
QDomElement docElem = doc.documentElement();
if (!docElem.isNull())
{
if (docElem.tagName() != "mythnotify")
if (docElem.tagName() != "mythmessage")
{
VERBOSE(VB_IMPORTANT, ERR +
"Unknown UDP packet (not <mythnotify> XML)");
"Unknown UDP packet (not <mythmessage> XML)");
return;
}

QString version = docElem.attribute("version", "");
if (version.isEmpty())
{
VERBOSE(VB_IMPORTANT, ERR +
"<mythnotify> missing 'version' attribute");
"<mythmessage> missing 'version' attribute");
return;
}
}
@@ -103,7 +103,7 @@ void MythUDPListener::Process(const QByteArray &buf)
QDomElement e = n.toElement();
if (!e.isNull())
{
if (e.tagName() == "container")
if (e.tagName() == "text")
{
QString msg = e.text();
VERBOSE(VB_GENERAL, LOC + msg);
@@ -0,0 +1,145 @@
// ANSI C headers
#include <cstdlib>

// POSIX headers
#include <sys/types.h>

// C++ headers
#include <iostream>
using namespace std;

// Qt headers
#include <QCoreApplication>
#include <QHostAddress>
#include <QUdpSocket>
#include <QString>
#include <QFile>

// MythTV headers
#include "exitcodes.h"
#include "compat.h"

#ifndef VERBOSE
#define VB_IMPORTANT 0
#define VERBOSE(LEVEL, MSG) \
do { cout << QString(MSG).toLocal8Bit().constData() << endl; } while (0)
#endif

const QString kMessage =
"<mythmessage version=\"1\">\n"
" <text>%message_text%</text>\n"
"</mythmessage>";

static void printHelp(void)
{
cout << "\nUsage: mythmessage --message_text=<yourtext> [OPTION]\n";
cout << "A utility to broadcast messages to all running mythfrontends.\n\n";
cout << " --udpport=\"value\" : (optional) UDP port to sent to (--udpport=6948)\n";
cout << " --bcastaddr\"value\" : (optional) IP address to send to\n";
cout << " (--bcastaddr=127.0.0.1)\n";
cout << " --verbose : (optional) extra debug information\n";
cout << " --help : (optional) this text\n";
}

int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);

QHostAddress address = QHostAddress::Broadcast;
unsigned short port = 6948;

QString message = kMessage;

bool verbose = false;

if (a.argc() == 0)
{
printHelp();
return MESSAGE_EXIT_OK;
}

for (int argpos = 1; argpos < a.argc(); ++argpos)
{
QString arg = a.argv()[argpos];

if (arg.startsWith("-h") || arg.startsWith("--help"))
{
printHelp();
return MESSAGE_EXIT_OK;
}
}

for (int argpos = 1; argpos < a.argc(); ++argpos)
{
QString arg = a.argv()[argpos];

if (arg.startsWith("--help") || arg.startsWith("--template"))
continue;

if (arg.startsWith("--verbose"))
{
verbose = true;
continue;
}

if (arg.startsWith("--udpport"))
{
port = arg.section("=", 1).toUShort();
if (port == 0)
port = 6948;
continue;
}

if (arg.startsWith("--bcastaddr"))
{
QString newaddr = arg.section("=", 1);
if (!newaddr.isEmpty())
address.setAddress(newaddr);
continue;
}

QString name = arg.section("=", 0, 0);
name.replace("--", "");

QString value = QString::fromLocal8Bit(
arg.section("=", 1).toLocal8Bit());
if (verbose)
{
QByteArray tmp_name = name.toLocal8Bit();
QByteArray tmp_value = value.toLocal8Bit();
cerr << "name: " << tmp_name.constData()
<< " -- value: " << tmp_value.constData() << endl;
}

name.append("%");
name.prepend("%");

message.replace(name, value);
}

if (verbose)
{
QByteArray tmp_message = message.toLocal8Bit();
cout << "output:\n" << tmp_message.constData() << endl;
}

QUdpSocket *sock = new QUdpSocket();
QByteArray utf8 = message.toUtf8();
int size = utf8.length();

if (sock->writeDatagram(utf8.constData(), size, address, port) < 0)
{
VERBOSE(VB_IMPORTANT, "Failed to send UDP/XML packet");
}
else
{
cout << "Sent UDP/XML packet to IP "
<< address.toString().toLocal8Bit().constData()
<< " and port: " << port << endl;
}

sock->deleteLater();

return MESSAGE_EXIT_OK;
}

@@ -0,0 +1,20 @@
include ( ../../settings.pro )
include ( ../programs-libs.pro )

QT += network

TEMPLATE = app
CONFIG += thread
TARGET = mythmessage
target.path = $${PREFIX}/bin
INSTALLS = target

LIBS += $$EXTRA_LIBS
INCLUDEPATH += ../../libs/libmythbase

QMAKE_CLEAN += $(TARGET)

# Input
SOURCES += main.cpp

mingw: LIBS += -lpthread -lwinmm -lws2_32
@@ -5,7 +5,7 @@ TEMPLATE = subdirs
# Directories
using_frontend {
SUBDIRS += mythavtest mythfrontend mythcommflag
SUBDIRS += mythtvosd mythjobqueue mythlcdserver
SUBDIRS += mythmessage mythjobqueue mythlcdserver
SUBDIRS += mythwelcome mythshutdown
SUBDIRS += mythpreviewgen
!mingw: SUBDIRS += mythtranscode/replex

0 comments on commit bd469cf

Please sign in to comment.
You can’t perform that action at this time.