From be915d3cfcc639d6303bf8bc0e5ad825e0ef2f95 Mon Sep 17 00:00:00 2001 From: Niklas Date: Wed, 5 Oct 2011 17:39:35 +0200 Subject: modified the check for carrier signal. on autoUp==true: if no interface is in running state, the app will check the state of the interface several times (can be specified by us with the _ifUpCountdown value) --- LogReceiver/networkdiscovery.cpp | 163 ++++++++++++++++++++++++++++++++------- 1 file changed, 133 insertions(+), 30 deletions(-) (limited to 'LogReceiver/networkdiscovery.cpp') diff --git a/LogReceiver/networkdiscovery.cpp b/LogReceiver/networkdiscovery.cpp index ee0b5ad..052663f 100644 --- a/LogReceiver/networkdiscovery.cpp +++ b/LogReceiver/networkdiscovery.cpp @@ -11,14 +11,18 @@ NetworkDiscovery::~NetworkDiscovery() { } -void NetworkDiscovery::initAndRun(QString serverIp, bool userChoice,QString serverPath, QString pathToExe, +void NetworkDiscovery::initAndRun(QString serverIp, bool userChoice, bool autoUp, QString pathToLogFile, QString serverPath, QString pathToExe, QStringList* args) { _serverIp = serverIp; _userChoice = userChoice; - _blocked = false; + _autoUp = autoUp; + _pathToLogFile = pathToLogFile; + _pathToDhcpcdExe = pathToExe; + _blocked = false; _numberOfProcesses = 0; + _ifUpCountdown = 10; if (serverPath != DEFAULT_QTSOCKETADDRESS) { _dhcpcdArguments.append("-q"); @@ -48,6 +52,7 @@ void NetworkDiscovery::initAndRun(QString serverIp, bool userChoice,QString serv } connect(_server, SIGNAL(newConnection()), this, SLOT(handleNewConnection())); + connect(this, SIGNAL(readyForRun()), this, SLOT(slotReadyForRun())); // check if the path to the customdhcpcd file is correct @@ -69,24 +74,90 @@ void NetworkDiscovery::initAndRun(QString serverIp, bool userChoice,QString serv // start the main work: - QList list = getListOfNetworkInterfaces(); + // if autoup enabled + // emit ... up + // else get list uped ifs (einmal simpel ohne autoup) + // emit run - if (list.size() > 0) { + if (_autoUp) { + getListOfNetworkInterfacesWithAutoUp(); + _timer = new QTimer(this); + connect(_timer, SIGNAL(timeout()), this, SLOT(checkForIsRunning())); + _timer->start(1000); - //list = checkCarrierState(list); + } else { + getListOfNetworkInterfaces(); + emit readyForRun(); + } +} - //dhcpcdArguments.append("-d"); - _numberOfProcesses = list.size(); - runDHCPCD(list); +void NetworkDiscovery::slotReadyForRun() { + if (_ifUpList.size() > 0) { + foreach(QString i, _ifUpList) { + emit addInterface(i); + } + _numberOfProcesses = _ifUpList.size(); + runDHCPCD( _ifUpList); } else { qDebug() << "list is empty. Have not found usable interface."; - emit abortBoot("Haven not found usable interface"); + emit + abortBoot("Have not found usable interface"); return; } +} + +void NetworkDiscovery::checkForIsRunning() { + bool isRunning = false; + QList copyOfIfDownList(_ifDownList); + foreach(QString i, _ifDownList) { + QNetworkInterface networkInterface = QNetworkInterface::interfaceFromName(i); + isRunning = (networkInterface.flags() & QNetworkInterface::IsRunning); + if (isRunning) { + _ifUpList.append(i); + _ifDownList.removeAt(_ifDownList.indexOf(i)); + } + } + _ifUpCountdown--; + if ((_ifUpCountdown <= 0 ) || _ifDownList.isEmpty()) { + // shut down timer + _timer->stop(); + emit readyForRun(); + } + /* + for (int ii = 0; ii < copyOfIfDownList.size(); ii++) { + QNetworkInterface networkInterface = + QNetworkInterface::interfaceFromName(_ifDownList.value(ii)); + isRunning = (networkInterface.flags() & QNetworkInterface::IsRunning); + if (isRunning) { + _ifUpList.append(_ifDownList.value(ii)); + _ifDownList.removeAt(ii); + } + } + */ } +// autoup mit qtimer + +/* + if(!(nI.flags() & QNetworkInterface::IsRunning)) { + continue; + } + +// if(!checkForIsRunning(nI)) { +// qDebug() << "--- still down"; +// continue; +// } + /* + if (!checkCarrierState(nI.humanReadableName())) { + continue; + } + */ + + + + int NetworkDiscovery::replaceDefaultRoute(QString &ifName, QString &gateway, int af, int mss) { _networkManager.replaceDefaultRoute(ifName, gateway, mss, AF_INET); } @@ -138,7 +209,27 @@ QString NetworkDiscovery::getGatewayForInterface(QString ifName) { return ifConf->getGateway(); } -QList NetworkDiscovery::getListOfNetworkInterfaces() { +QString NetworkDiscovery::readLogFile() { + // path to log file is in _pathToLogFile. initialized in initAndRun(). + QString retval("the log file"); + QFile logFile(_pathToLogFile); + if (logFile.exists()) { + if (logFile.open(QIODevice::ReadOnly | QIODevice::Text)); + return retval; + } + while (!logFile.atEnd()) { + retval.append(logFile.readLine()); + } + return retval; +} + +/** + * ================================================================================ + ********************************* Private Methods ******************************** + * ================================================================================ + **/ + +QList NetworkDiscovery::getListOfNetworkInterfacesWithAutoUp() { QList nIList = QNetworkInterface::allInterfaces(); QList result; @@ -151,19 +242,17 @@ QList NetworkDiscovery::getListOfNetworkInterfaces() { || checkBlackList(nI.humanReadableName())) { continue; } - if(!(nI.flags() & QNetworkInterface::IsUp)) { - _networkManager.bringInterfaceUP(nI.humanReadableName()); - } - /* - * if(!checkForIsRunning(nI)) { - continue; + if ((nI.flags() & QNetworkInterface::IsRunning)) { + _ifUpList.append(nI.humanReadableName()); + _ifMap.insert(nI.humanReadableName(), true); } - */ - if (!checkCarrierState(nI.humanReadableName())) { - continue; + else if (!(nI.flags() & QNetworkInterface::IsUp)) { + _networkManager.bringInterfaceUP(nI.humanReadableName()); + qDebug() << "--- bring up .."; + _ifDownList.append(nI.humanReadableName()); + _ifMap.insert(nI.humanReadableName(), false); } result.append(nI.humanReadableName()); - emit addInterface(nI.humanReadableName()); } } else { qDebug() << "no interfaces found!"; @@ -171,18 +260,32 @@ QList NetworkDiscovery::getListOfNetworkInterfaces() { return result; } -bool NetworkDiscovery::checkForIsRunning(QNetworkInterface networkInterface) { - bool retval = false; - for (int i = 0; i < 4; i++) { - retval = (networkInterface.flags() & QNetworkInterface::IsRunning); - if(retval) { - return retval; - } - sleep(1); +QList NetworkDiscovery::getListOfNetworkInterfaces() { + QList nIList = QNetworkInterface::allInterfaces(); + QList result; + + if (nIList.size() > 0) { + foreach(QNetworkInterface nI, nIList) + { + if (((!(nI.flags() & QNetworkInterface::CanBroadcast) + || nI.flags() & QNetworkInterface::IsLoopBack) + || nI.flags() & QNetworkInterface::IsPointToPoint) + || !(nI.flags() & QNetworkInterface::IsUp) + || !(nI.flags() & QNetworkInterface::IsRunning) + || checkBlackList(nI.humanReadableName())) { + continue; + } + _ifUpList.append(nI.humanReadableName()); + _ifMap.insert(nI.humanReadableName(), true); + result.append(nI.humanReadableName()); + } + } else { + qDebug() << "no interfaces found!"; } - return retval; + return result; } + QList NetworkDiscovery::checkCarrierState(QList &interfaces) { QList result; foreach(QString nI, interfaces) @@ -264,7 +367,7 @@ bool NetworkDiscovery::checkConnectivity(QString ifName) { int metric = 0; // get gateway address - QString pathToGatewayFile(DEFAULT_GATEWAY_INFO_LOCATION); + QString pathToGatewayFile(DEFAULT_INTERFACE_CONF_LOCATION); pathToGatewayFile += ifName; interfaceconfiguration *ifConf = new interfaceconfiguration(); ifConf->readConfigOutOfFile(pathToGatewayFile); -- cgit v1.2.3-55-g7522