diff --git a/server-node-control/control.c b/server-node-control/control.c index 96919373f8c8295da8cb840065a46ad324030cfb..d97ae910ec30ecf0aad4cff79f66be4e25b2910a 100644 --- a/server-node-control/control.c +++ b/server-node-control/control.c @@ -14,6 +14,10 @@ #define VALUE_MAX 256 #define DIRECTION_MAX 256 +#ifndef PTHREAD_STACK_MIN +#define PTHREAD_STACK_MIN 16384 +#endif + typedef struct { int socket; struct sockaddr_in address; @@ -72,7 +76,7 @@ static int PWMEnable(int pwmnum) { int fd; // TODO: Enter the enable path. - snprintf(path, DIRECTION_MAX, "/sys/class/pwm/pwmchip0/pwm0/enable", pwmnum); + snprintf(path, DIRECTION_MAX, "/sys/class/pwm/pwmchip0/pwm0/enable"); fd = open(path, O_WRONLY); if (-1 == fd) { fprintf(stderr, "Failed to open in enable!\n"); @@ -90,7 +94,7 @@ static int PWMWritePeriod(int pwmnum, int value) { int fd, byte; // TODO: Enter the period path. - snprintf(path, VALUE_MAX, "/sys/class/pwm/pwmchip0/pwm0/period", pwmnum); + snprintf(path, VALUE_MAX, "/sys/class/pwm/pwmchip0/pwm0/period"); fd = open(path, O_WRONLY); if (-1 == fd) { fprintf(stderr, "Failed to open in period!\n"); @@ -114,7 +118,7 @@ static int PWMWriteDutyCycle(int pwmnum, int value) { int fd, byte; // TODO: Enter the duty_cycle path. - snprintf(path, VALUE_MAX, "/sys/class/pwm/pwmchip0/pwm0/duty_cycle", pwmnum); + snprintf(path, VALUE_MAX, "/sys/class/pwm/pwmchip0/pwm0/duty_cycle"); fd = open(path, O_WRONLY); if (-1 == fd) { fprintf(stderr, "Failed to open in duty cycle!\n"); @@ -132,7 +136,7 @@ static int PWMWriteDutyCycle(int pwmnum, int value) { return (0); } -// 수신 스레드 함수 +// 클라이언트 수신 스레드 함수 void *ClientToServer(void *idx) { int i = *((int*) idx); char buffer[BUFFER_SIZE]; @@ -141,17 +145,17 @@ void *ClientToServer(void *idx) { while ((len = recv(clients[i].socket, buffer, BUFFER_SIZE, 0)) > 0) { buffer[len] = '\0'; - printf("Received from client %d: %s", &clients[i].address.sin_addr, buffer); inet_ntop(AF_INET, &clients[i].address.sin_addr, ip, INET_ADDRSTRLEN); + printf("Received from client %s: %s", ip, buffer); if (send(pySocket, buffer, strlen(buffer), 0) < 0) { - perror("Error sending message to server"); + perror("Send message to server error"); } } if (len == 0) { printf("Client disconnected: %d\n", clients[i].socket); } else if (len == -1) { - perror("recv failed"); + perror("recv error"); } clientCnt--; close(clients[i].socket); @@ -162,8 +166,7 @@ void *ServerToClient() { char buffer[BUFFER_SIZE]; int len; while (1) { - len = read(pySocket, buffer, BUFFER_SIZE - 1); - if (len <= 0) { + if (len = recv(pySocket, buffer, BUFFER_SIZE - 1, 0) <= 0) { printf("Server disconnected\n"); close(pySocket); exit(0); @@ -186,14 +189,20 @@ void *ServerToClient() { else if (buffer[2] == 'P') { // 충전기 노드에 주차 사실 알림 if (send(clients[0].socket, buffer, strlen(buffer), 0) < 0) { - perror("Error forwarding message to client"); + perror("Send message error"); + } + + if (send(clients[1].socket, buffer, strlen(buffer), 0) < 0) { + perror("Send message error"); } } else if (buffer[2] == 'E') { - // 충전기 노드에 주차 사실 알림 if (send(clients[0].socket, buffer, strlen(buffer), 0) < 0) { - perror("Error forwarding message to client"); + perror("Send message error"); + } + if (send(clients[1].socket, buffer, strlen(buffer), 0) < 0) { + perror("Send message error"); } if (PWMExport(PWM)) printf("export error\n"); if (PWMWritePeriod(PWM, 20000000)) printf("period error\n"); @@ -208,11 +217,19 @@ void *ServerToClient() { // 충전기 노드에 화재 사실 알림 // 화재 노드에 화재 사실 알림 if (send(clients[0].socket, buffer, strlen(buffer), 0) < 0) { - perror("Error forwarding message to client"); + perror("Send message error"); + } + + if (send(clients[2].socket, buffer, strlen(buffer), 0) < 0) { + perror("Send message error"); } + } - if (send(clients[3].socket, buffer, strlen(buffer), 0) < 0) { - perror("Error forwarding message to client"); + else if (buffer[0] == 'C') { + for (int x = 0; x < 3; x++) { + if (send(clients[x].socket, buffer, strlen(buffer), 0) < 0) { + perror("Send message error"); + } } } } @@ -223,20 +240,22 @@ int main() { pthread_attr_t attr, attr2, at[3]; struct sched_param param, param2; socklen_t clientAddrLen = sizeof(clientAddr); - param.sched_priority=3; - param2.sched_priority=2; - - pthread_attr_init(&attr); - pthread_attr_setschedpolicy(&attr, SCHED_RR); - pthread_attr_setinheritsched(&attr, PTHREAD_EXPLICIT_SCHED); - pthread_attr_setschedparam(&attr, ¶m); - - pthread_attr_init(&attr2); - pthread_attr_setschedpolicy(&attr2, SCHED_RR); - pthread_attr_setinheritsched(&attr2, PTHREAD_EXPLICIT_SCHED); - pthread_attr_setschedparam(&attr2, ¶m2); - at[0] = at[1] = attr2; - at[2] = attr; + param.sched_priority=sched_get_priority_max(SCHED_RR); + param2.sched_priority=sched_get_priority_max(SCHED_RR) - 1; + + pthread_attr_init(&at[2]); + pthread_attr_setschedpolicy(&at[2], SCHED_RR); + pthread_attr_setinheritsched(&at[2], PTHREAD_EXPLICIT_SCHED); + pthread_attr_setschedparam(&at[2], ¶m); + pthread_attr_setstacksize(&at[2], PTHREAD_STACK_MIN); + + for (int i = 0; i < 2; i++) { + pthread_attr_init(&at[i]); + pthread_attr_setschedpolicy(&at[i], SCHED_RR); + pthread_attr_setinheritsched(&at[i], PTHREAD_EXPLICIT_SCHED); + pthread_attr_setschedparam(&at[i], ¶m2); + pthread_attr_setstacksize(&at[i], PTHREAD_STACK_MIN); +} // 서버로 연결할 클라이언트 소켓 생성 if ((pySocket = socket(AF_INET, SOCK_STREAM, 0)) < 0) { @@ -248,18 +267,18 @@ int main() { pyAddr.sin_family = AF_INET; pyAddr.sin_port = htons(49152); if (inet_pton(AF_INET, "127.0.0.1", &pyAddr.sin_addr) <= 0) { - perror("Invalid address / Address not supported"); + perror("Address error"); return -1; } if (connect(pySocket, (struct sockaddr *)&pyAddr, sizeof(pyAddr)) < 0) { - perror("Connection failed"); + perror("Connection error"); return -1; } - printf("서버에 연결되었습니다.\n"); + printf("Connected to server\n"); // 클라이언트를 받을 서버 소켓 설정 if ((cSocket = socket(AF_INET, SOCK_STREAM, 0)) == 0) { - perror("Socket failed"); + perror("Socket error"); return -1; } cAddr.sin_family = AF_INET; @@ -267,13 +286,13 @@ int main() { cAddr.sin_port = htons(PORT); if (bind(cSocket, (struct sockaddr *)&cAddr, sizeof(cAddr)) < 0) { - perror("Bind failed"); + perror("Bind error"); close(cSocket); return -1; } if (listen(cSocket, 3) < 0) { - perror("Listen failed"); + perror("Listen error"); close(cSocket); return -1; } @@ -285,7 +304,7 @@ int main() { int x; char ip[INET_ADDRSTRLEN]; if ((x = accept(cSocket, (struct sockaddr *)&clientAddr, &clientAddrLen)) < 0) { - perror("Accept failed"); + perror("Accept error"); continue; } @@ -294,11 +313,22 @@ int main() { tmp->address = clientAddr; clients[clientCnt] = *tmp; inet_ntop(AF_INET, &tmp->address.sin_addr, ip, INET_ADDRSTRLEN); - int idx = ip[11] - '7'; + //int *idx = (int *)malloc(sizeof(int *)); + int *idx = ip[11] - '7'; + if (ip[11] - '7' > 2 || ip[11] - '7' < 0) *idx = 0; + + *idx = clientCnt; + + if (pthread_create(&clientThreads[*idx], &at[*idx], ClientToServer, (void *)idx) < 0) { + perror("Create thread error"); + close(x); + free(tmp); + } - if (pthread_create(&clientThreads[idx], &at[idx], ClientToServer, (void *)&idx) < 0) { - perror("Failed to create thread"); + if (pthread_detach(clientThreads[*idx]) < 0) { + perror("Detach thread error"); close(x); + free(tmp); } }