00001
00002
00003
00004
00005
00006
00007 #include "streaminput.h"
00008
00009 #include "mythtv/mythcontext.h"
00010
00011 #include <qapplication.h>
00012 #include <qsocket.h>
00013
00014
00015 StreamInput::StreamInput(const QUrl &source)
00016 : request(0), url(source), sock(0), stage(0)
00017 {
00018 }
00019
00020
00021 void StreamInput::setup()
00022 {
00023 if (! url.isValid())
00024 return;
00025
00026 QString protocol = url.protocol();
00027 QString host = url.host();
00028 QString path = url.path();
00029 int port = url.port();
00030
00031 if (protocol != "mqp" || host.isNull())
00032 return;
00033
00034 if (port == -1)
00035 port = 42666;
00036
00037 request = ".song " + path.utf8() + "\r\n";
00038
00039
00040 sock = new QSocket;
00041 connect(sock, SIGNAL(error(int)), this, SLOT(error(int)));
00042 connect(sock, SIGNAL(hostFound()), this, SLOT(hostfound()));
00043 connect(sock, SIGNAL(connected()), this, SLOT(connected()));
00044 connect(sock, SIGNAL(readyRead()), this, SLOT(readyread()));
00045
00046 sock->connectToHost(host, port);
00047
00048 while (stage != -1 && stage < 4)
00049 {
00050 qDebug("processing one event: stage %d %d %ld",
00051 stage, sock->canReadLine(), sock->bytesAvailable());
00052 qApp->processOneEvent();
00053 }
00054
00055 qDebug("disconnecting from socket");
00056 disconnect(sock, SIGNAL(error(int)), this, SLOT(error(int)));
00057 disconnect(sock, SIGNAL(hostFound()), this, SLOT(hostfound()));
00058 disconnect(sock, SIGNAL(connected()), this, SLOT(connected()));
00059 disconnect(sock, SIGNAL(readyRead()), this, SLOT(readyread()));
00060
00061 if (stage == -1)
00062 {
00063
00064 delete sock;
00065 sock = 0;
00066 }
00067 }
00068
00069
00070 void StreamInput::hostfound()
00071 {
00072 qDebug("host found");
00073 stage = 1;
00074 }
00075
00076
00077 void StreamInput::connected()
00078 {
00079 qDebug("connected... sending request '%s' %d", request.data(), request.length());
00080
00081 sock->writeBlock(request.data(), request.length());
00082 sock->flush();
00083
00084 stage = 2;
00085 }
00086
00087
00088 void StreamInput::readyread()
00089 {
00090 if (stage == 2)
00091 {
00092 qDebug("readyread... checking response");
00093
00094 if (! sock->canReadLine())
00095 {
00096 stage = -1;
00097 qDebug("can't read line");
00098 return;
00099 }
00100
00101 QString line = sock->readLine();
00102 if (line.isEmpty())
00103 {
00104 stage = -1;
00105 qDebug("line is empty");
00106 return;
00107 }
00108
00109 if (line.left(5) != "*GOOD")
00110 {
00111 VERBOSE(VB_IMPORTANT, QString("server error response: %1")
00112 .arg(line));
00113 stage = -1;
00114 return;
00115 }
00116
00117 stage = 3;
00118 }
00119 else if (sock->bytesAvailable() > 65536 || sock->atEnd())
00120 {
00121 stage = 4;
00122 }
00123 }
00124
00125
00126 void StreamInput::error(int err)
00127 {
00128 qDebug("socket error: %d", err);
00129
00130 stage = -1;
00131 }
00132