/* Creates and returns a jsonrpc_session to 'name', which should be a string * acceptable to stream_open() or pstream_open(). * * If 'name' is an active connection method, e.g. "tcp:127.1.2.3", the new * jsonrpc_session connects to 'name'. If 'retry' is true, then the new * session connects and reconnects to 'name', with backoff. If 'retry' is * false, the new session will only try to connect once and after a connection * failure or a disconnection jsonrpc_session_is_alive() will return false for * the new session. * * If 'name' is a passive connection method, e.g. "ptcp:", the new * jsonrpc_session listens for connections to 'name'. It maintains at most one * connection at any given time. Any new connection causes the previous one * (if any) to be dropped. */ struct jsonrpc_session * jsonrpc_session_open(const char *name, bool retry) { struct jsonrpc_session *s; s = xmalloc(sizeof *s); s->reconnect = reconnect_create(time_msec()); reconnect_set_name(s->reconnect, name); reconnect_enable(s->reconnect, time_msec()); s->rpc = NULL; s->stream = NULL; s->pstream = NULL; s->seqno = 0; s->dscp = 0; s->last_error = 0; if (!pstream_verify_name(name)) { reconnect_set_passive(s->reconnect, true, time_msec()); } else if (!retry) { reconnect_set_max_tries(s->reconnect, 1); reconnect_set_backoff(s->reconnect, INT_MAX, INT_MAX); } if (!stream_or_pstream_needs_probes(name)) { reconnect_set_probe_interval(s->reconnect, 0); } return s; }
/* Creates and returns a jsonrpc_session to 'name', which should be a string * acceptable to stream_open() or pstream_open(). * * If 'name' is an active connection method, e.g. "tcp:127.1.2.3", the new * jsonrpc_session connects and reconnects, with back-off, to 'name'. * * If 'name' is a passive connection method, e.g. "ptcp:", the new * jsonrpc_session listens for connections to 'name'. It maintains at most one * connection at any given time. Any new connection causes the previous one * (if any) to be dropped. */ struct jsonrpc_session * jsonrpc_session_open(const char *name) { struct jsonrpc_session *s; s = xmalloc(sizeof *s); s->reconnect = reconnect_create(time_msec()); reconnect_set_name(s->reconnect, name); reconnect_enable(s->reconnect, time_msec()); s->rpc = NULL; s->stream = NULL; s->pstream = NULL; s->seqno = 0; s->dscp = 0; if (!pstream_verify_name(name)) { reconnect_set_passive(s->reconnect, true, time_msec()); } if (!stream_or_pstream_needs_probes(name)) { reconnect_set_probe_interval(s->reconnect, 0); } return s; }
int main(void) { extern struct vlog_module VLM_reconnect; struct reconnect_stats prev; unsigned int old_max_tries; int old_time; char line[128]; vlog_set_levels(&VLM_reconnect, VLF_ANY_FACILITY, VLL_OFF); now = 1000; reconnect = reconnect_create(now); reconnect_set_name(reconnect, "remote"); reconnect_get_stats(reconnect, now, &prev); printf("### t=%d ###\n", now); old_time = now; old_max_tries = reconnect_get_max_tries(reconnect); while (fgets(line, sizeof line, stdin)) { struct reconnect_stats cur; struct svec args; fputs(line, stdout); if (line[0] == '#') { continue; } svec_init(&args); svec_parse_words(&args, line); svec_terminate(&args); if (!svec_is_empty(&args)) { run_command(args.n, args.names, commands); } svec_destroy(&args); if (old_time != now) { printf("\n### t=%d ###\n", now); } reconnect_get_stats(reconnect, now, &cur); diff_stats(&prev, &cur, now - old_time); prev = cur; if (reconnect_get_max_tries(reconnect) != old_max_tries) { old_max_tries = reconnect_get_max_tries(reconnect); printf(" %u tries left\n", old_max_tries); } old_time = now; } return 0; }
/* Creates and returns a jsonrpc_session that is initially connected to * 'jsonrpc'. If the connection is dropped, it will not be reconnected. * * On the assumption that such connections are likely to be short-lived * (e.g. from ovs-vsctl), informational logging for them is suppressed. */ struct jsonrpc_session * jsonrpc_session_open_unreliably(struct jsonrpc *jsonrpc) { struct jsonrpc_session *s; s = xmalloc(sizeof *s); s->reconnect = reconnect_create(time_msec()); reconnect_set_quiet(s->reconnect, true); reconnect_set_name(s->reconnect, jsonrpc_get_name(jsonrpc)); reconnect_set_max_tries(s->reconnect, 0); reconnect_connected(s->reconnect, time_msec()); s->rpc = jsonrpc; s->stream = NULL; s->pstream = NULL; s->seqno = 0; return s; }