summaryrefslogtreecommitdiffstats
blob: cb1e098247cc6a901c1f199e87a2dece682b44ea (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
#pragma once

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

#include "Swiften/Session/Session.h"
#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 ClientSession : public Session {
		public:
			enum State {
				Initial,
				WaitingForStreamStart,
				Negotiating,
				Compressing,
				Encrypting,
				WaitingForCredentials,
				Authenticating,
				BindingResource,
				StartingSession,
				SessionStarted,
				Error,
				Finished
			};

			ClientSession(
					const JID& jid, 
					boost::shared_ptr<Connection>, 
					TLSLayerFactory*, 
					PayloadParserFactoryCollection*, 
					PayloadSerializerCollection*);

			State getState() const {
				return state_;
			}

			boost::optional<SessionError> getError() const {
				return error_;
			}

			void sendCredentials(const String& password);
			void setCertificate(const PKCS12Certificate& certificate);

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

			virtual void handleSessionStarted();
			virtual void handleSessionFinished(const boost::optional<SessionError>& error);
			virtual void handleElement(boost::shared_ptr<Element>);
			virtual void handleStreamStart(const ProtocolHeader&);

			void handleTLSConnected();
			void handleTLSError();

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

		public:
			boost::signal<void ()> onNeedCredentials;
		
		private:
			TLSLayerFactory* tlsLayerFactory_;
			State state_;
			boost::optional<SessionError> error_;
			boost::shared_ptr<TLSLayer> tlsLayer_;
			boost::shared_ptr<WhitespacePingLayer> whitespacePingLayer_;
			bool needSessionStart_;
			PKCS12Certificate certificate_;
	};
}