Ported all process code from K3Process/K3ProcIO to KProcess based classes (see Proces...
[amarok.git] / src / mediadevice / daap / proxy.cpp
blob920b611bd9d01f07ebbc3e1834a1fa2c3520abda
1 /***************************************************************************
2 * copyright : (C) 2006 Ian Monroe <ian@monroe.nu> *
3 **************************************************************************/
5 /***************************************************************************
6 * *
7 * This program is free software; you can redistribute it and/or modify *
8 * it under the terms of the GNU General Public License as published by *
9 * the Free Software Foundation; either version 2 of the License, or *
10 * (at your option) any later version. *
11 * *
12 ***************************************************************************/
13 #include "amarok.h"
14 #include "amarokconfig.h"
15 #include "daapclient.h"
16 #include "daapreader/authentication/hasher.h"
17 #include "debug.h"
18 #include "Process.h"
19 #include "proxy.h"
21 #include <kapplication.h>
23 using namespace Daap;
25 //input url: daap://host:port/databaseId/music.ext
27 bundle->setUrl( Amarok::QStringx("http://%1:3689/databases/%2/items/%3.%4?%5").args(
28 QStringList() << m_host
29 << m_databaseId
30 << QString::number( (*it).asMap()["miid"].asList()[0].asInt() )
31 << (*it).asMap()["asfm"].asList()[0].asString()
32 << m_loginString ) );
35 Proxy::Proxy(KUrl stream, DaapClient* client, const char* name)
36 : QObject(client, name)
37 , m_proxy( new Amarok::ProcIO() )
39 DEBUG_BLOCK
40 //find the request id and increment it
41 const QString hostKey = stream.host() + ':' + QString::number(stream.port());
42 const int revisionId = client->incRevision( hostKey );
43 const int sessionId = client->getSession( hostKey );
44 //compose URL
45 KUrl realStream = realStreamUrl( stream, sessionId );
47 //get hash
48 char hash[33] = {0};
49 GenerateHash( 3
50 , reinterpret_cast<const unsigned char*>((realStream.path() + realStream.query()).ascii())
51 , 2
52 , reinterpret_cast<unsigned char*>(hash)
53 , revisionId );
55 // Find free port
56 MyServerSocket* socket = new MyServerSocket();
57 const int port = socket->port();
58 debug() << "Proxy server using port: " << port;
59 delete socket;
60 m_proxyUrl = KUrl( QString("http://localhost:%1/daap.mp3").arg( port ) );
61 //start proxy
62 m_proxy->setOutputChannelMode( ProcIO::MergedChannels );
63 *m_proxy << "amarok_proxy.rb";
64 *m_proxy << "--daap";
65 *m_proxy << QString::number( port );
66 *m_proxy << realStream.url();
67 *m_proxy << AmarokConfig::soundSystem();
68 *m_proxy << hash;
69 *m_proxy << QString::number( revisionId );
70 *m_proxy << Amarok::proxyForUrl( realStream.url() );
72 m_proxy->start();
73 if( m_proxy->error() == ProcIO::FailedToStart ) {
74 error() << "Failed to start amarok_proxy.rb";
75 return;
78 QString line;
79 while( true ) {
80 kapp->processEvents();
81 m_proxy->readln( line );
82 if( line == "AMAROK_PROXY: startup" ) break;
84 debug() << "started amarok_proxy.rb --daap " << QString::number( port ) << ' ' << realStream.url() << ' ' << AmarokConfig::soundSystem() << ' ' << hash << ' ' << revisionId;
85 connect( m_proxy, SIGNAL( finished( int ) ), this, SLOT( playbackStopped() ) );
86 connect( m_proxy, SIGNAL( readReady( ProcIO* ) ), this, SLOT( readProxy() ) );
89 Proxy::~Proxy()
91 delete m_proxy;
94 void
95 Proxy::playbackStopped()
97 deleteLater();
100 void
101 Proxy::readProxy()
103 QString line;
105 while( m_proxy->readln( line ) != -1 )
107 debug() << line;
111 KUrl Proxy::realStreamUrl( KUrl fakeStream, int sessionId )
113 KUrl realStream;
114 realStream.setProtocol( "http" );
115 realStream.setHost(fakeStream.host());
116 realStream.setPort(fakeStream.port());
117 realStream.setPath( "/databases" + fakeStream.directory() + "/items/" + fakeStream.fileName() );
118 realStream.setQuery( QString("?session-id=") + QString::number(sessionId) );
119 return realStream;
122 #include "proxy.moc"