summaryrefslogtreecommitdiffstats
blob: 5261bf23eb421d8f20bc15f7fde9968211d3c043 (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
/*
 * Copyright (c) 2010 Remko Tronçon
 * Licensed under the GNU General Public License v3.
 * See Documentation/Licenses/GPLv3.txt for more information.
 */

#pragma once

#include <boost/shared_ptr.hpp>
#include "Swiften/Base/boost_signalslib.h"
#include <boost/optional.hpp>
#include <boost/enable_shared_from_this.hpp>

#include "Swiften/JID/JID.h"
#include "Swiften/Elements/Element.h"
#include "Swiften/Network/Connection.h"
#include "Swiften/StreamStack/ConnectionLayer.h"

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

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

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

			void startSession();
			void finishSession();

			void sendElement(boost::shared_ptr<Element>);

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

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

			boost::signal<void (boost::shared_ptr<Element>)> onElementReceived;
			boost::signal<void (const boost::optional<SessionError>&)> onSessionFinished;
			boost::signal<void (const ByteArray&)> onDataWritten;
			boost::signal<void (const ByteArray&)> 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(boost::shared_ptr<Element>) = 0;
			virtual void handleStreamStart(const ProtocolHeader&) = 0;

			void initializeStreamStack();

			boost::shared_ptr<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;
			boost::shared_ptr<Connection> connection;
			PayloadParserFactoryCollection* payloadParserFactories;
			PayloadSerializerCollection* payloadSerializers;
			boost::shared_ptr<XMPPLayer> xmppLayer;
			boost::shared_ptr<ConnectionLayer> connectionLayer;
			StreamStack* streamStack;
			bool finishing;
	};
}