summaryrefslogtreecommitdiffstats
blob: 23cc6569f36cd4a1469b9477864f5f1c77b2ce99 (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
104
105
106
107
108
109
110
111
112
113
114
115
/*
 * Copyright (c) 2010-2016 Isode Limited.
 * All rights reserved.
 * See the COPYING file for more information.
 */

#pragma once

#include <memory>

#include <boost/optional.hpp>
#include <boost/signals2.hpp>

#include <Swiften/Base/API.h>
#include <Swiften/Base/SafeByteArray.h>
#include <Swiften/Elements/ToplevelElement.h>
#include <Swiften/JID/JID.h>
#include <Swiften/Network/Connection.h>
#include <Swiften/StreamStack/ConnectionLayer.h>

namespace Swift {
    class ProtocolHeader;
    class StreamStack;
    class JID;
    class Element;
    class PayloadParserFactoryCollection;
    class PayloadSerializerCollection;
    class XMPPLayer;
    class XMLParserFactory;

    class SWIFTEN_API Session : public std::enable_shared_from_this<Session> {
        public:
            enum SessionError {
                ConnectionReadError,
                ConnectionWriteError,
                XMLError,
                AuthenticationFailedError,
                NoSupportedAuthMechanismsError,
                UnexpectedElementError,
                ResourceBindError,
                SessionStartError,
                TLSError,
                ClientCertificateLoadError,
                ClientCertificateError
            };

            Session(
                    std::shared_ptr<Connection> connection,
                    PayloadParserFactoryCollection* payloadParserFactories,
                    PayloadSerializerCollection* payloadSerializers,
                    XMLParserFactory* xmlParserFactory);
            virtual ~Session();

            void startSession();
            void finishSession();

            void sendElement(std::shared_ptr<ToplevelElement>);

            const JID& getLocalJID() const {
                return localJID;
            }

            const JID& getRemoteJID() const {
                return remoteJID;
            }

            boost::signals2::signal<void (std::shared_ptr<ToplevelElement>)> onElementReceived;
            boost::signals2::signal<void (const boost::optional<SessionError>&)> onSessionFinished;
            boost::signals2::signal<void (const SafeByteArray&)> onDataWritten;
            boost::signals2::signal<void (const SafeByteArray&)> onDataRead;

        protected:
            void setRemoteJID(const JID& j) {
                remoteJID = j;
            }

            void setLocalJID(const JID& j) {
                localJID = j;
            }

            void finishSession(const SessionError&);

            virtual void handleSessionStarted() {}
            virtual void handleSessionFinished(const boost::optional<SessionError>&) {}
            virtual void handleElement(std::shared_ptr<ToplevelElement>) = 0;
            virtual void handleStreamStart(const ProtocolHeader&) = 0;

            void initializeStreamStack();

            XMPPLayer* getXMPPLayer() const {
                return xmppLayer;
            }

            StreamStack* getStreamStack() const {
                return streamStack;
            }

            void setFinished();

        private:
            void handleDisconnected(const boost::optional<Connection::Error>& error);

        private:
            JID localJID;
            JID remoteJID;
            std::shared_ptr<Connection> connection;
            PayloadParserFactoryCollection* payloadParserFactories;
            PayloadSerializerCollection* payloadSerializers;
            XMLParserFactory* xmlParserFactory;
            XMPPLayer* xmppLayer;
            ConnectionLayer* connectionLayer;
            StreamStack* streamStack;
            bool finishing;
    };
}