Fix coverity issue
[platform/core/security/ode.git] / lib / client.h
1 /*
2  *  Copyright (c) 2015-2017 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 #ifndef __ODE_CLIENT_H__
18 #define __ODE_CLIENT_H__
19
20 #include <string>
21 #include <memory>
22 #include <functional>
23
24 #include <klay/rmi/client.h>
25 #include <rmi/common.h>
26
27 typedef std::function<void(void*)> SignalListener;
28
29 typedef std::unique_ptr<rmi::Client> RmiClientPtr;
30
31 class ClientContext final {
32         const char* SUBSCRIBER_REGISTER = "ServerContext::registerNotificationSubscriber";
33         const char* SUBSCRIBER_UNREGISTER = "ServerContext::unregisterNotificationSubscriber";
34
35 public:
36         ClientContext() noexcept;
37         ~ClientContext() noexcept;
38
39         int connect() noexcept;
40         int connect(const std::string& address) noexcept;
41         void disconnect() noexcept;
42
43         /*
44          *  TODO use more generic subscription method below in internal/external
45          *  encryption and remove this one.
46          */
47         int subscribeSignal(const std::string& name, const SignalListener& listener, void* data);
48         void unsubscribeSignal(int subscriberId);
49
50         template<typename ...Args>
51         int subscribeSignal(const std::string& name, const std::function<void(Args...)>& listener)
52         {
53                 try {
54                         int ret = client->subscribe<Args...>(SUBSCRIBER_REGISTER, name, listener);
55                         if (ret < 0)
56                                 return ode::error::Unknown;
57                         return ret;
58                 } catch (runtime::Exception& e) {
59                         std::cout << e.what() << std::endl;
60                         return ode::error::Unknown;
61                 }
62         }
63
64         template<typename Interface, typename... Args>
65         Interface createInterface(Args&&... args) noexcept
66         {
67                 return Interface(client, std::forward<Args>(args)...);
68         }
69
70 private:
71         RmiClientPtr client;
72 };
73
74 #endif //__ODE_CLIENT_H__