composed-svc-daemon.c 5.33 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>
Philip Carns's avatar
Philip Carns committed
10 11 12
#include <string.h>
#include <stdlib.h>

13 14 15 16 17
#include <abt.h>
#include <abt-snoozer.h>
#include <margo.h>

#include "data-xfer-service.h"
Philip Carns's avatar
Philip Carns committed
18
#include "delegator-service.h"
19 20 21 22 23 24 25 26 27 28 29 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 59 60 61 62 63 64 65 66 67

/* 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;
    const struct hg_info *hgi;
    margo_instance_id mid;

    //printf("Got RPC request to shutdown\n");

    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;
    ABT_xstream svc1_xstream2;
    ABT_pool *handler_pool;
Philip Carns's avatar
Philip Carns committed
68 69 70 71
    char* svc_list;
    char* svc;
    hg_addr_t relay_addr = HG_ADDR_NULL;
    char* relay_addr_string;
72

Philip Carns's avatar
Philip Carns committed
73
    if(argc != 3)
74
    {
Philip Carns's avatar
Philip Carns committed
75 76
        fprintf(stderr, "Usage: ./server <listen_addr> <comma_separated_service_list>\n");
        fprintf(stderr, "Example: ./server na+sm:// delegator,data-xfer\n");
77 78 79
        return(-1);
    }

Philip Carns's avatar
Philip Carns committed
80 81 82
    svc_list = strdup(argv[2]);
    assert(svc_list);

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 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
    /* 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);

    /* register RPCs and services */
    /***************************************/

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

    handler_pool = margo_get_handler_pool(mid);
Philip Carns's avatar
Philip Carns committed
155 156 157 158 159 160 161 162 163 164 165 166 167 168
    svc = strtok(svc_list, ",");
    while(svc)
    {
        if(strcmp(svc, "data-xfer"))
        {
            data_xfer_service_register(mid, *handler_pool, 0);
        }
        else if(strcmp(svc, "delegator"))
        {
            relay_addr_string = getenv("RELAY_ADDR");
            if(relay_addr_string)
                ret = margo_addr_lookup(mid, relay_addr_string, &relay_addr); 
            else
                ret = HG_Addr_self(margo_get_class(mid), &relay_addr);
Philip Carns's avatar
Philip Carns committed
169
            assert(ret == 0);
Philip Carns's avatar
Philip Carns committed
170 171 172 173 174
            delegator_service_register(mid, *handler_pool, 0, relay_addr);
        }
        else
            assert(0);
    }
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190

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

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

    /*  TODO: rethink this; can't touch mid after wait for finalize */
#if 0
    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
191 192
    if(relay_addr != HG_ADDR_NULL)
        HG_Addr_free(margo_get_class(mid, relay_addr);
193 194 195 196 197 198 199 200 201 202 203 204 205
#endif

    ABT_xstream_join(svc1_xstream2);
    ABT_xstream_free(&svc1_xstream2);

    ABT_finalize();

    HG_Context_destroy(hg_context);
    HG_Finalize(hg_class);

    return(0);
}