svc2-client.c 3.14 KB
Newer Older
Philip Carns's avatar
Philip Carns committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
/*
 * (C) 2015 The University of Chicago
 * 
 * See COPYRIGHT in top-level directory.
 */

#include <stdio.h>
#include <assert.h>
#include <unistd.h>
#include <abt.h>

#include "svc2-client.h"
#include "svc2-proto.h"

/* NOTE: just making these global for test example, would be cleaner if there
 * were an instance pointer for the client.
 */
static hg_id_t svc2_do_thing_id = -1;
static hg_id_t svc2_do_other_thing_id = -1;

int svc2_register_client(margo_instance_id mid)
{

24 25 26 27
	svc2_do_thing_id = MARGO_REGISTER(mid, "svc2_do_thing",
        svc2_do_thing_in_t, svc2_do_thing_out_t, NULL);
	svc2_do_other_thing_id = MARGO_REGISTER(mid, "svc2_do_other_thing",
        svc2_do_other_thing_in_t, svc2_do_other_thing_out_t, NULL);
Philip Carns's avatar
Philip Carns committed
28 29 30 31

    return(0);
}

Philip Carns's avatar
Philip Carns committed
32
void svc2_do_thing(margo_instance_id mid, hg_addr_t svr_addr, uint32_t provider_id)
Philip Carns's avatar
Philip Carns committed
33 34 35 36
{
    hg_handle_t handle;
    svc2_do_thing_in_t in;
    svc2_do_thing_out_t out;
37
    hg_return_t hret;
Philip Carns's avatar
Philip Carns committed
38 39 40 41 42 43 44 45 46 47
    hg_size_t size;
    void* buffer;

    /* allocate buffer for bulk transfer */
    size = 512;
    buffer = calloc(1, 512);
    assert(buffer);
    sprintf((char*)buffer, "Hello world!\n");

    /* create handle */
48 49
    hret = margo_create(mid, svr_addr, svc2_do_thing_id, &handle);
    assert(hret == HG_SUCCESS);
Philip Carns's avatar
Philip Carns committed
50 51

    /* register buffer for rdma/bulk access by server */
52
    hret = margo_bulk_create(mid, 1, &buffer, &size, 
Philip Carns's avatar
Philip Carns committed
53
        HG_BULK_READ_ONLY, &in.bulk_handle);
54
    assert(hret == HG_SUCCESS);
Philip Carns's avatar
Philip Carns committed
55 56 57 58 59

    /* Send rpc. Note that we are also transmitting the bulk handle in the
     * input struct.  It was set above. 
     */ 
    in.input_val = 0;
60
    hret = margo_provider_forward(provider_id, handle, &in);
61
    assert(hret == HG_SUCCESS);
Philip Carns's avatar
Philip Carns committed
62 63

    /* decode response */
64 65
    hret = margo_get_output(handle, &out);
    assert(hret == HG_SUCCESS);
Philip Carns's avatar
Philip Carns committed
66 67

    /* clean up resources consumed by this rpc */
68 69
    margo_free_output(handle, &out);
    margo_bulk_free(in.bulk_handle);
70
    margo_destroy(handle);
Philip Carns's avatar
Philip Carns committed
71 72 73 74 75
    free(buffer);

    return;
}

Philip Carns's avatar
Philip Carns committed
76
void svc2_do_other_thing(margo_instance_id mid, hg_addr_t svr_addr, uint32_t provider_id)
Philip Carns's avatar
Philip Carns committed
77 78 79 80
{
    hg_handle_t handle;
    svc2_do_other_thing_in_t in;
    svc2_do_other_thing_out_t out;
81
    hg_return_t hret;
Philip Carns's avatar
Philip Carns committed
82 83 84 85 86 87 88 89 90 91
    hg_size_t size;
    void* buffer;

    /* allocate buffer for bulk transfer */
    size = 512;
    buffer = calloc(1, 512);
    assert(buffer);
    sprintf((char*)buffer, "Hello world!\n");

    /* create handle */
92 93
    hret = margo_create(mid, svr_addr, svc2_do_other_thing_id, &handle);
    assert(hret == HG_SUCCESS);
Philip Carns's avatar
Philip Carns committed
94 95

    /* register buffer for rdma/bulk access by server */
96
    hret = margo_bulk_create(mid, 1, &buffer, &size, 
Philip Carns's avatar
Philip Carns committed
97
        HG_BULK_READ_ONLY, &in.bulk_handle);
98
    assert(hret == HG_SUCCESS);
Philip Carns's avatar
Philip Carns committed
99 100 101 102 103

    /* Send rpc. Note that we are also transmitting the bulk handle in the
     * input struct.  It was set above. 
     */ 
    in.input_val = 0;
104
    hret = margo_provider_forward(provider_id, handle, &in);
105
    assert(hret == HG_SUCCESS);
Philip Carns's avatar
Philip Carns committed
106 107

    /* decode response */
108 109
    hret = margo_get_output(handle, &out);
    assert(hret == HG_SUCCESS);
Philip Carns's avatar
Philip Carns committed
110 111

    /* clean up resources consumed by this rpc */
112 113
    margo_free_output(handle, &out);
    margo_bulk_free(in.bulk_handle);
114
    margo_destroy(handle);
Philip Carns's avatar
Philip Carns committed
115 116 117 118
    free(buffer);

    return;
}