Add Echo service
authorZofia Abramowska <z.abramowska@samsung.com>
Tue, 20 May 2014 14:15:10 +0000 (16:15 +0200)
committerZofia Abramowska <z.abramowska@samsung.com>
Thu, 22 May 2014 16:07:19 +0000 (18:07 +0200)
Adding an example service for key-manager.

Change-Id: I72b30bed332785683fb104850a11b61c062ba8d1

12 files changed:
packaging/key-manager.spec
src/CMakeLists.txt
src/include/key-manager.h
src/manager/client/client-echo.cpp [new file with mode: 0644]
src/manager/common/protocols.cpp
src/manager/common/protocols.h
src/manager/main/key-manager-main.cpp
src/manager/service/echo.cpp [new file with mode: 0644]
src/manager/service/echo.h [new file with mode: 0644]
systemd/CMakeLists.txt
systemd/key-manager-echo.socket [new file with mode: 0644]
systemd/key-manager.service

index 77508b7..23f22cb 100644 (file)
@@ -72,6 +72,7 @@ mkdir -p %{buildroot}/etc/security/
 mkdir -p %{buildroot}/usr/lib/systemd/system/multi-user.target.wants
 mkdir -p %{buildroot}/usr/lib/systemd/system/sockets.target.wants
 ln -s ../key-manager.service %{buildroot}/usr/lib/systemd/system/multi-user.target.wants/key-manager.service
+ln -s ../key-manager-echo.socket %{buildroot}/usr/lib/systemd/system/sockets.target.wants/key-manager-echo.socket
 
 %clean
 rm -rf %{buildroot}
@@ -112,6 +113,8 @@ fi
 %attr(-,root,root) /usr/lib/systemd/system/multi-user.target.wants/key-manager.service
 %attr(-,root,root) /usr/lib/systemd/system/key-manager.service
 %attr(-,root,root) /usr/lib/systemd/system/key-manager.target
+%attr(-,root,root) /usr/lib/systemd/system/sockets.target.wants/key-manager-echo.socket 
+%attr(-,root,root) /usr/lib/systemd/system/key-manager-echo.socket 
 %{_datadir}/license/%{name}
 
 %files -n libkey-manager-client
index 4735dc7..07c477a 100644 (file)
@@ -14,6 +14,7 @@ SET(KEY_MANAGER_SOURCES
     ${KEY_MANAGER_PATH}/main/generic-socket-manager.cpp
     ${KEY_MANAGER_PATH}/main/socket-manager.cpp
     ${KEY_MANAGER_PATH}/main/key-manager-main.cpp
+    ${KEY_MANAGER_PATH}/service/echo.cpp
     )
 
 SET_SOURCE_FILES_PROPERTIES(
@@ -57,6 +58,7 @@ INCLUDE_DIRECTORIES(
 
 SET(KEY_MANAGER_CLIENT_SOURCES
     ${KEY_MANAGER_CLIENT_SRC_PATH}/client-common.cpp
+    ${KEY_MANAGER_CLIENT_SRC_PATH}/client-echo.cpp
     )
 
 ADD_LIBRARY(${TARGET_KEY_MANAGER_CLIENT} SHARED ${KEY_MANAGER_CLIENT_SOURCES})
index d25517a..a5e87c7 100644 (file)
 extern "C" {
 #endif
 
+/*
+ * This function was created mainly for testing key-manager client/service
+ * proper behaviour. It sends a message and returns message from service,
+ * which should be a pure echo.
+ *
+ * \param[in] Message for service
+ * \param[out] Response from service
+ *
+ * \return KEY_MANAGER_API_ERROR_INPUT_PARAM when trying to pass NULL message
+ * \return KEY_MANAGER_API_SUCCESS on success
+ */
 
+int key_manager_echo(const char* echo, char** oche);
 
 
 #ifdef __cplusplus
diff --git a/src/manager/client/client-echo.cpp b/src/manager/client/client-echo.cpp
new file mode 100644 (file)
index 0000000..e9ea64c
--- /dev/null
@@ -0,0 +1,57 @@
+/* Copyright (c) 2000 - 2013 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ *  Contact: Bumjin Im <bj.im@samsung.com>
+ *
+ *  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        client-echo.cpp
+ * @author      Zofia Abramowska (z.abramowska@samsung.com)
+ * @version     1.0
+ * @version     This file contains example of key-manager client implementation
+ */
+
+#include <dpl/log/log.h>
+#include <dpl/exception.h>
+
+#include <message-buffer.h>
+#include <client-common.h>
+#include <protocols.h>
+
+#include <key-manager.h>
+
+KEY_MANAGER_API
+int key_manager_echo(const char *echo, char** oche) {
+    using namespace CentralKeyManager;
+
+    if(echo == NULL){
+        LogDebug("Echo message is null");
+        return KEY_MANAGER_API_ERROR_INPUT_PARAM;
+    }
+
+    MessageBuffer send, recv;
+    Serialization::Serialize(send, std::string(echo));
+
+    int retCode = sendToServer(SERVICE_SOCKET_ECHO, send.Pop(), recv);
+
+    if(retCode != KEY_MANAGER_API_SUCCESS)
+        return retCode;
+
+    std::string response;
+    Deserialization::Deserialize(recv, response);
+
+    *oche = strdup(response.c_str());
+
+    return KEY_MANAGER_API_SUCCESS;
+}
index f12d7e2..5891808 100644 (file)
@@ -27,6 +27,7 @@
 
 namespace CentralKeyManager {
 
-
+    char const * const SERVICE_SOCKET_ECHO =
+        "/tmp/.key-manager-api-echo.sock";
 } // namespace CentralKeyManager
 
index 31bdc3e..6bbde5d 100644 (file)
@@ -29,6 +29,7 @@
 
 namespace CentralKeyManager {
 
+    extern char const * const SERVICE_SOCKET_ECHO;
 
 } // namespace CentralKeyManager
 
index 8964dd6..fde96bd 100644 (file)
@@ -30,6 +30,8 @@
 
 #include <socket-manager.h>
 
+#include <echo.h>
+
 IMPLEMENT_SAFE_SINGLETON(CentralKeyManager::Log::LogSystem);
 
 #define REGISTER_SOCKET_SERVICE(manager, service) \
@@ -76,6 +78,10 @@ int main(void) {
         LogInfo("Start!");
         CentralKeyManager::SocketManager manager;
 
+        auto echoService = new CentralKeyManager::EchoService;
+        echoService->Create();
+        manager.RegisterSocketService(echoService);
+
         manager.MainLoop();
     }
     UNHANDLED_EXCEPTION_HANDLER_END
diff --git a/src/manager/service/echo.cpp b/src/manager/service/echo.cpp
new file mode 100644 (file)
index 0000000..27f7f4c
--- /dev/null
@@ -0,0 +1,60 @@
+/*
+ *  Copyright (c) 2000 - 2013 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ *  Contact: Bumjin Im <bj.im@samsung.com>
+ *
+ *  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        server-main2.h
+ * @author      Bartlomiej Grzelewski (b.grzelewski@samsung.com)
+ * @version     1.0
+ * @brief       Implementation of sample service.
+ */
+
+#include <dpl/log/log.h>
+
+#include <protocols.h>
+#include <echo.h>
+
+namespace CentralKeyManager {
+
+GenericSocketService::ServiceDescriptionVector EchoService::GetServiceDescription() {
+    return ServiceDescriptionVector {{SERVICE_SOCKET_ECHO, "*"}};
+}
+
+void EchoService::accept(const AcceptEvent &event) {
+    LogDebug("Accept event. ConnectionID: " << event.connectionID.sock
+        << " ServiceID: " << event.interfaceID);
+}
+
+void EchoService::write(const WriteEvent &event) {
+    LogDebug("WriteEvent. ConnectionID: " << event.connectionID.sock <<
+        " Size: " << event.size << " Left: " << event.left);
+    if (event.left == 0)
+        m_serviceManager->Close(event.connectionID);
+}
+
+void EchoService::process(const ReadEvent &event) {
+    LogDebug("ReadEvent. ConnectionID: " << event.connectionID.sock <<
+      " Buffer size: " << event.rawBuffer.size());
+    m_serviceManager->Write(event.connectionID, event.rawBuffer);
+    LogDebug("Write completed");
+}
+
+void EchoService::close(const CloseEvent &event) {
+    LogDebug("CloseEvent. ConnectionID: " << event.connectionID.sock);
+}
+
+} // namespace CentralKeyManager
+
diff --git a/src/manager/service/echo.h b/src/manager/service/echo.h
new file mode 100644 (file)
index 0000000..cf3065e
--- /dev/null
@@ -0,0 +1,57 @@
+/*
+ *  Copyright (c) 2000 - 2013 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ *  Contact: Bumjin Im <bj.im@samsung.com>
+ *
+ *  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        echo.h
+ * @author      Bartlomiej Grzelewski (b.grzelewski@samsung.com)
+ * @version     1.0
+ * @brief       Sample service implementation.
+ */
+
+#ifndef _SECURITY_SERVER_ECHO_
+#define _SECURITY_SERVER_ECHO_
+
+#include <service-thread.h>
+#include <generic-socket-manager.h>
+
+#include <dpl/serialization.h>
+
+#include <message-buffer.h>
+
+namespace CentralKeyManager {
+
+class EchoService
+  : public CentralKeyManager::GenericSocketService
+  , public CentralKeyManager::ServiceThread<EchoService>
+{
+public:
+    ServiceDescriptionVector GetServiceDescription();
+
+    DECLARE_THREAD_EVENT(AcceptEvent, accept)
+    DECLARE_THREAD_EVENT(WriteEvent, write)
+    DECLARE_THREAD_EVENT(ReadEvent, process)
+    DECLARE_THREAD_EVENT(CloseEvent, close)
+
+    void accept(const AcceptEvent &event);
+    void write(const WriteEvent &event);
+    void process(const ReadEvent &event);
+    void close(const CloseEvent &event);
+};
+
+} // namespace CentralKeyManager
+
+#endif // _SECURITY_SERVER_ECHO_
index 8533981..07d5643 100644 (file)
@@ -1,6 +1,7 @@
 INSTALL(FILES
     ${CMAKE_SOURCE_DIR}/systemd/key-manager.service
     ${CMAKE_SOURCE_DIR}/systemd/key-manager.target
+    ${CMAKE_SOURCE_DIR}/systemd/key-manager-echo.socket
     DESTINATION
     /usr/lib/systemd/system
 )
diff --git a/systemd/key-manager-echo.socket b/systemd/key-manager-echo.socket
new file mode 100644 (file)
index 0000000..8adf906
--- /dev/null
@@ -0,0 +1,14 @@
+[Socket]
+ListenStream=/tmp/.key-manager-api-echo.sock
+SockMode=0777
+SmackLabelIpIn=*
+SmackLabelIpOut=@
+
+Service=key-manager.service
+
+[Unit]
+Wants=key-manager.service
+Before=key-manager.service
+
+[Install]
+WantedBy=sockets.target
index d6d1381..c33689c 100644 (file)
@@ -4,6 +4,7 @@ Description=Start the Central Key Manager
 [Service]
 Type=notify
 ExecStart=/usr/bin/key-manager
+Sockets=key-manager-echo.socket
 
 [Install]
 WantedBy=multi-user.target