summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLAN-TW <lantw44@gmail.com>2013-11-13 23:14:55 +0800
committerLAN-TW <lantw44@gmail.com>2013-11-13 23:14:55 +0800
commit513dd685fa8b7819d1a80b9c010aca883ff80003 (patch)
tree50d59f044fa2f4c4d8c559a51f34dac166dbe4e5
parent15d0398e07b2a2d25bd5aeae1be2fb30d9a9665b (diff)
downloadsp2013-513dd685fa8b7819d1a80b9c010aca883ff80003.tar
sp2013-513dd685fa8b7819d1a80b9c010aca883ff80003.tar.gz
sp2013-513dd685fa8b7819d1a80b9c010aca883ff80003.tar.bz2
sp2013-513dd685fa8b7819d1a80b9c010aca883ff80003.tar.lz
sp2013-513dd685fa8b7819d1a80b9c010aca883ff80003.tar.xz
sp2013-513dd685fa8b7819d1a80b9c010aca883ff80003.tar.zst
sp2013-513dd685fa8b7819d1a80b9c010aca883ff80003.zip
HW2: 加入讀取 big_judge 要求的功能
-rw-r--r--hw2/configure.ac3
-rw-r--r--hw2/judge.c121
2 files changed, 111 insertions, 13 deletions
diff --git a/hw2/configure.ac b/hw2/configure.ac
index 380edd5..24fdbf9 100644
--- a/hw2/configure.ac
+++ b/hw2/configure.ac
@@ -13,12 +13,15 @@ AC_CANONICAL_HOST
AC_CANONICAL_BUILD
AH_TEMPLATE([OS_IS_LINUX])
AH_TEMPLATE([OS_IS_FREEBSD])
+AH_TEMPLATE([_WITH_GETLINE])
case "$host_os" in
*linux*)
AC_DEFINE([OS_IS_LINUX], [1])
;;
*freebsd*)
AC_DEFINE([OS_IS_FREEBSD], [1])
+ AC_DEFINE([_WITH_GETLINE])
+ ;;
esac
# Checks for programs.
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;