tizen 2.3.1 release
[framework/web/wearable/wrt-security.git] / socket_connection / client / SecuritySocketClient.h
1 /*
2  * Copyright (c) 2012 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  *    Licensed under the Apache License, Version 2.0 (the "License");
5  *    you may not use this file except in compliance with the License.
6  *    You may obtain a copy of the License at
7  *
8  *        http://www.apache.org/licenses/LICENSE-2.0
9  *
10  *    Unless required by applicable law or agreed to in writing, software
11  *    distributed under the License is distributed on an "AS IS" BASIS,
12  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *    See the License for the specific language governing permissions and
14  *    limitations under the License.
15  */
16 /**
17  * @file        SecuritySocketClient.h
18  * @author      Zofia Abramowska (z.abramowska@samsung.com)
19  * @version     1.0
20  * @brief       Header of socket client class.
21  */
22
23 #ifndef SECURITYSOCKETCLIENT_H_
24 #define SECURITYSOCKETCLIENT_H_
25
26 #include <memory>
27 #include <string>
28 #include <dpl/log/log.h>
29 #include "SocketConnection.h"
30
31 /* IMPORTANT:
32  * Methods connect(), call() and disconnected() should be called one by one.
33  * Between connect() and disconnect() you can use call() only once.
34  * It is because of timeout on call, e.g. to avoid waiting for corrupted data.
35  */
36
37 /* USAGE:
38  * Class should be used according to this scheme:
39  * SecuritySocketClient client("Interface Name");
40  * (...)
41  * client.connect();
42  * client.call("Method name", in_arg1, in_arg2, ..., in_argN,
43  *             out_arg1, out_arg2, ..., out_argM);
44  * client.disconnect();
45  * (...)
46  *
47  * input parameters of the call are passed with reference,
48  * output ones are passed as pointers - parameters MUST be passed this way.
49  *
50  * Currently client supports serialization and deserialization of simple types
51  * (int, char, float, unsigned), strings (std::string and char*) and
52  * some STL containers (std::vector, std::list, std::map, std::pair).
53  * Structures and classes are not (yet) supported.
54  */
55
56 class SecuritySocketClient {
57 public:
58     class Exception
59     {
60     public:
61         DECLARE_EXCEPTION_TYPE(DPL::Exception, Base)
62         DECLARE_EXCEPTION_TYPE(Base, SecuritySocketClientException)
63     };
64
65     SecuritySocketClient(const std::string &interfaceName);
66     void connect();
67     void disconnect();
68
69     void call(std::string methodName){
70         make_call(m_interfaceName);
71         make_call(methodName);
72     }
73
74     template<typename ...Args>
75     void call(std::string methodName, const Args&... args){
76         make_call(m_interfaceName);
77         make_call(methodName);
78         make_call(args...);
79     }
80
81 private:
82     template<typename T, typename ...Args>
83     void make_call(const T& invalue, const Args&... args){
84         make_call(invalue);
85         make_call(args...);
86     }
87
88     template<typename T>
89     void make_call(const T& invalue){
90         Try {
91             m_socketConnector->write(invalue);
92         }
93         Catch (SocketConnection::Exception::SocketConnectionException){
94             LogError("Socket connection write error");
95             ReThrowMsg(Exception::SecuritySocketClientException,"Socket connection write error");
96         }
97     }
98
99     template<typename T, typename ...Args>
100     void make_call(const T* invalue, const Args&... args){
101         make_call(invalue);
102         make_call(args...);
103     }
104
105     template<typename T>
106     void make_call(const T* invalue){
107         Try {
108             m_socketConnector->write(invalue);
109         }
110         Catch (SocketConnection::Exception::SocketConnectionException){
111             LogError("Socket connection write error");
112             ReThrowMsg(Exception::SecuritySocketClientException,"Socket connection write error");
113         }
114     }
115
116     template<typename T, typename ...Args>
117     void make_call(T * outvalue, const Args&... args){
118         make_call(outvalue);
119         make_call(args...);
120     }
121
122     template<typename T>
123     void make_call(T* outvalue){
124         Try {
125             m_socketConnector->read(outvalue);
126         }
127         Catch (SocketConnection::Exception::SocketConnectionException){
128             LogError("Socket connection read error");
129             ReThrowMsg(Exception::SecuritySocketClientException,"Socket connection read error");
130         }
131     }
132
133
134 private:
135     void throwWithErrnoMessage(const std::string& specificInfo);
136     std::string m_serverAddress;
137     std::string m_interfaceName;
138     std::unique_ptr<SocketConnection> m_socketConnector;
139     int m_socketFd;
140 };
141
142 #endif /* SECURITYSOCKETCLIENT_H_ */