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