libtasks Documentation  1.6
uwsgi_thrift_processor.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2013-2014 ADTECH GmbH
3  * Licensed under MIT (https://github.com/adtechlabs/libtasks/blob/master/COPYING)
4  *
5  * Author: Andreas Pohl
6  */
7 
8 #ifndef _THRIFT_PROCESSOR_H_
9 #define _THRIFT_PROCESSOR_H_
10 
11 #include <arpa/inet.h>
12 #include <boost/shared_ptr.hpp>
13 #include <thrift/protocol/TBinaryProtocol.h>
14 
15 #include <tasks/net/uwsgi_task.h>
17 
18 namespace tasks {
19 namespace net {
20 
21 template <class processor_type, class handler_type>
23  public:
25 
26  virtual bool handle_request() {
27  using namespace apache::thrift::protocol;
28  using namespace apache::thrift::transport;
29  typedef uwsgi_thrift_transport<uwsgi_request> in_transport_type;
30  typedef uwsgi_thrift_transport<http_response> out_transport_type;
31  typedef TBinaryProtocol protocol_type;
32 
33  boost::shared_ptr<in_transport_type> in_transport(new in_transport_type(request_p()));
34  boost::shared_ptr<out_transport_type> out_transport(new out_transport_type(response_p()));
35  boost::shared_ptr<protocol_type> in_protocol(new protocol_type(in_transport));
36  boost::shared_ptr<protocol_type> out_protocol(new protocol_type(out_transport));
37 
38  // Process message
39  try {
40  boost::shared_ptr<handler_type> handler(new handler_type());
41  handler->set_uwsgi_task(this);
42  processor_type proc(handler);
43 
44  // Read a thrift object from the request, call the handler and write the respnding
45  // thrift object to the uwsgi response.
46  if (proc.process(in_protocol, out_protocol, NULL)) {
47  response().set_status("200 OK");
48  } else {
49  response().set_status("400 Bad Request");
50  }
51  } catch (TTransportException& e) {
53  std::string("TTransportException: ") + std::string(e.what()));
54  set_exception(ex);
55  response().set_status("400 Bad Request");
56  }
57 
58  response().set_header("Content-Type", "application/x-thrift");
59  send_response();
60 
61  return true;
62  }
63 };
64 
65 } // net
66 } // tasks
67 
68 #endif // _THRIFT_PROCESSOR_H_
69 
70 
71 
72 
73 
http_response & response()
Definition: uwsgi_task.h:55
uwsgi_request * request_p()
Definition: uwsgi_task.h:49
The socket class.
Definition: socket.h:35
void set_status(std::string status)
Definition: http_response.h:23
void set_header(std::string header, std::string value)
Definition: http_base.h:46
void send_response()
Send the resonse back.
Definition: uwsgi_task.h:67
Tasks execption class.
The base class for the uwsgi protocol implementation.
Definition: uwsgi_task.h:30
void set_exception(tasks_exception &e)
Set an exception to report an error.
Definition: error_base.h:54
http_response * response_p()
Definition: uwsgi_task.h:61