request.hpp 3.14 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

Matthieu Dorier's avatar
Matthieu Dorier committed
18 19 20 21 22 23 24
/**
 * @brief A request object is created whenever a server
 * receives an RPC. The object is passed as first argument to
 * the function associated with the RPC. The request allows
 * one to get information from the caller and to respond to
 * the RPC.
 */
Matthieu Dorier's avatar
Matthieu Dorier committed
25 26
class request {

27
	friend class engine;
Matthieu Dorier's avatar
Matthieu Dorier committed
28 29 30

private:

Matthieu Dorier's avatar
Matthieu Dorier committed
31
    engine*     m_engine;
Matthieu Dorier's avatar
Matthieu Dorier committed
32
	hg_handle_t m_handle;
33
    bool        m_disable_response;
Matthieu Dorier's avatar
Matthieu Dorier committed
34

Matthieu Dorier's avatar
Matthieu Dorier committed
35 36 37 38 39 40 41 42
    /**
     * @brief Constructor. Made private since request are only created
     * by the engine within RPC callbacks.
     *
     * @param e engine object that created the request.
     * @param h handle of the RPC that was received.
     * @param disable_resp whether responses are disabled.
     */
Matthieu Dorier's avatar
Matthieu Dorier committed
43 44
	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
45 46 47

public:

Matthieu Dorier's avatar
Matthieu Dorier committed
48 49 50
    /**
     * @brief Copy constructor.
     */
Matthieu Dorier's avatar
Matthieu Dorier committed
51
	request(const request& other)
Matthieu Dorier's avatar
Matthieu Dorier committed
52
	: m_engine(other.m_engine), m_handle(other.m_handle), m_disable_response(other.m_disable_response) {
Matthieu Dorier's avatar
Matthieu Dorier committed
53 54 55
		margo_ref_incr(m_handle);
	}

Matthieu Dorier's avatar
Matthieu Dorier committed
56 57 58
    /**
     * @brief Move constructor.
     */
Matthieu Dorier's avatar
Matthieu Dorier committed
59 60
	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
61 62 63
		other.m_handle = HG_HANDLE_NULL;
	}

Matthieu Dorier's avatar
Matthieu Dorier committed
64 65 66
    /**
     * @brief Copy-assignment operator.
     */
Matthieu Dorier's avatar
Matthieu Dorier committed
67 68 69
	request& operator=(const request& other) {
		if(m_handle == other.m_handle) return *this;
		margo_destroy(m_handle);
Matthieu Dorier's avatar
Matthieu Dorier committed
70 71
        m_engine           = other.m_engine;
		m_handle           = other.m_handle;
72
        m_disable_response = other.m_disable_response;
Matthieu Dorier's avatar
Matthieu Dorier committed
73 74 75 76
		margo_ref_incr(m_handle);
		return *this;
	}

Matthieu Dorier's avatar
Matthieu Dorier committed
77 78 79
    /**
     * @brief Move-assignment operator.
     */
Matthieu Dorier's avatar
Matthieu Dorier committed
80 81 82
	request& operator=(request&& other) {
		if(m_handle == other.m_handle) return *this;
		margo_destroy(m_handle);
Matthieu Dorier's avatar
Matthieu Dorier committed
83 84
        m_engine           = other.m_engine;
		m_handle           = other.m_handle;
85
        m_disable_response = other.m_disable_response;
Matthieu Dorier's avatar
Matthieu Dorier committed
86 87 88 89
		other.m_handle = HG_HANDLE_NULL;
		return *this;
	}

Matthieu Dorier's avatar
Matthieu Dorier committed
90 91 92
    /**
     * @brief Destructor.
     */
Matthieu Dorier's avatar
Matthieu Dorier committed
93 94 95 96
	~request() {
		margo_destroy(m_handle);
	}

Matthieu Dorier's avatar
Matthieu Dorier committed
97 98 99 100 101 102 103 104
    /**
     * @brief Responds to the sender of the RPC.
     * Serializes the series of arguments provided and
     * send the resulting buffer to the sender.
     *
     * @tparam T Types of parameters to serialize.
     * @param t Parameters to serialize.
     */
105 106
	template<typename ... T>
	void respond(T&&... t) const {
107
        if(m_disable_response) return; // XXX throwing an exception?
Matthieu Dorier's avatar
Matthieu Dorier committed
108
		if(m_handle != HG_HANDLE_NULL) {
109
            buffer b;
Matthieu Dorier's avatar
Matthieu Dorier committed
110
            buffer_output_archive arch(b, *m_engine);
111 112
            serialize_many(arch, std::forward<T>(t)...);
			margo_respond(m_handle, &b);
Matthieu Dorier's avatar
Matthieu Dorier committed
113 114
		}
	}
Matthieu Dorier's avatar
Matthieu Dorier committed
115

Matthieu Dorier's avatar
Matthieu Dorier committed
116 117 118 119 120
    /**
     * @brief Get the endpoint corresponding to the sender of the RPC.
     *
     * @return endpoint corresponding to the sender of the RPC.
     */
Matthieu Dorier's avatar
Matthieu Dorier committed
121
    endpoint get_endpoint() const;
Matthieu Dorier's avatar
Matthieu Dorier committed
122 123 124 125 126
};

}

#endif