GitLab maintenance scheduled for Tomorrow, 2020-03-31, from 17:00 to 18:00 CT - Services will be unavailable during this time.

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

#include <stdio.h>
#include <assert.h>
#include <unistd.h>
#include <abt.h>
#include <abt-snoozer.h>
#include <margo.h>

Philip Carns's avatar
Philip Carns committed
14 15
#include "svc1-server.h"
#include "svc2-server.h"
16 17 18 19 20 21 22 23 24 25 26

/* example server program that starts a skeleton for sub-services within
 * this process to register with
 */

/* this is a "common" rpc that is handled by the core daemon
 */

static void my_rpc_shutdown_ult(hg_handle_t handle)
{
    hg_return_t hret;
Philip Carns's avatar
Philip Carns committed
27
    const struct hg_info *hgi;
28 29
    margo_instance_id mid;

30
    //printf("Got RPC request to shutdown\n");
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 59 60 61 62

    hgi = HG_Get_info(handle);
    assert(hgi);
    mid = margo_hg_class_to_instance(hgi->hg_class);

    hret = margo_respond(mid, handle, NULL);
    assert(hret == HG_SUCCESS);

    HG_Destroy(handle);

    /* NOTE: we assume that the server daemon is using
     * margo_wait_for_finalize() to suspend until this RPC executes, so there
     * is no need to send any extra signal to notify it.
     */
    margo_finalize(mid);

    return;
}
DEFINE_MARGO_RPC_HANDLER(my_rpc_shutdown_ult)


int main(int argc, char **argv) 
{
    int ret;
    margo_instance_id mid;
    hg_context_t *hg_context;
    hg_class_t *hg_class;
    char proto[12] = {0};
    int i;
    hg_addr_t addr_self;
    char addr_self_string[128];
    hg_size_t addr_self_string_sz = 128;
63 64
    ABT_xstream svc1_xstream2;
    ABT_pool svc1_pool2;
Philip Carns's avatar
Philip Carns committed
65
    ABT_pool *handler_pool;
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 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 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135

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

    /* boilerplate HG initialization steps */
    /***************************************/
    hg_class = HG_Init(argv[1], HG_TRUE);
    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);
    }

    /* figure out what address this server is listening on */
    ret = HG_Addr_self(hg_class, &addr_self);
    if(ret != HG_SUCCESS)
    {
        fprintf(stderr, "Error: HG_Addr_self()\n");
        HG_Context_destroy(hg_context);
        HG_Finalize(hg_class);
        return(-1);
    }
    ret = HG_Addr_to_string(hg_class, addr_self_string, &addr_self_string_sz, addr_self);
    if(ret != HG_SUCCESS)
    {
        fprintf(stderr, "Error: HG_Addr_self()\n");
        HG_Context_destroy(hg_context);
        HG_Finalize(hg_class);
        HG_Addr_free(hg_class, addr_self);
        return(-1);
    }
    HG_Addr_free(hg_class, addr_self);

    for(i=0; i<11 && argv[1][i] != '\0' && argv[1][i] != ':'; i++)
        proto[i] = argv[1][i];
    printf("# accepting RPCs on address \"%s://%s\"\n", proto, addr_self_string);

    /* set up argobots */
    /***************************************/
    ret = ABT_init(argc, argv);
    if(ret != 0)
    {
        fprintf(stderr, "Error: ABT_init()\n");
        return(-1);
    }

    /* set primary ES to idle without polling */
    ret = ABT_snoozer_xstream_self_set();
    if(ret != 0)
    {
        fprintf(stderr, "Error: ABT_snoozer_xstream_self_set()\n");
        return(-1);
    }

    /* actually start margo */
    /***************************************/
    mid = margo_init(0, 0, hg_context);
    assert(mid);

136 137 138
    /* register RPCs and services */
    /***************************************/

139 140 141 142 143 144
    /* register a shutdown RPC as just a generic handler; not part of a
     * multiplexed service
     */
    MERCURY_REGISTER(hg_class, "my_shutdown_rpc", void, void,
        my_rpc_shutdown_ult_handler);

145 146 147
    /* register svc1, with mplex_id 1, to execute on the default handler pool
     * used by Margo
     */
Philip Carns's avatar
Philip Carns committed
148 149
    handler_pool = margo_get_handler_pool(mid);
    ret = svc1_register(mid, *handler_pool, 1);
150 151
    assert(ret == 0);

152 153 154 155 156 157 158 159 160 161
    /* create a dedicated and pool for another instance of svc1 */
    ret = ABT_snoozer_xstream_create(1, &svc1_pool2, &svc1_xstream2);
    assert(ret == 0);
    /* register svc1, with mplex_id 2, to execute on a separate pool.  This
     * will result in svc1 being registered twice, with the client being able
     * to dictate which instance they want to target
     */
    ret = svc1_register(mid, svc1_pool2, 2);
    assert(ret == 0);

162 163 164
    /* register svc2, with mplex_id 3, to execute on the default handler pool
     * used by Margo
     */
Philip Carns's avatar
Philip Carns committed
165 166
    handler_pool = margo_get_handler_pool(mid);
    ret = svc2_register(mid, *handler_pool, 3);
167 168 169 170 171
    assert(ret == 0);


    /* shut things down */
    /****************************************/
172

173 174 175 176 177 178 179
    /* 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);

Philip Carns's avatar
Philip Carns committed
180 181
    /*  TODO: rethink this; can't touch mid after wait for finalize */
#if 0
Philip Carns's avatar
Philip Carns committed
182 183 184
    svc1_deregister(mid, *handler_pool, 1);
    svc1_deregister(mid, svc1_pool2, 2);
    svc2_deregister(mid, *handler_pool, 3);
Philip Carns's avatar
Philip Carns committed
185
#endif
186 187 188

    ABT_xstream_join(svc1_xstream2);
    ABT_xstream_free(&svc1_xstream2);
189

190 191 192 193 194 195 196 197
    ABT_finalize();

    HG_Context_destroy(hg_context);
    HG_Finalize(hg_class);

    return(0);
}