margo-example-server.c 2.2 KB
Newer Older
1 2 3 4 5 6 7 8 9
/*
 * (C) 2015 The University of Chicago
 * 
 * See COPYRIGHT in top-level directory.
 */

#include <stdio.h>
#include <assert.h>
#include <unistd.h>
10
#include <mercury.h>
11 12 13 14 15 16 17 18 19 20 21
#include <abt.h>
#include <margo.h>

#include "my-rpc.h"

/* example server program.  Starts HG engine, registers the example RPC type,
 * and then executes indefinitely.
 */

int main(int argc, char **argv) 
{
22
    hg_return_t hret;
23 24 25 26 27 28 29 30
    margo_instance_id mid;
    hg_addr_t addr_self;
    char addr_self_string[128];
    hg_size_t addr_self_string_sz = 128;

    if(argc != 2)
    {
        fprintf(stderr, "Usage: ./server <listen_addr>\n");
31
        fprintf(stderr, "Example: ./server na+sm://\n");
32 33 34
        return(-1);
    }

35 36 37
    /* actually start margo -- this step encapsulates the Mercury and
     * Argobots initialization and must precede their use */
    /* Use the calling xstream to drive progress and execute handlers. */
38
    /***************************************/
39 40
    mid = margo_init(argv[1], MARGO_SERVER_MODE, 0, -1);
    if(mid == MARGO_INSTANCE_NULL)
41
    {
42
        fprintf(stderr, "Error: margo_init()\n");
43 44
        return(-1);
    }
45
    margo_diag_start(mid);
46 47
    
    /* figure out what address this server is listening on */
48 49
    hret = margo_addr_self(mid, &addr_self);
    if(hret != HG_SUCCESS)
50
    {
51 52
        fprintf(stderr, "Error: margo_addr_self()\n");
        margo_finalize(mid);
53 54
        return(-1);
    }
55 56
    hret = margo_addr_to_string(mid, addr_self_string, &addr_self_string_sz, addr_self);
    if(hret != HG_SUCCESS)
57
    {
58
        fprintf(stderr, "Error: margo_addr_to_string()\n");
59 60
        margo_addr_free(mid, addr_self);
        margo_finalize(mid);
61 62
        return(-1);
    }
63
    margo_addr_free(mid, addr_self);
64

65
    printf("# accepting RPCs on address \"%s\"\n", addr_self_string);
66 67

    /* register RPC */
68 69
    MARGO_REGISTER(mid, "my_rpc", my_rpc_in_t, my_rpc_out_t, my_rpc_ult);
	MARGO_REGISTER(mid, "my_shutdown_rpc", void, void, my_rpc_shutdown_ult);
70 71 72 73 74 75 76 77 78 79

    /* NOTE: there isn't anything else for the server to do at this point
     * except wait for itself to be shut down.  The
     * margo_wait_for_finalize() call here yields to let Margo drive
     * progress until that happens.
     */
    margo_wait_for_finalize(mid);

    return(0);
}