/[projects]/smsdaemon/embedded-http/embeddedhttp.cpp
ViewVC logotype

Diff of /smsdaemon/embedded-http/embeddedhttp.cpp

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 680 by torben, Wed Apr 28 07:38:39 2010 UTC revision 695 by torben, Thu Apr 29 18:18:18 2010 UTC
# Line 9  Line 9 
9  #include "Common.h"  #include "Common.h"
10  #include "ConfigFile.h"  #include "ConfigFile.h"
11  #include <sstream>  #include <sstream>
12    #include <iomanip>
13  #include <stdlib.h>  #include <stdlib.h>
14    
15  void show_secret(struct mg_connection *conn, const struct mg_request_info *request_info, void *user_data) {  void show_status(struct mg_connection *conn, const struct mg_request_info *request_info, void *user_data) {
16          Common* cmn = Common::instance();          Common* cmn = Common::instance();
17    
18    
# Line 28  void show_secret(struct mg_connection *c Line 29  void show_secret(struct mg_connection *c
29          os << "Status:" << std::endl;          os << "Status:" << std::endl;
30          os << "Recv: " << cmn->smsCounter.incomming << std::endl;          os << "Recv: " << cmn->smsCounter.incomming << std::endl;
31          os << "Send: " << cmn->smsCounter.outgoing << std::endl;          os << "Send: " << cmn->smsCounter.outgoing << std::endl;
32          os << "Uptime: " << days << "d, " << hour << ":" << min << ":" << sec;          os << "Uptime: " << days << "d, ";
33            os << std::setw(2) << std::setfill('0') << hour << ":";
34            os << std::setw(2) << min << ":" ;
35            os << std::setw(2) << sec;
36    
37          mg_printf(conn, "%s", "HTTP/1.1 200 OK\r\n");          mg_printf(conn, "%s", "HTTP/1.1 200 OK\r\n");
38          mg_printf(conn, "%s", "Content-Type: text/plain\r\n\r\n");          mg_printf(conn, "%s", "Content-Type: text/plain\r\n\r\n");
# Line 41  void show_index(struct mg_connection *co Line 45  void show_index(struct mg_connection *co
45          mg_printf(conn, "%s", "Content-Type: text/html\r\n\r\n");          mg_printf(conn, "%s", "Content-Type: text/html\r\n\r\n");
46                    
47          mg_printf(conn, "%s", "<html><body><h2>smsdaemon</h2>\n");          mg_printf(conn, "%s", "<html><body><h2>smsdaemon</h2>\n");
48          mg_printf(conn, "%s", "<a href='/status'>smsdaemon status</a>\n");          mg_printf(conn, "%s", "<a href='/status'>smsdaemon status</a><br>\n");
49            mg_printf(conn, "%s", "<a href='/log'>smsdaemon log</a><br>\n");
50    
51          mg_printf(conn, "%s", "</body></html>");          mg_printf(conn, "%s", "</body></html>");
52  }  }
53    
54    static void show_404(struct mg_connection *conn, const struct mg_request_info *request_info, void *user_data)
55    {
56            mg_printf(conn, "%s", "HTTP/1.1 200 OK\r\n");
57            mg_printf(conn, "%s", "Content-Type: text/plain\r\n\r\n");
58            mg_printf(conn, "%s", "Oops. File not found! ");
59    }
60    
61    void show_log(struct mg_connection *conn, const struct mg_request_info *request_info, void *user_data) {
62            mg_printf(conn, "%s", "HTTP/1.1 200 OK\r\n");
63            mg_printf(conn, "%s", "Content-Type: text/plain\r\n\r\n");
64    
65            mg_printf(conn, "%s", Logger::getLog().c_str() );
66    }
67    
68  void EmbeddedHttp::startServer() {  void EmbeddedHttp::startServer() {
69          std::string portStr = Common::instance()->GetConfigfile()->GetValue("smsdaemon", "httpport");          std::string portStr = Common::instance()->GetConfigfile()->GetValue("smsdaemon", "httpport");
70          int port = atoi( portStr.c_str() );          int port = atoi( portStr.c_str() );
# Line 59  void EmbeddedHttp::startServer() { Line 79  void EmbeddedHttp::startServer() {
79    
80                  m_context = mg_start();                  m_context = mg_start();
81                  mg_set_option(m_context, "ports", oss.str().c_str() );                  mg_set_option(m_context, "ports", oss.str().c_str() );
82                    mg_set_option(m_context, "root", "/nosuchpatch"); //set root to non-existant path to disable normal file serving - only callbacks are now serviced
83    
84                  mg_set_uri_callback(m_context, "/", &show_index, 0);                  mg_set_uri_callback(m_context, "/", &show_index, 0);
85                  mg_set_uri_callback(m_context, "/status", &show_secret, 0);                  mg_set_uri_callback(m_context, "/status", &show_status, 0);
86                    mg_set_uri_callback(m_context, "/log", &show_log, 0);
87                    mg_set_error_callback(m_context, 404, show_404, NULL);
88    
89    
90          } else {          } else {
91                  Logger::logMessage( "Mongoose http disabled" );                  Logger::logMessage( "Mongoose http disabled" );
92                  m_context = 0;                  m_context = 0;

Legend:
Removed from v.680  
changed lines
  Added in v.695

  ViewVC Help
Powered by ViewVC 1.1.20