• Skip to content
  • Skip to link menu
  • KDE API Reference
  • kdelibs-4.14.38 API Reference
  • KDE Home
  • Contact Us
 

KDECore

  • kdecore
  • network
k3resolver_p.h
Go to the documentation of this file.
1/* -*- C++ -*-
2 * Copyright (C) 2003-2005 Thiago Macieira <thiago@kde.org>
3 *
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining
6 * a copy of this software and associated documentation files (the
7 * "Software"), to deal in the Software without restriction, including
8 * without limitation the rights to use, copy, modify, merge, publish,
9 * distribute, sublicense, and/or sell copies of the Software, and to
10 * permit persons to whom the Software is furnished to do so, subject to
11 * the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included
14 * in all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 */
24
25#ifndef KRESOLVER_P_H
26#define KRESOLVER_P_H
27
28#include <config.h>
29#include <config-network.h>
30#include <sys/types.h>
31
32
33#include <QByteArray>
34#include <QList>
35#include <QThread>
36#include <QMutex>
37#include <QWaitCondition>
38#include <QSemaphore>
39#include <QEvent>
40
41#include "k3resolver.h"
42
43/* decide whether we need a mutex */
44#if !defined(HAVE_GETPROTOBYNAME_R) || !defined(HAVE_GETSERVBYNAME_R) || !defined(HAVE_GETHOSTBYNAME_R) || !defined(HAVE_GETSERVBYPORT_R)
45# define NEED_MUTEX
46extern QMutex getXXbyYYmutex;
47#endif
48
49/* some systems have the functions, but don't declare them */
50#if defined(HAVE_GETSERVBYNAME_R) && !HAVE_GETSERVBYNAME_R_PROTO
51extern "C" {
52 struct servent;
53 extern int getservbyname_r(const char* serv, const char* proto,
54 struct servent* servbuf,
55 char* buf, size_t buflen,
56 struct servent** result);
57 extern int getservbyport_r(int port, const char* proto,
58 struct servent* servbuf,
59 char* buf, size_t buflen,
60 struct servent** result);
61
62 struct protoent;
63 extern int getprotobyname_r(const char* proto, struct protoent* protobuf,
64 char *buf, size_t buflen,
65 struct protoent** result);
66 extern int getprotobynumber_r(int proto, struct protoent* protobuf,
67 char *buf, size_t buflen,
68 struct protoent** result);
69}
70#endif
71
72/* decide whether res_init is thread-safe or not */
73#if defined(__GLIBC__)
74# undef RES_INIT_THREADSAFE
75#endif
76
77namespace KNetwork
78{
79 // defined in network/qresolverworkerbase.h
80 class KResolverWorkerBase;
81 class KResolverWorkerFactoryBase;
82 class KResolverPrivate;
83
84 namespace Internal
85 {
86 class KResolverManager;
87 class KResolverThread;
88 struct RequestData;
89
90 struct InputData
91 {
92 QString node, service;
93 QByteArray protocolName;
94 int flags;
95 int familyMask;
96 int socktype;
97 int protocol;
98 };
99 }
100
101 class KResolverPrivate
102 {
103 public:
104 // parent class. Should never be changed!
105 KResolver* parent;
106 bool deleteWhenDone : 1;
107 bool waiting : 1;
108
109 // class status. Should not be changed by worker threads!
110 volatile int status;
111 volatile int errorcode, syserror;
112
113 // input data. Should not be changed by worker threads!
114 Internal::InputData input;
115
116 // mutex
117 QMutex mutex;
118
119 // output data
120 KResolverResults results;
121
122 explicit KResolverPrivate(KResolver* _parent,
123 const QString& _node = QString(),
124 const QString& _service = QString())
125 : parent(_parent), deleteWhenDone(false), waiting(false),
126 status(0), errorcode(0), syserror(0)
127 {
128 input.node = _node;
129 input.service = _service;
130 input.flags = 0;
131 input.familyMask = KResolver::AnyFamily;
132 input.socktype = 0;
133 input.protocol = 0;
134
135 results.setAddress(_node, _service);
136 }
137 };
138
139 namespace Internal
140 {
141 struct RequestData
142 {
143 // worker threads should not change values in the input data
144 KNetwork::KResolverPrivate *obj;
145 const KNetwork::Internal::InputData *input;
146 KNetwork::KResolverWorkerBase *worker; // worker class
147 RequestData *requestor; // class that requested us
148
149 volatile int nRequests; // how many requests that we made we still have left
150 };
151
152 /*
153 * @internal
154 * This class is the resolver manager
155 */
156 class KResolverManager
157 {
158 public:
159 enum EventTypes
160 { ResolutionCompleted = 1576 }; // arbitrary value;
161
162 /*
163 * This wait condition is used to notify wait states (KResolver::wait) that
164 * the resolver manager has finished processing one or more objects. All
165 * objects in wait state will be woken up and will check if they are done.
166 * If they aren't, they will go back to sleeping.
167 */
168 QWaitCondition notifyWaiters;
169
170 private:
171 /*
172 * This variable is used to count the number of threads that are running
173 */
174 volatile unsigned short runningThreads;
175
176 /*
177 * This variable is used to count the number of threads that are currently
178 * waiting for data.
179 */
180 unsigned short availableThreads;
181
182 /*
183 * This wait condition is used to notify worker threads that there is new
184 * data available that has to be processed. All worker threads wait on this
185 * waitcond for a limited amount of time.
186 */
187 QWaitCondition feedWorkers;
188
189 // this mutex protects the data in this object
190 QMutex mutex;
191
192 // hold a list of all the current threads we have
193 QList<KResolverThread*> workers;
194
195 // hold a list of all the new requests we have
196 QList<RequestData*> newRequests;
197
198 // hold a list of all the requests in progress we have
199 QList<RequestData*> currentRequests;
200
201 // hold a list of all the workers we have
202 QList<KNetwork::KResolverWorkerFactoryBase*> workerFactories;
203
204 // private constructor
205 KResolverManager();
206
207 public:
208 static KResolverManager* manager() KDE_NO_EXPORT; // creates and returns the global manager
209
210 // destructor
211 ~KResolverManager();
212
213 /*
214 * Register this thread in the pool
215 */
216 void registerThread(KResolverThread* id);
217
218 /*
219 * Unregister this thread from the pool
220 */
221 void unregisterThread(KResolverThread* id);
222
223 /*
224 * Requests new data to work on.
225 *
226 * This function should only be called from a worker thread. This function
227 * is thread-safe.
228 *
229 * If there is data to be worked on, this function will return it. If there is
230 * none, this function will return a null pointer.
231 */
232 RequestData* requestData(KResolverThread* id, int maxWaitTime);
233
234 /*
235 * Releases the resources and returns the resolved data.
236 *
237 * This function should only be called from a worker thread. It is
238 * thread-safe. It does not post the event to the manager.
239 */
240 void releaseData(KResolverThread *id, RequestData* data);
241
242 /*
243 * Registers a new worker class by way of its factory.
244 *
245 * This function is NOT thread-safe.
246 */
247 void registerNewWorker(KNetwork::KResolverWorkerFactoryBase *factory);
248
249 /*
250 * Enqueues new resolutions.
251 */
252 void enqueue(KNetwork::KResolver *obj, RequestData* requestor);
253
254 /*
255 * Dispatch a new request
256 */
257 void dispatch(RequestData* data);
258
259 /*
260 * Dequeues a resolution.
261 */
262 void dequeue(KNetwork::KResolver *obj);
263
264 /*
265 * Notifies the manager that the given resolution is about to
266 * be deleted. This function should only be called by the
267 * KResolver destructor.
268 */
269 void aboutToBeDeleted(KNetwork::KResolver *obj);
270
271 /*
272 * Notifies the manager that new events are ready.
273 */
274 void newEvent();
275
276 /*
277 * This function is called by the manager to receive a new event. It operates
278 * on the eventSemaphore() semaphore, which means it will block till there
279 * is at least one event to go.
280 */
281 void receiveEvent();
282
283 private:
284 /*
285 * finds a suitable worker for this request
286 */
287 KNetwork::KResolverWorkerBase *findWorker(KNetwork::KResolverPrivate *p);
288
289 /*
290 * finds data for this request
291 */
292 RequestData* findData(KResolverThread*);
293
294 /*
295 * Handle completed requests.
296 *
297 * This function is called by releaseData above
298 */
299 void handleFinished();
300
301 /*
302 * Handle one completed request.
303 *
304 * This function is called by handleFinished above.
305 */
306 bool handleFinishedItem(RequestData* item);
307
308 /*
309 * Notifies the parent class that this request is done.
310 *
311 * This function deletes the request
312 */
313 void doNotifying(RequestData *p);
314
315 /*
316 * Dequeues and notifies an object that is in Queued state
317 * Returns true if the object is no longer queued; false if it could not
318 * be dequeued (i.e., it's running)
319 */
320 bool dequeueNew(KNetwork::KResolver* obj);
321 };
322
323 /*
324 * @internal
325 * This class is a worker thread in the resolver system.
326 * This class must be thread-safe.
327 */
328 class KResolverThread: public QThread
329 {
330 private:
331 // private constructor. Only the manager can create worker threads
332 KResolverThread();
333 RequestData* data;
334
335 protected:
336 virtual void run(); // here the thread starts
337
338 friend class KNetwork::Internal::KResolverManager;
339 friend class KNetwork::KResolverWorkerBase;
340
341 public:
342 bool checkResolver(); // see KResolverWorkerBase::checkResolver
343 void acquireResolver(); // see KResolverWorkerBase::acquireResolver
344 void releaseResolver(); // see KResolverWorkerBase::releaseResolver
345 };
346
347 } // namespace Internal
348
349} // namespace KNetwork
350
351
352#endif
KNetwork::Internal::KResolverManager
Definition: k3resolver_p.h:157
KNetwork::Internal::KResolverManager::registerThread
void registerThread(KResolverThread *id)
Definition: k3resolvermanager.cpp:343
KNetwork::Internal::KResolverManager::manager
static KResolverManager * manager() KDE_NO_EXPORT
Definition: k3resolvermanager.cpp:318
KNetwork::Internal::KResolverManager::unregisterThread
void unregisterThread(KResolverThread *id)
Definition: k3resolvermanager.cpp:347
KNetwork::Internal::KResolverManager::notifyWaiters
QWaitCondition notifyWaiters
Definition: k3resolver_p.h:168
KNetwork::Internal::KResolverManager::aboutToBeDeleted
void aboutToBeDeleted(KNetwork::KResolver *obj)
KNetwork::Internal::KResolverManager::releaseData
void releaseData(KResolverThread *id, RequestData *data)
Definition: k3resolvermanager.cpp:409
KNetwork::Internal::KResolverManager::newEvent
void newEvent()
KNetwork::Internal::KResolverManager::dispatch
void dispatch(RequestData *data)
Definition: k3resolvermanager.cpp:692
KNetwork::Internal::KResolverManager::enqueue
void enqueue(KNetwork::KResolver *obj, RequestData *requestor)
Definition: k3resolvermanager.cpp:650
KNetwork::Internal::KResolverManager::dequeue
void dequeue(KNetwork::KResolver *obj)
Definition: k3resolvermanager.cpp:827
KNetwork::Internal::KResolverManager::registerNewWorker
void registerNewWorker(KNetwork::KResolverWorkerFactoryBase *factory)
Definition: k3resolvermanager.cpp:506
KNetwork::Internal::KResolverManager::requestData
RequestData * requestData(KResolverThread *id, int maxWaitTime)
Definition: k3resolvermanager.cpp:353
KNetwork::Internal::KResolverManager::EventTypes
EventTypes
Definition: k3resolver_p.h:160
KNetwork::Internal::KResolverManager::ResolutionCompleted
@ ResolutionCompleted
Definition: k3resolver_p.h:160
KNetwork::Internal::KResolverManager::receiveEvent
void receiveEvent()
KNetwork::Internal::KResolverThread
Definition: k3resolver_p.h:329
KNetwork::KResolverPrivate
Definition: k3resolver_p.h:102
KNetwork::KResolverPrivate::status
volatile int status
Definition: k3resolver_p.h:110
KNetwork::KResolverPrivate::syserror
volatile int syserror
Definition: k3resolver_p.h:111
KNetwork::KResolverPrivate::parent
KResolver * parent
Definition: k3resolver_p.h:105
KNetwork::KResolverPrivate::results
KResolverResults results
Definition: k3resolver_p.h:120
KNetwork::KResolverPrivate::errorcode
volatile int errorcode
Definition: k3resolver_p.h:111
KNetwork::KResolverPrivate::deleteWhenDone
bool deleteWhenDone
Definition: k3resolver_p.h:106
KNetwork::KResolverPrivate::waiting
bool waiting
Definition: k3resolver_p.h:107
KNetwork::KResolverPrivate::KResolverPrivate
KResolverPrivate(KResolver *_parent, const QString &_node=QString(), const QString &_service=QString())
Definition: k3resolver_p.h:122
KNetwork::KResolverPrivate::input
Internal::InputData input
Definition: k3resolver_p.h:114
KNetwork::KResolverPrivate::mutex
QMutex mutex
Definition: k3resolver_p.h:117
KNetwork::KResolverResults
Name and service resolution results.
Definition: k3resolver.h:213
KNetwork::KResolverResults::setAddress
void setAddress(const QString &host, const QString &service)
Sets the new nodename and service name.
Definition: k3resolver.cpp:260
KNetwork::KResolverWorkerBase
Definition: k3resolverworkerbase.h:65
KNetwork::KResolverWorkerFactoryBase
Definition: k3resolverworkerbase.h:292
KNetwork::KResolver
Name and service resolution class.
Definition: k3resolver.h:313
KNetwork::KResolver::AnyFamily
@ AnyFamily
Definition: k3resolver.h:347
QList
Definition: kaboutdata.h:33
QString
QThread
k3resolver.h
getXXbyYYmutex
QMutex getXXbyYYmutex
Definition: k3resolver.cpp:64
KNetwork
A namespace to store all networking-related (socket) classes.
Definition: k3bufferedsocket.h:35
KNetwork::Internal::InputData
Definition: k3resolver_p.h:91
KNetwork::Internal::InputData::flags
int flags
Definition: k3resolver_p.h:94
KNetwork::Internal::InputData::socktype
int socktype
Definition: k3resolver_p.h:96
KNetwork::Internal::InputData::service
QString service
Definition: k3resolver_p.h:92
KNetwork::Internal::InputData::node
QString node
Definition: k3resolver_p.h:92
KNetwork::Internal::InputData::familyMask
int familyMask
Definition: k3resolver_p.h:95
KNetwork::Internal::InputData::protocol
int protocol
Definition: k3resolver_p.h:97
KNetwork::Internal::InputData::protocolName
QByteArray protocolName
Definition: k3resolver_p.h:93
KNetwork::Internal::RequestData
Definition: k3resolver_p.h:142
KNetwork::Internal::RequestData::obj
KNetwork::KResolverPrivate * obj
Definition: k3resolver_p.h:144
KNetwork::Internal::RequestData::input
const KNetwork::Internal::InputData * input
Definition: k3resolver_p.h:145
KNetwork::Internal::RequestData::requestor
RequestData * requestor
Definition: k3resolver_p.h:147
KNetwork::Internal::RequestData::nRequests
volatile int nRequests
Definition: k3resolver_p.h:149
KNetwork::Internal::RequestData::worker
KNetwork::KResolverWorkerBase * worker
Definition: k3resolver_p.h:146
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Mon Feb 20 2023 00:00:00 by doxygen 1.9.6 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

KDECore

Skip menu "KDECore"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Modules
  • Related Pages

kdelibs-4.14.38 API Reference

Skip menu "kdelibs-4.14.38 API Reference"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDEWebKit
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver
Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal