summaryrefslogtreecommitdiffstats
blob: f67364313f22bb45a8abc2cd13f18c0ebc81632c (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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
/*
 * Copyright (c) 2010-2017 Isode Limited.
 * All rights reserved.
 * See the COPYING file for more information.
 */

#pragma once

#include <memory>
#include <string>

#include <boost/signals2.hpp>

#include <Swiften/Base/API.h>
#include <Swiften/Base/Error.h>
#include <Swiften/Base/SafeByteArray.h>
#include <Swiften/Component/ComponentConnector.h>
#include <Swiften/Component/ComponentError.h>
#include <Swiften/Component/ComponentSession.h>
#include <Swiften/Component/ComponentSessionStanzaChannel.h>
#include <Swiften/Elements/Message.h>
#include <Swiften/Elements/Presence.h>
#include <Swiften/Entity/Entity.h>
#include <Swiften/JID/JID.h>
#include <Swiften/Parser/PayloadParsers/FullPayloadParserFactoryCollection.h>
#include <Swiften/Serializer/PayloadSerializers/FullPayloadSerializerCollection.h>

namespace Swift {
    class BasicSessionStream;
    class ComponentSession;
    class IQRouter;
    class NetworkFactories;

    /**
     * The central class for communicating with an XMPP server as a component.
     *
     * This class is responsible for setting up the connection with the XMPP
     * server and authenticating the component.
     *
     * This class can be used directly in your application, although the Component
     * subclass provides more functionality and interfaces, and is better suited
     * for most needs.
     */
    class SWIFTEN_API CoreComponent : public Entity {
        public:
            CoreComponent(const JID& jid, const std::string& secret, NetworkFactories* networkFactories);
            virtual ~CoreComponent();

            void connect(const std::string& host, int port);
            void disconnect();

            void sendMessage(std::shared_ptr<Message>);
            void sendPresence(std::shared_ptr<Presence>);
            void sendData(const std::string& data);

            IQRouter* getIQRouter() const {
                return iqRouter_;
            }

            StanzaChannel* getStanzaChannel() const {
                return stanzaChannel_;
            }

            bool isAvailable() const {
                return stanzaChannel_->isAvailable();
            }

            /**
             * Returns the JID of the component
             */
            const JID& getJID() const {
                return jid_;
            }

        public:
            boost::signals2::signal<void (const ComponentError&)> onError;
            boost::signals2::signal<void ()> onConnected;
            boost::signals2::signal<void (const SafeByteArray&)> onDataRead;
            boost::signals2::signal<void (const SafeByteArray&)> onDataWritten;

            boost::signals2::signal<void (std::shared_ptr<Message>)> onMessageReceived;
            boost::signals2::signal<void (std::shared_ptr<Presence>) > onPresenceReceived;

        private:
            void handleConnectorFinished(std::shared_ptr<Connection>);
            void handleStanzaChannelAvailableChanged(bool available);
            void handleSessionFinished(std::shared_ptr<Error>);
            void handleDataRead(const SafeByteArray&);
            void handleDataWritten(const SafeByteArray&);

        private:
            NetworkFactories* networkFactories;
            JID jid_;
            std::string secret_;
            ComponentSessionStanzaChannel* stanzaChannel_;
            IQRouter* iqRouter_;
            ComponentConnector::ref connector_;
            std::shared_ptr<Connection> connection_;
            std::shared_ptr<BasicSessionStream> sessionStream_;
            std::shared_ptr<ComponentSession> session_;
            bool disconnectRequested_;
    };
}