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 /resource/listener.cpp | |
download | sink-ed20c3082d4fd5e90703e4d6c37093dcecb5cfd1.tar.gz sink-ed20c3082d4fd5e90703e4d6c37093dcecb5cfd1.zip |
sketch in the client/resource model
Diffstat (limited to 'resource/listener.cpp')
-rw-r--r-- | resource/listener.cpp | 71 |
1 files changed, 71 insertions, 0 deletions
diff --git a/resource/listener.cpp b/resource/listener.cpp new file mode 100644 index 0000000..efdfe3e --- /dev/null +++ b/resource/listener.cpp | |||
@@ -0,0 +1,71 @@ | |||
1 | #include "listener.h" | ||
2 | |||
3 | #include "common/console.h" | ||
4 | |||
5 | #include <QLocalSocket> | ||
6 | |||
7 | Listener::Listener(const QString &resource, QObject *parent) | ||
8 | : QObject(parent), | ||
9 | m_server(new QLocalServer(this)) | ||
10 | { | ||
11 | connect(m_server, &QLocalServer::newConnection, | ||
12 | this, &Listener::acceptConnection); | ||
13 | Console::main()->log(QString("Trying to open %1").arg(resource)); | ||
14 | if (!m_server->listen(resource)) { | ||
15 | // FIXME: multiple starts need to be handled here | ||
16 | m_server->removeServer(resource); | ||
17 | if (!m_server->listen(resource)) { | ||
18 | Console::main()->log("Utter failure to start server"); | ||
19 | exit(-1); | ||
20 | } | ||
21 | } | ||
22 | |||
23 | if (m_server->isListening()) { | ||
24 | Console::main()->log(QString("Listening on %1").arg(m_server->serverName())); | ||
25 | } | ||
26 | } | ||
27 | |||
28 | Listener::~Listener() | ||
29 | { | ||
30 | } | ||
31 | |||
32 | void Listener::closeAllConnections() | ||
33 | { | ||
34 | //TODO: close all client connectionsin m_connections | ||
35 | } | ||
36 | |||
37 | void Listener::acceptConnection() | ||
38 | { | ||
39 | Console::main()->log(QString("Accepting connection")); | ||
40 | QLocalSocket *connection = m_server->nextPendingConnection(); | ||
41 | |||
42 | if (!connection) { | ||
43 | return; | ||
44 | } | ||
45 | |||
46 | Console::main()->log("Got a connection"); | ||
47 | Client client(connection); | ||
48 | m_connections << client; | ||
49 | connect(connection, &QLocalSocket::disconnected, | ||
50 | this, &Listener::clientDropped); | ||
51 | |||
52 | } | ||
53 | |||
54 | void Listener::clientDropped() | ||
55 | { | ||
56 | QLocalSocket *connection = qobject_cast<QLocalSocket *>(sender()); | ||
57 | if (!connection) { | ||
58 | return; | ||
59 | } | ||
60 | |||
61 | Console::main()->log("Dropping connection..."); | ||
62 | QMutableListIterator<Client> it(m_connections); | ||
63 | while (it.hasNext()) { | ||
64 | const Client &client = it.next(); | ||
65 | if (client.m_socket == connection) { | ||
66 | Console::main()->log(" dropped..."); | ||
67 | it.remove(); | ||
68 | break; | ||
69 | } | ||
70 | } | ||
71 | } | ||