margo-example-server.c 2.86 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
    margo_instance_id mid;
    hg_addr_t addr_self;
    char addr_self_string[128];
    hg_size_t addr_self_string_sz = 128;
Philip Carns's avatar
Philip Carns committed
27 28 29
    int ret;
    int num_rpc_threads;
    int dedicated_progress;
30

Philip Carns's avatar
Philip Carns committed
31
    if(argc != 4)
32
    {
Philip Carns's avatar
Philip Carns committed
33 34
        fprintf(stderr, "Usage: ./server <listen_addr> <dedicated_progress?> <num_rpc_threads>\n");
        fprintf(stderr, "Example: ./server na+sm:// 0 -1\n");
35 36 37
        return(-1);
    }

Philip Carns's avatar
Philip Carns committed
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
    ret = sscanf(argv[2], "%d", &dedicated_progress);
    if(ret != 1)
    {
        fprintf(stderr, "Usage: ./server <listen_addr> <dedicated_progress?> <num_rpc_threads>\n");
        fprintf(stderr, "Example: ./server na+sm:// 0 0 -1\n");
        return(-1);
    }

    ret = sscanf(argv[3], "%d", &num_rpc_threads);
    if(ret != 1)
    {
        fprintf(stderr, "Usage: ./server <listen_addr> <dedicated_progress?> <num_rpc_threads>\n");
        fprintf(stderr, "Example: ./server na+sm:// 0 0 -1\n");
        return(-1);
    }
    
54 55
    /* actually start margo -- this step encapsulates the Mercury and
     * Argobots initialization and must precede their use */
Philip Carns's avatar
Philip Carns committed
56 57 58
    /* NOTE: for debugging #40, use dedicate progress thread and N rpc
     * threads
     */
59
    /***************************************/
Philip Carns's avatar
Philip Carns committed
60
    mid = margo_init(argv[1], MARGO_SERVER_MODE, dedicated_progress, num_rpc_threads);
61
    if(mid == MARGO_INSTANCE_NULL)
62
    {
63
        fprintf(stderr, "Error: margo_init()\n");
64 65 66 67
        return(-1);
    }
    
    /* figure out what address this server is listening on */
68 69
    hret = margo_addr_self(mid, &addr_self);
    if(hret != HG_SUCCESS)
70
    {
71 72
        fprintf(stderr, "Error: margo_addr_self()\n");
        margo_finalize(mid);
73 74
        return(-1);
    }
75 76
    hret = margo_addr_to_string(mid, addr_self_string, &addr_self_string_sz, addr_self);
    if(hret != HG_SUCCESS)
77
    {
78
        fprintf(stderr, "Error: margo_addr_to_string()\n");
79 80
        margo_addr_free(mid, addr_self);
        margo_finalize(mid);
81 82
        return(-1);
    }
83
    margo_addr_free(mid, addr_self);
84

85
    printf("# accepting RPCs on address \"%s\"\n", addr_self_string);
86 87

    /* register RPC */
88 89
    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);
90 91 92 93 94 95 96 97 98 99

    /* 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);
}