my-rpc.c 4.25 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
/*
 * (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(hg_handle_t handle)
{
    hg_return_t hret;
    my_rpc_out_t out;
    my_rpc_in_t in;
    hg_size_t size;
    void *buffer;
    hg_bulk_t bulk_handle;
Philip Carns's avatar
Philip Carns committed
29
    const struct hg_info *hgi;
30
#if 0
31
    int ret;
32 33 34 35 36
    int fd;
    char filename[256];
#endif
    margo_instance_id mid;

37 38
    hret = margo_get_input(handle, &in);
    assert(hret == HG_SUCCESS);
39 40 41 42 43 44 45 46 47

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

48 49
    /* get handle info and margo instance */
    hgi = margo_get_info(handle);
50
    assert(hgi);
51
    mid = margo_hg_info_get_instance(hgi);
52
    assert(mid != MARGO_INSTANCE_NULL);
53

54 55 56 57
    /* register local target buffer for bulk access */
    hret = margo_bulk_create(mid, 1, &buffer,
        &size, HG_BULK_WRITE_ONLY, &bulk_handle);
    assert(hret == HG_SUCCESS);
58 59

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

    /* write to a file; would be done with abt-io if we enabled it */
#if 0
Philip Carns's avatar
Philip Carns committed
67
    sprintf(filename, "/tmp/margo-%d.txt", in.input_val);
68 69 70 71 72 73 74 75 76
    fd = abt_io_open(aid, filename, O_WRONLY|O_CREAT, S_IWUSR|S_IRUSR);
    assert(fd > -1);

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

    abt_io_close(aid, fd);
#endif

77 78
    margo_free_input(handle, &in);

79
    hret = margo_respond(handle, &out);
80 81
    assert(hret == HG_SUCCESS);

82
    margo_bulk_free(bulk_handle);
83
    margo_destroy(handle);
84 85 86 87 88 89 90 91 92 93 94 95 96
    free(buffer);

    return;
}
DEFINE_MARGO_RPC_HANDLER(my_rpc_ult)

static void my_rpc_shutdown_ult(hg_handle_t handle)
{
    hg_return_t hret;
    margo_instance_id mid;

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

97
    /* get handle info and margo instance */
98
    mid = margo_hg_handle_get_instance(handle);
99
    assert(mid != MARGO_INSTANCE_NULL);
100

101
    hret = margo_respond(handle, NULL);
102 103
    assert(hret == HG_SUCCESS);

104
    margo_destroy(handle);
105 106 107 108 109 110 111 112 113 114

    /* 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)
Philip Carns's avatar
Philip Carns committed
115 116 117 118 119 120 121 122 123

static void my_rpc_hang_ult(hg_handle_t handle)
{
    hg_return_t hret;
    my_rpc_out_t out;
    my_rpc_in_t in;
    hg_size_t size;
    void *buffer;
    hg_bulk_t bulk_handle;
Philip Carns's avatar
Philip Carns committed
124
    const struct hg_info *hgi;
Philip Carns's avatar
Philip Carns committed
125 126
    margo_instance_id mid;

127 128
    hret = margo_get_input(handle, &in);
    assert(hret == HG_SUCCESS);
Philip Carns's avatar
Philip Carns committed
129 130 131 132

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

133 134
    /* get handle info and margo instance */
    hgi = margo_get_info(handle);
Philip Carns's avatar
Philip Carns committed
135
    assert(hgi);
136
    mid = margo_hg_info_get_instance(hgi);
137
    assert(mid != MARGO_INSTANCE_NULL);
Philip Carns's avatar
Philip Carns committed
138 139 140 141 142 143 144 145 146 147

    /* sleep for an hour (to allow client to test timeout capability) */
    margo_thread_sleep(mid, 1000*60*60);

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

    /* register local target buffer for bulk access */
148
    hret = margo_bulk_create(mid, 1, &buffer,
Philip Carns's avatar
Philip Carns committed
149
        &size, HG_BULK_WRITE_ONLY, &bulk_handle);
150
    assert(hret == HG_SUCCESS);
Philip Carns's avatar
Philip Carns committed
151 152

    /* do bulk transfer from client to server */
153
    hret = margo_bulk_transfer(mid, HG_BULK_PULL,
Philip Carns's avatar
Philip Carns committed
154
        hgi->addr, in.bulk_handle, 0,
155
        bulk_handle, 0, size);
156
    assert(hret == HG_SUCCESS);
Philip Carns's avatar
Philip Carns committed
157

158
    margo_free_input(handle, &in);
Philip Carns's avatar
Philip Carns committed
159

160
    hret = margo_respond(handle, &out);
Philip Carns's avatar
Philip Carns committed
161 162
    assert(hret == HG_SUCCESS);

163
    margo_bulk_free(bulk_handle);
164
    margo_destroy(handle);
Philip Carns's avatar
Philip Carns committed
165 166 167 168 169
    free(buffer);

    return;
}
DEFINE_MARGO_RPC_HANDLER(my_rpc_hang_ult)