summaryrefslogtreecommitdiffstats
blob: 46d4e163bce038b883afbedd7cfb5156933853b7 (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
// TODO: whitespacePingLayer_->setInactive();

#include "Swiften/Session/BasicSessionStream.h"

#include <boost/bind.hpp>

#include "Swiften/StreamStack/XMPPLayer.h"
#include "Swiften/StreamStack/StreamStack.h"
#include "Swiften/StreamStack/ConnectionLayer.h"
#include "Swiften/StreamStack/WhitespacePingLayer.h"
#include "Swiften/StreamStack/TLSLayer.h"
#include "Swiften/StreamStack/TLSLayerFactory.h"

namespace Swift {

BasicSessionStream::BasicSessionStream(boost::shared_ptr<Connection> connection, PayloadParserFactoryCollection* payloadParserFactories, PayloadSerializerCollection* payloadSerializers, TLSLayerFactory* tlsLayerFactory) : tlsLayerFactory(tlsLayerFactory) {
	xmppLayer = boost::shared_ptr<XMPPLayer>(
			new XMPPLayer(payloadParserFactories, payloadSerializers));
	xmppLayer->onStreamStart.connect(boost::ref(onStreamStartReceived));
	xmppLayer->onElement.connect(boost::ref(onElementReceived));
	xmppLayer->onError.connect(boost::bind(
      &BasicSessionStream::handleXMPPError, this));

	connectionLayer = boost::shared_ptr<ConnectionLayer>(
      new ConnectionLayer(connection));

	streamStack = new StreamStack(xmppLayer, connectionLayer);
}

BasicSessionStream::~BasicSessionStream() {
  delete streamStack;
}

void BasicSessionStream::writeHeader(const ProtocolHeader& header) {
	xmppLayer->writeHeader(header);
}

void BasicSessionStream::writeElement(boost::shared_ptr<Element> element) {
	xmppLayer->writeElement(element);
}

bool BasicSessionStream::supportsTLSEncryption() {
  return tlsLayerFactory && tlsLayerFactory->canCreate();
}

void BasicSessionStream::addTLSEncryption() {
	tlsLayer = tlsLayerFactory->createTLSLayer();
  streamStack->addLayer(tlsLayer);
  // TODO: Add tls layer certificate if needed
  tlsLayer->onError.connect(boost::bind(&BasicSessionStream::handleTLSError, this));
  tlsLayer->connect();
}

void BasicSessionStream::addWhitespacePing() {
  whitespacePingLayer = boost::shared_ptr<WhitespacePingLayer>(new WhitespacePingLayer());
  streamStack->addLayer(whitespacePingLayer);
  whitespacePingLayer->setActive();
}

void BasicSessionStream::resetXMPPParser() {
  xmppLayer->resetParser();
}

void BasicSessionStream::handleXMPPError() {
  // TODO
}

void BasicSessionStream::handleTLSError() {
  // TODO
}

};