Commit b9ad3cc1 authored by lahiker42's avatar lahiker42

Add --autoreconnect option.


git-svn-id: https://protobuf-c.googlecode.com/svn/trunk@283 00440858-1255-0410-a3e6-75ea37f81c3a
parent 17bdf7ca
......@@ -29,6 +29,7 @@ usage (void)
"Options:\n"
" --tcp=HOST:PORT Port to listen on for RPC clients.\n"
" --unix=PATH Unix-domain socket to listen on.\n"
" --autoreconnect=MILLIS Try to reconnect to the client if it fails.\n"
);
}
......@@ -98,6 +99,20 @@ handle_query_response (const Foo__LookupResult *result,
* (protobuf_c_boolean *) closure_data = 1;
}
/* Run the main-loop without blocking. It would be nice
if there was a simple API for this (protobuf_c_dispatch_run_with_timeout?),
but there isn't for now. */
static void
do_nothing (ProtobufCDispatch *dispatch, void *unused)
{
}
static void
run_main_loop_without_blocking (ProtobufCDispatch *dispatch)
{
protobuf_c_dispatch_add_idle (dispatch, do_nothing, NULL);
protobuf_c_dispatch_run (dispatch);
}
int main(int argc, char**argv)
{
ProtobufCService *service;
......@@ -105,6 +120,7 @@ int main(int argc, char**argv)
ProtobufC_RPC_AddressType address_type=0;
const char *name = NULL;
unsigned i;
int autoreconnect_millis = -1;
for (i = 1; i < (unsigned) argc; i++)
{
......@@ -118,6 +134,10 @@ int main(int argc, char**argv)
address_type = PROTOBUF_C_RPC_ADDRESS_LOCAL;
name = strchr (argv[i], '=') + 1;
}
else if (starts_with (argv[i], "--autoreconnect="))
{
autoreconnect_millis = atoi (strchr (argv[i], '=') + 1);
}
else
usage ();
}
......@@ -130,6 +150,9 @@ int main(int argc, char**argv)
die ("error creating client");
client = (ProtobufC_RPC_Client *) service;
if (autoreconnect_millis >= 0)
protobuf_c_rpc_client_set_autoreconnect_period (client, autoreconnect_millis);
fprintf (stderr, "Connecting... ");
while (!protobuf_c_rpc_client_is_connected (client))
protobuf_c_dispatch_run (protobuf_c_dispatch_default ());
......@@ -143,6 +166,12 @@ int main(int argc, char**argv)
fprintf (stderr, ">> ");
if (fgets (buf, sizeof (buf), stdin) == NULL)
break;
/* In order to prevent having the client get unduly stuck
in an error state, exercise the main-loop, which will
give the connection process time to run. */
run_main_loop_without_blocking (protobuf_c_dispatch_default ());
if (is_whitespace (buf))
continue;
chomp_trailing_whitespace (buf);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment