Tizen 2.1 base
[framework/security/security-server.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 #include <dpl/dbus/dbus_client.h>
38 #include <dpl/log/log.h>
39 #include <dpl/scoped_ptr.h>
40 #include "SecuritySocketClient.h"
41 #include <string>
42 #include <memory>
43
44
45 namespace WrtSecurity {
46 namespace Communication {
47 class Client
48 {
49 public:
50     class Exception
51     {
52     public:
53         DECLARE_EXCEPTION_TYPE(DPL::Exception, Base)
54         DECLARE_EXCEPTION_TYPE(Base, SecurityCommunicationClientException)
55     };
56
57     explicit Client(const std::string &intefaceName);
58
59
60
61     template<typename ... Args>
62     void call(const char* methodName, const Args& ... args)
63     {
64
65         connect();
66         Try{
67         #ifdef DBUS_CONNECTION
68             m_dbusClient->call(methodName, args...);
69         } Catch (DPL::DBus::Client::Exception::DBusClientException){
70         #endif //DBUS_CONNECTION
71         #ifdef SOCKET_CONNECTION
72             m_socketClient->call(methodName, args...);
73         } Catch (SecuritySocketClient::Exception::SecuritySocketClientException){
74         #endif //SOCKET_CONNECTION
75             LogError("Error getting response");
76             disconnect();
77             ReThrowMsg(Exception::SecurityCommunicationClientException,
78                        "Error getting response");
79         }
80         LogInfo("Call served");
81         disconnect();
82   }
83
84     template<typename ...Args>
85     void call(std::string methodName, const Args&... args)
86     {
87         call(methodName.c_str(), args...);
88     }
89
90
91 private:
92
93     void connect();
94     void disconnect();
95
96     std::string m_interfaceName;
97     #ifdef DBUS_CONNECTION
98     std::unique_ptr<DPL::DBus::Client> m_dbusClient;
99     #endif
100
101     #ifdef SOCKET_CONNECTION
102     std::unique_ptr<SecuritySocketClient> m_socketClient;
103     #endif
104 };
105 } // namespace Communication
106 } // namespace WrtSecurity
107
108 #endif /* SECURITYCOMMUNICATIONCLIENT_H_ */