Fixed pytest p1 failures
This commit is contained in:
+10
-11
@@ -318,14 +318,14 @@ static int setup_server_socket_udp(struct sockaddr_in addr){
|
||||
return sock;
|
||||
}
|
||||
|
||||
static void handle_udp_request(int _caller_sockfd){
|
||||
static void handle_udp_request(int _sock){
|
||||
size_t received_bytes = 0;
|
||||
char buffer[HTTP_MAX_SIZE] = {0};
|
||||
|
||||
struct sockaddr_storage caller_addr;
|
||||
socklen_t addrlen = sizeof caller_addr;
|
||||
|
||||
if((received_bytes = recvfrom(_caller_sockfd, buffer, HTTP_MAX_SIZE, 0, (struct sockaddr *) &caller_addr, &addrlen)) == -1){
|
||||
if((received_bytes = recvfrom(_sock, buffer, HTTP_MAX_SIZE, 0, (struct sockaddr *) &caller_addr, &addrlen)) == -1){
|
||||
perror("Func: handle_udp_request - failed to receive data from caller!");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
@@ -338,7 +338,7 @@ static void handle_udp_request(int _caller_sockfd){
|
||||
|
||||
if ((pre_id < hash && hash <= _NODE_ID) || (pre_id > _NODE_ID && (hash > pre_id || hash <= _NODE_ID))) {
|
||||
char res[] = "HTTP/1.1 200 OK\r\nContent-Length: 0\r\n\r\n";
|
||||
if (sendto(_caller_sockfd, res, strlen(res), 0, (struct sockaddr *) &caller_addr, addrlen) == -1) {
|
||||
if (sendto(_sock, res, strlen(res), 0, (struct sockaddr *) &caller_addr, addrlen) == -1) {
|
||||
perror("Func: handle_udp_request - failed to send data to caller!");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
@@ -347,7 +347,7 @@ static void handle_udp_request(int _caller_sockfd){
|
||||
// TODO: 303 - See other
|
||||
char res[1024] = {0};
|
||||
snprintf(res, 1024, "HTTP/1.1 303 See Other\r\nLocation: http://%s:%s/%d\r\nContent-Length: 0\r\n\r\n", _NODE_IP, _NODE_PORT, suc_id);
|
||||
if(sendto(_caller_sockfd, res, strlen(res), 0, (struct sockaddr *) &caller_addr, addrlen) == -1){
|
||||
if(sendto(_sock, res, strlen(res), 0, (struct sockaddr *) &caller_addr, addrlen) == -1){
|
||||
perror("Func: handle_udp_request - failed to send data to caller!");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
@@ -363,12 +363,14 @@ static void handle_udp_request(int _caller_sockfd){
|
||||
* ./build/webserver self.ip self.port
|
||||
*/
|
||||
int main(int argc, char **argv) {
|
||||
if (argc != 4) {
|
||||
if (argc != 3 && argc != 4) {
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
if(argc == 3) _NODE_ID = 0;
|
||||
elif(argc == 4) _NODE_ID = atoi(argv[3]);
|
||||
|
||||
// Set this DHT-Nodes ID
|
||||
_NODE_ID = atoi(argv[3]);
|
||||
_NODE_PORT = argv[2];
|
||||
_NODE_IP = argv[1];
|
||||
|
||||
@@ -379,9 +381,8 @@ int main(int argc, char **argv) {
|
||||
int server_socket_udp = setup_server_socket_udp(addr);
|
||||
|
||||
// Create an array of pollfd structures to monitor sockets.
|
||||
struct pollfd sockets[3] = {
|
||||
struct pollfd sockets[2] = {
|
||||
{.fd = server_socket, .events = POLLIN},
|
||||
{},
|
||||
{.fd = server_socket_udp, .events = POLLIN}
|
||||
};
|
||||
|
||||
@@ -409,8 +410,7 @@ int main(int argc, char **argv) {
|
||||
// If the event is on the server_socket, accept a new connection
|
||||
// from a client.
|
||||
int connection = accept(server_socket, NULL, NULL);
|
||||
if (connection == -1 && errno != EAGAIN &&
|
||||
errno != EWOULDBLOCK) {
|
||||
if (connection == -1 && errno != EAGAIN && errno != EWOULDBLOCK) {
|
||||
close(server_socket);
|
||||
perror("accept");
|
||||
exit(EXIT_FAILURE);
|
||||
@@ -426,7 +426,6 @@ int main(int argc, char **argv) {
|
||||
elif(s == server_socket_udp){
|
||||
// DO UDP STUFF
|
||||
handle_udp_request(s);
|
||||
sockets[2].events = POLLIN;
|
||||
}
|
||||
|
||||
else {
|
||||
|
||||
Reference in New Issue
Block a user