Move Cache to client commons library 90/26590/10
authorZofia Abramowska <z.abramowska@samsung.com>
Tue, 26 Aug 2014 11:27:31 +0000 (13:27 +0200)
committerZofia Abramowska <z.abramowska@samsung.com>
Mon, 1 Sep 2014 05:56:36 +0000 (07:56 +0200)
Change-Id: Ia47e434063b7a1f6d079a29e0076ab0c2392832f

14 files changed:
CMakeLists.txt
packaging/cynara.spec
src/CMakeLists.txt
src/admin/CMakeLists.txt
src/client-common/CMakeLists.txt [new file with mode: 0644]
src/client-common/cache/CacheInterface.h [moved from src/client/cache/CacheInterface.h with 69% similarity]
src/client-common/cache/CapacityCache.cpp [moved from src/client/cache/CapacityCache.cpp with 100% similarity]
src/client-common/cache/CapacityCache.h [moved from src/client/cache/CapacityCache.h with 92% similarity]
src/client-common/plugins/NaiveInterpreter.h [moved from src/client/cache/NaiveInterpreter.h with 89% similarity]
src/client-common/plugins/PluginInterface.h [moved from src/client/cache/PolicyGetter.h with 50% similarity]
src/client/CMakeLists.txt
src/client/cache/PolicyGetter.cpp [deleted file]
src/client/logic/Logic.cpp
src/client/logic/Logic.h

index 354d39b..38c37d7 100644 (file)
@@ -55,6 +55,7 @@ ENDIF (CMAKE_BUILD_TYPE MATCHES "DEBUG")
 
 SET(TARGET_CYNARA "cynara")
 SET(TARGET_LIB_CYNARA "cynara-client")
+SET(TARGET_LIB_CYNARA_COMMON "cynara-client-commons")
 SET(TARGET_LIB_CYNARA_ADMIN "cynara-admin")
 SET(TARGET_CYNARA_COMMON "cynara-commons")
 SET(TARGET_CYNARA_TESTS "cynara-tests")
index a06f5cc..0fcd718 100644 (file)
@@ -193,6 +193,7 @@ fi
 %license LICENSE
 %defattr(-,root,root,-)
 %{_libdir}/libcynara-client.so.*
+%{_libdir}/libcynara-client-commons.so.*
 
 %files -n libcynara-client-devel
 %defattr(-,root,root,-)
@@ -200,6 +201,7 @@ fi
 %{_includedir}/cynara/cynara-client-error.h
 %{_libdir}/pkgconfig/cynara-client.pc
 %{_libdir}/libcynara-client.so
+%{_libdir}/libcynara-client-commons.so
 
 %files -n libcynara-admin
 %manifest libcynara-admin.manifest
index 852e0a5..596e84a 100644 (file)
@@ -40,12 +40,12 @@ INCLUDE_DIRECTORIES(SYSTEM
 SET(CYNARA_PATH ${PROJECT_SOURCE_DIR}/src)
 
 INCLUDE_DIRECTORIES(
-    ${CYNARA_PATH}/include
     ${CYNARA_PATH}/common
     )
 
 ADD_SUBDIRECTORY(include)
 ADD_SUBDIRECTORY(common)
 ADD_SUBDIRECTORY(client)
+ADD_SUBDIRECTORY(client-common)
 ADD_SUBDIRECTORY(admin)
 ADD_SUBDIRECTORY(service)
index bbf0ebc..9f53d55 100644 (file)
@@ -27,6 +27,7 @@ SET(LIB_CYNARA_ADMIN_SOURCES
     )
 
 INCLUDE_DIRECTORIES(
+    ${CYNARA_PATH}/include
     ${CYNARA_LIB_CYNARA_ADMIN_PATH}
     )
 
diff --git a/src/client-common/CMakeLists.txt b/src/client-common/CMakeLists.txt
new file mode 100644 (file)
index 0000000..fdc41c6
--- /dev/null
@@ -0,0 +1,47 @@
+# Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
+#
+#    Licensed under the Apache License, Version 2.0 (the "License");
+#    you may not use this file except in compliance with the License.
+#    You may obtain a copy of the License at
+#
+#        http://www.apache.org/licenses/LICENSE-2.0
+#
+#    Unless required by applicable law or agreed to in writing, software
+#    distributed under the License is distributed on an "AS IS" BASIS,
+#    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+#    See the License for the specific language governing permissions and
+#    limitations under the License.
+#
+# @file        CMakeLists.txt
+# @author      Zofia Abramowska <z.abramowska@samsung.com>
+#
+
+SET(LIB_CYNARA_CLIENT_COMMON_VERSION_MAJOR 0)
+SET(LIB_CYNARA_CLIENT_COMMON_VERSION ${LIB_CYNARA_CLIENT_COMMON_VERSION_MAJOR}.0.2)
+
+SET(LIB_CYNARA_COMMON_PATH ${CYNARA_PATH}/client-common)
+
+INCLUDE_DIRECTORIES(
+    ${LIB_CYNARA_COMMON_PATH}
+    ${CYNARA_PATH}/include
+    )
+
+SET(LIB_CYNARA_COMMON_SOURCES
+    ${LIB_CYNARA_COMMON_PATH}/cache/CapacityCache.cpp
+    )
+
+ADD_LIBRARY(${TARGET_LIB_CYNARA_COMMON} SHARED ${LIB_CYNARA_COMMON_SOURCES})
+
+SET_TARGET_PROPERTIES(
+    ${TARGET_LIB_CYNARA_COMMON}
+    PROPERTIES
+        COMPILE_FLAGS "-D_GNU_SOURCE"
+        SOVERSION ${LIB_CYNARA_CLIENT_COMMON_VERSION_MAJOR}
+        VERSION ${LIB_CYNARA_CLIENT_COMMON_VERSION}
+    )
+
+TARGET_LINK_LIBRARIES(${TARGET_LIB_CYNARA_COMMON}
+    ${TARGET_CYNARA_COMMON}
+    )
+
+INSTALL(TARGETS ${TARGET_LIB_CYNARA_COMMON} DESTINATION ${LIB_INSTALL_DIR})
similarity index 69%
rename from src/client/cache/CacheInterface.h
rename to src/client-common/cache/CacheInterface.h
index ee2b27d..cdf3be7 100644 (file)
  * @brief       This file contains cache interface definitions.
  */
 
-#ifndef SRC_CLIENT_CACHE_CACHEINTERFACE_H_
-#define SRC_CLIENT_CACHE_CACHEINTERFACE_H_
+#ifndef SRC_CLIENT_COMMON_CACHE_CACHEINTERFACE_H_
+#define SRC_CLIENT_COMMON_CACHE_CACHEINTERFACE_H_
 
 #include <map>
 #include <memory>
 #include <string>
 
 #include <cynara-client.h>
+#include <plugins/PluginInterface.h>
 #include <types/ClientSession.h>
 #include <types/PolicyKey.h>
 #include <types/PolicyResult.h>
 
 namespace Cynara {
 
-class InterpreterInterface;
-typedef std::shared_ptr<InterpreterInterface> InterpreterInterfacePtr;
-
 class PluginCache;
 typedef std::shared_ptr<PluginCache> PluginCachePtr;
 
-class ResultGetterInterface;
-typedef std::shared_ptr<ResultGetterInterface> ResultGetterInterfacePtr;
-
-class ResultGetterInterface {
-public:
-    virtual int requestResult(const PolicyKey &key, PolicyResult &result) noexcept = 0;
-    virtual ~ResultGetterInterface() {};
-};
-
-class InterpreterInterface {
-public:
-    virtual bool isCacheable(const PolicyResult &result) noexcept = 0;
-    virtual bool isUsable(const PolicyResult &result) noexcept = 0;
-    virtual int toResult(const PolicyResult &result) noexcept = 0;
-
-    virtual ~InterpreterInterface() {};
-};
-
 class PluginCache {
 public:
     PluginCache() {}
@@ -84,4 +64,4 @@ protected:
 
 } // namespace Cynara
 
-#endif // SRC_CLIENT_CACHE_CACHEINTERFACE_H_
+#endif // SRC_CLIENT_COMMON_CACHE_CACHEINTERFACE_H_
similarity index 92%
rename from src/client/cache/CapacityCache.h
rename to src/client-common/cache/CapacityCache.h
index 6fe9c83..0a607fd 100644 (file)
@@ -20,8 +20,8 @@
  * @brief       This file contains capacity cache header.
  */
 
-#ifndef  SRC_CLIENT_CACHE_CAPACITYCACHE_H_
-#define  SRC_CLIENT_CACHE_CAPACITYCACHE_H_
+#ifndef  SRC_CLIENT_COMMON_CACHE_CAPACITYCACHE_H_
+#define  SRC_CLIENT_COMMON_CACHE_CAPACITYCACHE_H_
 
 #include <list>
 #include <unordered_map>
@@ -62,7 +62,7 @@ private:
 
 } //namespace Cynara
 
-#endif // SRC_CLIENT_CACHE_CAPACITYCACHE_H_
+#endif // SRC_CLIENT_COMMON_CACHE_CAPACITYCACHE_H_
 
 
 
similarity index 89%
rename from src/client/cache/NaiveInterpreter.h
rename to src/client-common/plugins/NaiveInterpreter.h
index 9574f2c..adac014 100644 (file)
@@ -19,8 +19,8 @@
  * @version     1.0
  * @brief       This file contains PolicyType naive interpreter implementation.
  */
-#ifndef SRC_CLIENT_CACHE_NAIVEINTERPRETER_H_
-#define SRC_CLIENT_CACHE_NAIVEINTERPRETER_H_
+#ifndef SRC_CLIENT_COMMON_PLUGINS_NAIVEINTERPRETER_H_
+#define SRC_CLIENT_COMMON_PLUGINS_NAIVEINTERPRETER_H_
 
 #include <attributes/attributes.h>
 #include <cache/CacheInterface.h>
@@ -45,6 +45,6 @@ class NaiveInterpreter : public InterpreterInterface {
 
 } // namespace Cynara
 
-#endif // SRC_CLIENT_CACHE_NAIVEINTERPRETER_H_
+#endif // SRC_CLIENT_COMMON_PLUGINS_NAIVEINTERPRETER_H_
 
 
similarity index 50%
rename from src/client/cache/PolicyGetter.h
rename to src/client-common/plugins/PluginInterface.h
index c653887..5edec7a 100644 (file)
@@ -1,3 +1,4 @@
+
 /*
  *  Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
  *
  *  limitations under the License
  */
 /*
- * @file        PolicyGetter.h
+ * @file        PluginInterface.h
  * @author      Zofia Abramowska <z.abramowska@samsung.com>
  * @version     1.0
- * @brief       This file contains Cynara PolicyResult getter implementation.
+ * @brief       This file contains plugin interface definitions.
  */
 
-#ifndef SRC_CLIENT_CACHE_POLICYGETTER_H_
-#define SRC_CLIENT_CACHE_POLICYGETTER_H_
-
-#include <log/log.h>
+#ifndef SRC_CLIENT_COMMON_PLUGINS_PLUGININTERFACE_H_
+#define SRC_CLIENT_COMMON_PLUGINS_PLUGININTERFACE_H_
 
-#include <cache/CacheInterface.h>
-#include <sockets/SocketClient.h>
+#include <memory>
 
+#include <types/PolicyResult.h>
 
 namespace Cynara {
 
-class PolicyGetter : public ResultGetterInterface {
-public:
-    PolicyGetter(const SocketClientPtr &socketClient) : m_socketClient(socketClient) {}
-    int requestResult(const PolicyKey &key, PolicyResult &result) noexcept;
+class InterpreterInterface;
+typedef std::shared_ptr<InterpreterInterface> InterpreterInterfacePtr;
 
-private:
-    ProtocolFrameSequenceNumber generateSequenceNumber(void) {
-        static ProtocolFrameSequenceNumber sequenceNumber = 0;
-        return ++sequenceNumber;
-    }
+class InterpreterInterface {
+public:
+    virtual bool isCacheable(const PolicyResult &result) noexcept = 0;
+    virtual bool isUsable(const PolicyResult &result) noexcept = 0;
+    virtual int toResult(const PolicyResult &result) noexcept = 0;
 
-    SocketClientPtr m_socketClient;
+    virtual ~InterpreterInterface() {};
 };
 
-} //namespace Cynara
-
-#endif // SRC_CLIENT_CACHE_POLICYGETTER_H_
-
-
-
+}
 
+#endif // SRC_CLIENT_COMMON_PLUGINS_PLUGININTERFACE_H_
index 4431556..09a023e 100644 (file)
 #
 # @file        CMakeLists.txt
 # @author      Lukasz Wojciechowski <l.wojciechow@partner.samsung.com>
+# @author      Zofia Abramowska <z.abramowska@samsung.com>
 #
 
 SET(LIB_CYNARA_VERSION_MAJOR 0)
 SET(LIB_CYNARA_VERSION ${LIB_CYNARA_VERSION_MAJOR}.0.2)
 
-SET(CYNARA_LIB_CYNARA_PATH ${CYNARA_PATH}/client)
+SET(LIB_CYNARA_PATH ${CYNARA_PATH}/client)
 
-SET(LIB_CYNARA_SOURCES
-    ${CYNARA_LIB_CYNARA_PATH}/api/client-api.cpp
-    ${CYNARA_LIB_CYNARA_PATH}/cache/CapacityCache.cpp
-    ${CYNARA_LIB_CYNARA_PATH}/cache/PolicyGetter.cpp
-    ${CYNARA_LIB_CYNARA_PATH}/logic/Logic.cpp
+INCLUDE_DIRECTORIES(
+    ${LIB_CYNARA_PATH}
+    ${CYNARA_PATH}/include
+    ${CYNARA_PATH}/client-common
     )
 
-INCLUDE_DIRECTORIES(
-    ${CYNARA_LIB_CYNARA_PATH}
+SET(LIB_CYNARA_SOURCES
+    ${LIB_CYNARA_PATH}/api/client-api.cpp
+    ${LIB_CYNARA_PATH}/logic/Logic.cpp
     )
 
 ADD_LIBRARY(${TARGET_LIB_CYNARA} SHARED ${LIB_CYNARA_SOURCES})
@@ -37,14 +38,13 @@ ADD_LIBRARY(${TARGET_LIB_CYNARA} SHARED ${LIB_CYNARA_SOURCES})
 SET_TARGET_PROPERTIES(
     ${TARGET_LIB_CYNARA}
     PROPERTIES
-        COMPILE_FLAGS "-D_GNU_SOURCE -fPIC -fvisibility=hidden"
+        COMPILE_FLAGS "-D_GNU_SOURCE -fvisibility=hidden"
         SOVERSION ${LIB_CYNARA_VERSION_MAJOR}
         VERSION ${LIB_CYNARA_VERSION}
     )
 
 TARGET_LINK_LIBRARIES(${TARGET_LIB_CYNARA}
-    ${CYNARA_DEP_LIBRARIES}
-    ${TARGET_CYNARA_COMMON}
+    ${TARGET_LIB_CYNARA_COMMON}
     )
 
 INSTALL(TARGETS ${TARGET_LIB_CYNARA} DESTINATION ${LIB_INSTALL_DIR})
diff --git a/src/client/cache/PolicyGetter.cpp b/src/client/cache/PolicyGetter.cpp
deleted file mode 100644 (file)
index f1f2e49..0000000
+++ /dev/null
@@ -1,67 +0,0 @@
-/*
- *  Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
- *
- *  Licensed under the Apache License, Version 2.0 (the "License");
- *  you may not use this file except in compliance with the License.
- *  You may obtain a copy of the License at
- *
- *      http://www.apache.org/licenses/LICENSE-2.0
- *
- *  Unless required by applicable law or agreed to in writing, software
- *  distributed under the License is distributed on an "AS IS" BASIS,
- *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- *  See the License for the specific language governing permissions and
- *  limitations under the License
- */
-/*
- * @file        PolicyGetter.cpp
- * @author      Zofia Abramowska <z.abramowska@samsung.com>
- * @version     1.0
- * @brief       This file contains PolicyResult getter class implementation.
- */
-
-#include <cinttypes>
-
-#include <cynara-client-error.h>
-#include <exceptions/ServerConnectionErrorException.h>
-#include <log/log.h>
-#include <request/CheckRequest.h>
-#include <request/pointers.h>
-#include <response/CheckResponse.h>
-#include <response/pointers.h>
-
-#include <cache/PolicyGetter.h>
-
-namespace Cynara {
-
-int PolicyGetter::requestResult(const PolicyKey &key, PolicyResult &result) noexcept {
-    ProtocolFrameSequenceNumber sequenceNumber = generateSequenceNumber();
-
-    //Ask cynara service
-    CheckResponsePtr checkResponse;
-    try {
-        RequestPtr request = std::make_shared<CheckRequest>(key, sequenceNumber);
-        ResponsePtr response = m_socketClient->askCynaraServer(request);
-        if (!response) {
-            LOGW("Disconnected by cynara server.");
-            return CYNARA_API_SERVICE_NOT_AVAILABLE;
-        }
-        checkResponse = std::dynamic_pointer_cast<CheckResponse>(response);
-        if (!checkResponse) {
-            LOGC("Critical error. Casting Response to CheckResponse failed.");
-            return CYNARA_API_ACCESS_DENIED;
-        }
-
-        LOGD("checkResponse: policyType = %" PRIu16 ", metadata = %s",
-             checkResponse->m_resultRef.policyType(),
-             checkResponse->m_resultRef.metadata().c_str());
-    } catch (const ServerConnectionErrorException &ex) {
-        LOGE("Cynara service not available.");
-        return CYNARA_API_SERVICE_NOT_AVAILABLE;
-    }
-
-    result = checkResponse->m_resultRef;
-    return CYNARA_API_SUCCESS;
-}
-
-} // namespace Cynara
index a609314..2444bff 100644 (file)
 
 #include <memory>
 
+#include <cache/CapacityCache.h>
 #include <common.h>
+#include <cynara-client-error.h>
+#include <exceptions/ServerConnectionErrorException.h>
+#include <log/log.h>
+#include <plugins/NaiveInterpreter.h>
 #include <protocol/Protocol.h>
 #include <protocol/ProtocolClient.h>
+#include <request/CheckRequest.h>
+#include <request/pointers.h>
+#include <response/CheckResponse.h>
+#include <response/pointers.h>
 #include <sockets/SocketClient.h>
-#include <types/PolicyKey.h>
 
-#include <cache/CapacityCache.h>
-#include <cache/NaiveInterpreter.h>
-#include <cache/PolicyGetter.h>
-#include "Logic.h"
+#include <logic/Logic.h>
 
 namespace Cynara {
 
 const std::string clientSocketPath("/run/cynara/cynara.socket");
 
+static ProtocolFrameSequenceNumber generateSequenceNumber(void) {
+    static ProtocolFrameSequenceNumber sequenceNumber = 0;
+    return ++sequenceNumber;
+}
+
 Logic::Logic() {
     m_socket = std::make_shared<SocketClient>(clientSocketPath, std::make_shared<ProtocolClient>());
-    m_resultGetter = std::make_shared<PolicyGetter>(m_socket);
     m_cache = std::make_shared<CapacityCache>();
     auto naiveInterpreter = std::make_shared<NaiveInterpreter>();
     m_cache->registerPlugin(PredefinedPolicyType::ALLOW, naiveInterpreter);
@@ -63,7 +72,7 @@ int Logic::check(const std::string &client, const ClientSession &session, const
 
     //No value in Cache
     PolicyResult result;
-    ret = m_resultGetter->requestResult(key, result);
+    ret = requestResult(key, result);
     if (ret != CYNARA_API_SUCCESS) {
         LOGE("Error fetching new entry.");
         return ret;
@@ -72,6 +81,36 @@ int Logic::check(const std::string &client, const ClientSession &session, const
     return m_cache->update(session, key, result);
 }
 
+int Logic::requestResult(const PolicyKey &key, PolicyResult &result) noexcept {
+    ProtocolFrameSequenceNumber sequenceNumber = generateSequenceNumber();
+
+    //Ask cynara service
+    CheckResponsePtr checkResponse;
+    try {
+        RequestPtr request = std::make_shared<CheckRequest>(key, sequenceNumber);
+        ResponsePtr response = m_socket->askCynaraServer(request);
+        if (!response) {
+            LOGW("Disconnected by cynara server.");
+            return CYNARA_API_SERVICE_NOT_AVAILABLE;
+        }
+        checkResponse = std::dynamic_pointer_cast<CheckResponse>(response);
+        if (!checkResponse) {
+            LOGC("Critical error. Casting Response to CheckResponse failed.");
+            return CYNARA_API_ACCESS_DENIED;
+        }
+
+        LOGD("checkResponse: policyType = %" PRIu16 ", metadata = %s",
+             checkResponse->m_resultRef.policyType(),
+             checkResponse->m_resultRef.metadata().c_str());
+    } catch (const ServerConnectionErrorException &ex) {
+        LOGE("Cynara service not available.");
+        return CYNARA_API_SERVICE_NOT_AVAILABLE;
+    }
+
+    result = checkResponse->m_resultRef;
+    return CYNARA_API_SUCCESS;
+}
+
 void Logic::onDisconnected(void) {
     m_cache->clear();
 }
index daab6f4..16d63ab 100644 (file)
@@ -26,6 +26,8 @@
 #include <string>
 
 #include <sockets/SocketClient.h>
+#include <types/PolicyKey.h>
+#include <types/PolicyResult.h>
 
 #include <api/ApiInterface.h>
 #include <cache/CacheInterface.h>
@@ -35,11 +37,10 @@ namespace Cynara {
 class Logic : public ApiInterface {
 private:
     SocketClientPtr m_socket;
-    ResultGetterInterfacePtr m_resultGetter;
     PluginCachePtr m_cache;
 
     void onDisconnected(void);
-
+    int requestResult(const PolicyKey &key, PolicyResult &result) noexcept;
 public:
     Logic();
     virtual ~Logic() {};