2 * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved
4 * Contact: Jan Olszak <j.olszak@samsung.com>
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
10 * http://www.apache.org/licenses/LICENSE-2.0
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
21 * @author Jan Olszak (j.olszak@samsung.com)
22 * @brief Handling client connections
27 #include "ipc/client.hpp"
28 #include "ipc/internals/socket.hpp"
29 #include "ipc/exception.hpp"
34 Client::Client(const std::string& socketPath)
35 : mProcessor("[CLIENT] "),
36 mSocketPath(socketPath)
38 LOGS("Client Constructor");
39 setNewPeerCallback(nullptr);
40 setRemovedPeerCallback(nullptr);
45 LOGS("Client Destructor");
48 } catch (IPCException& e) {
49 LOGE("Error in Client's destructor: " << e.what());
53 void Client::start(const bool usesExternalPolling)
56 // Initialize the connection with the server
57 if (usesExternalPolling) {
60 mProcessor.start(usesExternalPolling);
62 LOGD("Connecting to " + mSocketPath);
63 auto socketPtr = std::make_shared<Socket>(Socket::connectSocket(mSocketPath));
64 mServiceFD = mProcessor.addPeer(socketPtr);
67 bool Client::isStarted()
69 return mProcessor.isStarted();
82 void Client::startPoll()
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();
91 void Client::stopPoll()
93 LOGS("Client stopPoll");
95 mIPCGSourcePtr->removeFD(mProcessor.getEventFD());
96 mIPCGSourcePtr->detach();
97 mIPCGSourcePtr.reset();
100 void Client::handle(const FileDescriptor fd, const short pollEvent)
102 LOGS("Client handle");
105 LOGW("Client stopped");
109 if (fd == mProcessor.getEventFD() && (pollEvent & POLLIN)) {
110 mProcessor.handleEvent();
113 } else if (pollEvent & POLLIN) {
114 mProcessor.handleInput(fd);
117 } else if (pollEvent & POLLHUP) {
118 mProcessor.handleLostConnection(fd);
123 void Client::setNewPeerCallback(const PeerCallback& newPeerCallback)
125 LOGS("Client setNewPeerCallback");
126 auto callback = [newPeerCallback, this](FileDescriptor fd) {
127 if (mIPCGSourcePtr) {
128 mIPCGSourcePtr->addFD(fd);
130 if (newPeerCallback) {
134 mProcessor.setNewPeerCallback(callback);
137 void Client::setRemovedPeerCallback(const PeerCallback& removedPeerCallback)
139 LOGS("Client setRemovedPeerCallback");
140 auto callback = [removedPeerCallback, this](FileDescriptor fd) {
141 if (mIPCGSourcePtr) {
142 mIPCGSourcePtr->removeFD(fd);
144 if (removedPeerCallback) {
145 removedPeerCallback(fd);
148 mProcessor.setRemovedPeerCallback(callback);
151 void Client::removeMethod(const MethodID methodID)
153 LOGS("Client removeMethod methodID: " << methodID);
154 mProcessor.removeMethod(methodID);