-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.c
More file actions
366 lines (291 loc) · 10.2 KB
/
server.c
File metadata and controls
366 lines (291 loc) · 10.2 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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
/*
This code primarily comes from
http://www.prasannatech.net/2008/07/socket-programming-tutorial.html
and
http://www.binarii.com/files/papers/c_sockets.txt
*/
#include "server_helper.h"
#include "arduino_funcs.h"
// void* say_hello(void* p) {
// int i = 0;
// while(quit != 'q') {
// // pthread_mutex_lock(&lock);
// printf("%d\n", i++);
// // sleep(2);
// if (i % 5 == 0) {
// // pthread_mutex_unlock(&lock);
// }
// }
// pthread_exit(NULL);
// }
int start_server(int PORT_NUMBER) {
pthread_mutex_t lock;
pthread_mutex_init(&lock, NULL);
/*************************************************************************/
/********************* Getting the server set up *************************/
/*************************************************************************/
// structs to represent the server and client
struct sockaddr_in server_addr, client_addr;
int sock; // socket descriptor
// 1. socket: creates a socket descriptor that you later use to make other system calls
if ((sock = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
perror("Socket");
exit(1);
}
int temp;
if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &temp, sizeof(int)) == -1) {
perror("Setsockopt");
exit(1);
}
// configure the server
server_addr.sin_port = htons(PORT_NUMBER); // specify port number
server_addr.sin_family = AF_INET;
server_addr.sin_addr.s_addr = INADDR_ANY;
bzero(&(server_addr.sin_zero), 8);
// 2. bind: use the socket and associate it with the port number
if (bind(sock, (struct sockaddr *)&server_addr, sizeof(struct sockaddr)) == -1) {
perror("Unable to bind");
exit(1);
}
// 3. listen: indicates that we want to listen to the port to which we bound; second arg is number of allowed connections
if (listen(sock, 1) == -1) {
perror("Listen");
exit(1);
}
// once you get here, the server is set up and about to start listening
printf("\nServer configured to listen on port %d\n", PORT_NUMBER);
fflush(stdout);
// 4. accept: wait here until we get a connection on that port
int sin_size = sizeof(struct sockaddr_in);
/*************************************************************************/
/*************************************************************************/
/*************************************************************************/
/***************** Open Arduino Connection to Server *********************/
/*************************************************************************/
char* quit = malloc(sizeof(char));
// set quit flag to '\0'
*quit = '\0';
// retrieve file descriptor from Arduino
// int ard_fd = get_started(filename);
// write initial null char to Arduino to clear out
// write_to_arduino(ard_fd, '\0');
// create a packet with relevant data
packet* pack = malloc(sizeof(packet));
// pack->filename = filename;
// pack->ard_fd = ard_fd;
pack->quit_flag = 0;
pack->lock = &lock;
pack->is_Celsius = 1;
pack->ctrl_signal = '\0';
pack->requesting = 0;
pthread_t ard_t;
pthread_create(&ard_t, NULL, &handle_arduino, pack);
// pthread_mutex_lock(&lock);
int quit_flag = pack->quit_flag;
// pthread_mutex_unlock(&lock);
// keep on accepting requests as long as
// haven't received command to close server
while (!quit_flag) {
pthread_t close_s;
pthread_create(&close_s, NULL, &close_server, pack);
/******** set up select() for accept()ing HTML requests ********/
int sret; // to get return value from select()
fd_set readfds; // bit array to represent fds
struct timeval timeout; // struct to determine how long to wait before timeout
FD_ZERO(&readfds); // zero out bit array
FD_SET(sock, &readfds); // set bit array
timeout.tv_sec = 3; // set timeout time
timeout.tv_usec = 0;
/***************************************************************/
sret = select(8, &readfds, NULL, NULL, &timeout); // run the select()
if (sret < 0) {
perror("sret < 0 server");
exit(errno);
}
// if have received an HTTP request...
if (sret != 0) {
pack->requesting = 1;
// accept
int fd = accept(sock, (struct sockaddr *)&client_addr,(socklen_t *)&sin_size);
if (fd != -1) {
pack->client_addr = client_addr; // add additional info to packet
pack->fd = fd;
pthread_t req; // create request thread
pthread_create(&req, NULL, &handle_connection, pack);
pthread_join(req, NULL); // join request thread
}
}
// pthread_mutex_lock(&lock);
quit_flag = pack->quit_flag;
// pthread_mutex_unlock(&lock);
pthread_join(close_s, NULL); // join close thread
pack->requesting = 0;
}
pthread_join(ard_t, NULL); // join thread that handles Arduino behavior
// free() quit
free(quit);
// free() pack
free(pack);
// destroy lock
pthread_mutex_destroy(&lock);
// 8. close: close the socket
close(sock);
printf("Server shutting down\n");
return 0;
}
/**
* function for thread to wait for user
* input to close the server
* @param p [description]
*/
void* close_server(void* p) {
packet* pack = (packet*) p;
pthread_mutex_t* lock = pack->lock;
int flags = fcntl(STDIN_FILENO, F_GETFL);
fcntl(0, F_SETFL, flags | O_NONBLOCK);
// int fd = 0;
// int sret; // to get return value from select()
// fd_set readfds; // bit array to represent fds
// struct timeval timeout; // struct to determine how long to wait before timeout
// FD_ZERO(&readfds); // zero out bit array
// FD_SET(fd, &readfds); // set bit array
// timeout.tv_sec = 3; // set timeout time
// timeout.tv_usec = 0;
char buf[10];
buf[0] = '\0';
// pthread_mutex_lock(lock);
// sret = select(8, &readfds, NULL, NULL, &timeout);
// scanf("%s", buf);
// scanf("%s", buf);
int bytes_read = read(0, buf, sizeof(buf) - 1);
if (bytes_read > 0) {
buf[bytes_read] = '\0';
printf("\n\n%d bytes read: typed %s\n\n", bytes_read, buf);
if (buf[0] == 'q') {
pthread_mutex_lock(lock);
pack->quit_flag = 1;
pthread_mutex_unlock(lock);
}
} else {
printf("timed out\n");
}
pthread_exit(NULL);
}
/**
* handle connection when request comes in
* @param p packet containing relevant data
*/
void* handle_connection(void* p) {
// unpack packet
packet* pack = (packet*) p;
struct sockaddr_in client_addr = pack->client_addr;
int fd = pack->fd;
pthread_mutex_t* lock = pack->lock;
printf("Server got a connection from (%s, %d)\n", inet_ntoa(client_addr.sin_addr), ntohs(client_addr.sin_port));
// buffer to read data into
char request[1024];
// 5. recv: read incoming message (request) into buffer
int bytes_received = recv(fd,request,1024,0);
// null-terminate the string
request[bytes_received] = '\0';
// print it to standard out
printf("This is the incoming request:\n%s\n", request);
// parse request
char* req = get_path(request);
if (req == NULL) {
close(fd);
pthread_exit(NULL);
}
// ignore favicon.ico requests
if (strcmp(req, "favicon.ico") == 0) {
close(fd);
free(req);
pthread_exit(NULL);
}
// figure out filetype of requested file
int type = get_filetype(req);
char* type_msg;
switch(type) {
case 1:
type_msg = "text/html";
break;
case 2:
type_msg = "text/css";
break;
case 3:
type_msg = "application/javascript";
break;
case 4:
type_msg = "text/javascript";
}
// this is the message that we'll send back
char* reply = malloc(sizeof(char) * 100);
if (reply == NULL) {
close(fd);
pthread_exit(NULL);
}
sprintf(reply, "HTTP/1.1 200 OK\nContent-Type: %s\n\n", type_msg);
// read the HTML file, and append it to the reply
char* add = read_html_file(req);
free(req);
// handle invalid requests
if (add == NULL) {
free(reply);
close(fd);
pthread_exit(NULL);
}
// realloc() reply
reply = (char*) realloc(reply, sizeof(char) * ((strlen(reply) + strlen(add) + 1)));
// concatenate
strcat(reply, add);
free(add);
// 6. send: send the outgoing message (response) over the socket
// note that the second argument is a char*, and the third is the number of chars
send(fd, reply, strlen(reply), 0);
// handle if it's a POST request
if (is_get(request) == 1) {
char* post = get_post(request);
char c = parse_post(post);
char* kvp = create_first_pair("status", "0");
if (kvp != NULL) {
if (c == '\0') {
change_status(&kvp, '1');
}
printf("writting error file\n");
write_to_file(kvp, "error.json");
destroy_kvps(&kvp);
}
while (pack->ctrl_signal != '\0' && pack->ctrl_signal != 'q'){
printf("control is still %c\n", pack->ctrl_signal);
sleep(3);
// block
}
pthread_mutex_lock(lock);
pack->ctrl_signal = c;
if (c == 'f') {
pack->is_Celsius = 0;
}
if (c == 'c') {
pack->is_Celsius = 1;
}
pthread_mutex_unlock(lock);
}
free(reply);
// 7. close: close the connection
close(fd);
printf("Server closed connection\n");
pthread_exit(NULL);
}
int main(int argc, char *argv[]) {
// check the number of arguments
if (argc != 2) {
printf("\nUsage: %s [port_number]\n", argv[0]);
exit(-1);
}
int port_number = atoi(argv[1]);
if (port_number <= 1024) {
printf("\nPlease specify a port number greater than 1024\n");
exit(-1);
}
start_server(port_number);
}