tizen 2.3.1 release
[framework/web/mobile/wrt-security.git] / communication_client / include / SecurityCommunicationClient.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        SecurityCommunicationClient.h
18  * @author      Zofia Abramowska (z.abramowska@samsung.com)
19  * @version     1.0
20  * @brief       This is header of class used by IPC client with implemented templates
21  *
22  */
23
24 /*
25  * This class hides implementation of specific communication types
26  * and enables switching between them by #defined macros.
27  *
28  * supported types : DBUS_CONNECTION
29  *
30  * IMPORTANT : Exactly ONE type MUST be defined.
31  *
32  */
33
34 #ifndef SECURITYCOMMUNICATIONCLIENT_H_
35 #define SECURITYCOMMUNICATIONCLIENT_H_
36
37 #ifdef DBUS_CONNECTION
38 #include <dpl/dbus/dbus_client.h>
39 #endif
40 #include <dpl/log/log.h>
41 #include "SecuritySocketClient.h"
42 #include <string>
43 #include <memory>
44
45
46 namespace WrtSecurity {
47 namespace Communication {
48 class Client
49 {
50 public:
51     class Exception
52     {
53     public:
54         DECLARE_EXCEPTION_TYPE(DPL::Exception, Base)
55         DECLARE_EXCEPTION_TYPE(Base, SecurityCommunicationClientException)
56     };
57
58     explicit Client(const std::string &intefaceName);
59
60
61
62     template<typename ... Args>
63     void call(const char* methodName, const Args& ... args)
64     {
65
66         connect();
67         Try{
68         #ifdef DBUS_CONNECTION
69             m_dbusClient->call(methodName, args...);
70         } Catch (DPL::DBus::Client::Exception::DBusClientException){
71         #endif //DBUS_CONNECTION
72         #ifdef SOCKET_CONNECTION
73             m_socketClient->call(methodName, args...);
74         } Catch (SecuritySocketClient::Exception::SecuritySocketClientException){
75         #endif //SOCKET_CONNECTION
76             LogError("Error getting response");
77             disconnect();
78             ReThrowMsg(Exception::SecurityCommunicationClientException,
79                        "Error getting response");
80         }
81         LogInfo("Call served");
82         disconnect();
83   }
84
85     template<typename ...Args>
86     void call(std::string methodName, const Args&... args)
87     {
88         call(methodName.c_str(), args...);
89     }
90
91
92 private:
93
94     void connect();
95     void disconnect();
96
97     std::string m_interfaceName;
98     #ifdef DBUS_CONNECTION
99     std::unique_ptr<DPL::DBus::Client> m_dbusClient;
100     #endif
101
102     #ifdef SOCKET_CONNECTION
103     std::unique_ptr<SecuritySocketClient> m_socketClient;
104     #endif
105 };
106 } // namespace Communication
107 } // namespace WrtSecurity
108
109 #endif /* SECURITYCOMMUNICATIONCLIENT_H_ */