b96bcd4258ecedc69e24cc8a5727b1e921d2e305
[platform/core/security/vasum.git] / common / ipc / service.cpp
1 // /*
2 // *  Copyright (c) 2014 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   Implementation of the IPC handling class
23 //  */
24
25 #include "config.hpp"
26
27 #include "ipc/service.hpp"
28 #include "ipc/exception.hpp"
29 #include "logger/logger.hpp"
30
31 using namespace std::placeholders;
32
33 namespace vasum {
34 namespace ipc {
35
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))
41
42 {
43     LOGS("Service Constructor");
44 }
45
46 Service::~Service()
47 {
48     LOGS("Service Destructor");
49     try {
50         stop();
51     } catch (IPCException& e) {
52         LOGE("Error in Service's destructor: " << e.what());
53     }
54 }
55
56 void Service::start(const bool usesExternalPolling)
57 {
58     LOGS("Service start");
59     mProcessor.start(usesExternalPolling);
60
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) {
64         mAcceptor.start();
65     }
66 }
67
68 bool Service::isStarted()
69 {
70     return mProcessor.isStarted();
71 }
72
73 void Service::stop()
74 {
75     LOGS("Service stop");
76     mAcceptor.stop();
77     mProcessor.stop();
78 }
79
80 std::vector<FileDescriptor> Service::getFDs()
81 {
82     std::vector<FileDescriptor> fds;
83     fds.push_back(mAcceptor.getEventFD());
84     fds.push_back(mAcceptor.getConnectionFD());
85     fds.push_back(mProcessor.getEventFD());
86
87     return fds;
88 }
89
90 void Service::handle(const FileDescriptor fd, const short pollEvent)
91 {
92     if (fd == mProcessor.getEventFD() && (pollEvent & POLLIN)) {
93         mProcessor.handleEvent();
94         return;
95
96     } else if (fd == mAcceptor.getConnectionFD() && (pollEvent & POLLIN)) {
97         mAcceptor.handleConnection();
98         return;
99
100     } else if (fd == mAcceptor.getEventFD() && (pollEvent & POLLIN)) {
101         mAcceptor.handleEvent();
102         return;
103
104     } else if (pollEvent & POLLIN) {
105         mProcessor.handleInput(fd);
106         return;
107
108     } else if (pollEvent & POLLHUP) {
109         mProcessor.handleLostConnection(fd);
110         return;
111     }
112 }
113
114
115 void Service::setNewPeerCallback(const PeerCallback& newPeerCallback)
116 {
117     LOGS("Service setNewPeerCallback");
118     mProcessor.setNewPeerCallback(newPeerCallback);
119 }
120
121 void Service::setRemovedPeerCallback(const PeerCallback& removedPeerCallback)
122 {
123     LOGS("Service setRemovedPeerCallback");
124     mProcessor.setRemovedPeerCallback(removedPeerCallback);
125 }
126
127 void Service::removeMethod(const MethodID methodID)
128 {
129     LOGS("Service removeMethod methodID: " << methodID);
130     mProcessor.removeMethod(methodID);
131 }
132
133
134 } // namespace ipc
135 } // namespace vasum