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