server2.c
3.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#include <stdio.h>
#include <stdlib.h>
#include <libwebsockets.h>
#include <unistd.h>
#include <termios.h>
#define MAX_FRAME_SIZE 1024
#define WAIT_DELAY 50
#define TAILLE_MAX_BUFFER 50
#define TAILLE_MOT 8
static int callback_http(struct libwebsocket_context * this,
struct libwebsocket *wsi,
enum libwebsocket_callback_reasons reason, void *user,
void *in, size_t len)
{
return 0;
}
static int callback_dumb_increment(struct libwebsocket_context * this,
struct libwebsocket *wsi,
enum libwebsocket_callback_reasons reason,
void *user, char *in, size_t len)
{
switch (reason) {
case LWS_CALLBACK_ESTABLISHED: // just log message that someone is connecting
printf("connection established\n");
break;
case LWS_CALLBACK_RECEIVE: {
// Création du buffer pour contenir notre réponse
char* out[4];
out[1] = strtok((char*)in,",");
out[2] = strtok(NULL,",");
out[3] = strtok(NULL,",");
out[4] = strtok(NULL,",");
char commande[50];
sprintf(commande, "python get_data.py %s %s %s %s", out[1],out[2],out[3],out[4]);
FILE *file = popen(commande,"r");
char a[TAILLE_MAX_BUFFER], b[TAILLE_MAX_BUFFER], c[TAILLE_MAX_BUFFER];
char heure[TAILLE_MOT],minute[TAILLE_MOT],temperature[TAILLE_MOT];
fscanf(file,"%s",a);
fscanf(file,"%s",b);
fscanf(file,"%s",c);
pclose(file);
int i;
for(i=0;i<8;i++){
heure[i]=a[i+2];
minute[i]=b[i+1];
temperature[i]=c[i+1];
}
heure[8]='\0';
minute[8]='\0';
temperature[8]='\0';
printf("%s\n",heure);
printf("%s\n",minute);
printf("%s\n",temperature);
// release memory back into the wild
break;
}
default:
break;
}
return 0;
}
static struct libwebsocket_protocols protocols[] = {
/* first protocol must always be HTTP handler */
{
"http-only", // name
callback_http, // callback
0 // per_session_data_size
},
{
"dumb-increment-protocol", // protocol name - very important!
callback_dumb_increment, // callback
0 // we don't use any per session data
},
{
NULL, NULL, 0 /* End of list */
}
};
int main(void) {
int port=9000;
struct lws_context_creation_info info;
memset(&info,0,sizeof info);
info.port=port;
info.protocols=protocols;
info.gid=-1;
info.uid=-1;
struct libwebsocket_context *context=libwebsocket_create_context(&info);
if(context==NULL){
fprintf(stderr, "libwebsocket init failed\n");
return -1;
}
printf("starting server...\n");
while(1){
libwebsocket_service(context,WAIT_DELAY);
}
libwebsocket_context_destroy(context);
return 0;
}