KDECore
k3resolverworkerbase.h
Go to the documentation of this file.00001 /* -*- C++ -*- 00002 * Copyright (C) 2003-2005 Thiago Macieira <thiago@kde.org> 00003 * 00004 * 00005 * Permission is hereby granted, free of charge, to any person obtaining 00006 * a copy of this software and associated documentation files (the 00007 * "Software"), to deal in the Software without restriction, including 00008 * without limitation the rights to use, copy, modify, merge, publish, 00009 * distribute, sublicense, and/or sell copies of the Software, and to 00010 * permit persons to whom the Software is furnished to do so, subject to 00011 * the following conditions: 00012 * 00013 * The above copyright notice and this permission notice shall be included 00014 * in all copies or substantial portions of the Software. 00015 * 00016 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 00017 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 00018 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 00019 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 00020 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 00021 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 00022 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 00023 */ 00024 00025 #ifndef KRESOLVERWORKERBASE_H 00026 #define KRESOLVERWORKERBASE_H 00027 00028 #include "k3resolver.h" 00029 00030 // forward declarations 00031 class QString; 00032 template <class T> class QValueList; 00033 00034 namespace KNetwork { 00035 00036 namespace Internal 00037 { 00038 class KResolverThread; 00039 struct InputData; 00040 } 00041 00064 class KResolverWorkerBase 00065 { 00066 public: 00067 00074 class ResolverLocker 00075 { 00076 public: 00080 ResolverLocker(KResolverWorkerBase* parent) 00081 : parent( parent ) 00082 { 00083 parent->acquireResolver(); 00084 } 00085 00089 ~ResolverLocker() 00090 { 00091 parent->releaseResolver(); 00092 } 00093 00099 void openClose() 00100 { 00101 parent->releaseResolver(); 00102 parent->acquireResolver(); 00103 } 00104 00105 private: 00107 KResolverWorkerBase* parent; 00108 }; 00109 private: 00110 // this will be like our d pointer 00111 KNetwork::Internal::KResolverThread *th; 00112 const KNetwork::Internal::InputData *input; 00113 friend class KNetwork::Internal::KResolverThread; 00114 friend class KNetwork::Internal::KResolverManager; 00115 friend class KResolverWorkerBase::ResolverLocker; 00116 00117 int m_finished : 1; 00118 int m_reserved : 31; // reserved 00119 00120 public: 00128 KResolverResults results; 00129 00130 public: 00131 // default constructor 00132 KResolverWorkerBase(); 00133 00134 // virtual destructor 00135 virtual ~KResolverWorkerBase(); 00136 00140 QString nodeName() const; 00141 00145 QString serviceName() const; 00146 00150 int flags() const; 00151 00155 int familyMask() const; 00156 00160 int socketType() const; 00161 00165 int protocol() const; 00166 00170 QByteArray protocolName() const; 00171 00178 void finished(); 00179 00180 protected: 00181 // like a QThread 00194 virtual bool run() = 0; 00195 00215 virtual bool preprocess() = 0; 00216 00225 virtual bool postprocess(); 00226 00230 inline void setError(int errorcode, int syserror = 0) 00231 { results.setError(errorcode, syserror); } 00232 00248 bool enqueue(KResolver* other); 00249 00253 bool enqueue(KResolverWorkerBase* worker); 00254 00261 bool checkResolver(); 00262 00272 void acquireResolver(); 00273 00278 void releaseResolver(); 00279 00280 }; 00281 00291 class KResolverWorkerFactoryBase 00292 { 00293 public: 00294 virtual ~KResolverWorkerFactoryBase() {} 00295 virtual KResolverWorkerBase* create() const = 0; 00301 static void registerNewWorker(KResolverWorkerFactoryBase* factory); 00302 }; 00303 00313 template<class Worker> 00314 class KResolverWorkerFactory: public KResolverWorkerFactoryBase 00315 { 00316 public: 00317 virtual KResolverWorkerBase* create() const 00318 { return new Worker; } 00319 }; 00320 00321 } // namespace KNetwork 00322 00323 #endif