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 Implementation of the IPC handling class
27 #include "ipc/service.hpp"
28 #include "ipc/exception.hpp"
29 #include "logger/logger.hpp"
31 using namespace std::placeholders;
36 Service::Service(const std::string& socketPath,
37 const PeerCallback& addPeerCallback,
38 const PeerCallback& removePeerCallback)
39 : mProcessor("[SERVICE] "),
40 mAcceptor(socketPath, std::bind(&Processor::addPeer, &mProcessor, _1))
43 LOGS("Service Constructor");
44 setNewPeerCallback(addPeerCallback);
45 setRemovedPeerCallback(removePeerCallback);
50 LOGS("Service Destructor");
53 } catch (IPCException& e) {
54 LOGE("Error in Service's destructor: " << e.what());
58 void Service::start(const bool usesExternalPolling)
60 LOGS("Service start");
61 if (usesExternalPolling) {
64 mProcessor.start(usesExternalPolling);
66 // There can be an incoming connection from mAcceptor before mProcessor is listening,
67 // but it's OK. It will handle the connection when ready. So no need to wait for mProcessor.
68 if (!usesExternalPolling) {
73 bool Service::isStarted()
75 return mProcessor.isStarted();
89 void Service::startPoll()
91 LOGS("Service startPoll");
93 mIPCGSourcePtr = IPCGSource::create(std::bind(&Service::handle, this, _1, _2));
94 mIPCGSourcePtr->addFD(mAcceptor.getEventFD());
95 mIPCGSourcePtr->addFD(mAcceptor.getConnectionFD());
96 mIPCGSourcePtr->addFD(mProcessor.getEventFD());
97 mIPCGSourcePtr->attach();
100 void Service::stopPoll()
102 LOGS("Service stopPoll");
104 mIPCGSourcePtr->removeFD(mAcceptor.getEventFD());
105 mIPCGSourcePtr->removeFD(mAcceptor.getConnectionFD());
106 mIPCGSourcePtr->removeFD(mProcessor.getEventFD());
107 mIPCGSourcePtr->detach();
108 mIPCGSourcePtr.reset();
111 void Service::handle(const FileDescriptor fd, const short pollEvent)
113 LOGS("Service handle");
116 LOGW("Service stopped");
120 if (fd == mProcessor.getEventFD() && (pollEvent & POLLIN)) {
121 mProcessor.handleEvent();
124 } else if (fd == mAcceptor.getConnectionFD() && (pollEvent & POLLIN)) {
125 mAcceptor.handleConnection();
128 } else if (fd == mAcceptor.getEventFD() && (pollEvent & POLLIN)) {
129 mAcceptor.handleEvent();
132 } else if (pollEvent & POLLIN) {
133 mProcessor.handleInput(fd);
136 } else if (pollEvent & POLLHUP) {
137 mProcessor.handleLostConnection(fd);
142 void Service::setNewPeerCallback(const PeerCallback& newPeerCallback)
144 LOGS("Service setNewPeerCallback");
145 auto callback = [newPeerCallback, this](FileDescriptor fd) {
146 if (mIPCGSourcePtr) {
147 mIPCGSourcePtr->addFD(fd);
149 if (newPeerCallback) {
153 mProcessor.setNewPeerCallback(callback);
156 void Service::setRemovedPeerCallback(const PeerCallback& removedPeerCallback)
158 LOGS("Service setRemovedPeerCallback");
159 auto callback = [removedPeerCallback, this](FileDescriptor fd) {
160 if (mIPCGSourcePtr) {
161 mIPCGSourcePtr->removeFD(fd);
163 if (removedPeerCallback) {
164 removedPeerCallback(fd);
167 mProcessor.setRemovedPeerCallback(callback);
170 void Service::removeMethod(const MethodID methodID)
172 LOGS("Service removeMethod methodID: " << methodID);
173 mProcessor.removeMethod(methodID);