Example #1
0
int
main(int argc, char *argv[])
{
    DB_ENV *dbenv;
    extern char *optarg;
    const char *home;
    char ch, *host, *portstr;
    int local_is_set, ret, totalsites;
    u_int16_t port;
    /* Used to track whether this is a replica or a master. */
    APP_DATA my_app_data;

    dbenv = NULL;
    ret = local_is_set = totalsites = 0;
    home = NULL;

    my_app_data.is_master = 0;  /* Assume that we start as a replica */

    if ((ret = create_env(progname, &dbenv)) != 0)
	goto err;

    /* Make APP_DATA available through the environment handle. */
    dbenv->app_private = &my_app_data;

    /* Default priority is 100. */
    dbenv->rep_set_priority(dbenv, 100);
    /* Permanent messages require at least one ack. */
    dbenv->repmgr_set_ack_policy(dbenv, DB_REPMGR_ACKS_ONE);
    /* Give 500 microseconds to receive the ack. */
    dbenv->rep_set_timeout(dbenv, DB_REP_ACK_TIMEOUT, 500);

    /* Collect the command line options. */
    while ((ch = getopt(argc, argv, "h:l:n:p:r:")) != EOF)
	switch (ch) {
	case 'h':
	    home = optarg;
	    break;
	/* Set the host and port used by this environment. */
	case 'l':
	    host = strtok(optarg, ":");
	    if ((portstr = strtok(NULL, ":")) == NULL) {
		fprintf(stderr, "Bad host specification.\n");
		goto err;
	    }
	    port = (unsigned short)atoi(portstr);
	    if (dbenv->repmgr_set_local_site(dbenv, host, port, 0) != 0) {
		fprintf(stderr,
		    "Could not set local address %s.\n", host);
		goto err;
	    }
	    local_is_set = 1;
	    break;
	/* Set the number of sites in this replication group. */
	case 'n':
	    totalsites = atoi(optarg);
	    if ((ret = dbenv->rep_set_nsites(dbenv, totalsites)) != 0)
		dbenv->err(dbenv, ret, "set_nsites");
	    break;
	/* Set this replica's election priority. */
	case 'p':
	    dbenv->rep_set_priority(dbenv, atoi(optarg));
	    break;
	/* Identify another site in the replication group. */
	case 'r':
	    host = strtok(optarg, ":");
	    if ((portstr = strtok(NULL, ":")) == NULL) {
		fprintf(stderr, "Bad host specification.\n");
		goto err;
	    }
	    port = (unsigned short)atoi(portstr);
	    if (dbenv->repmgr_add_remote_site(dbenv, host, port, 0, 0) != 0) {
		fprintf(stderr,
		    "Could not add site %s.\n", host);
		goto err;
	    }
	    break;
	case '?':
	default:
	    usage();
	}

    /* Error check command line. */
    if (home == NULL || !local_is_set || !totalsites)
	usage();

    if ((ret = env_init(dbenv, home)) != 0)
	goto err;

    if ((ret = dbenv->repmgr_start(dbenv, 3, DB_REP_ELECTION)) != 0)
	goto err;

    if ((ret = doloop(dbenv)) != 0) {
	dbenv->err(dbenv, ret, "Application failed");
	goto err;
    }

err: if (dbenv != NULL)
	(void)dbenv->close(dbenv, 0);

    return (ret);

}