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

#include <stdio.h>
#include <assert.h>
#include <unistd.h>
#include <abt.h>
Philip Carns's avatar
Philip Carns committed
11 12
#include <abt-snoozer.h>
#include <margo.h>
13 14 15 16 17 18 19 20 21 22

#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) 
{
    int ret;
23
    margo_instance_id mid;
24 25
    ABT_xstream progress_xstream;
    ABT_pool progress_pool;
26 27
    hg_context_t *hg_context;
    hg_class_t *hg_class;
28
    
29
    if(argc != 2)
30
    {
31 32
        fprintf(stderr, "Usage: ./server <listen_addr>\n");
        fprintf(stderr, "Example: ./server tcp://3344\n");
33 34 35
        return(-1);
    }

36
    /* boilerplate HG initialization steps */
37
    /***************************************/
38
    hg_class = HG_Init(argv[1], HG_TRUE);
39 40 41 42 43 44 45 46 47 48 49 50 51 52
    if(!hg_class)
    {
        fprintf(stderr, "Error: HG_Init()\n");
        return(-1);
    }
    hg_context = HG_Context_create(hg_class);
    if(!hg_context)
    {
        fprintf(stderr, "Error: HG_Context_create()\n");
        HG_Finalize(hg_class);
        return(-1);
    }

    /* set up argobots */
53
    /***************************************/
54 55 56 57 58 59 60
    ret = ABT_init(argc, argv);
    if(ret != 0)
    {
        fprintf(stderr, "Error: ABT_init()\n");
        return(-1);
    }

Philip Carns's avatar
Philip Carns committed
61 62
    /* set primary ES to idle without polling */
    ret = ABT_snoozer_xstream_self_set();
63 64
    if(ret != 0)
    {
Philip Carns's avatar
Philip Carns committed
65
        fprintf(stderr, "Error: ABT_snoozer_xstream_self_set()\n");
66 67 68
        return(-1);
    }

69
    /* actually start margo */
70
    mid = margo_init(0, 0, hg_context);
71
    assert(mid);
72 73

    /* register RPC */
74 75
    MERCURY_REGISTER(hg_class, "my_rpc", my_rpc_in_t, my_rpc_out_t, 
        my_rpc_ult_handler);
76 77
    MERCURY_REGISTER(hg_class, "my_shutdown_rpc", void, void, 
        my_rpc_shutdown_ult_handler);
78

79 80 81 82
    /* 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.
83 84
     */
    margo_wait_for_finalize(mid);
85 86 87

    ABT_finalize();

88 89 90
    HG_Context_destroy(hg_context);
    HG_Finalize(hg_class);

91 92 93
    return(0);
}