diff --git a/examples/2_dynamic_pdl/amy.c b/examples/2_dynamic_pdl/amy.c index f965c107d42368117f2b5f4ca7d39533142ed640..489a6b4870a7c880f3ca3f970fd2773216eac768 100644 --- a/examples/2_dynamic_pdl/amy.c +++ b/examples/2_dynamic_pdl/amy.c @@ -11,7 +11,7 @@ int main() { // create a connector with one outgoing network channel. Connector* c = connector_new(); printf("configuring...\n"); - check("config ", connector_configure(c, pdl, "forward")); + check("config ", connector_configure(c, pdl, "dynamic")); check("bind 0 ", connector_bind_native(c, 0)); check("bind 1 ", connector_bind_passive(c, 1, "127.0.0.1:7000")); printf("connecting...\n"); diff --git a/examples/3_atomicity/amy.c b/examples/3_atomicity/amy.c new file mode 100644 index 0000000000000000000000000000000000000000..7b5d12685dc22c4815fd826300c18f84d869a413 --- /dev/null +++ b/examples/3_atomicity/amy.c @@ -0,0 +1,34 @@ +#include +#include +#include +#include "../../reowolf.h" +#include "../utility.c" + +int main() { // AMY + char * pdl = buffer_pdl("eg_protocols.pdl"); + + Connector* c = connector_new(); + printf("configuring...\n"); + + check("config ", connector_configure(c, pdl, "sync_two")); + check("bind 0 ", connector_bind_native(c, 0)); + check("bind 1 ", connector_bind_native(c, 1)); + check("bind 2 ", connector_bind_passive(c, 2, "127.0.0.1:7000")); + check("bind 3 ", connector_bind_passive(c, 3, "127.0.0.1:7001")); + printf("connecting...\n"); + check("connect", connector_connect(c, 5000)); + + int round, code; + for(round = 0; true; round++) { + printf("\nround %d\n", round); + + check("put ", connector_put(c, 0, "one", 3)); + check("put ", connector_put(c, 1, "two", 3)); + check("sync ", connector_sync(c, 10000)); + } + + printf("cleaning up...\n"); + connector_destroy(c); + free(pdl); + return 0; +} \ No newline at end of file diff --git a/examples/4_atomicity/bob.c b/examples/3_atomicity/bob.c similarity index 96% rename from examples/4_atomicity/bob.c rename to examples/3_atomicity/bob.c index 12dbd9cbbf8d96ed3afd749f462fe90dbb6e47b7..60f4a020c4213a7aea0ef06ee8b6e789d2071ba2 100644 --- a/examples/4_atomicity/bob.c +++ b/examples/3_atomicity/bob.c @@ -34,7 +34,7 @@ int main() { printf("Receiving messages [%c, %c]\n", yn[0], yn[1]); if (yn[0] == 'y') check("get first ", connector_get(c, 0)); if (yn[1] == 'y') check("get second ", connector_get(c, 1)); - check("sync ", connector_sync(c, 3000)); + check("sync ", connector_sync(c, 1000)); for (i = 0; i < 2; i++) { if (yn[i] == 'y') { check("read ", connector_gotten(c, i, &msg, &msg_len)); diff --git a/examples/3_nondeterminism/make.sh b/examples/3_atomicity/make.sh similarity index 100% rename from examples/3_nondeterminism/make.sh rename to examples/3_atomicity/make.sh diff --git a/examples/3_nondeterminism/amy.c b/examples/3_nondeterminism/amy.c deleted file mode 100644 index a1db378c504569e946011b32ac1ba0a77f7f315d..0000000000000000000000000000000000000000 --- a/examples/3_nondeterminism/amy.c +++ /dev/null @@ -1,58 +0,0 @@ -#include -#include -#include -#include "../../reowolf.h" -#include "../utility.c" - -// amy indefinitely offers 0/1 messages with contents: the number of total sent messages -int main() { - char * pdl = buffer_pdl("eg_protocols.pdl"); - - Connector* c = connector_new(); - printf("configuring...\n"); - check("config ", connector_configure(c, pdl, "sync")); - check("bind 0 ", connector_bind_native(c, 0)); - check("bind 1 ", connector_bind_passive(c, 1, "127.0.0.1:7000")); - printf("connecting...\n"); - check("connect", connector_connect(c, 5000)); - - // amy offers a message to her peer. - // the message is the number of messages the peer previously received. - - int send_next = 0; - char msg_buf[32]; - int code, i; - for (i = 0; 1; i++) { - itoa(send_next, msg_buf, 10); - printf("\nround %d. Will send msg `%s` next\n", i, msg_buf); - - // option (a): no messages sent - check("next_batch ", connector_next_batch(c)); - - // option (b): one message sent - check("put ", connector_put(c, 0, msg_buf, strlen(msg_buf) + 1)); - - code = connector_sync(c, 3000); - check("sync ", code); - - // reflect on the outcome of the exchange - if (code == 0) { - printf("Sent no message!\n"); - } else if (code == 1) { - printf("Sent message `%s`!\n", msg_buf); - send_next++; - } else { - printf( - "Connector error! %d (%s)\nBreaking loop!\n", - code, connector_error_peek() - ); - break; - } - } - - printf("destroying...\n"); - connector_destroy(c); - printf("exiting...\n"); - free(pdl); - return 0; -} \ No newline at end of file diff --git a/examples/3_nondeterminism/bob.c b/examples/3_nondeterminism/bob.c deleted file mode 100644 index 5ece1f5b47085cbb8ab3d97ac1a107387f629398..0000000000000000000000000000000000000000 --- a/examples/3_nondeterminism/bob.c +++ /dev/null @@ -1,46 +0,0 @@ -#include -#include "../../reowolf.h" -#include "../utility.c" - -// bob indefinitely chooses between receiving or not receiving a message (user inputs y/n) -int main() { - - Connector* c = connector_new(); - printf("configuring...\n"); - char * pdl = buffer_pdl("eg_protocols.pdl"); - check("config ", connector_configure(c, pdl, "bob3")); - check("bind 0 ", connector_bind_active(c, 0, "127.0.0.1:7000")); - check("bind 1 ", connector_bind_native(c, 1)); - - printf("connecting...\n"); - check("connect", connector_connect(c, 5000)); - - const unsigned char * msg; - int i, code, msg_len; - char yn; - - for (i = 0; true; i++) { - printf("\nround %d...\n", i); - printf("Should I receive a message? (y/n): "); - scanf(" %c", &yn); - if (yn == 'y') { - printf("OK! Let's receive a message!\n"); - connector_get(c, 0); - } else if (yn == 'n') { - printf("OK! Let's NOT receive a message!\n"); - } else { - printf("Expected (y/n) input!"); - continue; - } - check("sync ", connector_sync(c, 1000)); - if (yn == 'y') { - check("read ", connector_gotten(c, 0, &msg, &msg_len)); - printf("Got message: `%.*s`\n", msg_len, msg); - } - } - - printf("cleaning up\n"); - connector_destroy(c); - free(pdl); - return 0; -} \ No newline at end of file diff --git a/examples/4_atomicity/make.sh b/examples/4_atomicity/make.sh deleted file mode 100644 index 29265a080aec2c404da4a511f2c78a513596c536..0000000000000000000000000000000000000000 --- a/examples/4_atomicity/make.sh +++ /dev/null @@ -1,5 +0,0 @@ -#!/bin/sh - -LIB_PATH="../" -gcc -L $LIB_PATH -lreowolf_rs -Wl,-R$LIB_PATH amy.c -o amy -gcc -L $LIB_PATH -lreowolf_rs -Wl,-R$LIB_PATH bob.c -o bob diff --git a/examples/4_nondeterminism/amy.c b/examples/4_nondeterminism/amy.c new file mode 100644 index 0000000000000000000000000000000000000000..543ee8d69868c715837efa83db0725ffc6bbc59b --- /dev/null +++ b/examples/4_nondeterminism/amy.c @@ -0,0 +1,38 @@ +#include +#include +#include +#include "../../reowolf.h" +#include "../utility.c" + +int main() { // AMY + char * pdl = buffer_pdl("eg_protocols.pdl"); + + Connector* c = connector_new(); + printf("configuring...\n"); + + check("config ", connector_configure(c, pdl, "sync_two")); + check("bind 0 ", connector_bind_native(c, 0)); + check("bind 1 ", connector_bind_native(c, 1)); + check("bind 2 ", connector_bind_passive(c, 2, "127.0.0.1:7000")); + check("bind 3 ", connector_bind_passive(c, 3, "127.0.0.1:7001")); + printf("connecting...\n"); + check("connect", connector_connect(c, 5000)); + + int round, code; + for(round = 0; true; round++) { + printf("\nround %d\n", round); + + check("next ", connector_next_batch(c)); + check("put ", connector_put(c, 0, "one", 3)); + check("put ", connector_put(c, 1, "two", 3)); + code = connector_sync(c, 3000); + check("sync ", code); + if(code == 0) printf("sent neither message!"); + else printf("sent both messages!"); + } + + printf("cleaning up...\n"); + connector_destroy(c); + free(pdl); + return 0; +} \ No newline at end of file diff --git a/examples/4_nondeterminism/make.sh b/examples/4_nondeterminism/make.sh new file mode 100644 index 0000000000000000000000000000000000000000..47ee3dafa30728d73105d4d36dbd58ca09827fbb --- /dev/null +++ b/examples/4_nondeterminism/make.sh @@ -0,0 +1,4 @@ +#!/bin/sh + +LIB_PATH="../" +gcc -L $LIB_PATH -lreowolf_rs -Wl,-R$LIB_PATH amy.c -o amy \ No newline at end of file diff --git a/examples/4_atomicity/amy.c b/examples/5_recovery/amy.c similarity index 58% rename from examples/4_atomicity/amy.c rename to examples/5_recovery/amy.c index e7406dc01e23ff64daf9ce0e142414acdd0aefcf..717fba1351219e8ad69e991ca4cdc634b59ce804 100644 --- a/examples/4_atomicity/amy.c +++ b/examples/5_recovery/amy.c @@ -18,26 +18,20 @@ int main() { // AMY printf("connecting...\n"); check("connect", connector_connect(c, 5000)); - int i, code; - while (1) { - printf("\nround %d\n", i); - - // batch 0: no messages sent - check("next_batch ", connector_next_batch(c)); - - // batch 1: put 0 and put 1 - check("put ", connector_put(c, 0, "one", 3)); - check("put ", connector_put(c, 1, "two", 3)); + int round, code; + for(round = 0; true; round++) { + printf("\nround %d\n", round); + + check("next ", connector_next_batch(c)); + check("put ", connector_put(c, 0, "one", 3)); + check("put ", connector_put(c, 1, "two", 3)); code = connector_sync(c, 3000); - - if (code == 0) printf("Sent neither message!"); - else if (code == 1) printf("Sent both messages!"); - else if (code == -1) printf("Sync failed!\n"); + if(code == 0) printf("sent neither message!"); + else if(code == 1 ) printf("sent both messages!"); + else if(code == -1) printf("No interaction! Recovered state.\n"); else { - printf( - "Connector error! %d (%s)\nBreaking loop!\n", - code, connector_error_peek() - ); + printf("Unrecoverable error!\n"); + connector_dump_log(c); break; } } diff --git a/examples/5_recovery/bob.c b/examples/5_recovery/bob.c index 0de10465504bc3fc136cb1ee3411d508a2b4df8d..7a74479040060ade4aa184cd7ae04436f646ae45 100644 --- a/examples/5_recovery/bob.c +++ b/examples/5_recovery/bob.c @@ -34,22 +34,20 @@ int main() { printf("Receiving messages [%c, %c]\n", yn[0], yn[1]); if (yn[0] == 'y') check("get first ", connector_get(c, 0)); if (yn[1] == 'y') check("get second ", connector_get(c, 1)); - code = connector_sync(c, 1000); - if (code == 0) printf("sync succeeded!\n"); - else if (code == -1) { - printf("sync failed; state recovered!\n"); - continue; - } else { - printf("Unrecoverable error! code %d\n", code); - connector_dump_log(c); - } - - for (i = 0; i < 2; i++) { - if (yn[i] == 'y') { + if(code >= 0) { + for (i = 0; i < 2; i++) { + if (yn[i] == 'y') { check("read ", connector_gotten(c, i, &msg, &msg_len)); - printf("Got %s msg `%.*s`\n", nth[i], msg_len, msg); + printf("Got %s msg `%.*s`\n", nth[i], msg_len, msg); + } } + } else if(code == -1) { + printf("No interaction! Recovered state.\n"); + } else { + printf("Unrecoverable error!\n"); + connector_dump_log(c); + break; } } diff --git a/examples/5_recovery/make.sh b/examples/5_recovery/make.sh index 76e7564aa5e4e27d9f1de8f7f7adc865f73ac9fe..29265a080aec2c404da4a511f2c78a513596c536 100644 --- a/examples/5_recovery/make.sh +++ b/examples/5_recovery/make.sh @@ -1,4 +1,5 @@ #!/bin/sh LIB_PATH="../" +gcc -L $LIB_PATH -lreowolf_rs -Wl,-R$LIB_PATH amy.c -o amy gcc -L $LIB_PATH -lreowolf_rs -Wl,-R$LIB_PATH bob.c -o bob diff --git a/examples/eg_protocols.pdl b/examples/eg_protocols.pdl index 27237f06cac58e23f76dcd03b8a42a327aecf811..20d134b9ef3ab7725484093bb04a050455a75b0e 100644 --- a/examples/eg_protocols.pdl +++ b/examples/eg_protocols.pdl @@ -1,8 +1,12 @@ + primitive forward(in i, out o) { while(true) synchronous { put(o, get(i)); } } +composite dynamic(in i, out o) { + new forward(i, o); +} primitive sync(in i, out o) { while(true) synchronous { if(fires(i)) { @@ -20,13 +24,6 @@ primitive sync_two(in ia, in ib, out oa, out ob) { } } -composite bob3(in i, out o) { - new sync(i, o); -} -composite bob5(in i, out o) { - new sync(i, o); -} - primitive xor_three(in ai, out ao, in bi, out bo, in ci, out co) { synchronous { if (fires(ai)) put(ao, get(ai));