diff options
| -rw-r--r-- | build.c | 164 | ||||
| -rw-r--r-- | src/engine.c | 102 | ||||
| -rw-r--r-- | src/log.c | 27 | ||||
| -rw-r--r-- | src/logger/logger.c | 5 | ||||
| -rw-r--r-- | src/logger/logger.h | 25 | ||||
| -rw-r--r-- | src/server.c | 17 |
6 files changed, 168 insertions, 172 deletions
@@ -6,6 +6,19 @@ #include <time.h> #include <string.h> +int status; + +#define RUN_CMD(...) do { \ + if (fork() == 0) { \ + char* args[] = __VA_ARGS__; \ + print_cmd(args); \ + execvp(args[0], args); \ + return 0; \ + } else { \ + wait(&status); \ + } \ +} while (0); + void print_cmd(char** args) { int i = 0; while (args[i] != NULL) { @@ -14,6 +27,11 @@ void print_cmd(char** args) { printf("\n"); } +int clean_mode(); +int test_mode(); +int server_mode(); +int engine_mode(); + // https://github.com/tsoding/nob.h/blob/0a08926d8094fc4ae678155c5d73ae21d1f96f3f/nob.h#L2413 int needs_rebuild(char* bin_file, char* source_file) { struct stat statbuf = {0}; @@ -32,113 +50,75 @@ int needs_rebuild(char* bin_file, char* source_file) { return input_path_time > output_path_time; } +#define ENGINE_O "output/engine.o" +#define SERVER_O "output/server.o" +#define LOG_O "output/log.o" + +#define LOGGER_C "src/logger/logger.c" + int main(int argc, char** argv) { if (needs_rebuild(argv[0], __FILE__)) { - if (fork() == 0) { - char* args[] = {"gcc", __FILE__, "-o", argv[0], NULL}; - print_cmd(args); - execvp(args[0], args); + RUN_CMD({"gcc", __FILE__, "-o", argv[0], NULL}); + if (status != 0) { + // gcc error return 0; - } else { - int x; - wait(&x); - if (x != 0) { - // gcc error - return 0; - } - print_cmd(argv); - execvp(argv[0], argv); } + print_cmd(argv); + execvp(argv[0], argv); } - if (fork() == 0) { - char* args[] = {"mkdir", "-p", "output", NULL}; - print_cmd(args); - execvp(args[0], args); - return 0; - } else { - wait(NULL); - } + RUN_CMD({"rm", "/dev/mqueue/logs", NULL}); + RUN_CMD({"mkdir", "-p", "output", NULL}); + RUN_CMD({"gcc", "src/log.c", "-o", LOG_O, NULL}); if (argc > 1) { if (strcmp(argv[1], "test") == 0) { - printf("Test mode\n"); - if (fork() == 0) { - char* args[] = {"gcc", "generate/find_tests.c", "-o", "output/find_tests.o", NULL}; - print_cmd(args); - execvp(args[0], args); - return 0; - } else { - wait(NULL); - } - if (fork() == 0) { - char* args[] = {"./output/find_tests.o", NULL}; - print_cmd(args); - execvp(args[0], args); - return 0; - } else { - wait(NULL); - } - printf("\n"); - if (fork() == 0) { - char* args[] = {"gcc", "tests/main.c", "-o", "output/test.o", NULL}; - print_cmd(args); - execvp(args[0], args); - return 0; - } else { - wait(NULL); - } - - char* args[] = {"./output/test.o", NULL}; - print_cmd(args); - execvp(args[0], args); - return 0; + return test_mode(); } if (strcmp(argv[1], "server") == 0) { - printf("Server mode\n"); - if (fork() == 0) { - char* args[] = {"rm", "/dev/mqueue/server_to_engine", "/dev/mqueue/engine_to_server", NULL}; - print_cmd(args); - execvp(args[0], args); - return 0; - } else { - wait(NULL); - } - if (fork() == 0) { - char* args[] = {"gcc", "src/server.c", "-lcrypto", "-o", "output/server.o", NULL}; - print_cmd(args); - execvp(args[0], args); - return 0; - } else { - wait(NULL); - } - - if (fork() == 0) { - char* args[] = {"gcc", "src/engine.c", "-o", "output/main.o", NULL}; - print_cmd(args); - execvp(args[0], args); - return 0; - } else { - wait(NULL); - } - - char* args[] = {"./output/server.o", "./output/main.o", NULL}; - print_cmd(args); - execvp(args[0], args); - return 0; + return server_mode(); + } + if (strcmp(argv[1], "clean") == 0) { + return clean_mode(); } } + return engine_mode(); +} - if (fork() == 0) { - char* args[] = {"gcc", "src/engine.c", "-o", "output/main.o", NULL}; - print_cmd(args); - execvp(args[0], args); - return 0; - } else { - wait(NULL); - } +int clean_mode() { + char* args[] = {"rm", "-r", "output", NULL}; + print_cmd(args); + execvp(args[0], args); + return 0; +} + +int test_mode() { + RUN_CMD({"gcc", "generate/find_tests.c", LOGGER_C, "-o", "output/find_tests.o", NULL}); + RUN_CMD({"./output/find_tests.o", NULL}); + printf("\n"); + RUN_CMD({"gcc", "tests/main.c", LOGGER_C, "-o", "output/test.o", NULL}); + + char* args[] = {LOG_O, "./output/test.o", NULL}; + print_cmd(args); + execvp(args[0], args); + return 0; +} + +int server_mode() { + RUN_CMD({"rm", "/dev/mqueue/server_to_engine", "/dev/mqueue/engine_to_server", NULL}); + RUN_CMD({"gcc", "src/server.c", LOGGER_C, "-lcrypto", "-o", SERVER_O, NULL}); + RUN_CMD({"gcc", "src/engine.c", LOGGER_C, "-o", ENGINE_O, NULL}); + + char* args[] = {LOG_O, SERVER_O, ENGINE_O, NULL}; + print_cmd(args); + execvp(args[0], args); + return 0; +} + +int engine_mode() { + RUN_CMD({"gcc", "src/engine.c", LOGGER_C, "-o", ENGINE_O, NULL}); - char* args[] = {"./output/main.o", NULL}; + char* args[] = {LOG_O, ENGINE_O, NULL}; print_cmd(args); execvp(args[0], args); return 0; diff --git a/src/engine.c b/src/engine.c index b4a4051..3c55146 100644 --- a/src/engine.c +++ b/src/engine.c @@ -2,16 +2,9 @@ #include <assert.h> #include "engine/bitboard.h" #include "engine/moves.h" - -void comms_main(); -int direct_run(); - -int main(int argc, char** argv) { - if (argc == 1) return direct_run(); - assert(argc == 2); - comms_main(); - return 0; -} +#include "fcntl.h" +#include "ipc.c" +#include "logger/logger.h" struct sqrstr { char data[2]; @@ -21,65 +14,38 @@ struct sqrstr sqr_to_str(int i) { return (struct sqrstr) { .data = {(i / 10) + '0', (i % 10) + '0' } }; } -#include "ipc.c" -void comms_main() { - mqd_t tx = mq_open("/engine_to_server", O_WRONLY); - mqd_t rx = mq_open("/server_to_engine", O_RDONLY); - - while (1) { - char* x = read_queue(rx); - printf("Engine received: %s", x); - free(x); - - send_queue(tx, "Sending", 8); - - moves_t moves = moves_init(); - position_t position = position_starting(); - - get_moves(&moves, position); - char buf[128]; - for (int i = 0; i <= moves.length / 32; i++) { - for (int ii = 0; ii < 32; ii++) { - if (moves.length <= 4 * i + ii) continue; - struct sqrstr from_str = sqr_to_str(moves.moves[4 * i + ii].from); - struct sqrstr to_str = sqr_to_str(moves.moves[4 * i + ii].to); - buf[4 * ii + 0] = from_str.data[0]; - buf[4 * ii + 1] = from_str.data[1]; - buf[4 * ii + 2] = to_str.data[0]; - buf[4 * ii + 3] = to_str.data[1]; - } - send_queue(tx, buf, sizeof(buf)); - } - } -} - -int direct_run() { - moves_t moves = moves_init(); - position_t position = position_starting(); - -position.bitboards[WHITE_KING] = 16ULL; -position.bitboards[WHITE_QUEEN] = 268435456ULL; -position.bitboards[WHITE_ROOK] = 16777344ULL; -position.bitboards[WHITE_BISHOP] = 67108896ULL; -position.bitboards[WHITE_KNIGHT] = 66ULL; -position.bitboards[WHITE_PAWN] = 65280ULL; -position.bitboards[BLACK_KING] = 1152921504606846976ULL; -position.bitboards[BLACK_QUEEN] = 576460752303423488ULL; -position.bitboards[BLACK_ROOK] = 9295429630892703744ULL; -position.bitboards[BLACK_BISHOP] = 2594073385365405696ULL; -position.bitboards[BLACK_KNIGHT] = 4611687117939015680ULL; -position.bitboards[BLACK_PAWN] = 71776119061217280ULL; - - get_queen_moves(&moves, position); - - bitboard_t b = 0; - - for (int i = 0; i < moves.length; i++) { - b |= (bitboard_t)1 << moves.moves[i].to; - printf("%d %d | ", (moves.moves[i].from), (moves.moves[i].to)); - } +int main(void) { + mqd_t logging = mq_open("/logs", O_WRONLY); + log_infof("Hello World"); + // mqd_t tx = mq_open("/engine_to_server", O_WRONLY); + // mqd_t rx = mq_open("/server_to_engine", O_RDONLY); + // + // while (1) { + // char* x = read_queue(rx); + // log_infof("Engine received: %s", x); + // free(x); + // + // send_queue(tx, "Sending", 8); + // + // moves_t moves = moves_init(); + // position_t position = position_starting(); + // + // get_moves(&moves, position); + // char buf[128]; + // for (int i = 0; i <= moves.length / 32; i++) { + // for (int ii = 0; ii < 32; ii++) { + // if (moves.length <= 4 * i + ii) continue; + // struct sqrstr from_str = sqr_to_str(moves.moves[4 * i + ii].from); + // struct sqrstr to_str = sqr_to_str(moves.moves[4 * i + ii].to); + // buf[4 * ii + 0] = from_str.data[0]; + // buf[4 * ii + 1] = from_str.data[1]; + // buf[4 * ii + 2] = to_str.data[0]; + // buf[4 * ii + 3] = to_str.data[1]; + // } + // send_queue(tx, buf, sizeof(buf)); + // } + // } - print_bitboard(b); return 0; } diff --git a/src/log.c b/src/log.c new file mode 100644 index 0000000..f921d52 --- /dev/null +++ b/src/log.c @@ -0,0 +1,27 @@ +#include <stdio.h> +#include "ipc.c" +#include "unistd.h" + +int main(int argc, char** argv) { + mqd_t rx = mq_open("/logs", O_CREAT | O_RDONLY, 0666, &attr); + + for (int i = 1; i < argc; i++) { + printf("Running: %s\n", argv[i]); + if (fork() == 0) { + char* new_argv[] = {argv[i], NULL}; + execvp(argv[i], new_argv); + } + } + + while (1) { + char* buf = read_queue(rx); + if (buf[0] == 'e') { + printf("[\x1b[31mERRO\x1b[0m] %s\n", buf + 1); + } else if (buf[0] == 'w') { + printf("[\x1b[1;33mWARN\x1b[0m] %s\n", buf + 1); + } else if (buf[0] == 'i') { + printf("[\x1b[32mINFO\x1b[0m] %s\n", buf + 1); + } + free(buf); + } +} diff --git a/src/logger/logger.c b/src/logger/logger.c new file mode 100644 index 0000000..9f1e612 --- /dev/null +++ b/src/logger/logger.c @@ -0,0 +1,5 @@ +#include "logger.h" + +void send_log(mqd_t target, const char* msg, int n) { + mq_send(target, msg, n, 0); +} diff --git a/src/logger/logger.h b/src/logger/logger.h new file mode 100644 index 0000000..ed265e9 --- /dev/null +++ b/src/logger/logger.h @@ -0,0 +1,25 @@ +#include <stdio.h> +#include <mqueue.h> + +#define log_infof(...) do { \ + char log_buf[1024]; \ + log_buf[0] = 'i'; \ + snprintf(log_buf + 1, 1023, __VA_ARGS__); \ + send_log(logging, log_buf, 1024); \ +} while(0); + +#define log_warnf(...) do { \ + char log_buf[1024]; \ + log_buf[0] = 'w'; \ + snprintf(log_buf, 1023, __VA_ARGS__); \ + send_log(logging, log_buf, 1024) \ +} while(0); + +#define log_errof(...) do { \ + char log_buf[1024]; \ + log_buf[0] = 'e'; \ + snprintf(log_buf, 1023, __VA_ARGS__); \ + send_log(logging, log_buf, 1024) \ +} while(0); + +void send_log(mqd_t target, const char* msg, int n); diff --git a/src/server.c b/src/server.c index d2ed0e5..8b39d38 100644 --- a/src/server.c +++ b/src/server.c @@ -2,12 +2,12 @@ #include <netinet/in.h> #include <openssl/sha.h> #include <stdio.h> -#include <stdlib.h> #include <string.h> #include <sys/socket.h> #include <sys/wait.h> #include <assert.h> #include <unistd.h> +#include "logger/logger.h" #define PORT 3456 @@ -16,18 +16,11 @@ #include "server/comms.c" #include "ipc.c" -int main(int argc, char** argv) { - assert(argc == 2); - +int main(void) { + mqd_t logging = mq_open("/logs", O_WRONLY); mqd_t tx = mq_open("/server_to_engine", O_CREAT | O_WRONLY, 0666, &attr); mqd_t rx = mq_open("/engine_to_server", O_CREAT | O_RDONLY, 0666, &attr); - if (fork() == 0) { - char* args[] = {argv[1], "listen", NULL}; - execvp(args[0], args); - return 0; - } - int server_fd = socket(AF_INET, SOCK_STREAM, 0); int opt = 1; @@ -40,7 +33,7 @@ int main(int argc, char** argv) { bind(server_fd, (struct sockaddr *)&addr, sizeof(addr)); listen(server_fd, 16); - printf("Listening on %d\n", PORT); + log_infof("Listening on %d\n", PORT); for (;;) { int client = accept(server_fd, NULL, NULL); @@ -50,7 +43,7 @@ int main(int argc, char** argv) { int n = recv(client, req, sizeof(req) - 1, 0); if (n <= 0) { - printf("Client close"); + log_infof("Client close"); close(client); continue; } |
