summaryrefslogtreecommitdiffstats
blob: 312ecf23ce4d1e0ae7608bc688fc3b5473478a1a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
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
/*
 * Copyright (c) 2010-2016 Isode Limited.
 * All rights reserved.
 * See the COPYING file for more information.
 */

#pragma once

#include <boost/signals2.hpp>

#include <Swiften/Base/API.h>
#include <Swiften/Queries/Request.h>

namespace Swift {
    /**
     * GenericRequest is used for managing the sending of, and handling of replies to, iq stanzas that do not have their own Request types.
     *
     * To create an iq stanza, call a constructor with the type of the iq that needs to be sent (either Set or Get), addressing information (clients should use the constructor that doesn't specify a sender), the payload that should be sent in the iq, and the IQRouter for the connection, obtained through the Client or CoreClient object.
     *
     * Having created a GenericRequest, connect to the onResponse signal to be told when a response (either a result or an error) has been received by Swiften.
     *
     * To send the iq, then call send() - onResponse will be called when a reply is received.
     */
    template<typename PAYLOAD_TYPE>
    class SWIFTEN_API GenericRequest : public Request {
        public:
            typedef std::shared_ptr<GenericRequest<PAYLOAD_TYPE> > ref;

        public:
            /**
             * Create a request suitable for client use.
             * @param type Iq type - Get or Set.
             * @param receiver JID to send request to.
             * @param payload Payload to send in stanza.
             * @param router IQRouter instance for current connection.
             */
            GenericRequest(
                    IQ::Type type,
                    const JID& receiver,
                    std::shared_ptr<Payload> payload,
                    IQRouter* router) :
                        Request(type, receiver, payload, router) {
            }

            /**
             * Create a request suitable for component or server use. As a client, use the other constructor instead.
             * @param type Iq type - Get or Set.
             * @param sender JID to use in "from" of stanza.
             * @param receiver JID to send request to.
             * @param payload Payload to send in stanza.
             * @param router IQRouter instance for current connection.
             */
            GenericRequest(
                    IQ::Type type,
                    const JID& sender,
                    const JID& receiver,
                    std::shared_ptr<Payload> payload,
                    IQRouter* router) :
                        Request(type, sender, receiver, payload, router) {
            }

            /**
             * Internal method, do not use.
             */
            virtual void handleResponse(std::shared_ptr<Payload> payload, ErrorPayload::ref error) {
                onResponse(std::dynamic_pointer_cast<PAYLOAD_TYPE>(payload), error);
            }

        public:
            std::shared_ptr<PAYLOAD_TYPE> getPayloadGeneric() const {
                return std::dynamic_pointer_cast<PAYLOAD_TYPE>(getPayload());
            }

        public:
            /**
             * Signal emitted when a reply to the iq has been received. Contains a payload if one was present, and an error if one was present.
             */
            boost::signals2::signal<void (std::shared_ptr<PAYLOAD_TYPE>, ErrorPayload::ref)> onResponse;
    };
}