From a49ca97a7b2ad61385d0f6f22ec598a63d889ae7 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Remko=20Tron=C3=A7on?= <git@el-tramo.be>
Date: Fri, 15 Oct 2010 22:40:51 +0200
Subject: Added EchoComponent.


diff --git a/Documentation/SwiftenDevelopersGuide/Examples/EchoBot/.gitignore b/Documentation/SwiftenDevelopersGuide/Examples/EchoBot/.gitignore
index 28275ad..81f2be3 100644
--- a/Documentation/SwiftenDevelopersGuide/Examples/EchoBot/.gitignore
+++ b/Documentation/SwiftenDevelopersGuide/Examples/EchoBot/.gitignore
@@ -1,2 +1,3 @@
 EchoBot?
 *.cpp.xml
+EchoComponent
diff --git a/Documentation/SwiftenDevelopersGuide/Examples/EchoBot/EchoComponent.cpp b/Documentation/SwiftenDevelopersGuide/Examples/EchoBot/EchoComponent.cpp
new file mode 100644
index 0000000..67f469d
--- /dev/null
+++ b/Documentation/SwiftenDevelopersGuide/Examples/EchoBot/EchoComponent.cpp
@@ -0,0 +1,64 @@
+/*
+ * Copyright (c) 2010 Remko Tronçon
+ * Licensed under the GNU General Public License v3.
+ * See Documentation/Licenses/GPLv3.txt for more information.
+ */
+
+#include <iostream>
+#include <boost/bind.hpp>
+
+#include "Swiften/Swiften.h"
+
+using namespace Swift;
+using namespace boost;
+
+class EchoComponent {
+	public:
+		EchoComponent() {
+			component = new Component(JID("echo.wonderland.lit"), "EchoSecret");
+			component->onConnected.connect(bind(&EchoComponent::handleConnected, this));
+			component->onMessageReceived.connect(
+					bind(&EchoComponent::handleMessageReceived, this, _1));
+			component->onPresenceReceived.connect(
+					bind(&EchoComponent::handlePresenceReceived, this, _1));
+			tracer = new ComponentXMLTracer(component);
+			component->connect("wonderland.lit", 5347);
+		}
+
+		~EchoComponent() {
+			delete tracer;
+			delete component;
+		}
+	
+	private:
+		void handlePresenceReceived(Presence::ref presence) {
+			// Automatically approve subscription requests
+			if (presence->getType() == Presence::Subscribe) {
+				Presence::ref response = Presence::create();
+				response->setTo(presence->getFrom());
+				response->setType(Presence::Subscribed);
+				component->sendPresence(response);
+			}
+		}
+
+		void handleConnected() {
+		}
+
+		void handleMessageReceived(Message::ref message) {
+			// Echo back the incoming message
+			message->setTo(message->getFrom());
+			message->setFrom(JID());
+			component->sendMessage(message);
+		}
+
+	private:
+		Component* component;
+		ComponentXMLTracer* tracer;
+};
+
+int main(int, char**) {
+	SimpleEventLoop eventLoop;
+	EchoComponent bot;
+	eventLoop.run();
+	return 0;
+}
diff --git a/Documentation/SwiftenDevelopersGuide/Examples/EchoBot/SConscript b/Documentation/SwiftenDevelopersGuide/Examples/EchoBot/SConscript
index ceead6b..6a3bcb4 100644
--- a/Documentation/SwiftenDevelopersGuide/Examples/EchoBot/SConscript
+++ b/Documentation/SwiftenDevelopersGuide/Examples/EchoBot/SConscript
@@ -13,3 +13,4 @@ example_env.MergeFlags(example_env["PLATFORM_FLAGS"])
 
 for i in range(1,6) :
 	example_env.Program("EchoBot" + str(i), ["EchoBot" + str(i) + ".cpp"])
+example_env.Program("EchoComponent", "EchoComponent.cpp")
diff --git a/Swiften/Component/ComponentXMLTracer.h b/Swiften/Component/ComponentXMLTracer.h
new file mode 100644
index 0000000..512e69c
--- /dev/null
+++ b/Swiften/Component/ComponentXMLTracer.h
@@ -0,0 +1,34 @@
+/*
+ * Copyright (c) 2010 Remko Tronçon
+ * Licensed under the GNU General Public License v3.
+ * See Documentation/Licenses/GPLv3.txt for more information.
+ */
+
+#pragma once
+
+#include <boost/bind.hpp>
+
+#include "Swiften/Component/Component.h"
+
+namespace Swift {
+	class ComponentXMLTracer {
+		public:
+			ComponentXMLTracer(Component* component) {
+				component->onDataRead.connect(boost::bind(&ComponentXMLTracer::printData, '<', _1));
+				component->onDataWritten.connect(boost::bind(&ComponentXMLTracer::printData, '>', _1));
+			}
+
+		private:
+			static void printData(char direction, const String& data) {
+				printLine(direction);
+				std::cerr << data << std::endl;
+			}
+
+			static void printLine(char c) {
+				for (unsigned int i = 0; i < 80; ++i) {
+					std::cerr << c;
+				}
+				std::cerr << std::endl;
+			}
+	};
+}
diff --git a/Swiften/SConscript b/Swiften/SConscript
index ca4f91a..fb79963 100644
--- a/Swiften/SConscript
+++ b/Swiften/SConscript
@@ -257,7 +257,7 @@ if env["SCONS_STAGE"] == "build" :
 	# Generate the Swiften header
 	swiften_header = "#pragma once\n"
 	top_path = env.Dir("..").abspath
-	public_dirs = ["Queries", "Client", "Elements"]
+	public_dirs = ["Queries", "Client", "Component", "Elements"]
 	for public_dir in public_dirs :
 		for root, dirs, files in os.walk(env.Dir(public_dir).abspath) :
 			if root.endswith("UnitTest") :
diff --git a/Swiften/Serializer/ComponentHandshakeSerializer.cpp b/Swiften/Serializer/ComponentHandshakeSerializer.cpp
index de1958e..011d59e 100644
--- a/Swiften/Serializer/ComponentHandshakeSerializer.cpp
+++ b/Swiften/Serializer/ComponentHandshakeSerializer.cpp
@@ -15,7 +15,7 @@ ComponentHandshakeSerializer::ComponentHandshakeSerializer() {
 
 String ComponentHandshakeSerializer::serialize(boost::shared_ptr<Element> element)  const {
 	boost::shared_ptr<ComponentHandshake> handshake(boost::dynamic_pointer_cast<ComponentHandshake>(element));
-	return "<handshake>" + handshake->getData() + "</challenge>";
+	return "<handshake>" + handshake->getData() + "</handshake>";
 }
 
 }
-- 
cgit v0.10.2-6-g49f6