summaryrefslogtreecommitdiffstats
blob: 17c10b9caa26c53008807dec9b5ef2cdb4515544 (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
116
117
118
119
120
121
122
123
124
125
126
127
#ifndef SWIFTEN_Session_H
#define SWIFTEN_Session_H

#include <boost/signal.hpp>
#include <boost/shared_ptr.hpp>

#include "Swiften/Base/String.h"
#include "Swiften/JID/JID.h"
#include "Swiften/Elements/Element.h"
#include "Swiften/Network/Connection.h"
#include "Swiften/TLS/PKCS12Certificate.h"

namespace Swift {
	class PayloadParserFactoryCollection;
	class PayloadSerializerCollection;
	class ConnectionFactory;
	class Connection;
	class StreamStack;
	class XMPPLayer;
	class ConnectionLayer;
	class TLSLayerFactory;
	class TLSLayer;
	class WhitespacePingLayer;

	class Session {
		public:
			enum State {
				Initial,
				Connecting,
				WaitingForStreamStart,
				Negotiating,
				Compressing,
				Encrypting,
				WaitingForCredentials,
				Authenticating,
				BindingResource,
				StartingSession,
				SessionStarted,
				Error
			};
			enum SessionError {
				NoError,
				DomainNameResolveError,
				ConnectionError,
				ConnectionReadError,
				ConnectionWriteError,
				XMLError,
				AuthenticationFailedError,
				NoSupportedAuthMechanismsError,
				UnexpectedElementError,
				ResourceBindError,
				SessionStartError,
				TLSError,
				ClientCertificateLoadError,
				ClientCertificateError
			};

			Session(const JID& jid, ConnectionFactory*, TLSLayerFactory*, PayloadParserFactoryCollection*, PayloadSerializerCollection*);
			~Session();

			State getState() const {
				return state_;
			}

			SessionError getError() const {
				return error_;
			}

			const JID& getJID() const {
				return jid_;
			}

			void start();
			void stop();
			void sendCredentials(const String& password);
			void sendElement(boost::shared_ptr<Element>);
			void setCertificate(const PKCS12Certificate& certificate);

		protected:
			StreamStack* getStreamStack() const {
				return streamStack_;
			}

		private:
			void initializeStreamStack();
			void sendStreamHeader();
			void sendSessionStart();

			void handleConnected();
			void handleDisconnected(const boost::optional<Connection::Error>&);
			void handleElement(boost::shared_ptr<Element>);
			void handleStreamStart();
			void handleTLSConnected();
			void handleTLSError();

			void setError(SessionError);
			bool checkState(State);

		public:
			boost::signal<void ()> onSessionStarted;
			boost::signal<void (SessionError)> onError;
			boost::signal<void ()> onNeedCredentials;
			boost::signal<void (boost::shared_ptr<Element>) > onElementReceived;
			boost::signal<void (const ByteArray&)> onDataWritten;
			boost::signal<void (const ByteArray&)> onDataRead;
		
		private:
			JID jid_;
			ConnectionFactory* connectionFactory_;
			TLSLayerFactory* tlsLayerFactory_;
			PayloadParserFactoryCollection* payloadParserFactories_;
			PayloadSerializerCollection* payloadSerializers_;
			State state_;
			SessionError error_;
			boost::shared_ptr<Connection> connection_;
			boost::shared_ptr<XMPPLayer> xmppLayer_;
			boost::shared_ptr<TLSLayer> tlsLayer_;
			boost::shared_ptr<ConnectionLayer> connectionLayer_;
			boost::shared_ptr<WhitespacePingLayer> whitespacePingLayer_;
			StreamStack* streamStack_;
			bool needSessionStart_;
			PKCS12Certificate certificate_;
	};

}

#endif