diff options
author | Aaron Seigo <aseigo@kde.org> | 2014-11-20 16:28:31 +0100 |
---|---|---|
committer | Aaron Seigo <aseigo@kde.org> | 2014-11-20 16:28:31 +0100 |
commit | ed20c3082d4fd5e90703e4d6c37093dcecb5cfd1 (patch) | |
tree | c4ad96fde99c38ee66c0ba7d0f8231bc5326b3d6 /client/resourceaccess.cpp | |
download | sink-ed20c3082d4fd5e90703e4d6c37093dcecb5cfd1.tar.gz sink-ed20c3082d4fd5e90703e4d6c37093dcecb5cfd1.zip |
sketch in the client/resource model
Diffstat (limited to 'client/resourceaccess.cpp')
-rw-r--r-- | client/resourceaccess.cpp | 95 |
1 files changed, 95 insertions, 0 deletions
diff --git a/client/resourceaccess.cpp b/client/resourceaccess.cpp new file mode 100644 index 0000000..1eadc34 --- /dev/null +++ b/client/resourceaccess.cpp | |||
@@ -0,0 +1,95 @@ | |||
1 | #include "resourceaccess.h" | ||
2 | |||
3 | #include "common/console.h" | ||
4 | |||
5 | #include <QDebug> | ||
6 | #include <QProcess> | ||
7 | |||
8 | ResourceAccess::ResourceAccess(const QString &resourceName, QObject *parent) | ||
9 | : QObject(parent), | ||
10 | m_resourceName(resourceName), | ||
11 | m_socket(new QLocalSocket(this)), | ||
12 | m_startingProcess(false) | ||
13 | { | ||
14 | Console::main()->log(QString("Starting access to %1").arg(m_socket->serverName())); | ||
15 | connect(m_socket, &QLocalSocket::connected, | ||
16 | this, &ResourceAccess::connected); | ||
17 | connect(m_socket, &QLocalSocket::disconnected, | ||
18 | this, &ResourceAccess::disconnected); | ||
19 | connect(m_socket, SIGNAL(error(QLocalSocket::LocalSocketError)), | ||
20 | this, SLOT(connectionError(QLocalSocket::LocalSocketError))); | ||
21 | } | ||
22 | |||
23 | ResourceAccess::~ResourceAccess() | ||
24 | { | ||
25 | |||
26 | } | ||
27 | |||
28 | QString ResourceAccess::resourceName() const | ||
29 | { | ||
30 | return m_resourceName; | ||
31 | } | ||
32 | |||
33 | bool ResourceAccess::isReady() const | ||
34 | { | ||
35 | return m_socket->isValid(); | ||
36 | } | ||
37 | |||
38 | void ResourceAccess::open() | ||
39 | { | ||
40 | static int count = 0; | ||
41 | if (m_startingProcess) { | ||
42 | QMetaObject::invokeMethod(this, "open", Qt::QueuedConnection); | ||
43 | } | ||
44 | |||
45 | if (m_socket->isValid()) { | ||
46 | return; | ||
47 | } | ||
48 | |||
49 | ++count; | ||
50 | if (count > 10000) { | ||
51 | return; | ||
52 | } | ||
53 | |||
54 | m_socket->setServerName(m_resourceName); | ||
55 | Console::main()->log(QString("Opening: %1").arg(m_socket->serverName())); | ||
56 | //FIXME: race between starting the exec and opening the socket? | ||
57 | m_socket->open(); | ||
58 | } | ||
59 | |||
60 | void ResourceAccess::close() | ||
61 | { | ||
62 | Console::main()->log(QString("Closing: %1").arg(m_socket->fullServerName())); | ||
63 | m_socket->close(); | ||
64 | } | ||
65 | |||
66 | void ResourceAccess::connected() | ||
67 | { | ||
68 | m_startingProcess = false; | ||
69 | Console::main()->log(QString("Connected: %1").arg(m_socket->fullServerName())); | ||
70 | emit ready(true); | ||
71 | } | ||
72 | |||
73 | void ResourceAccess::disconnected() | ||
74 | { | ||
75 | Console::main()->log(QString("Disconnected: %1").arg(m_socket->fullServerName())); | ||
76 | emit ready(false); | ||
77 | } | ||
78 | |||
79 | void ResourceAccess::connectionError(QLocalSocket::LocalSocketError error) | ||
80 | { | ||
81 | Console::main()->log(QString("Could not connect to %1 due to error %2").arg(m_socket->serverName()).arg(error)); | ||
82 | if (m_startingProcess) { | ||
83 | QMetaObject::invokeMethod(this, "open", Qt::QueuedConnection); | ||
84 | return; | ||
85 | } | ||
86 | |||
87 | m_startingProcess = true; | ||
88 | Console::main()->log(QString("Attempting to start resource...")); | ||
89 | QStringList args; | ||
90 | args << m_resourceName; | ||
91 | if (QProcess::startDetached("toynadi_resource", args)) { | ||
92 | m_socket->open(); | ||
93 | } | ||
94 | } | ||
95 | |||