request.hpp 1.93 KB
Newer Older
Matthieu Dorier's avatar
Matthieu Dorier committed
1 2 3 4 5
/*
 * (C) 2017 The University of Chicago
 * 
 * See COPYRIGHT in top-level directory.
 */
Matthieu Dorier's avatar
Matthieu Dorier committed
6 7 8 9
#ifndef __THALLIUM_REQUEST_HPP
#define __THALLIUM_REQUEST_HPP

#include <margo.h>
10 11
#include <thallium/serialization/serialize.hpp>
#include <thallium/serialization/buffer_output_archive.hpp>
Matthieu Dorier's avatar
Matthieu Dorier committed
12 13 14

namespace thallium {

15
class engine;
Matthieu Dorier's avatar
Matthieu Dorier committed
16
class endpoint;
Matthieu Dorier's avatar
Matthieu Dorier committed
17 18 19

class request {

20
	friend class engine;
Matthieu Dorier's avatar
Matthieu Dorier committed
21 22 23

private:

Matthieu Dorier's avatar
Matthieu Dorier committed
24
    engine*     m_engine;
Matthieu Dorier's avatar
Matthieu Dorier committed
25
	hg_handle_t m_handle;
26
    bool        m_disable_response;
Matthieu Dorier's avatar
Matthieu Dorier committed
27

Matthieu Dorier's avatar
Matthieu Dorier committed
28 29
	request(engine& e, hg_handle_t h, bool disable_resp)
	: m_engine(&e), m_handle(h), m_disable_response(disable_resp) {}
Matthieu Dorier's avatar
Matthieu Dorier committed
30 31 32 33

public:

	request(const request& other)
Matthieu Dorier's avatar
Matthieu Dorier committed
34
	: m_engine(other.m_engine), m_handle(other.m_handle), m_disable_response(other.m_disable_response) {
Matthieu Dorier's avatar
Matthieu Dorier committed
35 36 37
		margo_ref_incr(m_handle);
	}

Matthieu Dorier's avatar
Matthieu Dorier committed
38 39
	request(request&& other)
	: m_engine(other.m_engine), m_handle(other.m_handle), m_disable_response(other.m_disable_response) {
Matthieu Dorier's avatar
Matthieu Dorier committed
40 41 42 43 44 45
		other.m_handle = HG_HANDLE_NULL;
	}

	request& operator=(const request& other) {
		if(m_handle == other.m_handle) return *this;
		margo_destroy(m_handle);
Matthieu Dorier's avatar
Matthieu Dorier committed
46 47
        m_engine           = other.m_engine;
		m_handle           = other.m_handle;
48
        m_disable_response = other.m_disable_response;
Matthieu Dorier's avatar
Matthieu Dorier committed
49 50 51 52 53 54 55
		margo_ref_incr(m_handle);
		return *this;
	}

	request& operator=(request&& other) {
		if(m_handle == other.m_handle) return *this;
		margo_destroy(m_handle);
Matthieu Dorier's avatar
Matthieu Dorier committed
56 57
        m_engine           = other.m_engine;
		m_handle           = other.m_handle;
58
        m_disable_response = other.m_disable_response;
Matthieu Dorier's avatar
Matthieu Dorier committed
59 60 61 62 63 64 65 66
		other.m_handle = HG_HANDLE_NULL;
		return *this;
	}

	~request() {
		margo_destroy(m_handle);
	}

67 68
	template<typename ... T>
	void respond(T&&... t) const {
69
        if(m_disable_response) return; // XXX throwing an exception?
Matthieu Dorier's avatar
Matthieu Dorier committed
70
		if(m_handle != HG_HANDLE_NULL) {
71
            buffer b;
Matthieu Dorier's avatar
Matthieu Dorier committed
72
            buffer_output_archive arch(b, *m_engine);
73 74
            serialize_many(arch, std::forward<T>(t)...);
			margo_respond(m_handle, &b);
Matthieu Dorier's avatar
Matthieu Dorier committed
75 76
		}
	}
Matthieu Dorier's avatar
Matthieu Dorier committed
77 78

    endpoint get_endpoint() const;
Matthieu Dorier's avatar
Matthieu Dorier committed
79 80 81 82 83
};

}

#endif