summaryrefslogtreecommitdiffstats
blob: 40bb391b589775194bfa8dd018b537027046779e (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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
/*
 * Copyright (c) 2010-2014 Isode Limited.
 * All rights reserved.
 * See the COPYING file for more information.
 */
/*
 * Copyright (c) 2015 Tarun Gupta.
 * Licensed under the simplified BSD license.
 * See Documentation/Licenses/BSD-simplified.txt for more information.
 */

package com.isode.stroke.component;

import com.isode.stroke.jid.JID;
import com.isode.stroke.elements.Stanza;
import com.isode.stroke.elements.Element;
import com.isode.stroke.elements.ProtocolHeader;
import com.isode.stroke.elements.ComponentHandshake;
import com.isode.stroke.elements.StreamFeatures;
import com.isode.stroke.session.SessionStream;
import com.isode.stroke.crypto.CryptoProvider;
import com.isode.stroke.crypto.JavaCryptoProvider;
import com.isode.stroke.signals.Signal1;
import com.isode.stroke.signals.Signal;
import com.isode.stroke.signals.SignalConnection;
import com.isode.stroke.signals.Slot1;

public class ComponentSession {

	public enum State {
		Initial,
		WaitingForStreamStart,
		Authenticating,
		Initialized,
		Finishing,
		Finished
	};

	public static class Error implements com.isode.stroke.base.Error {
		public enum Type {
			AuthenticationFailedError,
			UnexpectedElementError
		}
		public Type type;
		public Error(Type type) {
			if (type == null) {
				throw new IllegalStateException();
			}
			this.type = type;
		}
	};

	private JID jid = new JID();
	private String secret = "";
	private SessionStream stream;
	private CryptoProvider crypto;
	private com.isode.stroke.base.Error error;
	private State state;
	public final Signal onInitialized = new Signal();
	public final Signal1<com.isode.stroke.base.Error> onFinished = new Signal1<com.isode.stroke.base.Error>();
	public final Signal1<Stanza> onStanzaReceived = new Signal1<Stanza>();
	private SignalConnection onStreamStartReceivedConnection;
	private SignalConnection onElementReceivedConnection;
	private SignalConnection onClosedConnection;
	
	public static ComponentSession create(final JID jid, final String secret, SessionStream stream, CryptoProvider crypto) {
		return new ComponentSession(jid, secret, stream, crypto);
	}

	public State getState() {
		return state;
	}

	public void start() {
		onStreamStartReceivedConnection = stream.onStreamStartReceived.connect(new Slot1<ProtocolHeader>() {
			@Override
			public void call(ProtocolHeader p1) {
				handleStreamStart(p1);
			}
		});
		onElementReceivedConnection = stream.onElementReceived.connect(new Slot1<Element>() {
			@Override
			public void call(Element e1) {
				handleElement(e1);
			}
		});
		onClosedConnection = stream.onClosed.connect(new Slot1<SessionStream.Error>() {
			@Override
			public void call(SessionStream.Error e1) {
				handleStreamClosed(e1);
			}
		});

		assert(State.Initial.equals(state));
		state = State.WaitingForStreamStart;
		sendStreamHeader();
	}

	public void finish() {
		finishSession((Error.Type)null);
	}

	public void sendStanza(Stanza stanza) {
		stream.writeElement(stanza);
	}

	private ComponentSession(final JID jid, final String secret, SessionStream stream, CryptoProvider crypto) {
		this.jid = jid;
		this.secret = secret;
		this.stream = stream;
		this.crypto = crypto;
		this.state = State.Initial;
	}

	private void finishSession(Error.Type error) {
		Error localError = null;
		if (error != null) {
			localError = new Error(error);
		}
		finishSession(localError);
	}

	private void finishSession(com.isode.stroke.base.Error finishError) {
		state = State.Finishing;
		error = finishError;
		assert(stream.isOpen() == true);
		stream.writeFooter();
		stream.close();
	}

	private void sendStreamHeader() {
		ProtocolHeader header = new ProtocolHeader();
		header.setTo(jid.toString());
		stream.writeHeader(header);
	}

	private void handleElement(Element element) {
		if(element instanceof Stanza) {
			Stanza stanza = (Stanza)element;
			if (State.Initialized.equals(getState())) {
				onStanzaReceived.emit(stanza);
			}
			else {
				finishSession(Error.Type.UnexpectedElementError);
			}
		}
		else if (element instanceof ComponentHandshake) {
			if (!checkState(State.Authenticating)) {
				return;
			}
			stream.setWhitespacePingEnabled(true);
			state = State.Initialized;
			onInitialized.emit();
		}
		else if (State.Authenticating.equals(getState())) {
			if (element instanceof StreamFeatures) {
				// M-Link sends stream features, so swallow that.
			}
			else {
				// FIXME: We should actually check the element received
				finishSession(Error.Type.AuthenticationFailedError);
			}
		}
		else {
			finishSession(Error.Type.UnexpectedElementError);
		}
	}

	private void handleStreamStart(final ProtocolHeader header) {
		checkState(State.WaitingForStreamStart);
		state = State.Authenticating;
		stream.writeElement(new ComponentHandshake(ComponentHandshakeGenerator.getHandshake(header.getID(), secret, crypto)));
	}

	private void handleStreamClosed(SessionStream.Error streamError) {
		State oldState = state;
		state = State.Finished;
		stream.setWhitespacePingEnabled(false);
		onStreamStartReceivedConnection.disconnect();
		onElementReceivedConnection.disconnect();
		onClosedConnection.disconnect();
		if (State.Finishing.equals(oldState)) {
			onFinished.emit(error);
		}
		else {
			onFinished.emit(streamError);
		}
	}

	private boolean checkState(State state) {
		if (!(this.state.equals(state))) {
			finishSession(Error.Type.UnexpectedElementError);
			return false;
		}
		return true;
	}
}