bake-server-daemon.c 3.28 KB
Newer Older
1 2 3 4 5 6
/*
 * (C) 2015 The University of Chicago
 * 
 * See COPYRIGHT in top-level directory.
 */

7 8
#include "bake-config.h"

9 10 11 12 13
#include <stdio.h>
#include <assert.h>
#include <unistd.h>
#include <margo.h>
#include <libpmemobj.h>
14
#include <bake-server.h>
15

16 17
struct options
{
18
    char *listen_addr_str;
19
    char *bake_pool;
20 21 22 23 24
    char *host_file;
};

static void usage(int argc, char **argv)
{
25
    fprintf(stderr, "Usage: bake-server-daemon [OPTIONS] <listen_addr> <bake_pool>\n");
26
    fprintf(stderr, "       listen_addr is the Mercury address to listen on\n");
27
    fprintf(stderr, "       bake_pool is the path to the BAKE pool\n");
28
    fprintf(stderr, "       [-f filename] to write the server address to a file\n");
29
    fprintf(stderr, "Example: ./bake-server-daemon tcp://localhost:1234 /dev/shm/foo.dat\n");
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
    return;
}

static void parse_args(int argc, char **argv, struct options *opts)
{
    int opt;

    memset(opts, 0, sizeof(*opts));

    /* get options */
    while((opt = getopt(argc, argv, "f:")) != -1)
    {
        switch(opt)
        {
            case 'f':
                opts->host_file = optarg;
                break;
            default:
                usage(argc, argv);
                exit(EXIT_FAILURE);
        }
    }

    /* get required arguments after options */
    if((argc - optind) != 2)
    {
        usage(argc, argv);
        exit(EXIT_FAILURE);
    }
59
    opts->listen_addr_str = argv[optind++];
60
    opts->bake_pool = argv[optind++];
61 62 63 64

    return;
}

65 66
int main(int argc, char **argv) 
{
67 68
    struct options opts;
    margo_instance_id mid;
69
    int ret;
70

71 72 73 74
    parse_args(argc, argv, &opts);

    /* start margo */
    /* use the main xstream for driving progress and executing rpc handlers */
75
    mid = margo_init(opts.listen_addr_str, MARGO_SERVER_MODE, 0, -1);
76
    if(mid == MARGO_INSTANCE_NULL)
77
    {
78
        fprintf(stderr, "Error: margo_init()\n");
79 80 81
        return(-1);
    }

82 83 84 85 86 87 88 89
    if(opts.host_file)
    {
        /* write the server address to file if requested */
        FILE *fp;
        hg_addr_t self_addr;
        char self_addr_str[128];
        hg_size_t self_addr_str_sz = 128;
        hg_return_t hret;
90

91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
        /* figure out what address this server is listening on */
        hret = margo_addr_self(mid, &self_addr);
        if(hret != HG_SUCCESS)
        {
            fprintf(stderr, "Error: margo_addr_self()\n");
            margo_finalize(mid);
            return(-1);
        }
        hret = margo_addr_to_string(mid, self_addr_str, &self_addr_str_sz, self_addr);
        if(hret != HG_SUCCESS)
        {
            fprintf(stderr, "Error: margo_addr_to_string()\n");
            margo_addr_free(mid, self_addr);
            margo_finalize(mid);
            return(-1);
        }
        margo_addr_free(mid, self_addr);

        fp = fopen(opts.host_file, "w");
        if(!fp)
        {
            perror("fopen");
            margo_finalize(mid);
            return(-1);
        }

        fprintf(fp, "%s", self_addr_str);
        fclose(fp);
    }
120

121
    /* initialize the BAKE server */
122
    ret = bake_server_init(mid, opts.bake_pool);
123 124 125 126 127 128
    if(ret != 0)
    {
        fprintf(stderr, "Error: bake_server_init()\n");
        margo_finalize(mid);
        return(-1);
    }
129

130
    /* suspend until the BAKE server gets a shutdown signal from the client */
131
    margo_wait_for_finalize(mid);
132 133 134

    return(0);
}