bool HostPool::addNewHost( QString hostname, QString port, QString login, QString pass ) { QMutexLocker locker(&mPoolLock); // make sure this is really a new host if( mHostPool.find( hostname ) != mHostPool.end() ) { return false; } if( hostname.size() < 1 ) return false; int portnumber = 23; if( port.size() >= 1 ) { portnumber = port.toInt(); } int id = createNewHostID(); QSharedPointer<Host> pNewHost( new Host( portnumber, hostname, login, pass, id ) ); mHostPool.insert( hostname, pNewHost ); return true; }
bool HostManager::addHost(QString hostname, QString port, QString login, QString pass) { if (hostname.isEmpty()) { qDebug() << "HostManager::addHost(" << hostname.toUtf8().constData() << ") ERROR: an unnamed Host is not permitted, aborting and returning false!"; return false; } int portnumber = 23; if (!port.isEmpty() && port.toInt() > 0 && port.toInt() < 65536) { portnumber = port.toInt(); } mPoolReadWriteLock.lockForWrite(); // Will block until gets lock // make sure this is really a new host if (mHostPool.contains(hostname)) { mPoolReadWriteLock.unlock(); return false; } // Was an ONLY use of a createNewHostID() method here but that extra // function call was unnecessary and wastes time while we are locking access // to the host pool int id = mHostPool.size() + 1; QSharedPointer<Host> pNewHost(new Host(portnumber, hostname, login, pass, id)); if (Q_UNLIKELY(!pNewHost)) { qDebug() << "HostManager::addHost(" << hostname.toUtf8().constData() << ") ERROR: failed to create new Host for the host pool... releasing lock and aborting, returning false!"; mPoolReadWriteLock.unlock(); return false; } mHostPool.insert(hostname, pNewHost); mPoolReadWriteLock.unlock(); return true; }