00001 #include "httpconnectionhandlerpool.h"
00002
00003 HttpConnectionHandlerPool::HttpConnectionHandlerPool(QSettings* settings, HttpRequestHandler* requestHandler)
00004 : QObject()
00005 {
00006 Q_ASSERT(settings!=0);
00007 this->settings=settings;
00008 this->requestHandler=requestHandler;
00009 cleanupTimer.start(settings->value("cleanupInterval",1000).toInt());
00010 connect(&cleanupTimer, SIGNAL(timeout()), SLOT(cleanup()));
00011 }
00012
00013
00014 HttpConnectionHandlerPool::~HttpConnectionHandlerPool() {
00015 foreach(HttpConnectionHandler* handler, pool) {
00016 delete handler;
00017 }
00018 }
00019
00020
00021 HttpConnectionHandler* HttpConnectionHandlerPool::getConnectionHandler() {
00022 HttpConnectionHandler* freeHandler=0;
00023 foreach(HttpConnectionHandler* handler, pool) {
00024 if (!handler->isBusy()) {
00025 freeHandler=handler;
00026 }
00027 }
00028 if (!freeHandler) {
00029 int maxConnectionHandlers=settings->value("maxThreads",10).toInt();
00030 if (pool.count()<maxConnectionHandlers) {
00031 freeHandler=new HttpConnectionHandler(settings,requestHandler);
00032 pool.append(freeHandler);
00033 }
00034 }
00035 if (freeHandler) freeHandler->busy = true;
00036 return freeHandler;
00037 }
00038
00039
00040
00041 void HttpConnectionHandlerPool::cleanup() {
00042 int maxIdleHandlers=settings->value("minThreads",1).toInt();
00043 int idleCounter=0;
00044 foreach(HttpConnectionHandler* handler, pool) {
00045 if (!handler->isBusy()) {
00046 if (++idleCounter > maxIdleHandlers) {
00047 pool.removeOne(handler);
00048 qDebug("HttpConnectionHandlerPool: Removed connection handler (%p), pool size is now %i",handler,pool.size());
00049 connect(handler,SIGNAL(finished()),handler,SLOT(deleteLater()));
00050 handler->quit();
00051 break;
00052 }
00053 }
00054 }
00055 }