my-rpc.c 2.69 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/*
 * (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.
 */

21
static void my_rpc_ult(hg_handle_t handle)
22 23 24 25 26 27 28 29 30
{
    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;
31
#if 0
32 33
    int fd;
    char filename[256];
34
#endif
35
    margo_instance_id mid;
36

37
    ret = HG_Get_input(handle, &in);
38 39 40 41 42 43 44 45 46 47 48
    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 */
49
    hgi = HG_Get_info(handle);
50
    assert(hgi);
51
    ret = HG_Bulk_create(hgi->hg_class, 1, &buffer,
52 53 54
        &size, HG_BULK_WRITE_ONLY, &bulk_handle);
    assert(ret == 0);

55 56
    mid = margo_hg_class_to_instance(hgi->hg_class);

57
    /* do bulk transfer from client to server */
Jonathan Jenkins's avatar
Jonathan Jenkins committed
58
    ret = margo_bulk_transfer(mid, HG_BULK_PULL,
59 60 61 62
        hgi->addr, in.bulk_handle, 0,
        bulk_handle, 0, size);
    assert(ret == 0);

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

69
    ret = abt_io_pwrite(aid, fd, buffer, 512, 0);
70 71
    assert(ret == 512);

72
    abt_io_close(aid, fd);
73 74
#endif

75
    hret = HG_Respond(handle, NULL, NULL, &out);
76 77 78
    assert(hret == HG_SUCCESS);

    HG_Bulk_free(bulk_handle);
79
    HG_Destroy(handle);
80 81 82 83
    free(buffer);

    return;
}
84
DEFINE_MARGO_RPC_HANDLER(my_rpc_ult)
85

86
static void my_rpc_shutdown_ult(hg_handle_t handle)
87 88 89 90 91 92 93
{
    hg_return_t hret;
    struct hg_info *hgi;
    margo_instance_id mid;

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

94
    hgi = HG_Get_info(handle);
95 96 97
    assert(hgi);
    mid = margo_hg_class_to_instance(hgi->hg_class);

98
    hret = margo_respond(mid, handle, NULL);
99
    assert(hret == HG_SUCCESS);
Jonathan Jenkins's avatar
Jonathan Jenkins committed
100

101
    HG_Destroy(handle);
102

103 104 105 106
    /* 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.
     */
107 108 109 110 111
    margo_finalize(mid);

    return;
}
DEFINE_MARGO_RPC_HANDLER(my_rpc_shutdown_ult)