Statistics
| Branch: | Tag: | Revision:

mongoose / examples / connected_device_4 / server.c @ eaef5bd1

History | View | Annotate | Download (3.32 KB)

1
// Copyright (c) 2015 Cesanta Software Limited
2
// All rights reserved
3

    
4
#include "mongoose.h"
5

    
6
struct device_settings {
7
  char setting1[100];
8
  char setting2[100];
9
};
10

    
11
static const char *s_http_port = "8000";
12
static struct mg_serve_http_opts s_http_server_opts;
13
static struct device_settings s_settings = {"value1", "value2"};
14

    
15
static void handle_save(struct mg_connection *nc, struct http_message *hm) {
16
  // Get form variables and store settings values
17
  mg_get_http_var(&hm->body, "setting1", s_settings.setting1,
18
                  sizeof(s_settings.setting1));
19
  mg_get_http_var(&hm->body, "setting2", s_settings.setting2,
20
                  sizeof(s_settings.setting2));
21

    
22
  // Send response
23
  mg_http_send_redirect(nc, 302, mg_mk_str("/"), mg_mk_str(NULL));
24
}
25

    
26
static void handle_get_cpu_usage(struct mg_connection *nc) {
27
  // Generate random value, as an example of changing CPU usage
28
  // Getting real CPU usage depends on the OS.
29
  int cpu_usage = (double) rand() / RAND_MAX * 100.0;
30

    
31
  // Use chunked encoding in order to avoid calculating Content-Length
32
  mg_printf(nc, "%s", "HTTP/1.1 200 OK\r\nTransfer-Encoding: chunked\r\n\r\n");
33

    
34
  // Output JSON object which holds CPU usage data
35
  mg_printf_http_chunk(nc, "{ \"result\": %d }", cpu_usage);
36

    
37
  // Send empty chunk, the end of response
38
  mg_send_http_chunk(nc, "", 0);
39
}
40

    
41
static void handle_ssi_call(struct mg_connection *nc, const char *param) {
42
  if (strcmp(param, "setting1") == 0) {
43
    mg_printf_html_escape(nc, "%s", s_settings.setting1);
44
  } else if (strcmp(param, "setting2") == 0) {
45
    mg_printf_html_escape(nc, "%s", s_settings.setting2);
46
  }
47
}
48

    
49
static void ev_handler(struct mg_connection *nc, int ev, void *ev_data) {
50
  struct http_message *hm = (struct http_message *) ev_data;
51

    
52
  switch (ev) {
53
    case MG_EV_HTTP_REQUEST:
54
      if (mg_vcmp(&hm->uri, "/save") == 0) {
55
        handle_save(nc, hm);
56
      } else if (mg_vcmp(&hm->uri, "/get_cpu_usage") == 0) {
57
        handle_get_cpu_usage(nc);
58
      } else {
59
        mg_serve_http(nc, hm, s_http_server_opts);  // Serve static content
60
      }
61
      break;
62
    case MG_EV_SSI_CALL:
63
      handle_ssi_call(nc, ev_data);
64
      break;
65
    default:
66
      break;
67
  }
68
}
69

    
70
static void push_data_to_all_websocket_connections(struct mg_mgr *m) {
71
  struct mg_connection *c;
72
  int memory_usage = (double) rand() / RAND_MAX * 100.0;
73

    
74
  for (c = mg_next(m, NULL); c != NULL; c = mg_next(m, c)) {
75
    if (c->flags & MG_F_IS_WEBSOCKET) {
76
      mg_printf_websocket_frame(c, WEBSOCKET_OP_TEXT, "%d", memory_usage);
77
    }
78
  }
79
}
80

    
81
int main(void) {
82
  struct mg_mgr mgr;
83
  struct mg_connection *nc;
84
  cs_stat_t st;
85

    
86
  mg_mgr_init(&mgr, NULL);
87
  nc = mg_bind(&mgr, s_http_port, ev_handler);
88
  if (nc == NULL) {
89
    fprintf(stderr, "Cannot bind to %s\n", s_http_port);
90
    exit(1);
91
  }
92

    
93
  // Set up HTTP server parameters
94
  mg_set_protocol_http_websocket(nc);
95
  s_http_server_opts.document_root = "web_root";  // Set up web root directory
96

    
97
  if (mg_stat(s_http_server_opts.document_root, &st) != 0) {
98
    fprintf(stderr, "%s", "Cannot find web_root directory, exiting\n");
99
    exit(1);
100
  }
101

    
102
  printf("Starting web server on port %s\n", s_http_port);
103
  for (;;) {
104
    static time_t last_time;
105
    time_t now = time(NULL);
106
    mg_mgr_poll(&mgr, 1000);
107
    if (now - last_time > 0) {
108
      push_data_to_all_websocket_connections(&mgr);
109
      last_time = now;
110
    }
111
  }
112
  mg_mgr_free(&mgr);
113

    
114
  return 0;
115
}