IPC: Replace PeerID witch peer's file descriptor
[platform/core/security/vasum.git] / common / ipc / client.cpp
1 /*
2 *  Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
3 *
4 *  Contact: Jan Olszak <j.olszak@samsung.com>
5 *
6 *  Licensed under the Apache License, Version 2.0 (the "License");
7 *  you may not use this file except in compliance with the License.
8 *  You may obtain a copy of the License at
9 *
10 *      http://www.apache.org/licenses/LICENSE-2.0
11 *
12 *  Unless required by applicable law or agreed to in writing, software
13 *  distributed under the License is distributed on an "AS IS" BASIS,
14 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 *  See the License for the specific language governing permissions and
16 *  limitations under the License
17 */
18
19 /**
20  * @file
21  * @author  Jan Olszak (j.olszak@samsung.com)
22  * @brief   Handling client connections
23  */
24
25 #include "config.hpp"
26
27 #include "ipc/client.hpp"
28 #include "ipc/internals/socket.hpp"
29 #include "ipc/exception.hpp"
30
31 namespace security_containers {
32 namespace ipc {
33
34 Client::Client(const std::string& socketPath)
35     : mSocketPath(socketPath)
36 {
37     LOGD("Creating client");
38 }
39
40 Client::~Client()
41 {
42     LOGD("Destroying client...");
43     try {
44         stop();
45     } catch (IPCException& e) {
46         LOGE("Error in Client's destructor: " << e.what());
47     }
48     LOGD("Destroyed client");
49 }
50
51 void Client::start()
52 {
53     LOGD("Starting client...");
54
55     // Initialize the connection with the server
56     LOGD("Connecting to " + mSocketPath);
57     auto socketPtr = std::make_shared<Socket>(Socket::connectSocket(mSocketPath));
58     mServiceFD = mProcessor.addPeer(socketPtr);
59
60     // Start listening
61     mProcessor.start();
62
63     LOGD("Started client");
64 }
65
66 bool Client::isStarted()
67 {
68     return mProcessor.isStarted();
69 }
70
71 void Client::stop()
72 {
73     LOGD("Stopping client...");
74     mProcessor.stop();
75     LOGD("Stopped");
76 }
77
78 void Client::setNewPeerCallback(const PeerCallback& newPeerCallback)
79 {
80     mProcessor.setNewPeerCallback(newPeerCallback);
81 }
82
83 void Client::setRemovedPeerCallback(const PeerCallback& removedPeerCallback)
84 {
85     mProcessor.setRemovedPeerCallback(removedPeerCallback);
86 }
87
88 void Client::removeMethod(const MethodID methodID)
89 {
90     LOGD("Removing method id: " << methodID);
91     mProcessor.removeMethod(methodID);
92 }
93
94 } // namespace ipc
95 } // namespace security_containers