Prevent from server termination after SIGPIPE
[platform/core/security/vasum.git] / server / host-ipc-connection.hpp
1 /*
2  *  Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  *  Contact: Mateusz Malicki <m.malicki2@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  Mateusz Malicki (m.malicki2@samsung.com)
22  * @brief   HostIPCConnection class
23  */
24
25
26 #ifndef SERVER_HOST_IPC_CONNECTION_HPP
27 #define SERVER_HOST_IPC_CONNECTION_HPP
28
29 #include "api/messages.hpp"
30 #include "api/method-result-builder.hpp"
31 #include "api/ipc-method-result-builder.hpp"
32 #include "epoll/thread-dispatcher.hpp"
33 #include "ipc/service.hpp"
34
35 #include <functional>
36
37 namespace vasum {
38
39
40
41 class HostIPCConnection {
42 public:
43     template<typename ArgIn, typename ArgOut = api::Void>
44     class Callback {
45     public:
46         typedef typename std::remove_cv<ArgIn>::type in;
47         typedef ArgOut out;
48         typedef std::function<void(const in&, api::MethodResultBuilder::Pointer)> type;
49
50         static typename ipc::MethodHandler<out, in>::type
51         getCallbackWrapper(const type& callback) {
52             return [callback](const ipc::PeerID,
53                               const std::shared_ptr<in>& argIn,
54                               ipc::MethodResult::Pointer&& argOut)
55             {
56                 auto rb = std::make_shared<api::IPCMethodResultBuilder>(argOut);
57                 callback(*argIn, rb);
58             };
59         }
60     };
61
62     template<typename ArgOut>
63     class Callback<ArgOut, typename std::enable_if<!std::is_const<ArgOut>::value, api::Void>::type> {
64     public:
65         typedef api::Void in;
66         typedef ArgOut out;
67         typedef std::function<void(api::MethodResultBuilder::Pointer)> type;
68
69         static typename ipc::MethodHandler<out, in>::type
70         getCallbackWrapper(const type& callback) {
71             return [callback](const ipc::PeerID,
72                               const std::shared_ptr<in>& /* argIn */,
73                               ipc::MethodResult::Pointer&& argOut)
74             {
75                 auto rb = std::make_shared<api::IPCMethodResultBuilder>(argOut);
76                 callback(rb);
77             };
78         }
79     };
80
81     HostIPCConnection();
82     ~HostIPCConnection();
83
84     void setGetZoneDbusesCallback(const Callback<api::Dbuses>::type& callback);
85     void setGetZoneIdsCallback(const Callback<api::ZoneIds>::type& callback);
86     void setGetActiveZoneIdCallback(const Callback<api::ZoneId>::type& callback);
87     void setGetZoneInfoCallback(const Callback<const api::ZoneId, api::ZoneInfoOut>::type& callback);
88     void setSetNetdevAttrsCallback(const Callback<const api::SetNetDevAttrsIn>::type& callback);
89     void setGetNetdevAttrsCallback(const Callback<const api::GetNetDevAttrsIn, api::GetNetDevAttrs>::type& callback);
90     void setGetNetdevListCallback(const Callback<const api::ZoneId, api::NetDevList>::type& callback);
91     void setCreateNetdevVethCallback(const Callback<const api::CreateNetDevVethIn>::type& callback);
92     void setCreateNetdevMacvlanCallback(const Callback<const api::CreateNetDevMacvlanIn>::type& callback);
93     void setCreateNetdevPhysCallback(const Callback<const api::CreateNetDevPhysIn>::type& callback);
94     void setDestroyNetdevCallback(const Callback<const api::DestroyNetDevIn>::type& callback);
95     void setDeleteNetdevIpAddressCallback(const Callback<const api::DeleteNetdevIpAddressIn>::type& callback);
96     void setDeclareFileCallback(const Callback<const api::DeclareFileIn, api::Declaration>::type& callback);
97     void setDeclareMountCallback(const Callback<const api::DeclareMountIn, api::Declaration>::type& callback);
98     void setDeclareLinkCallback(const Callback<const api::DeclareLinkIn, api::Declaration>::type& callback);
99     void setGetDeclarationsCallback(const Callback<const api::ZoneId, api::Declarations>::type& callback);
100     void setRemoveDeclarationCallback(const Callback<const api::RemoveDeclarationIn>::type& callback);
101     void setSetActiveZoneCallback(const Callback<const api::ZoneId>::type& callback);
102     void setCreateZoneCallback(const Callback<const api::CreateZoneIn>::type& callback);
103     void setDestroyZoneCallback(const Callback<const api::ZoneId>::type& callback);
104     void setShutdownZoneCallback(const Callback<const api::ZoneId>::type& callback);
105     void setStartZoneCallback(const Callback<const api::ZoneId>::type& callback);
106     void setLockZoneCallback(const Callback<const api::ZoneId>::type& callback);
107     void setUnlockZoneCallback(const Callback<const api::ZoneId>::type& callback);
108     void setGrantDeviceCallback(const Callback<const api::GrantDeviceIn>::type& callback);
109     void setRevokeDeviceCallback(const Callback<const api::RevokeDeviceIn>::type& callback);
110     void signalZoneDbusState(const api::DbusState& dbusState);
111
112 private:
113     epoll::ThreadDispatcher mDispatcher;
114     std::unique_ptr<ipc::Service> mService;
115 };
116
117 } // namespace vasum
118
119 #endif // SERVER_HOST_IPC_CONNECTION_HPP