IPC: Support older glib
[platform/core/security/vasum.git] / common / ipc / client.cpp
1 /*
2 *  Copyright (c) 2015 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 vasum {
32 namespace ipc {
33
34 Client::Client(const std::string& socketPath)
35     : mProcessor("[CLIENT]  "),
36       mSocketPath(socketPath)
37 {
38     LOGS("Client Constructor");
39     setNewPeerCallback(nullptr);
40     setRemovedPeerCallback(nullptr);
41 }
42
43 Client::~Client()
44 {
45     LOGS("Client Destructor");
46     try {
47         stop();
48     } catch (IPCException& e) {
49         LOGE("Error in Client's destructor: " << e.what());
50     }
51 }
52
53 void Client::start(const bool usesExternalPolling)
54 {
55     LOGS("Client start");
56     // Initialize the connection with the server
57     if (usesExternalPolling) {
58         startPoll();
59     }
60     mProcessor.start(usesExternalPolling);
61
62     LOGD("Connecting to " + mSocketPath);
63     auto socketPtr = std::make_shared<Socket>(Socket::connectSocket(mSocketPath));
64     mServiceFD = mProcessor.addPeer(socketPtr);
65 }
66
67 bool Client::isStarted()
68 {
69     return mProcessor.isStarted();
70 }
71
72 void Client::stop()
73 {
74     LOGS("Client stop");
75     mProcessor.stop();
76
77     if (mIPCGSourcePtr) {
78         stopPoll();
79     }
80 }
81
82 void Client::startPoll()
83 {
84     LOGS("Client startPoll");
85     using namespace std::placeholders;
86     mIPCGSourcePtr = IPCGSource::create(std::bind(&Client::handle, this, _1, _2));
87     mIPCGSourcePtr->addFD(mProcessor.getEventFD());
88     mIPCGSourcePtr->attach();
89 }
90
91 void Client::stopPoll()
92 {
93     LOGS("Client stopPoll");
94
95     mIPCGSourcePtr->removeFD(mProcessor.getEventFD());
96     mIPCGSourcePtr->detach();
97     mIPCGSourcePtr.reset();
98 }
99
100 void Client::handle(const FileDescriptor fd, const short pollEvent)
101 {
102     LOGS("Client handle");
103
104     if (!isStarted()) {
105         LOGW("Client stopped");
106         return;
107     }
108
109     if (fd == mProcessor.getEventFD() && (pollEvent & POLLIN)) {
110         mProcessor.handleEvent();
111         return;
112
113     } else if (pollEvent & POLLIN) {
114         mProcessor.handleInput(fd);
115         return;
116
117     } else if (pollEvent & POLLHUP) {
118         mProcessor.handleLostConnection(fd);
119         return;
120     }
121 }
122
123 void Client::setNewPeerCallback(const PeerCallback& newPeerCallback)
124 {
125     LOGS("Client setNewPeerCallback");
126     auto callback = [newPeerCallback, this](FileDescriptor fd) {
127         if (mIPCGSourcePtr) {
128             mIPCGSourcePtr->addFD(fd);
129         }
130         if (newPeerCallback) {
131             newPeerCallback(fd);
132         }
133     };
134     mProcessor.setNewPeerCallback(callback);
135 }
136
137 void Client::setRemovedPeerCallback(const PeerCallback& removedPeerCallback)
138 {
139     LOGS("Client setRemovedPeerCallback");
140     auto callback = [removedPeerCallback, this](FileDescriptor fd) {
141         if (mIPCGSourcePtr) {
142             mIPCGSourcePtr->removeFD(fd);
143         }
144         if (removedPeerCallback) {
145             removedPeerCallback(fd);
146         }
147     };
148     mProcessor.setRemovedPeerCallback(callback);
149 }
150
151 void Client::removeMethod(const MethodID methodID)
152 {
153     LOGS("Client removeMethod methodID: " << methodID);
154     mProcessor.removeMethod(methodID);
155 }
156
157 } // namespace ipc
158 } // namespace vasum