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

#include "Swiften/StreamStack/XMPPLayer.h"
#include "Swiften/Parser/XMPPParser.h"
#include "Swiften/Serializer/XMPPSerializer.h"
#include "Swiften/Elements/ProtocolHeader.h"

namespace Swift {

XMPPLayer::XMPPLayer(
		PayloadParserFactoryCollection* payloadParserFactories,
		PayloadSerializerCollection* payloadSerializers,
		StreamType streamType) :
			payloadParserFactories_(payloadParserFactories), 
			payloadSerializers_(payloadSerializers),
			resetParserAfterParse_(false),
			inParser_(false) {
	xmppParser_ = new XMPPParser(this, payloadParserFactories_);
	xmppSerializer_ = new XMPPSerializer(payloadSerializers_, streamType);
}

XMPPLayer::~XMPPLayer() {
	delete xmppSerializer_;
	delete xmppParser_;
}

void XMPPLayer::writeHeader(const ProtocolHeader& header) {
	writeDataInternal(ByteArray(xmppSerializer_->serializeHeader(header)));
}

void XMPPLayer::writeFooter() {
	writeDataInternal(ByteArray(xmppSerializer_->serializeFooter()));
}

void XMPPLayer::writeElement(boost::shared_ptr<Element> element) {
	writeDataInternal(ByteArray(xmppSerializer_->serializeElement(element)));
}

void XMPPLayer::writeData(const String& data) {
	writeDataInternal(ByteArray(data));
}

void XMPPLayer::writeDataInternal(const ByteArray& data) {
	onWriteData(data);
	writeDataToChildLayer(data);
}

void XMPPLayer::handleDataRead(const ByteArray& data) {
	onDataRead(data);
	inParser_ = true;
	if (!xmppParser_->parse(String(data.getData(), data.getSize()))) {
		inParser_ = false;
		onError();
		return;
	}
	inParser_ = false;
	if (resetParserAfterParse_) {
		doResetParser();
	}
}

void XMPPLayer::doResetParser() {
	delete xmppParser_;
	xmppParser_ = new XMPPParser(this, payloadParserFactories_);
	resetParserAfterParse_ = false;
}

void XMPPLayer::handleStreamStart(const ProtocolHeader& header) {
	onStreamStart(header);
}

void XMPPLayer::handleElement(boost::shared_ptr<Element> stanza) {
	onElement(stanza);
}

void XMPPLayer::handleStreamEnd() {
}

void XMPPLayer::resetParser() {
	if (inParser_) {
		resetParserAfterParse_ = true;
	}
	else {
		doResetParser();
	}
}

}