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(addPeerCallback, removePeerCallback),
40 mAcceptor(socketPath, std::bind(&Processor::addPeer, &mProcessor, _1))
43 LOGD("Creating server");
48 LOGD("Destroying server...");
51 } catch (IPCException& e) {
52 LOGE("Error in Service's destructor: " << e.what());
59 LOGD("Starting server");
62 // There can be an incoming connection from mAcceptor before mProcessor is listening,
63 // but it's OK. It will handle the connection when ready. So no need to wait for mProcessor.
66 LOGD("Started server");
69 bool Service::isStarted()
71 return mProcessor.isStarted();
76 LOGD("Stopping server..");
82 std::vector<FileDescriptor> Service::getFDs()
84 std::vector<FileDescriptor> fds;
85 fds.push_back(mAcceptor.getEventFD());
86 fds.push_back(mAcceptor.getConnectionFD());
87 fds.push_back(mProcessor.getEventFD());
92 void Service::handle(const FileDescriptor fd, const short pollEvent)
94 if (fd == mProcessor.getEventFD() && (pollEvent & POLLIN)) {
95 mProcessor.handleEvent();
98 } else if (fd == mAcceptor.getConnectionFD() && (pollEvent & POLLIN)) {
99 mAcceptor.handleConnection();
102 } else if (fd == mAcceptor.getEventFD() && (pollEvent & POLLIN)) {
103 mAcceptor.handleEvent();
106 } else if (pollEvent & POLLIN) {
107 mProcessor.handleInput(fd);
110 } else if (pollEvent & POLLHUP) {
111 mProcessor.handleLostConnection(fd);
117 void Service::setNewPeerCallback(const PeerCallback& newPeerCallback)
119 mProcessor.setNewPeerCallback(newPeerCallback);
122 void Service::setRemovedPeerCallback(const PeerCallback& removedPeerCallback)
124 mProcessor.setRemovedPeerCallback(removedPeerCallback);
127 void Service::removeMethod(const MethodID methodID)
129 LOGD("Removing method " << methodID);
130 mProcessor.removeMethod(methodID);
131 LOGD("Removed " << methodID);