Changeset 12 for psi/trunk

Show
Ignore:
Timestamp:
09/05/06 01:09:16 (2 years ago)
Author:
dmik
Message:

CuteStuff/Network?: Added alternative implementation of the NDns class using the QDns functionality (necessary because NO_NDNS macro doesn't work properly but on some platforms it's better to use the standard QDns class for DNS resolving). The MAP_NDNS_TO_QDNS C++ macro must be defined to enable this implementation.

Location:
psi/trunk/cutestuff/network
Files:
2 modified

Legend:

Unmodified
Added
Removed
  • psi/trunk/cutestuff/network/ndns.cpp

    r2 r12  
    4848//! QString ip_address = dns.resultString(); 
    4949//! \endcode 
     50//! 
     51//! \note The MAP_NDNS_TO_QDNS macro can be used to use QDns to implement the 
     52//! NDns functionality. This is a but stupid, because it is supposed that if 
     53//! NO_NDNS macro is defined, QDns is directly used instead of NDns in the 
     54//! relevant sources. However, I (dmik) couln't get the DNS resolver working 
     55//! when NO_NDNS is defined and was too lazy to debug why. The reason for using 
     56//! QDns instead of NDns is pretty simple: it's impossible to cancel the async 
     57//! gethostbyname() request executed on a worker thread (for example, during 
     58//! application termination). As a result, in case of a lost connection (or 
     59//! whatever), Psi won't close until the gethostbyname() timeout expires which 
     60//! can take quite a while on OS/2. On the other hand, QDns is fully asynchronus 
     61//! there, so NDns is absolutely not necessary. 
    5062 
    5163#include"ndns.h" 
     
    5668#include<qeventloop.h> 
    5769 
    58 #ifdef Q_OS_UNIX 
     70#if !defined(MAP_NDNS_TO_QDNS) 
     71 
     72#if defined(Q_OS_UNIX) || defined(Q_OS_OS2) 
    5973#include<netdb.h> 
    6074#include<sys/types.h> 
     
    6781#endif 
    6882 
     83#endif 
     84 
    6985// CS_NAMESPACE_BEGIN 
     86 
     87#if defined(MAP_NDNS_TO_QDNS) 
     88 
     89// dummy methods necessary because MOC doesn't understand #ifdefs and such 
     90NDnsManager::NDnsManager() {} 
     91NDnsManager::~NDnsManager() {} 
     92bool NDnsManager::event(QEvent *e) { return false; } 
     93void NDnsManager::app_aboutToQuit() {} 
     94 
     95#else // !MAP_NDNS_TO_QDNS 
    7096 
    7197//! \if _hide_doc_ 
     
    248274} 
    249275 
     276#endif // !MAP_NDNS_TO_QDNS 
     277 
    250278 
    251279//---------------------------------------------------------------------------- 
     
    261289:QObject(parent) 
    262290{ 
     291#if defined(MAP_NDNS_TO_QDNS) 
     292        connect(&qdns, SIGNAL(resultsReady()), this, SIGNAL(resultsReady()));  
     293#endif   
    263294} 
    264295 
     
    267298NDns::~NDns() 
    268299{ 
     300#if !defined(MAP_NDNS_TO_QDNS) 
    269301        stop(); 
     302#endif   
    270303} 
    271304 
     
    274307void NDns::resolve(const QString &host) 
    275308{ 
     309#if defined(MAP_NDNS_TO_QDNS) 
     310        qdns.setLabel(host); 
     311        if (qdns.recordType() != QDns::A)  
     312                qdns.setRecordType(QDns::A); 
     313#else 
    276314        stop(); 
    277315        if(!man) 
    278316                man = new NDnsManager; 
    279317        man->resolve(this, host); 
     318#endif   
    280319} 
    281320 
     
    285324void NDns::stop() 
    286325{ 
     326#if defined(MAP_NDNS_TO_QDNS) 
     327        qdns.setRecordType(QDns::None); 
     328#else 
    287329        if(man) 
    288330                man->stop(this); 
     331#endif   
    289332} 
    290333 
     
    294337uint NDns::result() const 
    295338{ 
     339#if defined(MAP_NDNS_TO_QDNS) 
     340        QHostAddress addr; 
     341        QValueList<QHostAddress> list = qdns.addresses(); 
     342        if (!list.isEmpty()) 
     343                addr = list.first(); 
    296344        return addr.ip4Addr(); 
     345#else 
     346        return addr.ip4Addr(); 
     347#endif   
    297348} 
    298349 
     
    302353QString NDns::resultString() const 
    303354{ 
     355#if defined(MAP_NDNS_TO_QDNS) 
     356        QHostAddress addr; 
     357        QValueList<QHostAddress> list = qdns.addresses(); 
     358        if (!list.isEmpty()) 
     359                addr = list.first(); 
    304360        return addr.toString(); 
     361#else 
     362        return addr.toString(); 
     363#endif   
    305364} 
    306365 
     
    309368bool NDns::isBusy() const 
    310369{ 
     370#if defined(MAP_NDNS_TO_QDNS) 
     371        return qdns.isWorking(); 
     372#else 
    311373        if(!man) 
    312374                return false; 
    313375        return man->isBusy(this); 
    314 } 
     376#endif   
     377} 
     378 
     379#if !defined(MAP_NDNS_TO_QDNS) 
    315380 
    316381void NDns::finished(const QHostAddress &a) 
     
    374439} 
    375440 
     441#endif // !MAP_NDNS_TO_QDNS 
     442 
    376443// CS_NAMESPACE_END 
  • psi/trunk/cutestuff/network/ndns.h

    r2 r12  
    2828#include<qhostaddress.h> 
    2929 
     30#if defined(MAP_NDNS_TO_QDNS) 
     31#include <qdns.h> 
     32#endif 
     33 
    3034// CS_NAMESPACE_BEGIN 
    3135 
     36#if !defined(MAP_NDNS_TO_QDNS) 
    3237class NDnsWorker; 
    3338class NDnsManager; 
     39#endif 
    3440 
    3541class NDns : public QObject 
     
    5157 
    5258private: 
     59#if defined(MAP_NDNS_TO_QDNS) 
     60        QDns qdns; 
     61#else 
    5362        QHostAddress addr; 
    5463 
    5564        friend class NDnsManager; 
    5665        void finished(const QHostAddress &); 
     66#endif   
    5767}; 
    5868