diff options
author | dknn <yoann.blein@free.fr> | 2012-07-07 10:08:42 (GMT) |
---|---|---|
committer | dknn <yoann.blein@free.fr> | 2012-09-22 08:55:56 (GMT) |
commit | 62f54e12354e208c8931e069fce235c4ca14d205 (patch) | |
tree | 87e4c16fd4362b0edf2ab3345c64fe28454e8776 /Swiften/Serializer/PayloadSerializers | |
parent | 96d693d51973bd5b2ed47755c8726dc2076f6c53 (diff) | |
download | swift-contrib-62f54e12354e208c8931e069fce235c4ca14d205.zip swift-contrib-62f54e12354e208c8931e069fce235c4ca14d205.tar.bz2 |
Add raw UDP transport payload: element, serializer, parser and tests
Diffstat (limited to 'Swiften/Serializer/PayloadSerializers')
4 files changed, 180 insertions, 0 deletions
diff --git a/Swiften/Serializer/PayloadSerializers/JingleContentPayloadSerializer.cpp b/Swiften/Serializer/PayloadSerializers/JingleContentPayloadSerializer.cpp index c36d574..f2ba1a7 100644 --- a/Swiften/Serializer/PayloadSerializers/JingleContentPayloadSerializer.cpp +++ b/Swiften/Serializer/PayloadSerializers/JingleContentPayloadSerializer.cpp @@ -20,6 +20,7 @@ #include <Swiften/Serializer/PayloadSerializers/JingleIBBTransportPayloadSerializer.h> #include <Swiften/Serializer/PayloadSerializers/JingleS5BTransportPayloadSerializer.h> +#include <Swiften/Serializer/PayloadSerializers/JingleRawUDPTransportPayloadSerializer.h> #include "Swiften/FileTransfer/JingleTransport.h" @@ -58,11 +59,17 @@ std::string JingleContentPayloadSerializer::serializePayload(boost::shared_ptr<J JingleS5BTransportPayloadSerializer s5bSerializer; JingleS5BTransportPayload::ref s5b; + // JingleRawUDPTransportPayload + JingleRawUDPTransportPayloadSerializer rawUdpSerializer; + JingleRawUDPTransportPayload::ref rawUDP; + foreach(JingleTransportPayload::ref transport, payload->getTransports()) { if ((ibb = boost::dynamic_pointer_cast<JingleIBBTransportPayload>(transport))) { payloadXML.addNode(boost::make_shared<XMLRawTextNode>(ibbSerializer.serializePayload(ibb))); } else if ((s5b = boost::dynamic_pointer_cast<JingleS5BTransportPayload>(transport))) { payloadXML.addNode(boost::make_shared<XMLRawTextNode>(s5bSerializer.serializePayload(s5b))); + } else if ((rawUDP = boost::dynamic_pointer_cast<JingleRawUDPTransportPayload>(transport))) { + payloadXML.addNode(boost::make_shared<XMLRawTextNode>(rawUdpSerializer.serializePayload(rawUDP))); } } } diff --git a/Swiften/Serializer/PayloadSerializers/JingleRawUDPTransportPayloadSerializer.cpp b/Swiften/Serializer/PayloadSerializers/JingleRawUDPTransportPayloadSerializer.cpp new file mode 100644 index 0000000..af13003 --- /dev/null +++ b/Swiften/Serializer/PayloadSerializers/JingleRawUDPTransportPayloadSerializer.cpp @@ -0,0 +1,55 @@ +/* + * Copyright (c) 2012 Yoann Blein + * Licensed under the simplified BSD license. + * See Documentation/Licenses/BSD-simplified.txt for more information. + */ + +#include <Swiften/Serializer/PayloadSerializers/JingleRawUDPTransportPayloadSerializer.h> + +#include <boost/smart_ptr/make_shared.hpp> +#include <boost/lexical_cast.hpp> + +#include <Swiften/Base/foreach.h> +#include <Swiften/Serializer/XML/XMLElement.h> + +namespace Swift { + +JingleRawUDPTransportPayloadSerializer::JingleRawUDPTransportPayloadSerializer() { +} + +std::string JingleRawUDPTransportPayloadSerializer::serializePayload(boost::shared_ptr<JingleRawUDPTransportPayload> payload) const { + XMLElement payloadXML("transport", "urn:xmpp:jingle:transports:raw-udp:1"); + + foreach (JingleRawUDPTransportPayload::Candidate candidate, payload->getCandidates()) { + boost::shared_ptr<XMLElement> candidateXML = boost::make_shared<XMLElement>("candidate"); + candidateXML->setAttribute("component", boost::lexical_cast<std::string>(static_cast<int>(candidate.component))); + candidateXML->setAttribute("generation", boost::lexical_cast<std::string>(static_cast<int>(candidate.generation))); + candidateXML->setAttribute("id", candidate.cid); + candidateXML->setAttribute("ip", candidate.hostAddressPort.getAddress().toString()); + candidateXML->setAttribute("port", boost::lexical_cast<std::string>(candidate.hostAddressPort.getPort())); + if (candidate.type != JingleRawUDPTransportPayload::Candidate::Unknown) + candidateXML->setAttribute("type", typeToString(candidate.type)); + payloadXML.addNode(candidateXML); + } + + return payloadXML.serialize(); +} + +std::string JingleRawUDPTransportPayloadSerializer::typeToString(JingleRawUDPTransportPayload::Candidate::Type type) const { + switch (type) { + case JingleRawUDPTransportPayload::Candidate::Unknown: + return ""; + case JingleRawUDPTransportPayload::Candidate::Host: + return "host"; + case JingleRawUDPTransportPayload::Candidate::PeerReflected: + return "prflx"; + case JingleRawUDPTransportPayload::Candidate::Relayed: + return "relay"; + case JingleRawUDPTransportPayload::Candidate::ServerReflected: + return "srflx"; + } + assert(false); + return ""; +} + +} diff --git a/Swiften/Serializer/PayloadSerializers/JingleRawUDPTransportPayloadSerializer.h b/Swiften/Serializer/PayloadSerializers/JingleRawUDPTransportPayloadSerializer.h new file mode 100644 index 0000000..5479d1d --- /dev/null +++ b/Swiften/Serializer/PayloadSerializers/JingleRawUDPTransportPayloadSerializer.h @@ -0,0 +1,23 @@ +/* + * Copyright (c) 2012 Yoann Blein + * Licensed under the GNU General Public License v3. + * See Documentation/Licenses/GPLv3.txt for more information. + */ + +#pragma once + +#include <Swiften/Serializer/GenericPayloadSerializer.h> +#include <Swiften/Elements/JingleRawUDPTransportPayload.h> + +namespace Swift { + class JingleRawUDPTransportPayloadSerializer : public GenericPayloadSerializer<JingleRawUDPTransportPayload> { + public: + JingleRawUDPTransportPayloadSerializer(); + + virtual std::string serializePayload(boost::shared_ptr<JingleRawUDPTransportPayload>) const; + + private: + std::string typeToString(JingleRawUDPTransportPayload::Candidate::Type) const; + + }; +} diff --git a/Swiften/Serializer/PayloadSerializers/UnitTest/JingleSerializersTest.cpp b/Swiften/Serializer/PayloadSerializers/UnitTest/JingleSerializersTest.cpp index a89c91c..4a74a24 100644 --- a/Swiften/Serializer/PayloadSerializers/UnitTest/JingleSerializersTest.cpp +++ b/Swiften/Serializer/PayloadSerializers/UnitTest/JingleSerializersTest.cpp @@ -16,6 +16,7 @@ #include <Swiften/Serializer/PayloadSerializers/FullPayloadSerializerCollection.h> #include <Swiften/Serializer/PayloadSerializers/RTPPayloadTypeSerializer.h> #include <Swiften/Serializer/PayloadSerializers/JingleRTPDescriptionSerializer.h> +#include <Swiften/Serializer/PayloadSerializers/JingleRawUDPTransportPayloadSerializer.h> #include <Swiften/Elements/JingleFileTransferDescription.h> #include <Swiften/Elements/StreamInitiationFileInfo.h> #include <Swiften/Elements/JingleIBBTransportPayload.h> @@ -25,6 +26,7 @@ #include <Swiften/Elements/JingleFileTransferReceived.h> #include <Swiften/Elements/RTPPayloadType.h> #include <Swiften/Elements/JingleRTPDescription.h> +#include <Swiften/Elements/JingleRawUDPTransportPayload.h> #include <Swiften/Base/DateTime.h> using namespace Swift; @@ -56,6 +58,10 @@ class JingleSerializersTest : public CppUnit::TestFixture { CPPUNIT_TEST(testSerialize_Xep0167_Example1); CPPUNIT_TEST(testSerialize_Xep0167_Example48); + CPPUNIT_TEST(testSerialize_RawUDPTransportPayload); + + CPPUNIT_TEST(testSerialize_Xep0177_Example3); + CPPUNIT_TEST_SUITE_END(); boost::shared_ptr<JinglePayloadSerializer> createTestling() { @@ -660,6 +666,95 @@ class JingleSerializersTest : public CppUnit::TestFixture { CPPUNIT_ASSERT_EQUAL(expected, createTestling()->serialize(payload)); } + void testSerialize_RawUDPTransportPayload() { + std::string expected = + "<transport xmlns=\"urn:xmpp:jingle:transports:raw-udp:1\">" + "<candidate component=\"1\"" + " generation=\"0\"" + " id=\"a9j3mnbtu1\"" + " ip=\"10.1.1.104\"" + " port=\"13540\"" + " type=\"srflx\"/>" + "</transport>"; + + JingleRawUDPTransportPayload::ref transport = boost::make_shared<JingleRawUDPTransportPayload>(); + + JingleRawUDPTransportPayload::Candidate candidate; + candidate.component = 1; + candidate.generation = 0; + candidate.cid = "a9j3mnbtu1"; + candidate.hostAddressPort = HostAddressPort(HostAddress("10.1.1.104"), 13540); + candidate.type = JingleRawUDPTransportPayload::Candidate::ServerReflected; + transport->addCandidate(candidate); + + boost::shared_ptr<JingleRawUDPTransportPayloadSerializer> serializer = boost::make_shared<JingleRawUDPTransportPayloadSerializer>(); + CPPUNIT_ASSERT_EQUAL(expected, serializer->serialize(transport)); + } + + // http://xmpp.org/extensions/xep-0177.html#example-3 + void testSerialize_Xep0177_Example3() { + std::string expected = + "<jingle action=\"session-accept\"" + " initiator=\"romeo@montague.lit/orchard\"" + " responder=\"juliet@capulet.lit/balcony\"" + " sid=\"a73sjjvkla37jfea\"" + " xmlns=\"urn:xmpp:jingle:1\">" + "<content creator=\"initiator\" name=\"voice\">" + "<description media=\"audio\" xmlns=\"urn:xmpp:jingle:apps:rtp:1\">" + "<payload-type id=\"18\" name=\"G729\"/>" + "</description>" + "<transport xmlns=\"urn:xmpp:jingle:transports:raw-udp:1\">" + "<candidate component=\"1\"" + " generation=\"0\"" + " id=\"z7sdjb01hf\"" + " ip=\"208.68.163.214\"" + " port=\"9876\"/>" + "<candidate component=\"2\"" + " generation=\"0\"" + " id=\"hg92lsn10b\"" + " ip=\"208.68.163.214\"" + " port=\"9877\"/>" + "</transport>" + "</content>" + "</jingle>"; + + JinglePayload::ref payload = boost::make_shared<JinglePayload>(); + payload->setAction(JinglePayload::SessionAccept); + payload->setInitiator(JID("romeo@montague.lit/orchard")); + payload->setResponder(JID("juliet@capulet.lit/balcony")); + payload->setSessionID("a73sjjvkla37jfea"); + + JingleRTPDescription::ref desc = boost::make_shared<JingleRTPDescription>(JingleRTPDescription::Audio); + RTPPayloadType payloadType(18, "G729"); + desc->addPayloadType(payloadType); + + JingleRawUDPTransportPayload::ref transport = boost::make_shared<JingleRawUDPTransportPayload>(); + + JingleRawUDPTransportPayload::Candidate candidate1; + candidate1.component = 1; + candidate1.generation = 0; + candidate1.cid = "z7sdjb01hf"; + candidate1.hostAddressPort = HostAddressPort(HostAddress("208.68.163.214"), 9876); + transport->addCandidate(candidate1); + + JingleRawUDPTransportPayload::Candidate candidate2; + candidate2.component = 2; + candidate2.generation = 0; + candidate2.cid = "hg92lsn10b"; + candidate2.hostAddressPort = HostAddressPort(HostAddress("208.68.163.214"), 9877); + transport->addCandidate(candidate2); + + JingleContentPayload::ref content = boost::make_shared<JingleContentPayload>(); + content->setCreator(JingleContentPayload::InitiatorCreator); + content->setName("voice"); + content->addDescription(desc); + content->addTransport(transport); + + payload->addPayload(content); + + CPPUNIT_ASSERT_EQUAL(expected, createTestling()->serialize(payload)); + } + private: FullPayloadSerializerCollection collection; }; |