Statistics
| Branch: | Tag: | Revision:

mongoose / examples / sntp_client / sntp_client.c @ eaef5bd1

History | View | Annotate | Download (1.05 KB)

1
/*
2
 * Copyright (c) 2016 Cesanta Software Limited
3
 * All rights reserved
4
 */
5

    
6
#include "mongoose.h"
7

    
8
static int s_exit_flag = 0;
9
static const char *s_default_server = "pool.ntp.org";
10

    
11
static void ev_handler(struct mg_connection *c, int ev, void *ev_data) {
12
  struct mg_sntp_message *sm = (struct mg_sntp_message *) ev_data;
13
  time_t t;
14
  (void) c;
15

    
16
  switch (ev) {
17
    case MG_SNTP_REPLY:
18
      t = time(NULL);
19
      fprintf(stdout, "Local time: %s", ctime(&t));
20
      t = (time_t) sm->time;
21
      fprintf(stdout, "Time from %s: %s", s_default_server, ctime(&t));
22
      s_exit_flag = 1;
23
      break;
24
    case MG_SNTP_FAILED:
25
      fprintf(stderr, "Failed to get time\n");
26
      s_exit_flag = 1;
27
      break;
28
  }
29
}
30

    
31
int main() {
32
  struct mg_mgr mgr;
33
  struct mg_connection *c;
34

    
35
  mg_mgr_init(&mgr, NULL);
36

    
37
  c = mg_sntp_get_time(&mgr, ev_handler, s_default_server);
38

    
39
  if (c == NULL) {
40
    fprintf(stderr, "Failed to connect to %s\n", s_default_server);
41
    return -1;
42
  }
43

    
44
  while (s_exit_flag == 0) {
45
    mg_mgr_poll(&mgr, 1000);
46
  }
47

    
48
  mg_mgr_free(&mgr);
49

    
50
  return 0;
51
}