proc-read-responses.c 4.82 KB
Newer Older
1 2 3 4 5 6
/*
 * (C) 2017 The University of Chicago
 * 
 * See COPYRIGHT in top-level directory.
 */
#include <mercury_proc.h>
Matthieu Dorier's avatar
Matthieu Dorier committed
7 8 9 10 11 12
#include "src/io-chain/proc-read-responses.h"
#include "src/io-chain/read-responses.h"
#include "src/io-chain/read-resp-impl.h"
#include "src/omap-iter/proc-omap-iter.h"
#include "src/util/utlist.h"
#include "src/util/log.h"
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161

typedef hg_return_t (*encode_fn)(hg_proc_t, void*);
typedef hg_return_t (*decode_fn)(hg_proc_t, void*);

static hg_return_t encode_stat_response(hg_proc_t proc, rd_response_stat_t  r);
static hg_return_t decode_stat_response(hg_proc_t proc, rd_response_stat_t* r);
static hg_return_t encode_read_response(hg_proc_t proc, rd_response_read_t  r);
static hg_return_t decode_read_response(hg_proc_t proc, rd_response_read_t* r);
static hg_return_t encode_omap_response(hg_proc_t proc, rd_response_omap_t  r);
static hg_return_t decode_omap_response(hg_proc_t proc, rd_response_omap_t* r);

static encode_fn encode[] = {
	NULL,
	(encode_fn)encode_stat_response,
	(encode_fn)encode_read_response,
	(encode_fn)encode_omap_response
};

static decode_fn decode[] = {
	NULL,
	(decode_fn)decode_stat_response,
	(decode_fn)decode_read_response,
	(decode_fn)decode_omap_response
};

hg_return_t hg_proc_read_response_t(hg_proc_t proc, read_response_t* response)
{
	hg_return_t ret = HG_SUCCESS;
	rd_response_base_t elem;

	switch(hg_proc_get_op(proc)) {

	case HG_ENCODE:
		// encode the number of actions
		ret = hg_proc_memcpy(proc, &((*response)->num_responses),
				sizeof((*response)->num_responses));
		if(ret != HG_SUCCESS) return ret;

		DL_FOREACH((*response)->responses,elem) {
			read_resp_code_t opcode = elem->type;
			MOBJECT_ASSERT((opcode > 0 || opcode < _READ_RESPCODE_END_ENUM_),
					"Invalid response code");
			// encode the type of response
			ret = hg_proc_memcpy(proc, &opcode, sizeof(opcode));
			if(ret != HG_SUCCESS) return ret;
			// encode the response's arguments
			ret = encode[opcode](proc, elem);
			if(ret != HG_SUCCESS) return ret;
		}

		break;
	case HG_DECODE:
		// allocate the response chain
		*response = (read_response_t)calloc(1, sizeof(**response));
		// decode the number of responses in the chain
		ret = hg_proc_memcpy(proc, &((*response)->num_responses), sizeof((*response)->num_responses));
		if(ret != HG_SUCCESS) return ret;

		size_t i;
		rd_response_base_t next_resp;
		for(i = 0; i < (*response)->num_responses; i++) {
			// decode the current response's type
			read_resp_code_t opcode;
			ret = hg_proc_memcpy(proc, &opcode, sizeof(opcode));
			if(ret != HG_SUCCESS) return ret;
			MOBJECT_ASSERT((opcode > 0 || opcode < _READ_RESPCODE_END_ENUM_),
				"Invalid write_op opcode");
			// decode the response's arguments
			ret = decode[opcode](proc, &next_resp);
			if(ret != HG_SUCCESS) return ret;
			next_resp->type = opcode;
			// append to the list
			DL_APPEND((*response)->responses, next_resp);
		}
		
		break;
	case HG_FREE:

		free_read_responses(*response);
		break;
	}

	return ret;
}

hg_return_t encode_stat_response(hg_proc_t proc, rd_response_stat_t  r)
{
	hg_return_t ret;
	ret = hg_proc_uint64_t(proc, &(r->psize));
	if(ret != HG_SUCCESS) return ret;
	ret = hg_proc_memcpy(proc, &(r->pmtime), sizeof(r->pmtime));
	if(ret != HG_SUCCESS) return ret;
	ret = hg_proc_memcpy(proc, &(r->prval), sizeof(r->prval));
	return ret;
}

hg_return_t decode_stat_response(hg_proc_t proc, rd_response_stat_t* r)
{
	*r = (rd_response_stat_t)calloc(1, sizeof(**r));
	
	hg_return_t ret;
	ret = hg_proc_uint64_t(proc, &((*r)->psize));
	if(ret != HG_SUCCESS) return ret;
	ret = hg_proc_memcpy(proc, &((*r)->pmtime), sizeof((*r)->pmtime));
	if(ret != HG_SUCCESS) return ret;
	ret = hg_proc_memcpy(proc, &((*r)->prval), sizeof((*r)->prval));
	return ret;
}

hg_return_t encode_read_response(hg_proc_t proc, rd_response_read_t  r)
{
	hg_return_t ret;
	ret = hg_proc_hg_size_t(proc, &(r->bytes_read));
	if(ret != HG_SUCCESS) return ret;
	ret = hg_proc_memcpy(proc, &(r->prval), sizeof(r->prval));
	return ret;
}

hg_return_t decode_read_response(hg_proc_t proc, rd_response_read_t* r)
{
	*r = (rd_response_read_t)calloc(1, sizeof(**r));

	hg_return_t ret;
	ret = hg_proc_hg_size_t(proc, &((*r)->bytes_read));
	if(ret != HG_SUCCESS) return ret;
	ret = hg_proc_memcpy(proc, &((*r)->prval), sizeof((*r)->prval));
	return ret;

}

hg_return_t encode_omap_response(hg_proc_t proc, rd_response_omap_t  r)
{
	hg_return_t ret;
	ret = hg_proc_memcpy(proc, &(r->prval), sizeof(r->prval));
	if(ret != HG_SUCCESS) return ret;
	ret = hg_proc_mobject_store_omap_iter_t(proc, &(r->iter));
	if(ret != HG_SUCCESS) return ret;
}

hg_return_t decode_omap_response(hg_proc_t proc, rd_response_omap_t* r)
{
	*r = (rd_response_omap_t)calloc(1, sizeof(**r));

	hg_return_t ret;
	ret = hg_proc_memcpy(proc, &((*r)->prval), sizeof((*r)->prval));
	if(ret != HG_SUCCESS) return ret;
	ret = hg_proc_mobject_store_omap_iter_t(proc, &((*r)->iter));
	if(ret != HG_SUCCESS) return ret;
}