From d66658252e70abfc2d4eb7cf5f694ba5dc824291 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Remko=20Tron=C3=A7on?= <git@el-tramo.be>
Date: Sun, 19 Jul 2009 16:06:18 +0200
Subject: Factor out remote & local JID into Session.


diff --git a/Swiften/Client/ClientSession.cpp b/Swiften/Client/ClientSession.cpp
index 4fcf1f8..802ac0a 100644
--- a/Swiften/Client/ClientSession.cpp
+++ b/Swiften/Client/ClientSession.cpp
@@ -31,10 +31,11 @@ ClientSession::ClientSession(
 		PayloadParserFactoryCollection* payloadParserFactories, 
 		PayloadSerializerCollection* payloadSerializers) : 
 			Session(connection, payloadParserFactories, payloadSerializers),
-			jid_(jid), 
 			tlsLayerFactory_(tlsLayerFactory),
 			state_(Initial), 
 			needSessionStart_(false) {
+	setLocalJID(jid);
+	setRemoteJID(JID("", jid.getDomain()));
 }
 
 void ClientSession::handleSessionStarted() {
@@ -45,7 +46,7 @@ void ClientSession::handleSessionStarted() {
 
 void ClientSession::sendStreamHeader() {
 	ProtocolHeader header;
-	header.setTo(jid_.getDomain());
+	header.setTo(getRemoteJID());
 	getXMPPLayer()->writeHeader(header);
 }
 
@@ -103,8 +104,8 @@ void ClientSession::handleElement(boost::shared_ptr<Element> element) {
 			if (streamFeatures->hasResourceBind()) {
 				state_ = BindingResource;
 				boost::shared_ptr<ResourceBind> resourceBind(new ResourceBind());
-				if (!jid_.getResource().isEmpty()) {
-					resourceBind->setResource(jid_.getResource());
+				if (!getLocalJID().getResource().isEmpty()) {
+					resourceBind->setResource(getLocalJID().getResource());
 				}
 				getXMPPLayer()->writeElement(IQ::createRequest(IQ::Set, JID(), "session-bind", resourceBind));
 			}
@@ -151,8 +152,8 @@ void ClientSession::handleElement(boost::shared_ptr<Element> element) {
 				finishSession(UnexpectedElementError);
 			}
 			else if (iq->getType() == IQ::Result) {
-				jid_ = resourceBind->getJID();
-				if (!jid_.isValid()) {
+				setLocalJID(resourceBind->getJID());
+				if (!getLocalJID().isValid()) {
 					finishSession(ResourceBindError);
 				}
 				if (needSessionStart_) {
@@ -216,7 +217,7 @@ bool ClientSession::checkState(State state) {
 void ClientSession::sendCredentials(const String& password) {
 	assert(WaitingForCredentials);
 	state_ = Authenticating;
-	getXMPPLayer()->writeElement(boost::shared_ptr<Element>(new AuthRequest("PLAIN", PLAINMessage(jid_.getNode(), password).getValue())));
+	getXMPPLayer()->writeElement(boost::shared_ptr<Element>(new AuthRequest("PLAIN", PLAINMessage(getLocalJID().getNode(), password).getValue())));
 }
 
 void ClientSession::handleTLSConnected() {
diff --git a/Swiften/Client/ClientSession.h b/Swiften/Client/ClientSession.h
index 22e4a88..cb1e098 100644
--- a/Swiften/Client/ClientSession.h
+++ b/Swiften/Client/ClientSession.h
@@ -54,10 +54,6 @@ namespace Swift {
 				return error_;
 			}
 
-			const JID& getJID() const {
-				return jid_;
-			}
-
 			void sendCredentials(const String& password);
 			void setCertificate(const PKCS12Certificate& certificate);
 
@@ -80,7 +76,6 @@ namespace Swift {
 			boost::signal<void ()> onNeedCredentials;
 		
 		private:
-			JID jid_;
 			TLSLayerFactory* tlsLayerFactory_;
 			State state_;
 			boost::optional<SessionError> error_;
diff --git a/Swiften/Client/UnitTest/ClientSessionTest.cpp b/Swiften/Client/UnitTest/ClientSessionTest.cpp
index c86442d..a44b0df 100644
--- a/Swiften/Client/UnitTest/ClientSessionTest.cpp
+++ b/Swiften/Client/UnitTest/ClientSessionTest.cpp
@@ -259,7 +259,7 @@ class ClientSessionTest : public CppUnit::TestFixture {
 			processEvents();
 
 			CPPUNIT_ASSERT_EQUAL(ClientSession::SessionStarted, session->getState());
-			CPPUNIT_ASSERT_EQUAL(JID("me@foo.com/Bar"), session->getJID());
+			CPPUNIT_ASSERT_EQUAL(JID("me@foo.com/Bar"), session->getLocalJID());
 		}
 
 		void testResourceBind_ChangeResource() {
@@ -273,7 +273,7 @@ class ClientSessionTest : public CppUnit::TestFixture {
 			processEvents();
 
 			CPPUNIT_ASSERT_EQUAL(ClientSession::SessionStarted, session->getState());
-			CPPUNIT_ASSERT_EQUAL(JID("me@foo.com/Bar123"), session->getJID());
+			CPPUNIT_ASSERT_EQUAL(JID("me@foo.com/Bar123"), session->getLocalJID());
 		}
 
 		void testResourceBind_EmptyResource() {
@@ -287,7 +287,7 @@ class ClientSessionTest : public CppUnit::TestFixture {
 			processEvents();
 
 			CPPUNIT_ASSERT_EQUAL(ClientSession::SessionStarted, session->getState());
-			CPPUNIT_ASSERT_EQUAL(JID("me@foo.com/NewResource"), session->getJID());
+			CPPUNIT_ASSERT_EQUAL(JID("me@foo.com/NewResource"), session->getLocalJID());
 		}
 
 		void testResourceBind_Error() {
diff --git a/Swiften/Server/ServerFromClientSession.cpp b/Swiften/Server/ServerFromClientSession.cpp
index 4489654..3efacd7 100644
--- a/Swiften/Server/ServerFromClientSession.cpp
+++ b/Swiften/Server/ServerFromClientSession.cpp
@@ -42,7 +42,7 @@ void ServerFromClientSession::handleElement(boost::shared_ptr<Element> element)
 			}
 			else {
 				PLAINMessage plainMessage(authRequest->getMessage());
-				if (userRegistry_->isValidUserPassword(JID(plainMessage.getAuthenticationID(), domain_.getDomain()), plainMessage.getPassword())) {
+				if (userRegistry_->isValidUserPassword(JID(plainMessage.getAuthenticationID(), getLocalJID().getDomain()), plainMessage.getPassword())) {
 					getXMPPLayer()->writeElement(boost::shared_ptr<AuthSuccess>(new AuthSuccess()));
 					user_ = plainMessage.getAuthenticationID();
 					authenticated_ = true;
@@ -56,13 +56,13 @@ void ServerFromClientSession::handleElement(boost::shared_ptr<Element> element)
 		}
 		else if (IQ* iq = dynamic_cast<IQ*>(element.get())) {
 			if (boost::shared_ptr<ResourceBind> resourceBind = iq->getPayload<ResourceBind>()) {
-				jid_ = JID(user_, domain_.getDomain(), resourceBind->getResource());
+				setRemoteJID(JID(user_, getLocalJID().getDomain(), resourceBind->getResource()));
 				boost::shared_ptr<ResourceBind> resultResourceBind(new ResourceBind());
-				resultResourceBind->setJID(jid_);
+				resultResourceBind->setJID(getRemoteJID());
 				getXMPPLayer()->writeElement(IQ::createResult(JID(), iq->getID(), resultResourceBind));
 			}
 			else if (iq->getPayload<StartSession>()) {
-				getXMPPLayer()->writeElement(IQ::createResult(jid_, iq->getID()));
+				getXMPPLayer()->writeElement(IQ::createResult(getRemoteJID(), iq->getID()));
 				setInitialized();
 			}
 		}
@@ -70,7 +70,7 @@ void ServerFromClientSession::handleElement(boost::shared_ptr<Element> element)
 }
 
 void ServerFromClientSession::handleStreamStart(const ProtocolHeader& incomingHeader) {
-	domain_ = JID("", incomingHeader.getTo());
+	setLocalJID(JID("", incomingHeader.getTo()));
 	ProtocolHeader header;
 	header.setFrom(incomingHeader.getTo());
 	header.setID(id_);
diff --git a/Swiften/Server/ServerFromClientSession.h b/Swiften/Server/ServerFromClientSession.h
index 213f5c7..748f7eb 100644
--- a/Swiften/Server/ServerFromClientSession.h
+++ b/Swiften/Server/ServerFromClientSession.h
@@ -31,14 +31,6 @@ namespace Swift {
 					PayloadSerializerCollection* payloadSerializers,
 					UserRegistry* userRegistry);
 
-			const JID& getJID() const {
-				return jid_;
-			}
-
-			const JID& getDomain() const {
-				return domain_;
-			}
-
 		private:
 			void handleElement(boost::shared_ptr<Element>);
 			void handleStreamStart(const ProtocolHeader& header);
@@ -47,8 +39,6 @@ namespace Swift {
 			String id_;
 			UserRegistry* userRegistry_;
 			bool authenticated_;
-			JID domain_;
 			String user_;
-			JID jid_;
 	};
 }
diff --git a/Swiften/Session/Session.h b/Swiften/Session/Session.h
index b35179c..2c5ec34 100644
--- a/Swiften/Session/Session.h
+++ b/Swiften/Session/Session.h
@@ -46,6 +46,14 @@ namespace Swift {
 			void finishSession();
 			void sendElement(boost::shared_ptr<Element>);
 
+			const JID& getLocalJID() const {
+				return localJID;
+			}
+
+			const JID& getRemoteJID() const {
+				return remoteJID;
+			}
+
 			boost::signal<void (boost::shared_ptr<Element>)> onElementReceived;
 			boost::signal<void ()> onSessionStarted;
 			boost::signal<void (const boost::optional<SessionError>&)> onSessionFinished;
@@ -53,6 +61,16 @@ namespace Swift {
 			boost::signal<void (const ByteArray&)> onDataRead;
 
 		protected:
+			void setRemoteJID(const JID& j) {
+				assert(!isInitialized());
+				remoteJID = j;
+			}
+
+			void setLocalJID(const JID& j) {
+				assert(!isInitialized());
+				localJID = j;
+			}
+
 			void finishSession(const SessionError&);
 
 			virtual void handleSessionStarted() {}
@@ -81,6 +99,8 @@ namespace Swift {
 			void handleDisconnected(const boost::optional<Connection::Error>& error);
 
 		private:
+			JID localJID;
+			JID remoteJID;
 			boost::shared_ptr<Connection> connection;
 			PayloadParserFactoryCollection* payloadParserFactories;
 			PayloadSerializerCollection* payloadSerializers;
-- 
cgit v0.10.2-6-g49f6