summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
Diffstat (limited to 'Swiften/Client')
-rw-r--r--Swiften/Client/Client.cpp7
-rw-r--r--Swiften/Client/Client.h30
-rw-r--r--Swiften/Client/ClientError.h1
-rw-r--r--Swiften/Client/Session.cpp7
-rw-r--r--Swiften/Client/Session.h1
-rw-r--r--Swiften/Client/UnitTest/SessionTest.cpp75
6 files changed, 68 insertions, 53 deletions
diff --git a/Swiften/Client/Client.cpp b/Swiften/Client/Client.cpp
index e5bbf9d..d87673b 100644
--- a/Swiften/Client/Client.cpp
+++ b/Swiften/Client/Client.cpp
@@ -11,14 +11,14 @@ namespace Swift {
Client::Client(const JID& jid, const String& password) :
IQRouter(this), jid_(jid), password_(password), session_(0) {
- connectionFactory_ = new BoostConnectionFactory();
+ connectionFactory_ = new BoostConnectionFactory(&boostIOServiceThread_.getIOService());
tlsLayerFactory_ = new PlatformTLSLayerFactory();
}
Client::~Client() {
delete session_;
delete tlsLayerFactory_;
- delete connectionFactory_;
+ delete connectionFactory_;
}
void Client::connect() {
@@ -101,6 +101,9 @@ void Client::handleSessionError(Session::SessionError error) {
case Session::ConnectionReadError:
clientError = ClientError(ClientError::ConnectionReadError);
break;
+ case Session::ConnectionWriteError:
+ clientError = ClientError(ClientError::ConnectionWriteError);
+ break;
case Session::XMLError:
clientError = ClientError(ClientError::XMLError);
break;
diff --git a/Swiften/Client/Client.h b/Swiften/Client/Client.h
index 946bdbd..d876302 100644
--- a/Swiften/Client/Client.h
+++ b/Swiften/Client/Client.h
@@ -15,44 +15,46 @@
#include "Swiften/Queries/IQRouter.h"
#include "Swiften/Parser/PayloadParsers/FullPayloadParserFactoryCollection.h"
#include "Swiften/Serializer/PayloadSerializers/FullPayloadSerializerCollection.h"
+#include "Swiften/Network/BoostIOServiceThread.h"
namespace Swift {
class TLSLayerFactory;
class ConnectionFactory;
class Session;
- class Client : public StanzaChannel, public IQRouter {
- public:
- Client(const JID& jid, const String& password);
- ~Client();
+ class Client : public StanzaChannel, public IQRouter {
+ public:
+ Client(const JID& jid, const String& password);
+ ~Client();
void setCertificate(const String& certificate);
- void connect();
+ void connect();
void disconnect();
- virtual void sendIQ(boost::shared_ptr<IQ>);
+ virtual void sendIQ(boost::shared_ptr<IQ>);
virtual void sendMessage(boost::shared_ptr<Message>);
virtual void sendPresence(boost::shared_ptr<Presence>);
- public:
- boost::signal<void (ClientError)> onError;
- boost::signal<void ()> onConnected;
+ public:
+ boost::signal<void (ClientError)> onError;
+ boost::signal<void ()> onConnected;
boost::signal<void (const String&)> onDataRead;
boost::signal<void (const String&)> onDataWritten;
private:
- void send(boost::shared_ptr<Stanza>);
- virtual String getNewIQID();
+ void send(boost::shared_ptr<Stanza>);
+ virtual String getNewIQID();
void handleElement(boost::shared_ptr<Element>);
void handleSessionError(Session::SessionError error);
void handleNeedCredentials();
void handleDataRead(const ByteArray&);
void handleDataWritten(const ByteArray&);
- private:
+ private:
+ BoostIOServiceThread boostIOServiceThread_;
JID jid_;
- String password_;
+ String password_;
IDGenerator idGenerator_;
ConnectionFactory* connectionFactory_;
TLSLayerFactory* tlsLayerFactory_;
@@ -60,7 +62,7 @@ namespace Swift {
FullPayloadSerializerCollection payloadSerializers_;
Session* session_;
String certificate_;
- };
+ };
}
#endif
diff --git a/Swiften/Client/ClientError.h b/Swiften/Client/ClientError.h
index 38f20c0..d81cc0e 100644
--- a/Swiften/Client/ClientError.h
+++ b/Swiften/Client/ClientError.h
@@ -9,6 +9,7 @@ namespace Swift {
DomainNameResolveError,
ConnectionError,
ConnectionReadError,
+ ConnectionWriteError,
XMLError,
AuthenticationFailedError,
NoSupportedAuthMechanismsError,
diff --git a/Swiften/Client/Session.cpp b/Swiften/Client/Session.cpp
index c4adfa0..1ae97d6 100644
--- a/Swiften/Client/Session.cpp
+++ b/Swiften/Client/Session.cpp
@@ -52,10 +52,10 @@ Session::~Session() {
void Session::start() {
assert(state_ == Initial);
state_ = Connecting;
- connection_ = connectionFactory_->createConnection(jid_.getDomain());
+ connection_ = connectionFactory_->createConnection();
connection_->onConnected.connect(boost::bind(&Session::handleConnected, this));
connection_->onError.connect(boost::bind(&Session::handleConnectionError, this, _1));
- connection_->connect();
+ connection_->connect(jid_.getDomain());
}
void Session::stop() {
@@ -93,6 +93,9 @@ void Session::handleConnectionError(Connection::Error error) {
case Connection::ReadError:
setError(ConnectionReadError);
break;
+ case Connection::WriteError:
+ setError(ConnectionWriteError);
+ break;
case Connection::ConnectionError:
setError(ConnectionError);
break;
diff --git a/Swiften/Client/Session.h b/Swiften/Client/Session.h
index c49d877..516cb5a 100644
--- a/Swiften/Client/Session.h
+++ b/Swiften/Client/Session.h
@@ -43,6 +43,7 @@ namespace Swift {
DomainNameResolveError,
ConnectionError,
ConnectionReadError,
+ ConnectionWriteError,
XMLError,
AuthenticationFailedError,
NoSupportedAuthMechanismsError,
diff --git a/Swiften/Client/UnitTest/SessionTest.cpp b/Swiften/Client/UnitTest/SessionTest.cpp
index c2b99db..da05a06 100644
--- a/Swiften/Client/UnitTest/SessionTest.cpp
+++ b/Swiften/Client/UnitTest/SessionTest.cpp
@@ -57,9 +57,9 @@ class SessionTest : public CppUnit::TestFixture {
CPPUNIT_TEST(testSessionStart);
CPPUNIT_TEST(testSessionStart_Error);
CPPUNIT_TEST(testSessionStart_AfterResourceBind);
- CPPUNIT_TEST(testWhitespacePing);
+ CPPUNIT_TEST(testWhitespacePing);
CPPUNIT_TEST(testReceiveElementAfterSessionStarted);
- CPPUNIT_TEST(testSendElement);
+ CPPUNIT_TEST(testSendElement);
CPPUNIT_TEST_SUITE_END();
public:
@@ -74,8 +74,8 @@ class SessionTest : public CppUnit::TestFixture {
}
void tearDown() {
- delete tlsLayerFactory_;
- delete connectionFactory_;
+ delete tlsLayerFactory_;
+ delete connectionFactory_;
delete eventLoop_;
}
@@ -250,7 +250,7 @@ class SessionTest : public CppUnit::TestFixture {
CPPUNIT_ASSERT_EQUAL(Session::Negotiating, session->getState());
}
- void testAuthenticate_Unauthorized() {
+ void testAuthenticate_Unauthorized() {
std::auto_ptr<MockSession> session(createSession("me@foo.com/Bar"));
session->start();
getMockServer()->expectStreamStart();
@@ -265,9 +265,9 @@ class SessionTest : public CppUnit::TestFixture {
CPPUNIT_ASSERT_EQUAL(Session::Error, session->getState());
CPPUNIT_ASSERT_EQUAL(Session::AuthenticationFailedError, session->getError());
- }
+ }
- void testAuthenticate_NoValidAuthMechanisms() {
+ void testAuthenticate_NoValidAuthMechanisms() {
std::auto_ptr<MockSession> session(createSession("me@foo.com/Bar"));
session->start();
getMockServer()->expectStreamStart();
@@ -277,7 +277,7 @@ class SessionTest : public CppUnit::TestFixture {
CPPUNIT_ASSERT_EQUAL(Session::Error, session->getState());
CPPUNIT_ASSERT_EQUAL(Session::NoSupportedAuthMechanismsError, session->getError());
- }
+ }
void testResourceBind() {
std::auto_ptr<MockSession> session(createSession("me@foo.com/Bar"));
@@ -389,7 +389,7 @@ class SessionTest : public CppUnit::TestFixture {
CPPUNIT_ASSERT(sessionStarted_);
}
- void testWhitespacePing() {
+ void testWhitespacePing() {
std::auto_ptr<MockSession> session(createSession("me@foo.com/Bar"));
session->start();
getMockServer()->expectStreamStart();
@@ -397,7 +397,7 @@ class SessionTest : public CppUnit::TestFixture {
getMockServer()->sendStreamFeatures();
processEvents();
CPPUNIT_ASSERT(session->getWhitespacePingLayer());
- }
+ }
void testReceiveElementAfterSessionStarted() {
std::auto_ptr<MockSession> session(createSession("me@foo.com/Bar"));
@@ -407,11 +407,11 @@ class SessionTest : public CppUnit::TestFixture {
getMockServer()->sendStreamFeatures();
processEvents();
- getMockServer()->expectMessage();
- session->sendElement(boost::shared_ptr<Message>(new Message()));
+ getMockServer()->expectMessage();
+ session->sendElement(boost::shared_ptr<Message>(new Message()));
}
- void testSendElement() {
+ void testSendElement() {
std::auto_ptr<MockSession> session(createSession("me@foo.com/Bar"));
session->onElementReceived.connect(boost::bind(&SessionTest::addReceivedElement, this, _1));
session->start();
@@ -421,9 +421,9 @@ class SessionTest : public CppUnit::TestFixture {
getMockServer()->sendMessage();
processEvents();
- CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(receivedElements_.size()));
- CPPUNIT_ASSERT(boost::dynamic_pointer_cast<Message>(receivedElements_[0]));
- }
+ CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(receivedElements_.size()));
+ CPPUNIT_ASSERT(boost::dynamic_pointer_cast<Message>(receivedElements_[0]));
+ }
private:
struct MockConnection;
@@ -446,9 +446,9 @@ class SessionTest : public CppUnit::TestFixture {
needCredentials_ = true;
}
- void addReceivedElement(boost::shared_ptr<Element> element) {
- receivedElements_.push_back(element);
- }
+ void addReceivedElement(boost::shared_ptr<Element> element) {
+ receivedElements_.push_back(element);
+ }
private:
struct MockConnection : public Connection, public XMPPParserClient {
@@ -463,12 +463,11 @@ class SessionTest : public CppUnit::TestFixture {
direction(direction), type(type), element(element) {}
Direction direction;
- Type type;
+ Type type;
boost::shared_ptr<Element> element;
};
- MockConnection(const String& domain, bool fail) :
- Connection(domain),
+ MockConnection(bool fail) :
fail_(fail),
resetParser_(false),
parser_(0),
@@ -476,18 +475,23 @@ class SessionTest : public CppUnit::TestFixture {
parser_ = new XMPPParser(this, &payloadParserFactories_);
}
- ~MockConnection() {
- delete parser_;
- }
+ ~MockConnection() {
+ delete parser_;
+ }
void disconnect() {
}
- void connect() {
+ void listen() {
+ assert(false);
+ }
+
+ void connect(const String& domain) {
if (fail_) {
MainEventLoop::postEvent(boost::bind(boost::ref(onError), Connection::ConnectionError));
}
else {
+ domain_ = domain;
MainEventLoop::postEvent(boost::bind(boost::ref(onConnected)));
}
}
@@ -510,7 +514,7 @@ class SessionTest : public CppUnit::TestFixture {
}
void handleStreamStart(const String&, const String& to, const String&) {
- CPPUNIT_ASSERT_EQUAL(getDomain(), to);
+ CPPUNIT_ASSERT_EQUAL(domain_, to);
handleEvent(Event::StreamStartEvent);
}
@@ -544,7 +548,7 @@ class SessionTest : public CppUnit::TestFixture {
String serializeEvent(const Event& event) {
switch (event.type) {
case Event::StreamStartEvent:
- return serializer_.serializeHeader("", getDomain(), "");
+ return serializer_.serializeHeader("", domain_, "");
case Event::ElementEvent:
return serializer_.serializeElement(event.element);
case Event::StreamEndEvent:
@@ -571,7 +575,7 @@ class SessionTest : public CppUnit::TestFixture {
}
void expectAuth(const String& user, const String& password) {
- String s = String("") + '\0' + user + '\0' + password;
+ String s = String("") + '\0' + user + '\0' + password;
events_.push_back(Event(Event::In, Event::ElementEvent, boost::shared_ptr<AuthRequest>(new AuthRequest("PLAIN", ByteArray(s.getUTF8Data(), s.getUTF8Size())))));
}
@@ -584,10 +588,10 @@ class SessionTest : public CppUnit::TestFixture {
void expectSessionStart(const String& id) {
events_.push_back(Event(Event::In, Event::ElementEvent, IQ::createRequest(IQ::Set, JID(), id, boost::shared_ptr<StartSession>(new StartSession()))));
}
-
- void expectMessage() {
+
+ void expectMessage() {
events_.push_back(Event(Event::In, Event::ElementEvent, boost::shared_ptr<Message>(new Message())));
- }
+ }
void sendInvalidXML() {
sendData("<invalid xml/>");
@@ -675,6 +679,7 @@ class SessionTest : public CppUnit::TestFixture {
bool fail_;
bool resetParser_;
+ String domain_;
FullPayloadParserFactoryCollection payloadParserFactories_;
FullPayloadSerializerCollection payloadSerializers_;
XMPPParser* parser_;
@@ -684,8 +689,8 @@ class SessionTest : public CppUnit::TestFixture {
struct MockConnectionFactory : public ConnectionFactory {
MockConnectionFactory() : fail_(false) {}
- MockConnection* createConnection(const String& domain) {
- MockConnection* result = new MockConnection(domain, fail_);
+ MockConnection* createConnection() {
+ MockConnection* result = new MockConnection(fail_);
connections_.push_back(result);
return result;
}
@@ -746,7 +751,7 @@ class SessionTest : public CppUnit::TestFixture {
FullPayloadSerializerCollection payloadSerializers_;
bool sessionStarted_;
bool needCredentials_;
- std::vector< boost::shared_ptr<Element> > receivedElements_;
+ std::vector< boost::shared_ptr<Element> > receivedElements_;
typedef std::vector< boost::function<void ()> > EventQueue;
EventQueue events_;
};