From da9b541a1406854240d0d343aef31f7d89f1c26c Mon Sep 17 00:00:00 2001 From: marktwtn Date: Thu, 29 Aug 2019 00:49:45 +0800 Subject: [PATCH] feat: Parse command line argument of remote worker Add command line argument parsing in remote worker for allowing user to assign the IP address of the RabbitMQ broker. Close #185. --- src/remote_worker.c | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/src/remote_worker.c b/src/remote_worker.c index 848fbc4..d397dfa 100644 --- a/src/remote_worker.c +++ b/src/remote_worker.c @@ -6,23 +6,45 @@ * "LICENSE" at the root of this distribution. */ +#include #include "common.h" #include "constants.h" #include "dcurl.h" #include "remote_common.h" -int main(int argc, char const *const *argv) +int main(int argc, char *const *argv) { char trytes[TRANSACTION_TRYTES_LENGTH]; char buf[4]; int mwm; + int cmdOpt; + int optIdx; + const struct option longOpt[] = {{"broker", required_argument, NULL, 'b'}, + {NULL, 0, NULL, 0}}; amqp_connection_state_t conn; amqp_envelope_t envelope; + char *hostIP = NULL; + + /* Parse the command line options */ + /* TODO: Support macOS since getopt_long() is GNU extension */ + while (1) { + cmdOpt = getopt_long(argc, argv, "b:", longOpt, &optIdx); + if (cmdOpt == -1) + break; + + /* Invalid option */ + if (cmdOpt == '?') + break; + + if (cmdOpt == 'b') { + hostIP = optarg; + } + } dcurl_init(); - if (!connect_broker(&conn, NULL)) + if (!connect_broker(&conn, hostIP)) goto fail; if (!set_consuming_queue(&conn, 1, "incoming_queue"))