DSSignalBroker: add skeleton codes 48/242948/1
authorJunseok, Kim <juns.kim@samsung.com>
Fri, 14 Aug 2020 08:23:18 +0000 (17:23 +0900)
committerSung-Jin Park <sj76.park@samsung.com>
Wed, 2 Sep 2020 05:17:50 +0000 (14:17 +0900)
Change-Id: I90427a9efac9298c9222a9fff6e58a667e390b4d
Signed-off-by: Junseok, Kim <juns.kim@samsung.com>
src/DSSignal/DSSignalBroker.cpp [new file with mode: 0644]
src/DSSignal/DSSignalBroker.h [new file with mode: 0644]
src/meson.build
tests/DSSignalBroker-test.cpp [new file with mode: 0644]
tests/meson.build

diff --git a/src/DSSignal/DSSignalBroker.cpp b/src/DSSignal/DSSignalBroker.cpp
new file mode 100644 (file)
index 0000000..cdb1aad
--- /dev/null
@@ -0,0 +1,110 @@
+/*
+* Copyright © 2020 Samsung Electronics co., Ltd. All Rights Reserved.
+*
+* Permission is hereby granted, free of charge, to any person obtaining a
+* copy of this software and associated documentation files (the "Software"),
+* to deal in the Software without restriction, including without limitation
+* the rights to use, copy, modify, merge, publish, distribute, sublicense,
+* and/or sell copies of the Software, and to permit persons to whom the
+* Software is furnished to do so, subject to the following conditions:
+*
+* The above copyright notice and this permission notice (including the next
+* paragraph) shall be included in all copies or substantial portions of the
+* Software.
+*
+* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
+* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+* DEALINGS IN THE SOFTWARE.
+*/
+
+#include <DSSignalBroker.h>
+
+namespace display_server
+{
+
+std::mutex DSSignalBroker::__mutex;
+DSSignalBroker* DSSignalBroker::__signalBroker { nullptr };
+
+DSSignalBroker::DSSignalBroker(DSObject *parent)
+{
+}
+
+DSSignalBroker::~DSSignalBroker()
+{
+}
+
+DSSignalBroker *DSSignalBroker::getInstance()
+{
+       std::lock_guard<std::mutex> tLock(__mutex);
+
+       if (!__signalBroker)
+       {
+               __signalBroker = new DSSignalBroker(new DSObject);
+               DSLOG_INF("DSSignalBroker", "DSSignalBroker instance has been created !");
+       }
+
+       return __signalBroker;
+}
+
+void DSSignalBroker::releaseInstance()
+{
+       std::lock_guard<std::mutex> tLock(__mutex);
+
+       if (__signalBroker && __signalBroker->__signalHash.empty())
+       {
+               delete __signalBroker;
+               __signalBroker = nullptr;
+               DSLOG_INF("DSSignalBroker",     "DSSignalBroker instance has been removed !");
+       }
+}
+
+bool DSSignalBroker::registerCallback(std::string signalStr, DSObject *slot, std::function<void(void *)> func)
+{
+       if (!__signalBroker) return false;
+
+       if (__signalBroker->__signalHash.find(signalStr) == __signalBroker->__signalHash.end())
+               {
+                       DSLOG_INF("DSSignalBroker","registerCallback:: cannot find signal, add new one %s", signalStr.c_str());
+
+                       __signalBroker->__signalHash[signalStr] = new DSSignal<void *>();
+               }
+
+       __signalBroker->__signalHash[signalStr]->connect(slot, func);
+
+       return true;
+}
+
+bool DSSignalBroker::deregisterCallback(std::string signalStr, DSObject *slot, std::function<void(void *)> func)
+{
+       if (!__signalBroker) return false;
+
+       if (__signalBroker->__signalHash.find(signalStr) == __signalBroker->__signalHash.end())
+               return false;
+
+       // TODO: have to impl disconnect method in DSSignal
+       //__signalBroker->__signalHash[signalStr]->disconnect(slot, func);
+
+       return true;
+}
+
+bool DSSignalBroker::emitSignal(std::string signalStr, void *arg)
+{
+       if (!__signalBroker) return false;
+
+       if (__signalBroker->__signalHash.find(signalStr) == __signalBroker->__signalHash.end())
+               {
+                       DSLOG_INF("DSSignalBroker","emitSignal:: cannot find signal, add new one %s", signalStr.c_str());
+
+                       __signalBroker->__signalHash[signalStr] = new DSSignal<void *>();
+               }
+
+       __signalBroker->__signalHash[signalStr]->emit(arg);
+
+       return true;
+}
+
+} // end of namespace display_server
\ No newline at end of file
diff --git a/src/DSSignal/DSSignalBroker.h b/src/DSSignal/DSSignalBroker.h
new file mode 100644 (file)
index 0000000..2e503a0
--- /dev/null
@@ -0,0 +1,58 @@
+/*
+* Copyright © 2020 Samsung Electronics co., Ltd. All Rights Reserved.
+*
+* Permission is hereby granted, free of charge, to any person obtaining a
+* copy of this software and associated documentation files (the "Software"),
+* to deal in the Software without restriction, including without limitation
+* the rights to use, copy, modify, merge, publish, distribute, sublicense,
+* and/or sell copies of the Software, and to permit persons to whom the
+* Software is furnished to do so, subject to the following conditions:
+*
+* The above copyright notice and this permission notice (including the next
+* paragraph) shall be included in all copies or substantial portions of the
+* Software.
+*
+* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
+* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+* DEALINGS IN THE SOFTWARE.
+*/
+
+#ifndef __DS_SIGNAL_BROKER_H__
+#define __DS_SIGNAL_BROKER_H__
+
+#include <DSCore.h>
+#include <DSObject.h>
+#include <DSSignal.h>
+
+namespace display_server
+{
+
+class DSSignalBroker : public DSObject
+{
+public:
+       static DSSignalBroker *getInstance();
+       static void releaseInstance();
+
+       // TODO: needs arguments using signal (now only supports void*)
+       bool registerCallback(std::string signalStr, DSObject *slot, std::function<void(void *)> func);
+       bool deregisterCallback(std::string signalStr, DSObject *slot, std::function<void(void *)> func);
+       bool emitSignal(std::string signalStr, void *arg);
+
+private:
+       static std::mutex __mutex;
+       static DSSignalBroker *__signalBroker;
+
+       std::unordered_map<std::string, DSSignal<void *> *> __signalHash;
+
+       DSSignalBroker() = delete;
+       ~DSSignalBroker();
+       DSSignalBroker(DSObject *parent);
+};
+
+} // end of namespace display_server
+
+#endif // end of __DS_SIGNAL_BROKER_H__
index 9facc3c..9ebeb3e 100644 (file)
@@ -52,6 +52,8 @@ libds_srcs = [
        'DSSeat/DSTouch.h',
        'DSSignal/DSSignal.cpp',
        'DSSignal/DSSignal.h',
+       'DSSignal/DSSignalBroker.cpp',
+       'DSSignal/DSSignalBroker.h',
        'DSCore/DSStruct.h',
        'DSCore/DSCore.h',
        'DSWaylandExtension/DSWaylandExtension.cpp',
diff --git a/tests/DSSignalBroker-test.cpp b/tests/DSSignalBroker-test.cpp
new file mode 100644 (file)
index 0000000..1b3ace7
--- /dev/null
@@ -0,0 +1,85 @@
+/*
+* Copyright © 2020 Samsung Electronics co., Ltd. All Rights Reserved.
+*
+* Permission is hereby granted, free of charge, to any person obtaining a
+* copy of this software and associated documentation files (the "Software"),
+* to deal in the Software without restriction, including without limitation
+* the rights to use, copy, modify, merge, publish, distribute, sublicense,
+* and/or sell copies of the Software, and to permit persons to whom the
+* Software is furnished to do so, subject to the following conditions:
+*
+* The above copyright notice and this permission notice (including the next
+* paragraph) shall be included in all copies or substantial portions of the
+* Software.
+*
+* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
+* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+* DEALINGS IN THE SOFTWARE.
+*/
+
+#include <iostream>
+#include "libds-tests.h"
+#include "DSSignal.h"
+#include "DSSignalBroker.h"
+#include "DSDebugLog.h"
+
+using namespace display_server;
+
+class DSSignalBrokerTest : public ::testing::Test
+{
+public:
+       void SetUp(void) override
+       {
+               res_lambda = false;
+       }
+       void TearDown(void) override
+       {}
+
+       bool res_lambda;
+};
+
+TEST_F(DSSignalBrokerTest, getInstance)
+{
+       DSSignalBroker *sigbro = DSSignalBroker::getInstance();
+       EXPECT_TRUE(sigbro != nullptr);
+
+       if (sigbro)
+               sigbro->releaseInstance();
+}
+
+TEST_F(DSSignalBrokerTest, registerCallback)
+{
+       bool res = false;
+       DSSignalBroker *sigbro = DSSignalBroker::getInstance();
+       std::string testSignal = "TestSignalBroker";
+       EXPECT_TRUE(sigbro != nullptr);
+
+       res = sigbro->registerCallback(testSignal, new DSObject, [&](void * arg) {
+               DSLOG_INF("DSTEST", "%s is invoked!, input: %p", __func__, arg);
+               res_lambda = true;
+       });
+       EXPECT_TRUE(res);
+
+       if (sigbro)
+               sigbro->releaseInstance();
+}
+
+TEST_F(DSSignalBrokerTest, emitSignal)
+{
+       bool res = false;
+       DSSignalBroker *sigbro = DSSignalBroker::getInstance();
+       std::string testSignal = "TestSignalBroker";
+       EXPECT_TRUE(sigbro != nullptr);
+
+       // NOTICE: If registerCallback test didn't called, this test will fail.
+       res = sigbro->emitSignal(testSignal, nullptr);
+       EXPECT_TRUE(res);
+       EXPECT_TRUE(res_lambda);
+
+       if (sigbro)
+               sigbro->releaseInstance();
+}
index 1ef31cc..372bf7e 100644 (file)
@@ -17,6 +17,7 @@ libds_tests_srcs = [
        'DSDebugLog-test.cpp',
        'DSDisplayDeviceTDMImpl-test.cpp',
        'DSSignal-test.cpp',
+       'DSSignalBroker-test.cpp',
        'DSWindowManager-test.cpp',
        'DSWindow-test.cpp',
        'DSWindowShell-test.cpp',