1 /***************************************************************************
2 * copyright : (C) 2006 Ian Monroe <ian@monroe.nu>
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 **************************************************************************/
21 #include "amarokconfig.h"
22 #include "daapclient.h"
23 #include "daapreader/authentication/hasher.h"
28 #include <kapplication.h>
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
37 << QString::number( (*it).asMap()["miid"].asList()[0].asInt() )
38 << (*it).asMap()["asfm"].asList()[0].asString()
42 Proxy::Proxy(KUrl stream
, DaapClient
* client
, const char* name
)
43 : QObject(client
, name
)
44 , m_proxy( new Amarok::ProcIO() )
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
);
52 KUrl realStream
= realStreamUrl( stream
, sessionId
);
57 , reinterpret_cast<const unsigned char*>((realStream
.path() + realStream
.query()).ascii())
59 , reinterpret_cast<unsigned char*>(hash
)
63 MyServerSocket
* socket
= new MyServerSocket();
64 const int port
= socket
->port();
65 debug() << "Proxy server using port: " << port
;
67 m_proxyUrl
= KUrl( QString("http://localhost:%1/daap.mp3").arg( port
) );
69 m_proxy
->setOutputChannelMode( ProcIO::MergedChannels
);
70 *m_proxy
<< "amarok_proxy.rb";
72 *m_proxy
<< QString::number( port
);
73 *m_proxy
<< realStream
.url();
74 *m_proxy
<< AmarokConfig::soundSystem();
76 *m_proxy
<< QString::number( revisionId
);
77 *m_proxy
<< Amarok::proxyForUrl( realStream
.url() );
80 if( m_proxy
->error() == ProcIO::FailedToStart
) {
81 error() << "Failed to start amarok_proxy.rb";
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() ) );
102 Proxy::playbackStopped()
112 while( m_proxy
->readln( line
) != -1 )
118 KUrl
Proxy::realStreamUrl( KUrl fakeStream
, int sessionId
)
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
) );