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

#include <Swiften/Component/CoreComponent.h>

#include <boost/bind.hpp>
#include <iostream>

#include <Swiften/Component/ComponentSession.h>
#include <Swiften/Network/Connector.h>
#include <Swiften/Network/NetworkFactories.h>
#include <Swiften/TLS/PKCS12Certificate.h>
#include <Swiften/Session/BasicSessionStream.h>
#include <Swiften/Queries/IQRouter.h>
#include <Swiften/Base/IDGenerator.h>
#include <Swiften/Component/ComponentSessionStanzaChannel.h>

namespace Swift {

CoreComponent::CoreComponent(EventLoop* eventLoop, NetworkFactories* networkFactories, const JID& jid, const std::string& secret) : eventLoop(eventLoop), networkFactories(networkFactories), resolver_(eventLoop), jid_(jid), secret_(secret), disconnectRequested_(false) {
	stanzaChannel_ = new ComponentSessionStanzaChannel();
	stanzaChannel_->onMessageReceived.connect(boost::ref(onMessageReceived));
	stanzaChannel_->onPresenceReceived.connect(boost::ref(onPresenceReceived));
	stanzaChannel_->onAvailableChanged.connect(boost::bind(&CoreComponent::handleStanzaChannelAvailableChanged, this, _1));

	iqRouter_ = new IQRouter(stanzaChannel_);
	iqRouter_->setFrom(jid);
}

CoreComponent::~CoreComponent() {
	if (session_ || connection_) {
		std::cerr << "Warning: Component not disconnected properly" << std::endl;
	}
	delete iqRouter_;

	stanzaChannel_->onAvailableChanged.disconnect(boost::bind(&CoreComponent::handleStanzaChannelAvailableChanged, this, _1));
	stanzaChannel_->onMessageReceived.disconnect(boost::ref(onMessageReceived));
	stanzaChannel_->onPresenceReceived.disconnect(boost::ref(onPresenceReceived));
	delete stanzaChannel_;
}

void CoreComponent::connect(const std::string& host, int port) {
	assert(!connector_);
	connector_ = ComponentConnector::create(host, port, &resolver_, networkFactories->getConnectionFactory(), networkFactories->getTimerFactory());
	connector_->onConnectFinished.connect(boost::bind(&CoreComponent::handleConnectorFinished, this, _1));
	connector_->setTimeoutMilliseconds(60*1000);
	connector_->start();
}

void CoreComponent::handleConnectorFinished(boost::shared_ptr<Connection> connection) {
	connector_->onConnectFinished.disconnect(boost::bind(&CoreComponent::handleConnectorFinished, this, _1));
	connector_.reset();
	if (!connection) {
		if (!disconnectRequested_) {
			onError(ComponentError::ConnectionError);
		}
	}
	else {
		assert(!connection_);
		connection_ = connection;

		assert(!sessionStream_);
		sessionStream_ = boost::shared_ptr<BasicSessionStream>(new BasicSessionStream(ComponentStreamType, connection_, getPayloadParserFactories(), getPayloadSerializers(), NULL, networkFactories->getTimerFactory()));
		sessionStream_->onDataRead.connect(boost::bind(&CoreComponent::handleDataRead, this, _1));
		sessionStream_->onDataWritten.connect(boost::bind(&CoreComponent::handleDataWritten, this, _1));

		session_ = ComponentSession::create(jid_, secret_, sessionStream_);
		stanzaChannel_->setSession(session_);
		session_->onFinished.connect(boost::bind(&CoreComponent::handleSessionFinished, this, _1));
		session_->start();
	}
}

void CoreComponent::disconnect() {
	// FIXME: We should be able to do without this boolean. We just have to make sure we can tell the difference between
	// connector finishing without a connection due to an error or because of a disconnect.
	disconnectRequested_ = true;
	if (session_) {
		session_->finish();
	}
	else if (connector_) {
		connector_->stop();
		assert(!session_);
	}
	assert(!session_);
	assert(!sessionStream_);
	assert(!connector_);
	disconnectRequested_ = false;
}

void CoreComponent::handleSessionFinished(boost::shared_ptr<Error> error) {
	session_->onFinished.disconnect(boost::bind(&CoreComponent::handleSessionFinished, this, _1));
	session_.reset();

	sessionStream_->onDataRead.disconnect(boost::bind(&CoreComponent::handleDataRead, this, _1));
	sessionStream_->onDataWritten.disconnect(boost::bind(&CoreComponent::handleDataWritten, this, _1));
	sessionStream_.reset();

	connection_->disconnect();
	connection_.reset();

	if (error) {
		ComponentError componentError;
		if (boost::shared_ptr<ComponentSession::Error> actualError = boost::dynamic_pointer_cast<ComponentSession::Error>(error)) {
			switch(actualError->type) {
				case ComponentSession::Error::AuthenticationFailedError:
					componentError = ComponentError(ComponentError::AuthenticationFailedError);
					break;
				case ComponentSession::Error::UnexpectedElementError:
					componentError = ComponentError(ComponentError::UnexpectedElementError);
					break;
			}
		}
		else if (boost::shared_ptr<SessionStream::Error> actualError = boost::dynamic_pointer_cast<SessionStream::Error>(error)) {
			switch(actualError->type) {
				case SessionStream::Error::ParseError:
					componentError = ComponentError(ComponentError::XMLError);
					break;
				case SessionStream::Error::TLSError:
					assert(false);
					componentError = ComponentError(ComponentError::UnknownError);
					break;
				case SessionStream::Error::InvalidTLSCertificateError:
					assert(false);
					componentError = ComponentError(ComponentError::UnknownError);
					break;
				case SessionStream::Error::ConnectionReadError:
					componentError = ComponentError(ComponentError::ConnectionReadError);
					break;
				case SessionStream::Error::ConnectionWriteError:
					componentError = ComponentError(ComponentError::ConnectionWriteError);
					break;
			}
		}
		onError(componentError);
	}
}

void CoreComponent::handleDataRead(const SafeByteArray& data) {
	onDataRead(data);
}

void CoreComponent::handleDataWritten(const SafeByteArray& data) {
	onDataWritten(data);
}

void CoreComponent::handleStanzaChannelAvailableChanged(bool available) {
	if (available) {
		onConnected();
	}
}

void CoreComponent::sendMessage(boost::shared_ptr<Message> message) {
	stanzaChannel_->sendMessage(message);
}

void CoreComponent::sendPresence(boost::shared_ptr<Presence> presence) {
	stanzaChannel_->sendPresence(presence);
}

}