From a95b484414d17f52eb5ece6d144fff340c97f1c0 Mon Sep 17 00:00:00 2001 From: Benjamin Franzke Date: Fri, 9 Nov 2012 09:42:30 +0100 Subject: pd_servert: Fix space vs tabs --- pd_server.c | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/pd_server.c b/pd_server.c index d0b2315..80fcb4f 100644 --- a/pd_server.c +++ b/pd_server.c @@ -14,14 +14,17 @@ enum { }; int pay(int a, int b) -{ - if (a == COORPERATION && b == DEFECTION) return 1; - if (a == DEFECTION && b == DEFECTION) return 2; - if (a == COORPERATION && b == COORPERATION) return 3; - if (a == DEFECTION && b == COORPERATION) return 4; +{ + assert(a == COORPERATION || a == DEFECTION); + assert(b == COORPERATION || b == DEFECTION); + + if (a == COORPERATION && b == DEFECTION) return 1; + if (a == DEFECTION && b == DEFECTION) return 2; + if (a == COORPERATION && b == COORPERATION) return 3; + if (a == DEFECTION && b == COORPERATION) return 4; - exit(EXIT_FAILURE); -} + exit(EXIT_FAILURE); +} int main(int argc, char *argv[]) @@ -68,11 +71,11 @@ main(int argc, char *argv[]) continue; } - a_payment += pay(a, b); - b_payment += pay(b, a); + a_payment += pay(a, b); + b_payment += pay(b, a); - printf("A [%c]: %d Euro\tB [%c]: %d Euro\t\t\tA: %d\tB: %d\n", - a, pay(a, b), b, pay(b, a), a_payment, b_payment); + printf("A [%c]: %d Euro\tB [%c]: %d Euro\t\t\tA: %d\tB: %d\n", + a, pay(a, b), b, pay(b, a), a_payment, b_payment); write(player[0], &b, 1); write(player[1], &a, 1); -- cgit From 340fa59b67dfcc850717c7453f75e5126eaeb1e9 Mon Sep 17 00:00:00 2001 From: Benjamin Franzke Date: Fri, 9 Nov 2012 09:46:01 +0100 Subject: pd_server: Some code cleanup --- pd_server.c | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/pd_server.c b/pd_server.c index 80fcb4f..829a6f7 100644 --- a/pd_server.c +++ b/pd_server.c @@ -1,6 +1,7 @@ #include #include #include +#include #include #include #include @@ -29,12 +30,17 @@ int pay(int a, int b) int main(int argc, char *argv[]) { - int sock, player[2], read_fd; + int sock, player[2]; struct sockaddr_in serv_addr; int port = 8068; - int rounds, i; + int a_payment = 0, b_payment = 0, rounds, i; int optval = 1; + if (argc < 2) { + fprintf(stderr, "usage: %s number-of-rounds\n", argv[0]); + exit(EXIT_FAILURE); + } + rounds = atoi(argv[1]); sock = socket(AF_INET, SOCK_STREAM, 0); @@ -56,7 +62,6 @@ main(int argc, char *argv[]) for (i = 0; i < 2; ++i) { player[i] = accept(sock, NULL, NULL); } - int a_payment = 0, b_payment = 0; for (i = 0; i < rounds; ++i) { char a, b; read(player[0], &a, 1); -- cgit From 0e7508ba4366a6f4e6b2f0ac5cc520a2f37fb7d6 Mon Sep 17 00:00:00 2001 From: Benjamin Franzke Date: Fri, 9 Nov 2012 09:50:50 +0100 Subject: pd_server: Check rounds parameter to be a number --- pd_server.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/pd_server.c b/pd_server.c index 829a6f7..7c5a01f 100644 --- a/pd_server.c +++ b/pd_server.c @@ -35,14 +35,14 @@ main(int argc, char *argv[]) int port = 8068; int a_payment = 0, b_payment = 0, rounds, i; int optval = 1; + char *endptr; - if (argc < 2) { + errno = 0; + if (argc < 2 || (rounds = strtol(argv[1], NULL, 10)) <= 0 || errno!=0) { fprintf(stderr, "usage: %s number-of-rounds\n", argv[0]); exit(EXIT_FAILURE); } - rounds = atoi(argv[1]); - sock = socket(AF_INET, SOCK_STREAM, 0); if (sock < 0) return EXIT_FAILURE; -- cgit From f231e6db05ac4b09fa6aad0d17216f6ac21380d4 Mon Sep 17 00:00:00 2001 From: Benjamin Franzke Date: Fri, 9 Nov 2012 09:56:49 +0100 Subject: pd_server: Start a new round after one has finished So that 2 new clients can connect. --- pd_server.c | 49 ++++++++++++++++++++++++++----------------------- 1 file changed, 26 insertions(+), 23 deletions(-) diff --git a/pd_server.c b/pd_server.c index 7c5a01f..b8ff6f0 100644 --- a/pd_server.c +++ b/pd_server.c @@ -59,33 +59,36 @@ main(int argc, char *argv[]) } listen(sock, 2); - for (i = 0; i < 2; ++i) { - player[i] = accept(sock, NULL, NULL); - } - for (i = 0; i < rounds; ++i) { - char a, b; - read(player[0], &a, 1); - read(player[1], &b, 1); - - if (a != 'c' && a != 'd') { - printf("a sent incorrect char: %c\n", a); - continue; - } - if (b != 'c' && b != 'd') { - printf("b sent incorrect char: %c\n", b); - continue; + for (;;) { + for (i = 0; i < 2; ++i) { + player[i] = accept(sock, NULL, NULL); } + for (i = 0; i < rounds; ++i) { + char a, b; + read(player[0], &a, 1); + read(player[1], &b, 1); - a_payment += pay(a, b); - b_payment += pay(b, a); + if (a != 'c' && a != 'd') { + printf("a sent incorrect char: %c\n", a); + continue; + } + if (b != 'c' && b != 'd') { + printf("b sent incorrect char: %c\n", b); + continue; + } - printf("A [%c]: %d Euro\tB [%c]: %d Euro\t\t\tA: %d\tB: %d\n", - a, pay(a, b), b, pay(b, a), a_payment, b_payment); + a_payment += pay(a, b); + b_payment += pay(b, a); - write(player[0], &b, 1); - write(player[1], &a, 1); + printf("A [%c]: %d Euro\tB [%c]: %d Euro\t\t\t" + "A: %d\tB: %d\n", + a, pay(a,b), b, pay(b,a), a_payment, b_payment); + + write(player[0], &b, 1); + write(player[1], &a, 1); + } + close(player[0]); + close(player[1]); } - close(player[0]); - close(player[1]); close(sock); } -- cgit From 320f3b89263b9c775fd9bfa649b1cb8ad3c0f8b2 Mon Sep 17 00:00:00 2001 From: Benjamin Franzke Date: Fri, 9 Nov 2012 10:14:05 +0100 Subject: pd_client: Add some informative output --- pd_client.pl | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/pd_client.pl b/pd_client.pl index 7b32e3a..a484370 100644 --- a/pd_client.pl +++ b/pd_client.pl @@ -17,11 +17,12 @@ do(_, _, _, _, [e|_]) :- !. do(StreamIn, StreamOut, Module, ModuleState, Hist) :- call(Module, Hist, ModuleDecision, ModuleState, NewModuleState), write(StreamOut, ModuleDecision), flush_output(StreamOut), + format('Own choose:\t~w\n', [ModuleDecision]), loop(StreamIn, StreamOut, Module, NewModuleState, Hist). loop(StreamIn, StreamOut, Module, ModuleState, Hist) :- get_code(StreamIn, ChoiceCode), byte_to_atom(ChoiceCode, Choice), - write(Choice), + format('Opponent chose:\t~w\n', [Choice]), do(StreamIn, StreamOut, Module, ModuleState, [Choice|Hist]). % -1 = EOF -- cgit From c866029449a815764e3c8525bf17dac72280a72a Mon Sep 17 00:00:00 2001 From: Benjamin Franzke Date: Fri, 9 Nov 2012 10:57:39 +0100 Subject: pd_client: Use atom_codes/2 instead of name/2 As its recommended by the gprolog documentation --- pd_client.pl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pd_client.pl b/pd_client.pl index a484370..5e6c5b0 100644 --- a/pd_client.pl +++ b/pd_client.pl @@ -27,4 +27,4 @@ loop(StreamIn, StreamOut, Module, ModuleState, Hist) :- % -1 = EOF byte_to_atom(-1, e):-!. -byte_to_atom(Byte, Atom):-name(Atom, [Byte]). +byte_to_atom(Byte, Atom):-atom_codes(Atom, [Byte]). -- cgit