my-rpc.c 2.82 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
{
    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;
29
    const struct hg_info *hgi;
30
#if 0
31
    int ret;
32 33
    int fd;
    char filename[256];
34
#endif
35
    margo_instance_id mid;
36

37 38
    hret = margo_get_input(handle, &in);
    assert(hret == HG_SUCCESS);
39

Philip Carns's avatar
Philip Carns committed
40
    //printf("Got RPC request with input_val: %d\n", in.input_val);
41 42 43 44 45 46 47
    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 52
    mid = margo_hg_info_get_instance(hgi);
    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
    /* write to a file; would be done with abt-io if we enabled it */
66
#if 0
Philip Carns's avatar
Philip Carns committed
67
    sprintf(filename, "/tmp/margo-%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
#endif

77
    margo_free_input(handle, &in);
78

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
    free(buffer);

    return;
}
88
DEFINE_MARGO_RPC_HANDLER(my_rpc_ult)
89

90
static void my_rpc_shutdown_ult(hg_handle_t handle)
91 92 93 94 95 96
{
    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
    assert(hret == HG_SUCCESS);
Jonathan Jenkins's avatar
Jonathan Jenkins committed
103

104
    margo_destroy(handle);
105

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

    return;
}
DEFINE_MARGO_RPC_HANDLER(my_rpc_shutdown_ult)