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 | |
download | sink-ed20c3082d4fd5e90703e4d6c37093dcecb5cfd1.tar.gz sink-ed20c3082d4fd5e90703e4d6c37093dcecb5cfd1.zip |
sketch in the client/resource model
Diffstat (limited to 'resource')
-rw-r--r-- | resource/CMakeLists.txt | 15 | ||||
-rw-r--r-- | resource/listener.cpp | 71 | ||||
-rw-r--r-- | resource/listener.h | 37 | ||||
-rw-r--r-- | resource/main.cpp | 23 |
4 files changed, 146 insertions, 0 deletions
diff --git a/resource/CMakeLists.txt b/resource/CMakeLists.txt new file mode 100644 index 0000000..3de51b5 --- /dev/null +++ b/resource/CMakeLists.txt | |||
@@ -0,0 +1,15 @@ | |||
1 | project(toynadi_resource) | ||
2 | |||
3 | include_directories(${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_CURRENT_BINARY_DIR}) | ||
4 | |||
5 | set(common_path "../common/") | ||
6 | |||
7 | set(toynadiresource_SRCS | ||
8 | ${common_path}/console.cpp | ||
9 | main.cpp | ||
10 | listener.cpp | ||
11 | ) | ||
12 | |||
13 | add_executable(${PROJECT_NAME} ${toynadiresource_SRCS}) | ||
14 | qt5_use_modules(${PROJECT_NAME} Widgets Network) | ||
15 | install(TARGETS ${PROJECT_NAME} DESTINATION bin) | ||
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 | } | ||
diff --git a/resource/listener.h b/resource/listener.h new file mode 100644 index 0000000..79c7ba5 --- /dev/null +++ b/resource/listener.h | |||
@@ -0,0 +1,37 @@ | |||
1 | #pragma once | ||
2 | |||
3 | #include <QLocalServer> | ||
4 | #include <QLocalSocket> | ||
5 | #include <QList> | ||
6 | #include <QObject> | ||
7 | |||
8 | class Client | ||
9 | { | ||
10 | public: | ||
11 | Client(QLocalSocket *s) | ||
12 | : m_socket(s), | ||
13 | m_commanded(false) | ||
14 | { | ||
15 | } | ||
16 | |||
17 | QLocalSocket *m_socket; | ||
18 | bool m_commanded; | ||
19 | }; | ||
20 | |||
21 | class Listener : public QObject | ||
22 | { | ||
23 | public: | ||
24 | Listener(const QString &resourceName, QObject *parent = 0); | ||
25 | ~Listener(); | ||
26 | |||
27 | public Q_SLOTS: | ||
28 | void closeAllConnections(); | ||
29 | |||
30 | private Q_SLOTS: | ||
31 | void acceptConnection(); | ||
32 | void clientDropped(); | ||
33 | |||
34 | private: | ||
35 | QLocalServer *m_server; | ||
36 | QList<Client> m_connections; | ||
37 | }; \ No newline at end of file | ||
diff --git a/resource/main.cpp b/resource/main.cpp new file mode 100644 index 0000000..3bd4656 --- /dev/null +++ b/resource/main.cpp | |||
@@ -0,0 +1,23 @@ | |||
1 | |||
2 | #include <QApplication> | ||
3 | |||
4 | #include "common/console.h" | ||
5 | #include "listener.h" | ||
6 | |||
7 | int main(int argc, char *argv[]) | ||
8 | { | ||
9 | QApplication app(argc, argv); | ||
10 | |||
11 | new Console(QString("Toy Resource: %1").arg(argv[1])); | ||
12 | if (argc < 2) { | ||
13 | Console::main()->log("Not enough args"); | ||
14 | return app.exec(); | ||
15 | } | ||
16 | |||
17 | Listener *listener = new Listener(argv[1]); | ||
18 | |||
19 | QObject::connect(&app, &QCoreApplication::aboutToQuit, | ||
20 | listener, &Listener::closeAllConnections); | ||
21 | |||
22 | return app.exec(); | ||
23 | } \ No newline at end of file | ||