Add create/destroy in krate-admin-cli
[platform/core/security/krate.git] / lib / client.cpp
1 /*
2  *  Copyright (c) 2015 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 #include "client.h"
18
19 namespace {
20
21 const std::string SUBSCRIBER_REGISTER = "Server::registerNotificationSubscriber";
22 const std::string SUBSCRIBER_UNREGISTER = "Server::unregisterNotificationSubscriber";
23
24 const std::string KRATE_MANAGER_ADDRESS = "/tmp/.krate.sock";
25
26 } // namespace
27
28
29 KrateContext::KrateContext() noexcept
30 {
31 }
32
33 KrateContext::~KrateContext() noexcept
34 {
35         disconnect();
36 }
37
38 int KrateContext::connect(const std::string& address) noexcept
39 {
40         try {
41                 client.reset(new rmi::Client(address));
42                 client->connect();
43         } catch (runtime::Exception& e) {
44                 return -1;
45         }
46
47         return 0;
48 }
49
50 int KrateContext::connect() noexcept
51 {
52         return connect(KRATE_MANAGER_ADDRESS);
53 }
54
55 void KrateContext::disconnect() noexcept
56 {
57         client.reset();
58 }
59
60 int KrateContext::subscribeSignal(const std::string& name,
61                                                                         const SignalListener& listener,
62                                                                         void* data)
63 {
64         auto listenerDispatcher = [listener, data](std::string &name, std::string &from, std::string &object) {
65                 listener(from.c_str(), object.c_str(), data);
66         };
67
68         try {
69                 return client->subscribe<std::string, std::string, std::string>(SUBSCRIBER_REGISTER,
70                                                                                                                                                 name, listenerDispatcher);
71         } catch (runtime::Exception& e) {
72                 std::cout << e.what() << std::endl;
73                 return -1;
74         }
75 }
76
77 int KrateContext::unsubscribeSignal(int subscriberId)
78 {
79         return client->unsubscribe(SUBSCRIBER_UNREGISTER, subscriberId);
80 }