Fawkes API Fawkes Development Version

blackboard_processor.cpp

00001 
00002 /***************************************************************************
00003  *  blackboard_processor.cpp - Web request processor for BlackBoard info
00004  *
00005  *  Created: Thu Oct 23 16:10:21 2008
00006  *  Copyright  2006-2008  Tim Niemueller [www.niemueller.de]
00007  *
00008  ****************************************************************************/
00009 
00010 /*  This program is free software; you can redistribute it and/or modify
00011  *  it under the terms of the GNU General Public License as published by
00012  *  the Free Software Foundation; either version 2 of the License, or
00013  *  (at your option) any later version.
00014  *
00015  *  This program is distributed in the hope that it will be useful,
00016  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00017  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018  *  GNU Library General Public License for more details.
00019  *
00020  *  Read the full text in the LICENSE.GPL file in the doc directory.
00021  */
00022 
00023 #include "blackboard_processor.h"
00024 #include <webview/page_reply.h>
00025 
00026 #include <blackboard/blackboard.h>
00027 #include <interface/interface.h>
00028 #include <interface/field_iterator.h>
00029 #include <interface/interface_info.h>
00030 #include <utils/time/time.h>
00031 
00032 #include <string>
00033 #include <cstring>
00034 #include <cstdlib>
00035 
00036 using namespace fawkes;
00037 
00038 /** @class WebviewBlackBoardRequestProcessor "blackboard_processor.h"
00039  * BlackBoard web request processor.
00040  * Provides access to BlackBoard introspection features.
00041  * @author Tim Niemueller
00042  */
00043 
00044 /** Constructor.
00045  * @param baseurl Base URL where processor is mounted
00046  * @param blackboard BlackBoard instance
00047  */
00048 WebviewBlackBoardRequestProcessor::WebviewBlackBoardRequestProcessor(const char *baseurl,
00049                                                              BlackBoard *blackboard)
00050 {
00051   __baseurl     = strdup(baseurl);
00052   __baseurl_len = strlen(__baseurl);
00053   __blackboard  = blackboard;
00054 }
00055 
00056 
00057 /** Destructor. */
00058 WebviewBlackBoardRequestProcessor::~WebviewBlackBoardRequestProcessor()
00059 {
00060   free(__baseurl);
00061   for (__ifi = __interfaces.begin(); __ifi != __interfaces.end(); ++__ifi) {
00062     __blackboard->close(__ifi->second);
00063   }
00064   __interfaces.clear();
00065 }
00066 
00067 
00068 WebReply *
00069 WebviewBlackBoardRequestProcessor::process_request(const char *url,
00070                                                const char *method,
00071                                                const char *version,
00072                                                const char *upload_data,
00073                                                size_t *upload_data_size,
00074                                                void **session_data)
00075 {
00076   if ( strncmp(__baseurl, url, __baseurl_len) == 0 ) {
00077     // It is in our URL prefix range
00078     std::string subpath = std::string(url).substr(__baseurl_len);
00079 
00080     WebPageReply *r = new WebPageReply("BlackBoard");
00081     *r += "<h2>BlackBoard interfaces:</h2>\n";
00082 
00083     bool found_some = false;
00084     InterfaceInfoList *iil = __blackboard->list_all();
00085     iil->sort();
00086     for (InterfaceInfoList::iterator i = iil->begin(); i != iil->end(); ++i) {
00087       if (! found_some) {
00088         *r += "<table>\n";
00089         *r += "<tr><th>Interface</th><th>Reader(s)</th><th>Writer</th></tr>\n";
00090         found_some = true;
00091       }
00092       r->append_body("<tr><td><a href=\"%s/view/%s::%s\">%s::%s</a></td><td>%u</td><td style=\"color:%s\">%s</td></tr>\n",
00093                      __baseurl, i->type(), i->id(), i->type(), i->id(),
00094                      i->num_readers(), i->has_writer() ? "green" : "red", i->has_writer() ? "yes" : "no");
00095     }
00096     delete iil;
00097 
00098     if (found_some) {
00099       *r += "</table>\n";
00100     } else {
00101       *r += "<b>No interfaces found.</b>\n";
00102     }
00103 
00104     if (subpath.find("/view/") == 0) {
00105       std::string iuid = subpath.substr(subpath.find_first_not_of("/", std::string("/view/").length()));
00106       std::string iftype = iuid.substr(0, iuid.find("::"));
00107       std::string ifname = iuid.substr(iuid.find("::") + 2);
00108 
00109 
00110       r->append_body("<a href=\"%s\">Clear detailed</a>\n", __baseurl);
00111 
00112       r->append_body("<h2>Interface: %s</h2>\n", iuid.c_str());
00113       if (__interfaces.find(iuid) == __interfaces.end()) {
00114         try {
00115           Interface *iface = __blackboard->open_for_reading(iftype.c_str(), ifname.c_str());
00116           __interfaces[iuid] = iface;
00117         } catch (Exception &e) {
00118           r->append_body("Failed to open interface: %s\n", e.what());
00119         }
00120       }
00121       if (__interfaces.find(iuid) != __interfaces.end()) {
00122         Interface *iface = __interfaces[iuid];
00123         iface->read();
00124 
00125         r->append_body("<table>\n"
00126                        " <tr><td><b>Type:</b></td><td>%s</td></tr>\n"
00127                        " <tr><td><b>ID:</b></td><td>%s</td></tr>\n"
00128                        " <tr><td><b>Has writer?:</b></td><td>%s</td></tr>\n"
00129                        " <tr><td><b>Num readers:</b></td><td>%u</td></tr>\n"
00130                        " <tr><td><b>Serial:</b></td><td>%u</td></tr>\n"
00131                        " <tr><td><b>Data size:</b></td><td>%u</td></tr>\n"
00132                        " <tr><td><b>Hash:</b></td><td>%s</td></tr>\n"
00133                        " <tr><td><b>Data changed:</b></td>"
00134                        "<td>%s (last at %s)</td></tr>\n"
00135                        "</table>\n",
00136                        iface->type(), iface->id(), iface->has_writer() ? "yes" : "no",
00137                        iface->num_readers(), iface->serial(),
00138                        iface->datasize(), iface->hash_printable(),
00139                        iface->changed() ? "yes" : "no", iface->timestamp()->str());
00140 
00141         r->append_body("<table>\n"
00142                        " <tr>\n"
00143                        "  <th>Name</th><th>Type</th><th>Value</th>\n"
00144                        " </tr>\n");
00145         for (InterfaceFieldIterator fi = iface->fields(); fi != iface->fields_end(); ++fi) {
00146           bool is_string = (fi.get_type() == IFT_STRING);
00147           *r += " <tr>\n";
00148           if ( fi.get_length() > 1 ) {
00149             r->append_body("  <td>%s</td><td>%s [%zu]</td><td>%s%s%s</td>\n",
00150                            fi.get_name(), fi.get_typename(),
00151                            fi.get_length(), is_string ? "<pre>" : "",
00152                            fi.get_value_string(), is_string ? "</pre>" : "");
00153           } else {
00154             r->append_body("  <td>%s</td><td>%s</td><td>%s%s%s</td>\n",
00155                            fi.get_name(), fi.get_typename(), is_string ? "<pre>" : "",
00156                            fi.get_value_string(), is_string ? "</pre>" : "");
00157           }
00158           *r += " </tr>\n";
00159         }
00160         r->append_body("</table>\n");
00161       }
00162     }
00163 
00164     return r;
00165   } else {
00166     return NULL;
00167   }
00168 }
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends