/[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 675 by torben, Tue Apr 27 20:07:12 2010 UTC revision 680 by torben, Wed Apr 28 07:38:39 2010 UTC
# Line 4  Line 4 
4  #include <stdio.h>  #include <stdio.h>
5  #include "mongoose.h"  #include "mongoose.h"
6    
7    #include "Logger.h"
8    #include "version.h"
9    #include "Common.h"
10    #include "ConfigFile.h"
11    #include <sstream>
12    #include <stdlib.h>
13    
14    void show_secret(struct mg_connection *conn, const struct mg_request_info *request_info, void *user_data) {
15            Common* cmn = Common::instance();
16    
17    
18            int tot_sec = time(0) - cmn->daemonStart;
19    
20            int sec = tot_sec % 60;
21            int min = (tot_sec / 60) % 60;
22            int hour = (tot_sec / 3600) %24;
23            int days = tot_sec / (3600*24);
24    
25            std::ostringstream os;
26            os << VERSION << std::endl;
27            os << SVNVERSION << std::endl;
28            os << "Status:" << std::endl;
29            os << "Recv: " << cmn->smsCounter.incomming << std::endl;
30            os << "Send: " << cmn->smsCounter.outgoing << std::endl;
31            os << "Uptime: " << days << "d, " << hour << ":" << min << ":" << sec;
32    
33            mg_printf(conn, "%s", "HTTP/1.1 200 OK\r\n");
34            mg_printf(conn, "%s", "Content-Type: text/plain\r\n\r\n");
35    
36            mg_printf(conn, "%s", os.str().c_str() );      
37    }
38    
39    void show_index(struct mg_connection *conn, const struct mg_request_info *request_info, void *user_data) {
40            mg_printf(conn, "%s", "HTTP/1.1 200 OK\r\n");
41            mg_printf(conn, "%s", "Content-Type: text/html\r\n\r\n");
42            
43            mg_printf(conn, "%s", "<html><body><h2>smsdaemon</h2>\n");
44            mg_printf(conn, "%s", "<a href='/status'>smsdaemon status</a>\n");
45            mg_printf(conn, "%s", "</body></html>");
46    }
47    
48  void EmbeddedHttp::startServer() {  void EmbeddedHttp::startServer() {
49          mg_start();          std::string portStr = Common::instance()->GetConfigfile()->GetValue("smsdaemon", "httpport");
50            int port = atoi( portStr.c_str() );
51    
52            if (port > 0) {
53                    std::ostringstream oss;
54                    oss << "Starting mongoose embedded http engine (listenin on port " << port << ")";
55                    Logger::logMessage( oss.str() );
56    
57                    oss.str(""); //empty string stream
58                    oss << port;
59    
60                    m_context = mg_start();
61                    mg_set_option(m_context, "ports", oss.str().c_str() );
62                    mg_set_uri_callback(m_context, "/", &show_index, 0);
63                    mg_set_uri_callback(m_context, "/status", &show_secret, 0);
64            } else {
65                    Logger::logMessage( "Mongoose http disabled" );
66                    m_context = 0;
67            }
68  }  }
69    
70    
71  void EmbeddedHttp::stopServer() {  void EmbeddedHttp::stopServer() {
72            if (m_context != 0) {
73                    mg_stop(m_context);
74            }
75  }  }

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

  ViewVC Help
Powered by ViewVC 1.1.20