Fork Vasum on GitHub Official Vasum Wiki on Tizen.org
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
method-request.hpp
Go to the documentation of this file.
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 
25 #ifndef CARGO_IPC_INTERNALS_METHOD_REQUEST_HPP
26 #define CARGO_IPC_INTERNALS_METHOD_REQUEST_HPP
27 
29 #include "cargo-ipc/types.hpp"
30 #include "cargo-ipc/result.hpp"
31 #include "logger/logger-scope.hpp"
32 #include "cargo-fd/cargo-fd.hpp"
33 #include <utility>
34 
35 namespace cargo {
36 namespace ipc {
37 
39 public:
40  MethodRequest(const MethodRequest&) = delete;
41  MethodRequest& operator=(const MethodRequest&) = delete;
42 
43  template<typename SentDataType, typename ReceivedDataType>
44  static std::shared_ptr<MethodRequest> create(const MethodID methodID,
45  const PeerID& peerID,
46  const std::shared_ptr<SentDataType>& data,
48 
52  std::shared_ptr<void> data;
56 
57 private:
58  MethodRequest(const MethodID methodID, const PeerID& peerID)
59  : methodID(methodID),
60  peerID(peerID),
62  {}
63 };
64 
65 
66 template<typename SentDataType, typename ReceivedDataType>
67 std::shared_ptr<MethodRequest> MethodRequest::create(const MethodID methodID,
68  const PeerID& peerID,
69  const std::shared_ptr<SentDataType>& data,
70  const typename ResultHandler<ReceivedDataType>::type& process)
71 {
72  std::shared_ptr<MethodRequest> request(new MethodRequest(methodID, peerID));
73 
74  request->data = data;
75 
76  request->serialize = [](const int fd, std::shared_ptr<void>& data)->void {
77  LOGS("Method serialize, peerID: " << fd);
78  cargo::saveToFD<SentDataType>(fd, *std::static_pointer_cast<SentDataType>(data));
79  };
80 
81  request->parse = [](const int fd)->std::shared_ptr<void> {
82  LOGS("Method parse, peerID: " << fd);
83  std::shared_ptr<ReceivedDataType> data(new ReceivedDataType());
84  cargo::loadFromFD<ReceivedDataType>(fd, *data);
85  return data;
86  };
87 
88  if(process == nullptr){
89  request->process = [](ResultBuilder & ) {
90  LOGT("No method to process result");
91  };
92  } else {
93  request->process = [process](ResultBuilder & resultBuilder) {
94  LOGS("Method process");
95  process(resultBuilder.build<ReceivedDataType>());
96  };
97  }
98 
99  return request;
100 }
101 
102 } // namespace ipc
103 } // namespace cargo
104 
105 #endif // CARGO_IPC_INTERNALS_METHOD_REQUEST_HPP
MethodID methodID
Definition: method-request.hpp:49
std::function< void(Result< Data > &&) > type
Definition: result.hpp:73
std::function< void(ResultBuilder &)> ResultBuilderHandler
Definition: result-builder.hpp:66
static std::shared_ptr< MethodRequest > create(const MethodID methodID, const PeerID &peerID, const std::shared_ptr< SentDataType > &data, const typename ResultHandler< ReceivedDataType >::type &process)
Definition: method-request.hpp:67
#define LOGS(MSG)
Automatically create LoggerScope object which logs at the construction and destruction.
Definition: logger-scope.hpp:78
std::string PeerID
Definition: types.hpp:45
MethodRequest(const MethodID methodID, const PeerID &peerID)
Definition: method-request.hpp:58
Definition: method-request.hpp:38
Types definitions.
MessageID getNextMessageID()
Generate an unique message id.
Definition: types.cpp:46
std::function< std::shared_ptr< void >cargo::ipc::FileDescriptor fd)> ParseCallback
Generic function type used as callback for reading and parsing data.
Definition: types.hpp:72
Class for storing result of a method - data or exception.
std::shared_ptr< void > data
Definition: method-request.hpp:52
MethodRequest(const MethodRequest &)=delete
MethodRequest & operator=(const MethodRequest &)=delete
PeerID peerID
Definition: method-request.hpp:50
char data[368]
Definition: initctl.cpp:41
Definition: result-builder.hpp:36
unsigned int MethodID
Definition: types.hpp:43
std::function< void(cargo::ipc::FileDescriptor fd, std::shared_ptr< void > &data)> SerializeCallback
Generic function type used as callback for serializing and saving serialized data to the descriptor...
Definition: types.hpp:64
SerializeCallback serialize
Definition: method-request.hpp:53
Class for storing result of a method - data or exception.
ResultBuilderHandler process
Definition: method-request.hpp:55
#define LOGT(MESSAGE)
Logging tracing information.
Definition: logger.hpp:156
MessageID messageID
Definition: method-request.hpp:51
ParseCallback parse
Definition: method-request.hpp:54
std::string MessageID
Definition: types.hpp:44
Scope logger class declaration.