be95cee43738dd7f815431390f6b7e3fd1ca6dcb
[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(addPeerCallback, removePeerCallback),
40       mAcceptor(socketPath, std::bind(&Processor::addPeer, &mProcessor, _1))
41
42 {
43     LOGD("Creating server");
44 }
45
46 Service::~Service()
47 {
48     LOGD("Destroying server...");
49     try {
50         stop();
51     } catch (IPCException& e) {
52         LOGE("Error in Service's destructor: " << e.what());
53     }
54     LOGD("Destroyed");
55 }
56
57 void Service::start()
58 {
59     LOGD("Starting server");
60     mProcessor.start();
61
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.
64     mAcceptor.start();
65
66     LOGD("Started server");
67 }
68
69 bool Service::isStarted()
70 {
71     return mProcessor.isStarted();
72 }
73
74 void Service::stop()
75 {
76     LOGD("Stopping server..");
77     mAcceptor.stop();
78     mProcessor.stop();
79     LOGD("Stopped");
80 }
81
82 std::vector<FileDescriptor> Service::getFDs()
83 {
84     std::vector<FileDescriptor> fds;
85     fds.push_back(mAcceptor.getEventFD());
86     fds.push_back(mAcceptor.getConnectionFD());
87     fds.push_back(mProcessor.getEventFD());
88
89     return fds;
90 }
91
92 void Service::handle(const FileDescriptor fd, const short pollEvent)
93 {
94     if (fd == mProcessor.getEventFD() && pollEvent & POLLIN) {
95         mProcessor.handleEvent();
96         return;
97
98     } else if (fd == mAcceptor.getConnectionFD() && pollEvent & POLLIN) {
99         mAcceptor.handleConnection();
100         return;
101
102     } else if (fd == mAcceptor.getEventFD() && pollEvent & POLLIN) {
103         mAcceptor.handleEvent();
104         return;
105
106     } else if (pollEvent & POLLIN) {
107         mProcessor.handleInput(fd);
108         return;
109
110     } else if (pollEvent & POLLHUP) {
111         mProcessor.handleLostConnection(fd);
112         return;
113     }
114 }
115
116
117 void Service::setNewPeerCallback(const PeerCallback& newPeerCallback)
118 {
119     mProcessor.setNewPeerCallback(newPeerCallback);
120 }
121
122 void Service::setRemovedPeerCallback(const PeerCallback& removedPeerCallback)
123 {
124     mProcessor.setRemovedPeerCallback(removedPeerCallback);
125 }
126
127 void Service::removeMethod(const MethodID methodID)
128 {
129     LOGD("Removing method " << methodID);
130     mProcessor.removeMethod(methodID);
131     LOGD("Removed " << methodID);
132 }
133
134
135 } // namespace ipc
136 } // namespace vasum