diff options
author | LAN-TW <lantw44@gmail.com> | 2013-11-13 23:14:55 +0800 |
---|---|---|
committer | LAN-TW <lantw44@gmail.com> | 2013-11-13 23:14:55 +0800 |
commit | 513dd685fa8b7819d1a80b9c010aca883ff80003 (patch) | |
tree | 50d59f044fa2f4c4d8c559a51f34dac166dbe4e5 /hw2/judge.c | |
parent | 15d0398e07b2a2d25bd5aeae1be2fb30d9a9665b (diff) | |
download | sp2013-513dd685fa8b7819d1a80b9c010aca883ff80003.tar.gz sp2013-513dd685fa8b7819d1a80b9c010aca883ff80003.tar.zst sp2013-513dd685fa8b7819d1a80b9c010aca883ff80003.zip |
HW2: 加入讀取 big_judge 要求的功能
Diffstat (limited to 'hw2/judge.c')
-rw-r--r-- | hw2/judge.c | 121 |
1 files changed, 108 insertions, 13 deletions
diff --git a/hw2/judge.c b/hw2/judge.c index 141f359..ef9a07e 100644 --- a/hw2/judge.c +++ b/hw2/judge.c @@ -6,13 +6,52 @@ #include "logger.h" #include "xwrap.h" +#include <ctype.h> #include <errno.h> +#include <fcntl.h> +#include <stdbool.h> #include <stdio.h> #include <string.h> #include <sys/stat.h> #include <sys/types.h> #include <unistd.h> +#define FDATA_BUFSIZ 512 + +typedef struct { + int fd; + int fd_unused; + char* fname; + int score; + int ignore : 1; + char buf[FDATA_BUFSIZ]; + size_t buf_start; + size_t buf_len; +} FData; + +static int str2list (char* str, char* result[], int maxlen) { + int complen = 0; + char* now = str; + + while (complen < maxlen) { + for (; isspace (*now) && *now != '\0'; now++); + if (*now == '\0') { + break; + } + + result[complen++] = now; + for (; !isspace (*now) && *now != '\0'; now++); + if (*now == '\0') { + break; + } + + *now = '\0'; + now++; + } + + return complen; +} + int main (int argc, char* argv[]) { if (argc < 2) { fprintf (stderr, "Usage: %s judge_id\n", argv[0]); @@ -20,20 +59,46 @@ int main (int argc, char* argv[]) { } const char* judgename = argv[1]; - char* fifoname[5] = { - xstrcat ("judge", judgename, ".FIFO", NULL), - xstrcat ("judge", judgename, "_A.FIFO", NULL), - xstrcat ("judge", judgename, "_B.FIFO", NULL), - xstrcat ("judge", judgename, "_C.FIFO", NULL), - xstrcat ("judge", judgename, "_D.FIFO", NULL) + FData ffd[5] = { + { .fname = xstrcat ("judge", judgename, ".FIFO", NULL) }, // read + { .fname = xstrcat ("judge", judgename, "_A.FIFO", NULL) }, // write + { .fname = xstrcat ("judge", judgename, "_B.FIFO", NULL) }, // write + { .fname = xstrcat ("judge", judgename, "_C.FIFO", NULL) }, // write + { .fname = xstrcat ("judge", judgename, "_D.FIFO", NULL) } // write }; - for (int i = 0; i < ARRAY_LEN (fifoname, char*); i++) { - if (mkfifo (fifoname[i], S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP) < 0) { - fprintf (stderr, "%s: cannot create FIFO `%s\': %s", - argv[0], fifoname[i], strerror (errno)); + for (int i = 0; i < ARRAY_LEN (ffd, FData); i++) { + if (mkfifo (ffd[i].fname, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP) < 0) { + fprintf (stderr, "%s: cannot create FIFO `%s\': %s\n", + argv[0], ffd[i].fname, strerror (errno)); return 2; } + + int fdr = open (ffd[i].fname, O_RDONLY | O_NONBLOCK); + if (fdr < 0) { + fprintf (stderr, "%s: cannot open `%s\' for reading: %s\n", + argv[0], ffd[i].fname, strerror (errno)); + return 3; + } + int fdw = open (ffd[i].fname, O_WRONLY | O_NONBLOCK); + if (fdw < 0) { + fprintf (stderr, "%s: cannot open `%s\' for writing: %s\n", + argv[0], ffd[i].fname, strerror (errno)); + return 3; + } + + if (i) { + ffd[i].fd = fdw; + ffd[i].fd_unused = fdr; + } else { + ffd[i].fd = fdr; + ffd[i].fd_unused = fdw; + } + + ffd[i].score = 0; + ffd[i].ignore = false; + ffd[i].buf_start = 0; + ffd[i].buf_len = 0; } Comp135 comp_struct, *comp; @@ -41,12 +106,42 @@ int main (int argc, char* argv[]) { comp135_init (comp, argv[0], false); + char *linestr = NULL; + size_t linelen = 0; + bool request_exit = false; + while (!request_exit && getline (&linestr, &linelen, stdin) >= 0) { + char* pl[4]; + int p = str2list (linestr, pl, 4); + if (p < 4) { + fprintf (stderr, "Too few arguments: %d provided, 4 required\n", p); + goto new_loop_end; + } + + comp135_log (comp, "Request: player list: %s %s %s %s", + pl[0], pl[1], pl[2], pl[3]); + if (strcmp (pl[0], "-1") == 0 && + strcmp (pl[1], "-1") == 0 && + strcmp (pl[2], "-1") == 0 && + strcmp (pl[3], "-1") == 0) + { + comp135_log (comp, "Request: exit"); + request_exit = true; + goto new_loop_end; + } + +new_loop_end: + free (linestr); + linestr = NULL; + linelen = 0; + } comp135_destroy (comp); - for (int i = 0; i < ARRAY_LEN (fifoname, char*); i++) { - unlink (fifoname[i]); - free (fifoname[i]); + for (int i = 0; i < ARRAY_LEN (ffd, FData); i++) { + close (ffd[i].fd); + close (ffd[i].fd_unused); + unlink (ffd[i].fname); + free (ffd[i].fname); } return 0; |