/src/sip/zeroconf/tomahawkzeroconf.h
http://github.com/tomahawk-player/tomahawk · C Header · 152 lines · 108 code · 25 blank · 19 comment · 8 complexity · 3ca62578c756080ede63ea2fe03882da MD5 · raw file
- /* === This file is part of Tomahawk Player - <http://tomahawk-player.org> ===
- *
- * Copyright 2010-2011, Christian Muehlhaeuser <muesli@tomahawk-player.org>
- *
- * Tomahawk is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or
- * (at your option) any later version.
- *
- * Tomahawk is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with Tomahawk. If not, see <http://www.gnu.org/licenses/>.
- */
- #ifndef TOMAHAWKZCONF
- #define TOMAHAWKZCONF
- #define ZCONF_PORT 50210
- #include <QList>
- #include <QHostAddress>
- #include <QHostInfo>
- #include <QNetworkProxy>
- #include <QUdpSocket>
- #include <QTimer>
- #include "database/database.h"
- #include "network/servent.h"
- #include "../sipdllmacro.h"
- class SIPDLLEXPORT Node : public QObject
- {
- Q_OBJECT
- public:
- Node( const QString& i, const QString& n, int p )
- : ip( i ), nid( n ), port( p )
- {
- qDebug() << Q_FUNC_INFO;
- }
- ~Node()
- {
- qDebug() << Q_FUNC_INFO;
- }
- signals:
- void tomahawkHostFound( const QString&, int, const QString&, const QString& );
- public slots:
- void resolved( QHostInfo i )
- {
- emit tomahawkHostFound( ip, port, i.hostName(), nid );
- this->deleteLater();
- }
- void resolve()
- {
- QHostInfo::lookupHost( ip, this, SLOT( resolved( QHostInfo ) ) );
- }
- private:
- QString ip;
- QString nid;
- int port;
- };
- class SIPDLLEXPORT TomahawkZeroconf : public QObject
- {
- Q_OBJECT
- public:
- TomahawkZeroconf( int port, QObject* parent = 0 )
- : QObject( parent ), m_sock( this ), m_port( port )
- {
- qDebug() << Q_FUNC_INFO;
- m_sock.setProxy( QNetworkProxy::NoProxy );
- m_sock.bind( ZCONF_PORT, QUdpSocket::ShareAddress );
- connect( &m_sock, SIGNAL( readyRead() ), this, SLOT( readPacket() ) );
- }
- virtual ~TomahawkZeroconf()
- {
- qDebug() << Q_FUNC_INFO;
- }
- public slots:
- void advertise()
- {
- qDebug() << "Advertising us on the LAN";
- QByteArray advert = QString( "TOMAHAWKADVERT:%1:%2" )
- .arg( m_port )
- .arg( Database::instance()->dbid() )
- .toAscii();
- m_sock.writeDatagram( advert.data(), advert.size(),
- QHostAddress::Broadcast, ZCONF_PORT );
- }
- signals:
- // IP, port, name, session
- void tomahawkHostFound( const QString&, int, const QString&, const QString& );
- private slots:
- void readPacket()
- {
- if ( !m_sock.hasPendingDatagrams() )
- return;
- QByteArray datagram;
- datagram.resize( m_sock.pendingDatagramSize() );
- QHostAddress sender;
- quint16 senderPort;
- m_sock.readDatagram( datagram.data(), datagram.size(), &sender, &senderPort );
- qDebug() << "DATAGRAM RCVD" << QString::fromAscii( datagram ) << sender;
- // only process msgs originating on the LAN:
- if ( datagram.startsWith( "TOMAHAWKADVERT:" ) &&
- Servent::isIPWhitelisted( sender ) )
- {
- QStringList parts = QString::fromAscii( datagram ).split( ':' );
- if ( parts.length() == 3 )
- {
- bool ok;
- int port = parts.at(1).toInt( &ok );
- if(ok && Database::instance()->dbid() != parts.at( 2 ) )
- {
- qDebug() << "ADVERT received:" << sender << port;
- Node *n = new Node( sender.toString(), parts.at( 2 ), port );
- connect( n, SIGNAL( tomahawkHostFound( QString, int, QString, QString ) ),
- this, SIGNAL( tomahawkHostFound( QString, int, QString, QString ) ) );
- n->resolve();
- }
- }
- }
- if ( m_sock.hasPendingDatagrams() )
- QTimer::singleShot( 0, this, SLOT( readPacket() ) );
- }
- private:
- QUdpSocket m_sock;
- int m_port;
- };
- #endif