Revert previous commit, was incorrect
[amarok.git] / src / mediadevice / daap / proxy.cpp
blob982f156f2c920ee87a88e7ff4f46a41a492e2dd4
1 /***************************************************************************
2 * copyright : (C) 2006 Ian Monroe <ian@monroe.nu>
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation; either version 2 of
7 * the License or (at your option) version 3 or any later version
8 * accepted by the membership of KDE e.V. (or its successor approved
9 * by the membership of KDE e.V.), which shall act as a proxy
10 * defined in Section 14 of version 3 of the license.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 **************************************************************************/
20 #include "amarok.h"
21 #include "amarokconfig.h"
22 #include "daapclient.h"
23 #include "daapreader/authentication/hasher.h"
24 #include "debug.h"
25 #include "Process.h"
26 #include "proxy.h"
28 #include <kapplication.h>
30 using namespace Daap;
32 //input url: daap://host:port/databaseId/music.ext
34 bundle->setUrl( Amarok::QStringx("http://%1:3689/databases/%2/items/%3.%4?%5").args(
35 QStringList() << m_host
36 << m_databaseId
37 << QString::number( (*it).asMap()["miid"].asList()[0].asInt() )
38 << (*it).asMap()["asfm"].asList()[0].asString()
39 << m_loginString ) );
42 Proxy::Proxy(KUrl stream, DaapClient* client, const char* name)
43 : QObject(client, name)
44 , m_proxy( new Amarok::ProcIO() )
46 DEBUG_BLOCK
47 //find the request id and increment it
48 const QString hostKey = stream.host() + ':' + QString::number(stream.port());
49 const int revisionId = client->incRevision( hostKey );
50 const int sessionId = client->getSession( hostKey );
51 //compose URL
52 KUrl realStream = realStreamUrl( stream, sessionId );
54 //get hash
55 char hash[33] = {0};
56 GenerateHash( 3
57 , reinterpret_cast<const unsigned char*>((realStream.path() + realStream.query()).ascii())
58 , 2
59 , reinterpret_cast<unsigned char*>(hash)
60 , revisionId );
62 // Find free port
63 MyServerSocket* socket = new MyServerSocket();
64 const int port = socket->port();
65 debug() << "Proxy server using port: " << port;
66 delete socket;
67 m_proxyUrl = KUrl( QString("http://localhost:%1/daap.mp3").arg( port ) );
68 //start proxy
69 m_proxy->setOutputChannelMode( ProcIO::MergedChannels );
70 *m_proxy << "amarok_proxy.rb";
71 *m_proxy << "--daap";
72 *m_proxy << QString::number( port );
73 *m_proxy << realStream.url();
74 *m_proxy << AmarokConfig::soundSystem();
75 *m_proxy << hash;
76 *m_proxy << QString::number( revisionId );
77 *m_proxy << Amarok::proxyForUrl( realStream.url() );
79 m_proxy->start();
80 if( m_proxy->error() == ProcIO::FailedToStart ) {
81 error() << "Failed to start amarok_proxy.rb";
82 return;
85 QString line;
86 while( true ) {
87 kapp->processEvents();
88 m_proxy->readln( line );
89 if( line == "AMAROK_PROXY: startup" ) break;
91 debug() << "started amarok_proxy.rb --daap " << QString::number( port ) << ' ' << realStream.url() << ' ' << AmarokConfig::soundSystem() << ' ' << hash << ' ' << revisionId;
92 connect( m_proxy, SIGNAL( finished( int ) ), this, SLOT( playbackStopped() ) );
93 connect( m_proxy, SIGNAL( readReady( ProcIO* ) ), this, SLOT( readProxy() ) );
96 Proxy::~Proxy()
98 delete m_proxy;
101 void
102 Proxy::playbackStopped()
104 deleteLater();
107 void
108 Proxy::readProxy()
110 QString line;
112 while( m_proxy->readln( line ) != -1 )
114 debug() << line;
118 KUrl Proxy::realStreamUrl( KUrl fakeStream, int sessionId )
120 KUrl realStream;
121 realStream.setProtocol( "http" );
122 realStream.setHost(fakeStream.host());
123 realStream.setPort(fakeStream.port());
124 realStream.setPath( "/databases" + fakeStream.directory() + "/items/" + fakeStream.fileName() );
125 realStream.setQuery( QString("?session-id=") + QString::number(sessionId) );
126 return realStream;
129 #include "proxy.moc"