Mir
template_protobuf_message_processor.h
Go to the documentation of this file.
1 /*
2  * Copyright © 2014 Canonical Ltd.
3  *
4  * This program is free software: you can redistribute it and/or modify it
5  * under the terms of the GNU General Public License version 3,
6  * as published by the Free Software Foundation.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program. If not, see <http://www.gnu.org/licenses/>.
15  *
16  * Authored by: Alan Griffiths <alan@octopull.co.uk>
17  */
18 
19 
20 #ifndef MIR_FRONTEND_TEMPLATE_PROTOBUF_MESSAGE_PROCESSOR_H_
21 #define MIR_FRONTEND_TEMPLATE_PROTOBUF_MESSAGE_PROCESSOR_H_
22 
24 
25 #include <google/protobuf/service.h>
26 
27 #include <boost/exception/diagnostic_information.hpp>
28 
29 #include <memory>
30 
31 namespace mir
32 {
33 namespace frontend
34 {
35 namespace detail
36 {
37 // Utility metafunction result_ptr_t<> allows invoke() to pick the right
38 // send_response() overload. The base template resolves to the prototype
39 // "send_response(::google::protobuf::uint32 id, ::google::protobuf::Message* response)"
40 // Client code may specialize result_ptr_t to resolve to another overload.
41 template<typename ResultType> struct result_ptr_t
42 { typedef ::google::protobuf::Message* type; };
43 
44 // Boiler plate for unpacking a parameter message, invoking a server function, and
45 // sending the result message. Assumes the existence of Self::send_response().
46 template<class Self, class Server, class ServerX, class ParameterMessage, class ResultMessage>
47 void invoke(
48  Self* self,
49  Server* server,
50  void (ServerX::*function)(
51  ::google::protobuf::RpcController* controller,
52  const ParameterMessage* request,
53  ResultMessage* response,
54  ::google::protobuf::Closure* done),
55  Invocation const& invocation)
56 {
57  ParameterMessage parameter_message;
58  parameter_message.ParseFromString(invocation.parameters());
59  ResultMessage result_message;
60 
61  try
62  {
63  std::unique_ptr<google::protobuf::Closure> callback(
64  google::protobuf::NewPermanentCallback<
65  Self,
66  ::google::protobuf::uint32,
68  self,
69  &Self::send_response,
70  invocation.id(),
71  &result_message));
72 
73  (server->*function)(
74  0,
75  &parameter_message,
76  &result_message,
77  callback.get());
78  }
79  catch (std::exception const& x)
80  {
81  result_message.set_error(boost::diagnostic_information(x));
82  self->send_response(invocation.id(), &result_message);
83  }
84 }
85 }
86 }
87 }
88 
89 #endif /* MIR_FRONTEND_TEMPLATE_PROTOBUF_MESSAGE_PROCESSOR_H_ */
All things Mir.
Definition: buffer_stream.h:37
Definition: message_processor.h:39
Customise and run a Mir server.
Definition: server.h:68
void invoke(Self *self, Server *server, void(ServerX::*function)(::google::protobuf::RpcController *controller, const ParameterMessage *request, ResultMessage *response,::google::protobuf::Closure *done), Invocation const &invocation)
Definition: template_protobuf_message_processor.h:47
::google::protobuf::Message * type
Definition: template_protobuf_message_processor.h:42
::google::protobuf::uint32 id() const
Definition: dimensions.h:35
const ::std::string & parameters() const
Definition: template_protobuf_message_processor.h:41

Copyright © 2012,2013 Canonical Ltd.
Generated on Tue Mar 24 16:15:19 UTC 2015