Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[connectionagent] Rework service tracking
QConnectionAgent has both a hash map and an ordered list for keeping
track of services, and these need to be kept in sync for proper
operation. Since there isn't much benefit from the hash -- for the
most part, its keys are iterated and all related values accessed --
reworked the code so that there is only one container, an ordered list
that contains service paths and pointers to service objects.
  • Loading branch information
Hannu Mallat committed Oct 1, 2014
1 parent 5687abd commit ad7bc90
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 81 deletions.
127 changes: 50 additions & 77 deletions connd/qconnectionagent.cpp
Expand Up @@ -170,9 +170,9 @@ void QConnectionAgent::onConnectionRequest()
sendConnectReply("Suppress", 15);
qDebug() << flightModeSuppression;
bool okToRequest = true;
Q_FOREACH (const QString &path, servicesMap.keys()) {
qDebug() << "checking" <<servicesMap.value(path)->name() << servicesMap.value(path)->autoConnect();
if (servicesMap.value(path)->autoConnect()) {
Q_FOREACH (Service elem, orderedServicesList) {
qDebug() << "checking" << elem.service->name() << elem.service->autoConnect();
if (elem.service->autoConnect()) {
okToRequest = false;
break;
}
Expand All @@ -195,25 +195,29 @@ void QConnectionAgent::sendUserReply(const QVariantMap &input)

void QConnectionAgent::servicesListChanged(const QStringList &list)
{
bool ok = false;
Q_FOREACH(const QString &path,list) {
if (orderedServicesList.indexOf((path)) == -1) {
//added
bool changed = false;

Q_FOREACH(const QString &path, list) {
if (orderedServicesList.indexOf(path) == -1) {
//added
qDebug() << Q_FUNC_INFO << "added" << path;
ok = true;
changed = true;
break;
}
}
if (ok)
updateServicesMap();

Q_FOREACH(const QString &path,orderedServicesList) {
if (list.indexOf((path)) == -1) {
qDebug() << Q_FUNC_INFO << "removed" << path;
serviceRemoved(path);
ok = true;
//removed
if (!changed)
Q_FOREACH (Service elem, orderedServicesList) {
if (list.indexOf(elem.path) == -1) {
//removed
qDebug() << Q_FUNC_INFO << "removed" << elem.path;
changed = true;
break;
}
}
}

if (changed)
updateServices();
}

void QConnectionAgent::serviceErrorChanged(const QString &error)
Expand Down Expand Up @@ -285,7 +289,7 @@ void QConnectionAgent::serviceStateChanged(const QString &state)
netman->getTechnology(service->type())->setTethering(true);
}
} else {
updateServicesMap();
updateServices();
}
currentNetworkState = state;
QSettings confFile;
Expand All @@ -304,12 +308,12 @@ void QConnectionAgent::connectToType(const QString &type)
return;
}

Q_FOREACH (const QString &path, servicesMap.keys()) {
if (path.contains(type)) {
if (!isStateOnline(servicesMap.value(path)->state())) {
if (servicesMap.value(path)->autoConnect()) {
Q_FOREACH (Service elem, orderedServicesList) {
if (elem.path.contains(type)) {
if (!isStateOnline(elem.service->state())) {
if (elem.service->autoConnect()) {
qDebug() << "<<<<<<<<<<< requestConnect() >>>>>>>>>>>>";
servicesMap.value(path)->requestConnect();
elem.service->requestConnect();
return;
}
} else {
Expand All @@ -326,12 +330,11 @@ void QConnectionAgent::onScanFinished()
qDebug() << "<<<<<<<<<<<<<<<<<<<<<<<<<<<<>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>";
}

void QConnectionAgent::updateServicesMap()
void QConnectionAgent::updateServices()
{
qDebug() << Q_FUNC_INFO;
QStringList oldServices = orderedServicesList;
ServiceList oldServices = orderedServicesList;
orderedServicesList.clear();
servicesMap.clear();

Q_FOREACH (const QString &tech,techPreferenceList) {
QVector<NetworkService*> services = netman->getServices(tech);
Expand All @@ -341,8 +344,10 @@ void QConnectionAgent::updateServicesMap()

qDebug() << "known service:" << serv->name() << serv->strength();

servicesMap.insert(servicePath, serv);
orderedServicesList << servicePath;
Service elem;
elem.path = servicePath;
elem.service = serv;
orderedServicesList << elem;

if (!oldServices.contains(servicePath)) {
//new!
Expand Down Expand Up @@ -429,24 +434,14 @@ void QConnectionAgent::connmanAvailabilityChanged(bool b)
setup();
currentNetworkState = netman->state();
} else {
servicesMap.clear();
currentNetworkState = "error";
}
}

void QConnectionAgent::serviceAdded(const QString &srv)
{
qDebug() << Q_FUNC_INFO << "<<<<"<< srv;
updateServicesMap();
}

void QConnectionAgent::serviceRemoved(const QString &srv)
{
qDebug() << Q_FUNC_INFO << "<<<<" << srv;
if (orderedServicesList.contains(srv)) {
orderedServicesList.removeOne(srv);
servicesMap.remove(srv);
}
updateServices();
}

void QConnectionAgent::setup()
Expand All @@ -473,7 +468,7 @@ void QConnectionAgent::setup()
connect(ua,SIGNAL(browserRequested(QString,QString)),
this,SLOT(browserRequest(QString,QString)), Qt::UniqueConnection);

updateServicesMap();
updateServices();
offlineModeChanged(netman->offlineMode());

QSettings confFile;
Expand Down Expand Up @@ -513,7 +508,8 @@ void QConnectionAgent::technologyPowerChanged(bool powered)
QString bestService = findBestConnectableService();
if (!bestService.isEmpty()) {
qDebug() << "<<<<<<<<<<< requestConnect() >>>>>>>>>>>>";
servicesMap.value(bestService)->requestConnect();
int pos = orderedServicesList.indexOf(bestService);
orderedServicesList.at(pos).service->requestConnect();
}
}
}
Expand Down Expand Up @@ -631,8 +627,8 @@ void QConnectionAgent::serviceAutoconnectChanged(bool on)
QString selectedServicePath = findBestConnectableService();
if (!selectedServicePath.isEmpty()) {
qDebug() << "<<<<<<<<<<< requestConnect() >>>>>>>>>>>>";

servicesMap.value(selectedServicePath)->requestConnect();
int pos = orderedServicesList.indexOf(selectedServicePath);
orderedServicesList.at(pos).service->requestConnect();
}
} else {
if (!isStateOnline(service->state())) {
Expand Down Expand Up @@ -689,16 +685,16 @@ void QConnectionAgent::servicesChanged()
qDebug();
disconnect(netman,SIGNAL(servicesChanged()),this,SLOT(servicesChanged()));

updateServicesMap();
updateServices();
}

QString QConnectionAgent::findBestConnectableService()
{
for (int i = 0; i < orderedServicesList.count(); i++) {

QString path = orderedServicesList.at(i);
QString path = orderedServicesList.at(i).path;

NetworkService *service = servicesMap.value(path);
NetworkService *service = orderedServicesList.at(i).service;
if (!service)
continue;

Expand Down Expand Up @@ -726,29 +722,6 @@ QString QConnectionAgent::findBestConnectableService()
if (netman->defaultRoute()->type() == "wifi" && service->type() != "wifi")
return QString(); // prefer connected wifi

// if (service->type() == "cellular" && service->roaming() && !service->connected()) {
// QOfonoManager oManager;
// if (!oManager.available()) {
// qDebug() << "ofono not available.";
// }
// if (oManager.modems().count() < 1)
// return QString();
// QOfonoConnectionManager oConnManager;
// oConnManager.setModemPath(oManager.modems().at(0));
// if (oConnManager.roamingAllowed()) {
// if (askRoaming()) {
// // ask user
// if (!flightModeSuppression) {
// Q_EMIT connectionRequest();
// }
// return QString();
// }
// }
// //roaming and user doesnt want connection while roaming
// qDebug() << "roaming not allowed";
// return QString();
// }

if (isBestService(service)
&& service->favorite()) {
qDebug() << path;
Expand All @@ -760,9 +733,9 @@ QString QConnectionAgent::findBestConnectableService()

void QConnectionAgent::removeAllTypes(const QString &type)
{
Q_FOREACH (const QString &path, orderedServicesList) {
if (path.contains(type))
orderedServicesList.removeOne(path);
Q_FOREACH (Service elem, orderedServicesList) {
if (elem.path.contains(type))
orderedServicesList.remove(path);
}
}

Expand Down Expand Up @@ -851,14 +824,14 @@ void QConnectionAgent::stopTethering(bool keepPowered)
bool b = confFile.value("tetheringCellularConnected").toBool();
bool ab = confFile.value("tetheringCellularAutoconnect").toBool();

Q_FOREACH (const QString &path, servicesMap.keys()) {
if (path.contains("cellular")) {
if (isStateOnline(servicesMap.value(path)->state())) {
Q_FOREACH (Service elem, orderedServicesList) {
if (elem.path.contains("cellular")) {
if (isStateOnline(elem.service->state())) {
qDebug() << "disconnect mobile data";
if (!b)
servicesMap.value(path)->requestDisconnect();
elem.service->requestDisconnect();
if (!ab)
servicesMap.value(path)->setAutoConnect(false);
elem.service->setAutoConnect(false);
}
}
}
Expand Down
43 changes: 39 additions & 4 deletions connd/qconnectionagent.h
Expand Up @@ -26,6 +26,7 @@
#include <QQueue>
#include <QPair>
#include <QElapsedTimer>
#include <QVector>

class UserAgent;
class SessionAgent;
Expand Down Expand Up @@ -77,6 +78,42 @@ public Q_SLOTS:
void stopTethering(bool keepPowered = false);

private:

class Service
{
public:
QString path;
NetworkService *service;

bool operator==(const Service &other) const {
return other.path == path;
}
};

class ServiceList : public QVector<Service>
{
public:
int indexOf(const QString &path, int from = 0) const {
Service key;
key.path = path;
return QVector<Service>::indexOf(key, from);
}

bool contains(const QString &path) const {
Service key;
key.path = path;
return QVector<Service>::indexOf(key) >= 0;
}

void remove(const QString &path) {
Service key;
key.path = path;
int pos = QVector<Service>::indexOf(key);
if (pos >= 0)
QVector<Service>::remove(pos);
}
};

explicit QConnectionAgent(QObject *parent = 0);
static QConnectionAgent *self;
ConnAdaptor *connectionAdaptor;
Expand All @@ -87,8 +124,7 @@ public Q_SLOTS:

QString currentNetworkState;

QMap<QString,NetworkService *> servicesMap;
QStringList orderedServicesList;
ServiceList orderedServicesList;

QStringList techPreferenceList;
bool isEthernet;
Expand All @@ -112,7 +148,7 @@ public Q_SLOTS:

private slots:
void onScanFinished();
void updateServicesMap();
void updateServices();

void serviceErrorChanged(const QString &error);
void serviceStateChanged(const QString &state);
Expand All @@ -126,7 +162,6 @@ private slots:
void browserRequest(const QString &servicePath, const QString &url);
void techChanged();

void serviceRemoved(const QString &);
void serviceAdded(const QString &);
void servicesListChanged(const QStringList &);
void offlineModeChanged(bool);
Expand Down

0 comments on commit ad7bc90

Please sign in to comment.