IPC: Support older glib
[platform/core/security/vasum.git] / common / ipc / internals / method-request.hpp
1 /*
2 *  Copyright (c) 2015 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   Processor's request to call a method
23  */
24
25 #ifndef COMMON_IPC_INTERNALS_METHOD_REQUEST_HPP
26 #define COMMON_IPC_INTERNALS_METHOD_REQUEST_HPP
27
28 #include "ipc/types.hpp"
29 #include "logger/logger-scope.hpp"
30 #include "config/manager.hpp"
31
32 namespace vasum {
33 namespace ipc {
34
35 class MethodRequest {
36 public:
37     MethodRequest(const MethodRequest&) = delete;
38     MethodRequest& operator=(const MethodRequest&) = delete;
39
40     template<typename SentDataType, typename ReceivedDataType>
41     static std::shared_ptr<MethodRequest> create(const MethodID methodID,
42                                                  const FileDescriptor peerFD,
43                                                  const std::shared_ptr<SentDataType>& data,
44                                                  const typename ResultHandler<ReceivedDataType>::type& process);
45
46     MethodID methodID;
47     FileDescriptor peerFD;
48     MessageID messageID;
49     std::shared_ptr<void> data;
50     SerializeCallback serialize;
51     ParseCallback parse;
52     ResultHandler<void>::type process;
53
54 private:
55     MethodRequest(const MethodID methodID, const FileDescriptor peerFD)
56         : methodID(methodID),
57           peerFD(peerFD),
58           messageID(getNextMessageID())
59     {}
60 };
61
62
63 template<typename SentDataType, typename ReceivedDataType>
64 std::shared_ptr<MethodRequest> MethodRequest::create(const MethodID methodID,
65                                                      const FileDescriptor peerFD,
66                                                      const std::shared_ptr<SentDataType>& data,
67                                                      const typename ResultHandler<ReceivedDataType>::type& process)
68 {
69     std::shared_ptr<MethodRequest> request(new MethodRequest(methodID, peerFD));
70
71     request->data = data;
72
73     request->serialize = [](const int fd, std::shared_ptr<void>& data)->void {
74         LOGS("Method serialize, peerFD: " << fd);
75         config::saveToFD<SentDataType>(fd, *std::static_pointer_cast<SentDataType>(data));
76     };
77
78     request->parse = [](const int fd)->std::shared_ptr<void> {
79         LOGS("Method parse, peerFD: " << fd);
80         std::shared_ptr<ReceivedDataType> data(new ReceivedDataType());
81         config::loadFromFD<ReceivedDataType>(fd, *data);
82         return data;
83     };
84
85     request->process = [process](Status status, std::shared_ptr<void>& data)->void {
86         LOGS("Method process, status: " << toString(status));
87         std::shared_ptr<ReceivedDataType> tmpData = std::static_pointer_cast<ReceivedDataType>(data);
88         return process(status, tmpData);
89     };
90
91     return request;
92 }
93
94 } // namespace ipc
95 } // namespace vasum
96
97 #endif // COMMON_IPC_INTERNALS_METHOD_REQUEST_HPP