|
1 | | -/* Copyright 2013 Yurii Litvinov |
| 1 | +/* Copyright 2013 - 2014 Yurii Litvinov, CyberTech Labs Ltd. |
2 | 2 | * |
3 | 3 | * Licensed under the Apache License, Version 2.0 (the "License"); |
4 | 4 | * you may not use this file except in compliance with the License. |
|
25 | 25 | using namespace trikCommunicator; |
26 | 26 |
|
27 | 27 | TrikCommunicator::TrikCommunicator(trikControl::Brick &brick, QString const &startDirPath) |
28 | | - : mTrikScriptRunner(new trikScriptRunner::TrikScriptRunner(brick, startDirPath)) |
29 | | - , mHasScriptRunnerOwnership(true) |
| 28 | + : TrikCommunicator(new trikScriptRunner::TrikScriptRunner(brick, startDirPath), true) |
30 | 29 | { |
31 | | - init(); |
32 | 30 | } |
33 | 31 |
|
34 | 32 | TrikCommunicator::TrikCommunicator(trikScriptRunner::TrikScriptRunner &runner) |
35 | | - : mTrikScriptRunner(&runner) |
36 | | - , mHasScriptRunnerOwnership(false) |
| 33 | + : TrikCommunicator(&runner, false) |
37 | 34 | { |
38 | | - init(); |
39 | 35 | } |
40 | 36 |
|
41 | | -TrikCommunicator::~TrikCommunicator() |
| 37 | +TrikCommunicator::TrikCommunicator(trikScriptRunner::TrikScriptRunner * const runner, bool hasScriptRunnerOwnership) |
| 38 | + : trikKernel::TrikServer([this] () { return connectionFactory(); }) |
| 39 | + , mTrikScriptRunner(runner) |
| 40 | + , mHasScriptRunnerOwnership(hasScriptRunnerOwnership) |
42 | 41 | { |
43 | | - foreach (QThread *thread, mConnections.keys()) { |
44 | | - thread->quit(); |
45 | | - if (!thread->wait(1000)) { |
46 | | - qDebug() << "Unable to stop thread" << thread; |
47 | | - } |
48 | | - } |
49 | | - |
50 | | - qDeleteAll(mConnections); |
51 | | - qDeleteAll(mConnections.keys()); |
52 | | - |
53 | | - if (mHasScriptRunnerOwnership) { |
54 | | - delete mTrikScriptRunner; |
55 | | - } |
56 | | -} |
| 42 | + qRegisterMetaType<trikScriptRunner::TrikScriptRunner *>("trikScriptRunner::TrikScriptRunner *"); |
57 | 43 |
|
58 | | -void TrikCommunicator::startServer(int const &port) |
59 | | -{ |
60 | | - if (!listen(QHostAddress::Any, port)) { |
61 | | - qDebug() << "Can not start TrikCommunicator server"; |
62 | | - } else { |
63 | | - qDebug() << "TrikCommunicator started"; |
64 | | - } |
| 44 | + connect(mTrikScriptRunner, SIGNAL(completed(QString)), this, SIGNAL(finishedScript())); |
65 | 45 | } |
66 | 46 |
|
67 | | -void TrikCommunicator::sendMessage(QString const &message) |
| 47 | +TrikCommunicator::~TrikCommunicator() |
68 | 48 | { |
69 | | - for (Connection * const connection : mConnections.values()) { |
70 | | - connection->sendMessage(message); |
| 49 | + if (mHasScriptRunnerOwnership) { |
| 50 | + delete mTrikScriptRunner; |
71 | 51 | } |
72 | 52 | } |
73 | 53 |
|
74 | | -void TrikCommunicator::incomingConnection(int socketDescriptor) |
75 | | -{ |
76 | | - qDebug() << "New connection, socket descriptor: " << socketDescriptor; |
77 | | - |
78 | | - QThread * const connectionThread = new QThread(); |
79 | | - |
80 | | - connect(connectionThread, SIGNAL(finished()), connectionThread, SLOT(deleteLater())); |
81 | | - connect(connectionThread, SIGNAL(finished()), this, SLOT(onConnectionClosed())); |
82 | | - |
83 | | - Connection * const connectionWorker = new Connection(); |
84 | | - |
85 | | - connect(connectionWorker, SIGNAL(startedDirectScript()), this, SIGNAL(startedDirectScript())); |
86 | | - connect(connectionWorker, SIGNAL(startedScript(QString)), this, SIGNAL(startedScript(QString))); |
87 | | - |
88 | | - connectionWorker->moveToThread(connectionThread); |
89 | | - |
90 | | - mConnections.insert(connectionThread, connectionWorker); |
91 | | - |
92 | | - connectionThread->start(); |
93 | | - |
94 | | - QMetaObject::invokeMethod(connectionWorker, "init", Q_ARG(int, socketDescriptor) |
95 | | - , Q_ARG(trikScriptRunner::TrikScriptRunner *, mTrikScriptRunner)); |
96 | | -} |
97 | | - |
98 | | -void TrikCommunicator::onConnectionClosed() |
| 54 | +Connection *TrikCommunicator::connectionFactory() |
99 | 55 | { |
100 | | - QThread * const thread = static_cast<QThread *>(sender()); |
101 | | - |
102 | | - // Thread shall already be finished here. |
103 | | - delete mConnections.value(thread); |
104 | | - |
105 | | - mConnections.remove(thread); |
106 | | -} |
| 56 | + auto connection = new Connection(*mTrikScriptRunner); |
107 | 57 |
|
108 | | -void TrikCommunicator::init() |
109 | | -{ |
110 | | - qRegisterMetaType<trikScriptRunner::TrikScriptRunner *>("trikScriptRunner::TrikScriptRunner *"); |
| 58 | + connect(connection, SIGNAL(startedDirectScript()), this, SIGNAL(startedDirectScript())); |
| 59 | + connect(connection, SIGNAL(startedScript(QString)), this, SIGNAL(startedScript(QString))); |
111 | 60 |
|
112 | | - connect(mTrikScriptRunner, SIGNAL(completed(QString)), this, SIGNAL(finishedScript())); |
| 61 | + return connection; |
113 | 62 | } |
0 commit comments