00001
00002
00003
00004
00005
00006
00007 #include "streaminput.h"
00008
00009 #include "mythlogging.h"
00010
00011 #include <QApplication>
00012
00013 #define LOC QString("StreamInput: ")
00014 #define LOC_WARN QString("StreamInput, Warning: ")
00015 #define LOC_ERR QString("StreamInput, Error: ")
00016
00017 StreamInput::StreamInput(const QUrl &source) :
00018 request(QString::null), url(source), sock(NULL), stage(0)
00019 {
00020 }
00021
00022 void StreamInput::Setup(void)
00023 {
00024 if (!url.isValid())
00025 return;
00026
00027 QString protocol = url.scheme();
00028 QString host = url.host();
00029 QString path = url.path();
00030 int port = url.port();
00031
00032 if (protocol != "mqp" || host.isEmpty())
00033 return;
00034
00035 port = (port < 0) ? 42666 : port;
00036
00037 request = path;
00038 request.detach();
00039
00040 sock = new QTcpSocket();
00041 connect(sock, SIGNAL(Error(QAbstractSocket::SocketError)),
00042 this, SLOT( Error(QAbstractSocket::SocketError)));
00043 connect(sock, SIGNAL(hostFound()), this, SLOT(HostFound()));
00044 connect(sock, SIGNAL(connected()), this, SLOT(Connected()));
00045 connect(sock, SIGNAL(readyRead()), this, SLOT(Readyread()));
00046
00047 sock->connectToHost(host, port, QIODevice::ReadWrite);
00048
00049 while (stage != -1 && stage < 4)
00050 {
00051 LOG(VB_GENERAL, LOG_INFO, LOC +
00052 QString("Processing one event: stage %1 %2 %3")
00053 .arg(stage).arg(sock->canReadLine())
00054 .arg(sock->bytesAvailable()));
00055
00056 qApp->processEvents();
00057 }
00058
00059 LOG(VB_GENERAL, LOG_INFO, LOC + "Disconnecting from socket");
00060 disconnect(sock, SIGNAL(Error(QAbstractSocket::SocketError)),
00061 this, SLOT( Error(QAbstractSocket::SocketError)));
00062 disconnect(sock, SIGNAL(hostFound()), this, SLOT(HostFound()));
00063 disconnect(sock, SIGNAL(connected()), this, SLOT(Connected()));
00064 disconnect(sock, SIGNAL(readyRead()), this, SLOT(ReadyRead()));
00065
00066 if (stage == -1)
00067 {
00068
00069 delete sock;
00070 sock = NULL;
00071 }
00072 }
00073
00074
00075 void StreamInput::HostFound(void)
00076 {
00077 LOG(VB_GENERAL, LOG_INFO, LOC + "Host found");
00078 stage = 1;
00079 }
00080
00081
00082 void StreamInput::Connected(void)
00083 {
00084 QString tmp = QString(".song %1\r\n").arg(QString(request.toUtf8()));
00085 QByteArray ba = tmp.toAscii();
00086
00087 LOG(VB_GENERAL, LOG_INFO, LOC +
00088 QString("Connected... sending request '%1' %2")
00089 .arg(ba.constData()).arg(ba.length()));
00090
00091 sock->write(ba.constData(), ba.length());
00092 sock->flush();
00093
00094 stage = 2;
00095 }
00096
00097
00098 void StreamInput::ReadyRead(void)
00099 {
00100 if (stage == 2)
00101 {
00102 LOG(VB_GENERAL, LOG_INFO, LOC + "ReadyRead... checking response");
00103
00104 if (! sock->canReadLine())
00105 {
00106 stage = -1;
00107 LOG(VB_GENERAL, LOG_ERR, LOC + "ReadyRead... can't read line");
00108 return;
00109 }
00110
00111 QString line = sock->readLine();
00112 if (line.isEmpty())
00113 {
00114 stage = -1;
00115 LOG(VB_GENERAL, LOG_ERR, LOC + "ReadyRead... line is empty");
00116 return;
00117 }
00118
00119 if (line.left(5) != "*GOOD")
00120 {
00121 LOG(VB_GENERAL, LOG_ERR, LOC +
00122 QString("Server error response: %1").arg(line));
00123 stage = -1;
00124 return;
00125 }
00126
00127 stage = 3;
00128 }
00129 else if (sock->bytesAvailable() > 65536 || sock->atEnd())
00130 {
00131 stage = 4;
00132 }
00133 }
00134
00135 void StreamInput::Error(QAbstractSocket::SocketError)
00136 {
00137 LOG(VB_GENERAL, LOG_ERR, LOC +
00138 QString("Socket error: %1").arg(sock->errorString()));
00139
00140 stage = -1;
00141 }
00142