From c6be281ab1e092b427953e75deab663d1c6836eb Mon Sep 17 00:00:00 2001 From: Reinder Feenstra Date: Wed, 2 Mar 2022 23:50:57 +0100 Subject: [PATCH] added processMessage to network Object base class --- client/src/network/board.cpp | 4 +-- client/src/network/board.hpp | 7 ++--- client/src/network/connection.cpp | 41 +++-------------------------- client/src/network/inputmonitor.cpp | 1 + client/src/network/inputmonitor.hpp | 6 +---- client/src/network/object.hpp | 5 +++- client/src/network/outputmap.cpp | 4 +-- client/src/network/outputmap.hpp | 9 ++----- 8 files changed, 18 insertions(+), 59 deletions(-) diff --git a/client/src/network/board.cpp b/client/src/network/board.cpp index 7e53d498..5539ca29 100644 --- a/client/src/network/board.cpp +++ b/client/src/network/board.cpp @@ -3,7 +3,7 @@ * * This file is part of the traintastic source code. * - * Copyright (C) 2020-2021 Reinder Feenstra + * Copyright (C) 2020-2022 Reinder Feenstra * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License @@ -143,7 +143,7 @@ void Board::processMessage(const Message& message) break; } default: - Q_ASSERT(false); + Object::processMessage(message); break; } } diff --git a/client/src/network/board.hpp b/client/src/network/board.hpp index 65182bb8..b1bddfee 100644 --- a/client/src/network/board.hpp +++ b/client/src/network/board.hpp @@ -3,7 +3,7 @@ * * This file is part of the traintastic source code. * - * Copyright (C) 2020-2021 Reinder Feenstra + * Copyright (C) 2020-2022 Reinder Feenstra * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License @@ -32,9 +32,6 @@ #include #include "objectptr.hpp" -class Connection; -class Message; - class Board final : public Object { Q_OBJECT @@ -51,7 +48,7 @@ class Board final : public Object int m_getTileDataRequestId; void getTileDataResponse(const Message& response); - void processMessage(const Message& message); + void processMessage(const Message& message) final; public: inline static const QString classId = QStringLiteral("board"); diff --git a/client/src/network/connection.cpp b/client/src/network/connection.cpp index e2f889c4..cbdc58db 100644 --- a/client/src/network/connection.cpp +++ b/client/src/network/connection.cpp @@ -374,7 +374,7 @@ int Connection::getInputMonitorInputInfo(InputMonitor& inputMonitor) send(request, [&inputMonitor](const std::shared_ptr message) { - inputMonitor.processMessage(*message); + static_cast(inputMonitor).processMessage(*message); }); return request->requestId(); } @@ -386,16 +386,7 @@ int Connection::getOutputKeyboardOutputInfo(OutputKeyboard& object) send(request, [&object](const std::shared_ptr message) { - uint32_t count = message->read(); - while(count > 0) - { - const uint32_t address = message->read(); - const QString id = QString::fromUtf8(message->read()); - const TriState value = message->read(); - emit object.outputIdChanged(address, id); - emit object.outputValueChanged(address, value); - count--; - } + static_cast(object).processMessage(*message); }); return request->requestId(); } @@ -973,36 +964,12 @@ void Connection::processMessage(const std::shared_ptr message) case Message::Command::InputMonitorInputIdChanged: case Message::Command::InputMonitorInputValueChanged: - if(auto inputMonitor = std::dynamic_pointer_cast(m_objects.value(message->read()).lock())) - inputMonitor->processMessage(*message); - break; - case Message::Command::OutputKeyboardOutputIdChanged: - if(auto outputKeyboard = std::dynamic_pointer_cast(m_objects.value(message->read()).lock())) - { - const uint32_t address = message->read(); - const QString id = QString::fromUtf8(message->read()); - emit outputKeyboard->outputIdChanged(address, id); - } - break; - case Message::Command::OutputKeyboardOutputValueChanged: - if(auto outputKeyboard = std::dynamic_pointer_cast(m_objects.value(message->read()).lock())) - { - const uint32_t address = message->read(); - const TriState value = message->read(); - emit outputKeyboard->outputValueChanged(address, value); - } - break; - case Message::Command::BoardTileDataChanged: - if(auto board = std::dynamic_pointer_cast(m_objects.value(message->read()).lock())) - board->processMessage(*message); - break; - case Message::Command::OutputMapOutputsChanged: - if(auto outputMap = std::dynamic_pointer_cast(m_objects.value(message->read()).lock())) - outputMap->processMessage(*message); + if(auto object = m_objects.value(message->read()).lock()) + object->processMessage(*message); break; default: diff --git a/client/src/network/inputmonitor.cpp b/client/src/network/inputmonitor.cpp index 1e8536bf..f41dc62e 100644 --- a/client/src/network/inputmonitor.cpp +++ b/client/src/network/inputmonitor.cpp @@ -93,6 +93,7 @@ void InputMonitor::processMessage(const Message& message) return; } default: + Object::processMessage(message); break; } } diff --git a/client/src/network/inputmonitor.hpp b/client/src/network/inputmonitor.hpp index d9fe8990..4f5439f5 100644 --- a/client/src/network/inputmonitor.hpp +++ b/client/src/network/inputmonitor.hpp @@ -26,21 +26,17 @@ #include "object.hpp" #include -class Message; - class InputMonitor final : public Object { Q_OBJECT - friend class Connection; - private: int m_requestId; std::unordered_map m_inputIds; std::unordered_map m_inputValues; protected: - void processMessage(const Message& message); + void processMessage(const Message& message) final; public: inline static const QString classId = QStringLiteral("input_monitor"); diff --git a/client/src/network/object.hpp b/client/src/network/object.hpp index 59505c49..7658d611 100644 --- a/client/src/network/object.hpp +++ b/client/src/network/object.hpp @@ -3,7 +3,7 @@ * * This file is part of the traintastic source code. * - * Copyright (C) 2019-2021 Reinder Feenstra + * Copyright (C) 2019-2022 Reinder Feenstra * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License @@ -29,6 +29,7 @@ #include "interfaceitems.hpp" class Connection; +class Message; class AbstractProperty; class AbstractVectorProperty; class Method; @@ -45,6 +46,8 @@ class Object : public QObject const QString m_classId; InterfaceItems m_interfaceItems; + virtual void processMessage(const Message& /*message*/) {} + public: explicit Object(std::shared_ptr connection, Handle handle, const QString& classId); Object(const Object&) = delete; diff --git a/client/src/network/outputmap.cpp b/client/src/network/outputmap.cpp index 531162b5..dff6e332 100644 --- a/client/src/network/outputmap.cpp +++ b/client/src/network/outputmap.cpp @@ -3,7 +3,7 @@ * * This file is part of the traintastic source code. * - * Copyright (C) 2021 Reinder Feenstra + * Copyright (C) 2021-2022 Reinder Feenstra * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License @@ -93,7 +93,7 @@ void OutputMap::processMessage(const Message& message) break; default: - Q_ASSERT(false); + Object::processMessage(message); break; } } diff --git a/client/src/network/outputmap.hpp b/client/src/network/outputmap.hpp index e9b62dbf..fec1bd86 100644 --- a/client/src/network/outputmap.hpp +++ b/client/src/network/outputmap.hpp @@ -3,7 +3,7 @@ * * This file is part of the traintastic source code. * - * Copyright (C) 2021 Reinder Feenstra + * Copyright (C) 2021-2022 Reinder Feenstra * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License @@ -26,15 +26,10 @@ #include "object.hpp" #include "objectptr.hpp" -class Connection; -class Message; - class OutputMap final : public Object { Q_OBJECT - friend class Connection; - public: using Items = std::vector; using Outputs = std::vector; @@ -48,7 +43,7 @@ class OutputMap final : public Object void readOutputs(const Message& message); protected: - void processMessage(const Message& message); + void processMessage(const Message& message) final; public: inline static const QString classIdPrefix = QStringLiteral("output_map.");