Add SocketClient class
authorLukasz Wojciechowski <l.wojciechow@partner.samsung.com>
Mon, 30 Jun 2014 17:51:30 +0000 (19:51 +0200)
committerRafal Krypa <r.krypa@samsung.com>
Thu, 3 Jul 2014 12:19:10 +0000 (14:19 +0200)
SocketClient class is a class in client library that is
responsible for communication with Cynara service.
It has simple API askCynaraServer() which allows to send a request
and receive response with information about connection state.
Inside it keeps:
Socket - a client socket implementation;
Protocol - for serialization and deserialization of requests and
responses;
Queues - needed for writing and receiving data.

Change-Id: I6f54edd7a7986c60e50eb3119e431fc17da0646c

src/client/CMakeLists.txt
src/client/sockets/SocketClient.cpp [new file with mode: 0644]
src/client/sockets/SocketClient.h [new file with mode: 0644]

index 9b5e695..2f51764 100644 (file)
@@ -25,6 +25,7 @@ SET(LIB_CYNARA_SOURCES
     ${CYNARA_LIB_CYNARA_PATH}/api/client-api.cpp
     ${CYNARA_LIB_CYNARA_PATH}/logic/Logic.cpp
     ${CYNARA_LIB_CYNARA_PATH}/sockets/Socket.cpp
+    ${CYNARA_LIB_CYNARA_PATH}/sockets/SocketClient.cpp
     )
 
 INCLUDE_DIRECTORIES(
diff --git a/src/client/sockets/SocketClient.cpp b/src/client/sockets/SocketClient.cpp
new file mode 100644 (file)
index 0000000..7d47d48
--- /dev/null
@@ -0,0 +1,71 @@
+/*
+ *  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        SocketClient.cpp
+ * @author      Lukasz Wojciechowski <l.wojciechow@partner.samsung.com>
+ * @version     1.0
+ * @brief       This file contains implementation of cynara's socket client
+ */
+
+#include <memory>
+#include <string>
+
+#include <log/log.h>
+#include <protocol/Protocol.h>
+#include <protocol/ProtocolClient.h>
+#include <request/pointers.h>
+#include <request/Request.h>
+#include <request/RequestContext.h>
+#include <response/pointers.h>
+
+#include <sockets/Socket.h>
+
+#include "SocketClient.h"
+
+namespace Cynara {
+
+const std::string clientSocketPath("/run/cynara/cynara.socket");
+
+SocketClient::SocketClient() : m_socket(clientSocketPath),
+    m_protocol(std::make_shared<ProtocolClient>()) {
+}
+
+ResponsePtr SocketClient::askCynaraServer(RequestPtr request) {
+    //pass request to protocol
+    RequestContextPtr context = std::make_shared<RequestContext>(-1, ResponseTakerPtr(),
+                                                                 m_writeQueue);
+    request->execute(request, m_protocol, context);
+
+    //send request to cynara
+    if (!m_socket.sendToServer(m_writeQueue)) {
+        LOGW("Error sending request to Cynara. Service not available.");
+        return nullptr;
+    }
+
+    // receive response from cynara
+    while (true) {
+        if (!m_socket.receiveFromServer(m_readQueue)) {
+            LOGW("Error receiving response from Cynara. Service not available.");
+            return nullptr;
+        }
+        ResponsePtr response = m_protocol->extractResponseFromBuffer(m_readQueue);
+        if (response) {
+            return response;
+        }
+    }
+}
+
+} // namespace Cynara
diff --git a/src/client/sockets/SocketClient.h b/src/client/sockets/SocketClient.h
new file mode 100644 (file)
index 0000000..f0af703
--- /dev/null
@@ -0,0 +1,59 @@
+/*
+ *  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        SocketClient.h
+ * @author      Lukasz Wojciechowski <l.wojciechow@partner.samsung.com>
+ * @version     1.0
+ * @brief       This file contains definition of cynara's socket client
+ */
+
+#ifndef SRC_CLIENT_SOCKETS_SOCKETCLIENT_H_
+#define SRC_CLIENT_SOCKETS_SOCKETCLIENT_H_
+
+#include <memory>
+
+#include <common.h>
+#include <protocol/Protocol.h>
+#include <request/pointers.h>
+#include <response/pointers.h>
+#include <response/ResponseTaker.h>
+
+#include <sockets/Socket.h>
+
+namespace Cynara {
+
+class SocketClient;
+typedef std::shared_ptr<SocketClient> SocketClientPtr;
+
+class SocketClient {
+private:
+    Socket m_socket;
+    ProtocolPtr m_protocol;
+    BinaryQueue m_readQueue;
+    BinaryQueue m_writeQueue;
+
+public:
+    SocketClient();
+    virtual ~SocketClient() = default;
+
+    //returns pointer to response
+    //        or nullptr when connection to cynara service is lost
+    ResponsePtr askCynaraServer(RequestPtr request);
+};
+
+} // namespace Cynara
+
+#endif /* SRC_CLIENT_SOCKETS_SOCKETCLIENT_H_ */