diff --git a/examples/5_recovery/bob.c b/examples/5_recovery/bob.c index 91371cff82113e6c58d3dd225be6ae6960ca768c..0de10465504bc3fc136cb1ee3411d508a2b4df8d 100644 --- a/examples/5_recovery/bob.c +++ b/examples/5_recovery/bob.c @@ -2,64 +2,59 @@ #include "../../reowolf.h" #include "../utility.c" -int main() { // BOB! +int main() { - char * pdl = buffer_pdl("eg_protocols.pdl"); Connector* c = connector_new(); - printf("configuring...\n"); - check("config ", connector_configure(c, pdl, "recovery_bob")); + char * pdl = buffer_pdl("eg_protocols.pdl"); + check("config ", connector_configure(c, pdl, "sync_two")); check("bind 0 ", connector_bind_active(c, 0, "127.0.0.1:7000")); - check("bind 1 ", connector_bind_native(c, 1)); + check("bind 1 ", connector_bind_active(c, 1, "127.0.0.1:7001")); + check("bind 2 ", connector_bind_native(c, 2)); + check("bind 3 ", connector_bind_native(c, 3)); printf("connecting...\n"); check("connect", connector_connect(c, 5000)); - int msg_len; const unsigned char * msg; - - int i; - char msg_buf[1]; - int code; - char answer; + const char * nth[2] = {"first", "second"}; + int i, code, msg_len; + char yn[2]; - for (i = 0; true; i++) { + while(true) { printf("\nround %d...\n", i); - printf("Should I receive a message? (y/n): "); - scanf(" %c", &answer); - if (answer == 'y') { - printf("OK! Let's receive a message!\n"); - connector_get(c, 0); - } else if (answer == 'n') { - printf("OK! Let's NOT receive a message!\n"); - } else { - printf("Expected (y/n) input!"); + printf("Which of the two messages should we receive? (y/n)(y/n) (eg: yy): "); + scanf(" %c%c", &yn[0], &yn[1]); + for (i = 0; i < 2; i++) { + if (yn[i] != 'y' && yn[i] != 'n') { + printf("Expected (y/n) input!"); + continue; + } + } + 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); } - code = connector_sync(c, 1000); - - // lets see how it went - if (code == 0) { - printf("Success!\n"); - if (answer == 'y') { - check("read ", connector_gotten(c, 0, &msg, &msg_len)); - printf("Got message: `%.*s`\n", msg_len, msg); + 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); } - } else if (code == -1) { - printf("!!! UH OH! The round rolled back! Let's try again\n"); - } else { - printf( - "Something went wrong!\n", - code, connector_error_peek() - ); - break; } } - - printf("destroying...\n"); + + printf("cleaning up\n"); connector_destroy(c); - printf("exiting...\n"); free(pdl); return 0; } \ No newline at end of file