PageRenderTime 28ms CodeModel.GetById 14ms app.highlight 11ms RepoModel.GetById 1ms app.codeStats 0ms

/src/libtomahawk/network/Servent.h

http://github.com/tomahawk-player/tomahawk
C Header | 203 lines | 94 code | 44 blank | 65 comment | 0 complexity | 4cab369fe958b1793e887f2584525e3d MD5 | raw file
  1/* === This file is part of Tomahawk Player - <http://tomahawk-player.org> ===
  2 *
  3 *   Copyright 2010-2011, Christian Muehlhaeuser <muesli@tomahawk-player.org>
  4 *   Copyright 2010-2012, Jeff Mitchell <jeff@tomahawk-player.org>
  5 *   Copyright 2013,      Teo Mrnjavac <teo@kde.org>
  6 *   Copyright 2013, Uwe L. Korn <uwelk@xhochy.com>
  7 *
  8 *   Tomahawk is free software: you can redistribute it and/or modify
  9 *   it under the terms of the GNU General Public License as published by
 10 *   the Free Software Foundation, either version 3 of the License, or
 11 *   (at your option) any later version.
 12 *
 13 *   Tomahawk is distributed in the hope that it will be useful,
 14 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 15 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 16 *   GNU General Public License for more details.
 17 *
 18 *   You should have received a copy of the GNU General Public License
 19 *   along with Tomahawk. If not, see <http://www.gnu.org/licenses/>.
 20 */
 21
 22#ifndef SERVENT_H
 23#define SERVENT_H
 24
 25// time before new connection terminate if it could not be established
 26#define CONNECT_TIMEOUT 10000
 27
 28#include "network/Enums.h"
 29
 30#include "DllMacro.h"
 31#include "Typedefs.h"
 32
 33#include <QHostAddress>
 34#include <QTcpServer>
 35
 36#include <functional>
 37
 38class Connection;
 39class Connector;
 40class ControlConnection;
 41class NetworkReply;
 42class PeerInfo;
 43class PortFwdThread;
 44class ProxyConnection;
 45class QTcpSocketExtra;
 46class RemoteCollectionConnection;
 47class SipInfo;
 48class StreamConnection;
 49
 50class ServentPrivate;
 51
 52class DLLEXPORT Servent : public QTcpServer
 53{
 54Q_OBJECT
 55
 56public:
 57    static Servent* instance();
 58    static bool isValidExternalIP( const QHostAddress& addr );
 59    static SipInfo getSipInfoForOldVersions( const QList<SipInfo> &sipInfos );
 60
 61    explicit Servent( QObject* parent = 0 );
 62    virtual ~Servent();
 63
 64    /**
 65     * Start listening for connections.
 66     *
 67     * @param ha The address to listen on, pass QHostAddress::Any or QHostAddress::AnyIPv6 depending on your Qt version to listen on all interfaces.
 68     * @param upnp If true, try to create a port forward on the next router through UPnP.
 69     * @param port The port we should listen on, if not possible fallback to defaultPort.
 70     * @param mode How the external IP address should be determined.
 71     * @param defaultPort If we cannot listen on the port the user configured try this port as fallback.
 72     * @param autoDetectExternalIp If true, try to automatically detect the external port by querying a remote server.
 73     * @param externalHost manually supplied external hostname (only with mode == Tomahawk::Network::ExternalAddress::Static)
 74     * @param externalPort manually supplied external port (only with mode == Tomahawk::Network::ExternalAddress::Static)
 75     * @return True if we could listen on any of the supplied ports.
 76     */
 77    bool startListening( QHostAddress ha, bool upnp, int port,
 78                         Tomahawk::Network::ExternalAddress::Mode mode,
 79                         int defaultPort, bool autoDetectExternalIp = false,
 80                         const QString& externalHost = QString(),
 81                         int externalPort = -1 );
 82
 83    // creates new token that allows a controlconnection to be set up
 84    QString createConnectionKey( const QString& name = "", const QString &nodeid = "", const QString &key = "", bool onceOnly = true );
 85
 86    void registerOffer( const QString& key, Connection* conn );
 87    void registerLazyOffer( const QString& key, const Tomahawk::peerinfo_ptr& peerInfo, const QString &nodeid , const int timeout );
 88
 89    void registerControlConnection( ControlConnection* conn );
 90    void unregisterControlConnection( ControlConnection* conn );
 91    ControlConnection* lookupControlConnection( const SipInfo& sipInfo );
 92    ControlConnection* lookupControlConnection( const QString& nodeid );
 93
 94    void remoteIODeviceFactory( const Tomahawk::result_ptr& result, const QString& url,
 95                                    std::function< void ( const QString&, QSharedPointer< QIODevice >& ) > callback );
 96
 97    // you may call this method as often as you like for the same peerInfo, dupe checking is done inside
 98    void registerPeer( const Tomahawk::peerinfo_ptr& peerInfo );
 99    void handleSipInfo( const Tomahawk::peerinfo_ptr& peerInfo );
100
101    void initiateConnection( const SipInfo& sipInfo, Connection* conn );
102    void reverseOfferRequest( ControlConnection* orig_conn, const QString &theirdbid, const QString& key, const QString& theirkey );
103
104    bool visibleExternally() const;
105
106    /**
107     * Is the probality that this host supports IPv6 high?
108     *
109     * Though we cannot fully test for IPv6 connectivity, some guesses based on non-localhost addresses are done.
110     */
111    bool ipv6ConnectivityLikely() const;
112
113    /**
114     * The port this Peer listens directly (per default)
115     */
116    int port() const;
117
118    /**
119     * The IP addresses this Peer listens directly (per default)
120     */
121    QList< QHostAddress > addresses() const;
122
123    /**
124     * An additional address this peer listens to, e.g. via UPnP.
125     */
126    QString additionalAddress() const;
127
128    /**
129     * An additional port this peer listens to, e.g. via UPnP (only in combination with additionalAddress.
130     */
131    int additionalPort() const;
132
133    static bool isIPWhitelisted( QHostAddress ip );
134
135    bool connectedToSession( const QString& session );
136    unsigned int numConnectedPeers() const;
137
138    QList< StreamConnection* > streams() const;
139
140    bool isReady() const;
141
142    QList<SipInfo> getLocalSipInfos(const QString& nodeid, const QString &key);
143
144    void queueForAclResult( const QString& username, const QSet<Tomahawk::peerinfo_ptr>& peerInfos );
145signals:
146    void dbSyncTriggered();
147
148    /**
149     * @brief ipDetectionFailed Emitted when the automatic external IP detection failed.
150     * @param error If the failure was caused by a network error, this is its error code.
151     *              If the error wasn't network related, QNetworkReply::NoError will be returned.
152     * @param errorString A string explaining the error.
153     */
154    void ipDetectionFailed( QNetworkReply::NetworkError error, QString errorString );
155    void streamStarted( StreamConnection* );
156    void streamFinished( StreamConnection* );
157    void ready();
158
159protected:
160    void incomingConnection( qintptr sd ) Q_DECL_OVERRIDE;
161
162public slots:
163    void setExternalAddress( QHostAddress ha, unsigned int port );
164
165    void createParallelConnection( Connection* orig_conn, Connection* new_conn, const QString& key );
166
167    void registerStreamConnection( StreamConnection* );
168    void onStreamFinished( StreamConnection* sc );
169
170    void socketConnected();
171    void triggerDBSync();
172
173    void onSipInfoChanged();
174
175private slots:
176    void deleteLazyOffer( const QString& key );
177    void readyRead();
178    void socketError( QAbstractSocket::SocketError e );
179    void checkACLResult( const QString &nodeid, const QString &username, Tomahawk::ACLStatus::Type peerStatus );
180    void ipDetected();
181
182    Connection* claimOffer( ControlConnection* cc, const QString &nodeid, const QString &key, const QHostAddress peer = QHostAddress::Any );
183
184private:
185    Q_DECLARE_PRIVATE( Servent )
186    ServentPrivate* d_ptr;
187
188    void handoverSocket( Connection* conn, QTcpSocketExtra* sock );
189    void cleanupSocket( QTcpSocketExtra* sock );
190    void printCurrentTransfers();
191
192    /**
193     * Remove addresses from the list that we shall not use in Tomahawk (e.g.
194     * for sending to other peers).
195     *
196     * @param addresses The list that shall be cleanded.
197     */
198    void cleanAddresses( QList<QHostAddress>& addresses ) const;
199
200    static Servent* s_instance;
201};
202
203#endif // SERVENT_H