Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
Plasma
Plasma Firewall
Commits
f82ece4a
Commit
f82ece4a
authored
Nov 03, 2020
by
Tomaz Canabrava
Browse files
WIP test threads
parent
8b0e9fcb
Changes
5
Hide whitespace changes
Inline
Side-by-side
kcm/backends/netstat/conectionsmodel.cpp
View file @
f82ece4a
...
...
@@ -18,11 +18,8 @@ ConnectionsModel::ConnectionsModel(QObject *parent)
void
ConnectionsModel
::
start
()
{
connect
(
&
timer
,
&
QTimer
::
timeout
,
this
,
&
ConnectionsModel
::
refreshConnections
);
timer
.
setInterval
(
1000
);
timer
.
start
();
QTimer
::
singleShot
(
0
,
this
,
&
ConnectionsModel
::
refreshConnections
);
connect
(
&
m_netstatHelper
,
&
NetstatHelper
::
queryFinished
,
this
,
&
ConnectionsModel
::
refreshConnections
,
Qt
::
QueuedConnection
);
m_netstatHelper
.
start
();
}
bool
ConnectionsModel
::
busy
()
const
...
...
@@ -90,7 +87,7 @@ QHash<int, QByteArray> ConnectionsModel::roleNames() const
};
}
void
ConnectionsModel
::
refreshConnections
()
void
ConnectionsModel
::
refreshConnections
(
const
QVector
<
QStringList
>
&
values
)
{
if
(
m_busy
)
{
return
;
...
...
@@ -98,10 +95,8 @@ void ConnectionsModel::refreshConnections()
setBusy
(
true
);
NetstatHelper
helper
;
QVector
<
QStringList
>
result
=
helper
.
query
();
if
(
helper
.
hasError
())
{
emit
showErrorMessage
(
i18n
(
"Failed to get connections: %1"
,
helper
.
errorString
()));
if
(
m_netstatHelper
.
hasError
())
{
emit
showErrorMessage
(
i18n
(
"Failed to get connections: %1"
,
m_netstatHelper
.
errorString
()));
return
;
}
...
...
@@ -110,7 +105,7 @@ void ConnectionsModel::refreshConnections()
beginResetModel
();
m_connectionsData
.
clear
();
for
(
const
auto
connection
:
result
)
{
for
(
const
auto
connection
:
values
)
{
ConnectionsData
conn
{.
protocol
=
connection
.
at
(
0
),
.
localAddress
=
connection
.
at
(
1
),
.
foreignAddress
=
connection
.
at
(
2
),
...
...
kcm/backends/netstat/conectionsmodel.h
View file @
f82ece4a
...
...
@@ -10,6 +10,8 @@
#include
<QLoggingCategory>
#include
"netstathelper.h"
Q_DECLARE_LOGGING_CATEGORY
(
ConnectionsModelDebug
)
struct
ConnectionsData
{
...
...
@@ -60,7 +62,7 @@ signals:
void
showErrorMessage
(
const
QString
&
message
);
protected
slots
:
void
refreshConnections
();
void
refreshConnections
(
const
QVector
<
QStringList
>
&
values
);
private:
void
setBusy
(
bool
busy
);
...
...
@@ -68,6 +70,7 @@ private:
bool
m_busy
=
false
;
QVector
<
ConnectionsData
>
m_connectionsData
;
QTimer
timer
;
NetstatHelper
m_netstatHelper
;
};
#endif // CONECTIONSMODEL_H
kcm/backends/netstat/netstathelper.cpp
View file @
f82ece4a
...
...
@@ -15,11 +15,26 @@ Q_LOGGING_CATEGORY(NetstatHelperDebug, "netstat.helper")
NetstatHelper
::
NetstatHelper
()
:
m_hasError
(
false
)
{
connect
(
&
m_timer
,
&
QTimer
::
timeout
,
this
,
&
NetstatHelper
::
timeout
,
Qt
::
QueuedConnection
);
setTerminationEnabled
(
true
);
}
QVector
<
QStringList
>
NetstatHelper
::
query
()
void
NetstatHelper
::
timeout
()
{
if
(
m_elapsedTimer
.
elapsed
()
>
2000
)
// wait for at least 2s
{
qDebug
()
<<
"Force-Terminate the thread"
;
terminate
();
start
();
}
}
// This method runs in a different thread.
void
NetstatHelper
::
run
()
{
m_elapsedTimer
.
restart
();
m_hasError
=
false
;
QProcess
netstat
;
/* parameters passed to ss
* -r, --resolve resolve host names
...
...
@@ -43,9 +58,9 @@ QVector<QStringList> NetstatHelper::query()
m_errorString
=
netstat
.
readAllStandardError
();
}
else
{
result
=
parseSSOutput
(
netstat
.
readAllStandardOutput
());
emit
queryFinished
(
result
);
}
return
result
;
}
bool
NetstatHelper
::
hasError
()
const
...
...
kcm/backends/netstat/netstathelper.h
View file @
f82ece4a
...
...
@@ -6,28 +6,41 @@
#define NETSTATHELPER_H
#include
<QVariantMap>
#include
<QTimer>
#include
<QElapsedTimer>
#include
<QLoggingCategory>
#include
<QThread>
Q_DECLARE_LOGGING_CATEGORY
(
NetstatHelperDebug
)
class
NetstatHelper
:
public
Q
Object
class
NetstatHelper
:
public
Q
Thread
{
Q_OBJECT
public:
NetstatHelper
();
void
run
()
override
;
public
Q_SLOTS
:
QVector
<
QStringList
>
query
();
QString
errorString
()
const
;
bool
hasError
()
const
;
/*
if the query takes too long, this timeout happens,
closing the old process, and reopening.
*/
void
timeout
();
signals:
void
queryFinished
(
const
QVector
<
QStringList
>&
values
);
private:
QVector
<
QStringList
>
parseSSOutput
(
const
QByteArray
&
ss
);
QString
extractAndStrip
(
const
QString
&
src
,
const
int
&
index
,
const
int
&
size
);
QString
m_errorString
;
bool
m_hasError
;
QTimer
m_timer
;
QElapsedTimer
m_elapsedTimer
;
};
#endif // NETSTATHELPER_H
kcm/package/contents/ui/ConnectionsView.qml
View file @
f82ece4a
...
...
@@ -46,7 +46,7 @@ ViewBase {
base
.
errorMessage
.
visible
=
true
;
}
else
{
console
.
log
(
"
Starting netstat client
"
);
netStatClient
.
connectionsM
odel
.
start
();
base
.
m
odel
.
start
();
}
}
}
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment