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 #include "mir/cookie/authority.h"
26 
27 #include "mir_protobuf.pb.h"
28 
29 #include <google/protobuf/stubs/common.h>
30 #include <boost/exception/diagnostic_information.hpp>
31 
32 #include <memory>
33 #include <string>
34 
35 namespace mir
36 {
37 namespace frontend
38 {
39 namespace detail
40 {
41 // Utility metafunction result_ptr_t<> allows invoke() to pick the right
42 // send_response() overload. The base template resolves to the prototype
43 // "send_response(::google::protobuf::uint32 id, ::google::protobuf::Message* response)"
44 // Client code may specialize result_ptr_t to resolve to another overload.
45 template<typename ResultType> struct result_ptr_t
46 { typedef ::google::protobuf::MessageLite* type; };
47 
48 // Boiler plate for unpacking a parameter message, invoking a server function, and
49 // sending the result message. Assumes the existence of Self::send_response().
50 template<class Self, class Server, class ServerX, class ParameterMessage, class ResultMessage>
51 void invoke(
52  Self* self,
53  Server* server,
54  void (ServerX::*function)(
55  ParameterMessage const* request,
56  ResultMessage* response,
57  ::google::protobuf::Closure* done),
58  Invocation const& invocation)
59 {
60  ParameterMessage parameter_message;
61  if (!parameter_message.ParseFromString(invocation.parameters()))
62  BOOST_THROW_EXCEPTION(std::runtime_error("Failed to parse message parameters!"));
63  ResultMessage result_message;
64 
65  try
66  {
67  std::unique_ptr<google::protobuf::Closure> callback(
68  google::protobuf::NewPermanentCallback<
69  Self,
70  ::google::protobuf::uint32,
72  self,
73  &Self::send_response,
74  invocation.id(),
75  &result_message));
76 
77  (server->*function)(
78  &parameter_message,
79  &result_message,
80  callback.get());
81  }
82  catch (mir::cookie::SecurityCheckError const& /*err*/)
83  {
84  throw;
85  }
86  catch (mir::ClientVisibleError const& error)
87  {
88  auto client_error = result_message.mutable_structured_error();
89  client_error->set_code(error.code());
90  client_error->set_domain(error.domain());
91  self->send_response(invocation.id(), &result_message);
92  }
93  catch (std::exception const& x)
94  {
95  using namespace std::literals::string_literals;
96  result_message.set_error("Error processing request: "s +
97  x.what() + "\nInternal error details: " + boost::diagnostic_information(x));
98  self->send_response(invocation.id(), &result_message);
99  }
100 }
101 
102 }
103 }
104 }
105 
106 #endif /* MIR_FRONTEND_TEMPLATE_PROTOBUF_MESSAGE_PROCESSOR_H_ */
All things Mir.
Definition: atomic_callback.h:25
Definition: message_processor.h:40
Base class for exceptions which might be visible to clients.
Definition: client_visible_error.h:36
Customise and run a Mir server.
Definition: server.h:78
Definition: authority.h:35
google::protobuf::uint32 id() const
void invoke(Self *self, Server *server, void(ServerX::*function)(ParameterMessage const *request, ResultMessage *response,::google::protobuf::Closure *done), Invocation const &invocation)
Definition: template_protobuf_message_processor.h:51
const ::std::string & parameters() const
error
Definition: connector_report_tp.h:56
Definition: template_protobuf_message_processor.h:45
::google::protobuf::MessageLite * type
Definition: template_protobuf_message_processor.h:46
virtual uint32_t code() const noexcept=0
Error code within the domain().
virtual MirErrorDomain domain() const noexcept=0
Client-visible error domain.

Copyright © 2012-2015 Canonical Ltd.
Generated on Thu Sep 8 14:50:19 UTC 2016