2 // * Copyright (c) 2014 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] ", addPeerCallback, removePeerCallback),
40 mAcceptor(socketPath, std::bind(&Processor::addPeer, &mProcessor, _1))
43 LOGS("Service Constructor");
48 LOGS("Service Destructor");
51 } catch (IPCException& e) {
52 LOGE("Error in Service's destructor: " << e.what());
56 void Service::start(const bool usesExternalPolling)
58 LOGS("Service start");
59 mProcessor.start(usesExternalPolling);
61 // There can be an incoming connection from mAcceptor before mProcessor is listening,
62 // but it's OK. It will handle the connection when ready. So no need to wait for mProcessor.
63 if (!usesExternalPolling) {
68 bool Service::isStarted()
70 return mProcessor.isStarted();
80 std::vector<FileDescriptor> Service::getFDs()
82 std::vector<FileDescriptor> fds;
83 fds.push_back(mAcceptor.getEventFD());
84 fds.push_back(mAcceptor.getConnectionFD());
85 fds.push_back(mProcessor.getEventFD());
90 void Service::handle(const FileDescriptor fd, const short pollEvent)
92 if (fd == mProcessor.getEventFD() && (pollEvent & POLLIN)) {
93 mProcessor.handleEvent();
96 } else if (fd == mAcceptor.getConnectionFD() && (pollEvent & POLLIN)) {
97 mAcceptor.handleConnection();
100 } else if (fd == mAcceptor.getEventFD() && (pollEvent & POLLIN)) {
101 mAcceptor.handleEvent();
104 } else if (pollEvent & POLLIN) {
105 mProcessor.handleInput(fd);
108 } else if (pollEvent & POLLHUP) {
109 mProcessor.handleLostConnection(fd);
115 void Service::setNewPeerCallback(const PeerCallback& newPeerCallback)
117 LOGS("Service setNewPeerCallback");
118 mProcessor.setNewPeerCallback(newPeerCallback);
121 void Service::setRemovedPeerCallback(const PeerCallback& removedPeerCallback)
123 LOGS("Service setRemovedPeerCallback");
124 mProcessor.setRemovedPeerCallback(removedPeerCallback);
127 void Service::removeMethod(const MethodID methodID)
129 LOGS("Service removeMethod methodID: " << methodID);
130 mProcessor.removeMethod(methodID);