Fawkes API  Fawkes Development Version
tf_processor.cpp
1 
2 /***************************************************************************
3  * tf_processor.cpp - Web request processor for TF data
4  *
5  * Created: Wed Jun 19 17:45:47 2013
6  * Copyright 2006-2013 Tim Niemueller [www.niemueller.de]
7  *
8  ****************************************************************************/
9 
10 /* This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU Library General Public License for more details.
19  *
20  * Read the full text in the LICENSE.GPL file in the doc directory.
21  */
22 
23 #include "tf_processor.h"
24 #include <webview/page_reply.h>
25 #include <webview/file_reply.h>
26 #include <webview/error_reply.h>
27 
28 #include <string>
29 #include <cstring>
30 #include <cstdlib>
31 #include <cerrno>
32 
33 #include <gvc.h>
34 #include <gvcjob.h>
35 
36 
37 using namespace fawkes;
38 
39 /** @class WebviewTfRequestProcessor "tf_processor.h"
40  * Transfrom data web request processor.
41  * Provides access to the transforms data.
42  * @author Tim Niemueller
43  */
44 
45 /** Constructor.
46  * @param baseurl Base URL where processor is mounted
47  * @param transformer transformer listener to query for DOT graph
48  */
50  fawkes::tf::Transformer *transformer)
51 {
52  baseurl_ = strdup(baseurl);
53  baseurl_len_ = strlen(baseurl_);
54  transformer_ = transformer;
55 }
56 
57 
58 /** Destructor. */
60 {
61  free(baseurl_);
62 }
63 
64 
65 WebReply *
67 {
68  if ( strncmp(baseurl_, request->url().c_str(), baseurl_len_) == 0 ) {
69  // It is in our URL prefix range
70  std::string subpath = request->url().substr(baseurl_len_);
71 
72  if (subpath == "/graph.png") {
73  std::string graph = transformer_->all_frames_as_dot(true);
74 
75  FILE *f = tmpfile();
76  if (NULL == f) {
78  "Cannot open temp file: %s", strerror(errno));
79  }
80 
81  GVC_t* gvc = gvContext();
82  Agraph_t* G = agmemread((char *)graph.c_str());
83  gvLayout(gvc, G, (char *)"dot");
84  gvRender(gvc, G, (char *)"png", f);
85  gvFreeLayout(gvc, G);
86  agclose(G);
87  gvFreeContext(gvc);
88 
89  try {
91  return freply;
92  } catch (fawkes::Exception &e) {
94  }
95 
96  } else {
97  WebPageReply *r = new WebPageReply("Transforms");
98  r->append_body("<p><img src=\"%s/graph.png\" /></p>", baseurl_);
99  //r->append_body("<pre>%s</pre>", transformer_->all_frames_as_dot().c_str());
100  return r;
101  }
102  } else {
103  return NULL;
104  }
105 }
Dynamic raw file transfer reply.
Definition: file_reply.h:34
Fawkes library namespace.
virtual ~WebviewTfRequestProcessor()
Destructor.
WebviewTfRequestProcessor(const char *baseurl, fawkes::tf::Transformer *transformer)
Constructor.
Base class for exceptions in Fawkes.
Definition: exception.h:36
iterator begin()
Get iterator for messages.
Definition: exception.cpp:700
Web request meta data carrier.
Definition: request.h:42
Basic page reply.
Definition: page_reply.h:36
Basic web reply.
Definition: reply.h:36
void append_body(const char *format,...)
Append to body.
Definition: reply.cpp:224
const std::string & url() const
Get URL.
Definition: request.h:69
virtual fawkes::WebReply * process_request(const fawkes::WebRequest *request)
Process a request.
Coordinate transforms between any two frames in a system.
Definition: transformer.h:68
Static error page reply.
Definition: error_reply.h:33