]> rtime.felk.cvut.cz Git - hydro.git/blobdiff - app-bohyn/src/fcgi.cc
Added monitoring web system. Minor changes in regulator and in control.
[hydro.git] / app-bohyn / src / fcgi.cc
diff --git a/app-bohyn/src/fcgi.cc b/app-bohyn/src/fcgi.cc
new file mode 100644 (file)
index 0000000..8d6d795
--- /dev/null
@@ -0,0 +1,168 @@
+// #include <stdlib.h>
+#include <iostream>
+#include <string>
+#include <string.h>
+
+#include <QApplication>
+#include <QString>
+#include <QTextCodec>
+
+#include <fcgi_config.h>
+#include <fcgi_stdio.h>
+#include <ul_lib/ulan.h>
+
+#include "defines.h"
+#include "utils.h"
+
+#include "http.h"
+#include "uldy_server.h"
+// #include "node_cleaner.h"
+#include "node.h"
+#include "address_parser.h"
+#include "html_layer.h"
+// #include "forms.h"
+#include "graph_painter.h"
+
+using namespace std;
+
+extern CidPool cidPool;
+
+void myMessageOutput(QtMsgType type, const char *msg)
+ {
+     switch (type) {
+     case QtDebugMsg:
+         logToFile(QString("Debug: %1").arg(msg));
+         break;
+     case QtWarningMsg:
+         logToFile(QString("Warning: %1").arg(msg));
+         break;
+     case QtCriticalMsg:
+         logToFile(QString("Critical: %1").arg(msg));
+         break;
+     case QtFatalMsg:
+         logToFile(QString("Fatal: %1").arg(msg));
+//          abort();
+     }
+}
+
+int main(int argc, char *argv[]) {
+    logToFile("START SERVERU");
+    qInstallMsgHandler(myMessageOutput);
+    QApplication app(argc, argv, false);
+    QTime timer;
+    QByteArray ba;
+
+    /* globalni aktivace utf-8 */
+    QTextCodec::setCodecForCStrings(QTextCodec::codecForName("UTF-8"));
+
+    int currentNodeAddr = 0;
+    QString currentVarName = "";
+    
+    ULDYServer uldySrv;
+    uldySrv.start();
+
+    while (FCGI_Accept() >= 0) {
+        HttpRequest request;
+        HtmlLayer html(request);
+//         logToFile(QString("request %1").arg(request.method()));
+
+        html.startBuffering();
+
+        AddressParser parser(request.path());
+        currentNodeAddr = parser.getCurrentAddr();
+        currentVarName = parser.getCurrentVarName();
+
+        NodeMap &nodes = uldySrv.getNodesRef();
+        ValueCache &valueCache = uldySrv.getValueCacheRef();
+#ifndef TEST_RUN
+        uldySrv.tryRemoveNodes();
+#endif
+        bool printRefreshHeader = false;
+        try {
+            if (parser.getUrlPart(1) == "xml_config") {
+                
+                if (parser.getUrlPart(2) == "download") {
+                    html.stopBuffering();
+                    html.printXMLDownload(nodes);
+                    continue;
+                }
+
+                html.addCrumb(QString("%1xml_config/").arg(html.documentRoot()), "Správa konfigurace");
+                html.printNodesConfig(nodes);
+
+            } else if (currentNodeAddr == 0) {
+
+                printRefreshHeader = true;
+                html.home(nodes, valueCache);
+
+            } else {
+                Node &selectedNode = nodes.getNodeRef(currentNodeAddr);
+                html.addCrumb(QString("%1%2/").arg(html.documentRoot()).arg(currentNodeAddr), selectedNode.getCustomName());
+
+                if (currentVarName == "configure_vars") {
+                    /* konfigurace promennych nodu */
+                    html.addCrumb("./", "Konfigurace zařízení");
+                    html.printVarsConfig(nodes, selectedNode, cidPool);
+                } else if (currentVarName != "") {
+                    /* detail promenne */
+                    NodeVar selectedVar = selectedNode.getVar(currentVarName);
+                    if (parser.getUrlPart(3) == "refresh_value") {
+                        html.stopBuffering();
+                        html.printHTTPHeaders();
+                        html.printVarValueAjaxRespose(selectedNode, selectedVar);
+                        continue;
+                    } else {
+                        html.addCrumb("./", selectedVar.getCustomDescription());
+                        html.printVar(selectedVar, valueCache);
+                        printRefreshHeader = true;
+                    }
+                } else {
+                    /* detail nodu */
+                    html.printNode(selectedNode);
+                }
+
+            }
+        
+        } catch (NodeError err) {
+            html.stopBuffering();
+            html.printHTTP404Headers();
+            html.print404Error();
+            continue;
+        } catch (NodeVarError err) {
+            html.stopBuffering();
+            html.printHTTP404Headers();
+            html.print404Error();
+            continue;
+        } catch (HttpRedirect redir) {
+            html.stopBuffering();
+            html.printHTTPRedirectHeaders(redir.getUrl());
+            continue;
+        }
+        
+        html.stopBuffering();
+        
+        if (printRefreshHeader)
+            html.printHTTPRefreshHeaders(10);
+        else
+            html.printHTTPHeaders();
+        html.printDocumentStart();
+        html.printCrumbs();
+        html.printNodes(nodes);
+        html.printContentStart();
+        html.printBuffer();
+        html.printContentEnd();
+//         html.printEnvVars();
+        html.print("<hr />");
+//         html.print(cidPool.cidsToStr());
+        html.printDocumentEnd();
+
+    } /* while */
+
+//     nodeClenaer.stopCleaner();
+//     nodeClenaer.wait(3000);
+    
+    uldySrv.stopServer();
+    uldySrv.wait(3000);
+
+    return 0;
+}