my-rpc.c 2.77 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
/*
 * (C) 2015 The University of Chicago
 * 
 * See COPYRIGHT in top-level directory.
 */

#include <assert.h>
#include "my-rpc.h"

/* my-rpc:
 * This is an example RPC operation.  It includes a small bulk transfer, 
 * driven by the server, that moves data from the client to the server.  The
 * server writes the data to a local file in /tmp.
 */

/* The rpc handler is defined as a single ULT in Argobots.  It uses
 * underlying asynchronous operations for the HG transfer, open, write, and
 * close.
 */

static void my_rpc_ult(void *_arg)
{
    hg_handle_t *handle = _arg;

    hg_return_t hret;
    my_rpc_out_t out;
    my_rpc_in_t in;
    int ret;
    hg_size_t size;
    void *buffer;
    hg_bulk_t bulk_handle;
    struct hg_info *hgi;
33
#if 0
34 35
    int fd;
    char filename[256];
36
#endif
37
    margo_instance_id mid;
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52

    ret = HG_Get_input(*handle, &in);
    assert(ret == HG_SUCCESS);

    printf("Got RPC request with input_val: %d\n", in.input_val);
    out.ret = 0;

    /* set up target buffer for bulk transfer */
    size = 512;
    buffer = calloc(1, 512);
    assert(buffer);

    /* register local target buffer for bulk access */
    hgi = HG_Get_info(*handle);
    assert(hgi);
53
    ret = HG_Bulk_create(hgi->hg_class, 1, &buffer,
54 55 56
        &size, HG_BULK_WRITE_ONLY, &bulk_handle);
    assert(ret == 0);

57 58
    mid = margo_hg_class_to_instance(hgi->hg_class);

59
    /* do bulk transfer from client to server */
60
    ret = margo_bulk_transfer(mid, hgi->context, HG_BULK_PULL, 
61 62 63 64
        hgi->addr, in.bulk_handle, 0,
        bulk_handle, 0, size);
    assert(ret == 0);

65
    /* write to a file; would be done with abt-io if we enabled it */
66 67
#if 0
    sprintf(filename, "/tmp/hg-fiber-%d.txt", in.input_val);
68
    fd = abt_io_open(aid, filename, O_WRONLY|O_CREAT, S_IWUSR|S_IRUSR);
69 70
    assert(fd > -1);

71
    ret = abt_io_pwrite(aid, fd, buffer, 512, 0);
72 73
    assert(ret == 512);

74
    abt_io_close(aid, fd);
75 76 77 78 79 80 81 82 83 84 85 86
#endif

    hret = HG_Respond(*handle, NULL, NULL, &out);
    assert(hret == HG_SUCCESS);

    HG_Bulk_free(bulk_handle);
    HG_Destroy(*handle);
    free(buffer);
    free(handle);

    return;
}
87
DEFINE_MARGO_RPC_HANDLER(my_rpc_ult)
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102

static void my_rpc_shutdown_ult(void *_arg)
{
    hg_handle_t *handle = _arg;

    hg_return_t hret;
    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);

Jonathan Jenkins's avatar
Jonathan Jenkins committed
103
    hret = margo_respond(mid, *handle, NULL);
104
    assert(hret == HG_SUCCESS);
Jonathan Jenkins's avatar
Jonathan Jenkins committed
105

106 107
    HG_Destroy(*handle);

108 109 110 111
    /* 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.
     */
112 113 114 115 116
    margo_finalize(mid);

    return;
}
DEFINE_MARGO_RPC_HANDLER(my_rpc_shutdown_ult)