diff options
author | Remko Tronçon <git@el-tramo.be> | 2009-07-15 18:07:44 (GMT) |
---|---|---|
committer | Remko Tronçon <git@el-tramo.be> | 2009-07-15 18:07:44 (GMT) |
commit | f061668e3c1d5eac01b85303e2c81df2bc560e9a (patch) | |
tree | de56afe6d93cb36425f7c2e458575d27bd9105a4 /Limber/main.cpp | |
parent | 0930cd940963be0edfe7c80b4925babca0e01443 (diff) | |
download | swift-f061668e3c1d5eac01b85303e2c81df2bc560e9a.zip swift-f061668e3c1d5eac01b85303e2c81df2bc560e9a.tar.bz2 |
Make stream stack layers reference counted.
Diffstat (limited to 'Limber/main.cpp')
-rw-r--r-- | Limber/main.cpp | 25 |
1 files changed, 15 insertions, 10 deletions
diff --git a/Limber/main.cpp b/Limber/main.cpp index 200f2e4..5d1bb6a 100644 --- a/Limber/main.cpp +++ b/Limber/main.cpp @@ -3,6 +3,7 @@ #include <boost/asio.hpp> #include <boost/bind.hpp> #include <boost/shared_ptr.hpp> +#include <boost/enable_shared_from_this.hpp> #include "Swiften/Elements/IQ.h" #include "Swiften/Elements/RosterPayload.h" @@ -12,6 +13,7 @@ #include "Swiften/Base/IDGenerator.h" #include "Swiften/EventLoop/MainEventLoop.h" #include "Swiften/EventLoop/SimpleEventLoop.h" +#include "Swiften/EventLoop/EventOwner.h" #include "Swiften/Elements/Stanza.h" #include "Swiften/Network/ConnectionServer.h" #include "Swiften/Network/BoostConnection.h" @@ -22,9 +24,12 @@ using namespace Swift; -class BoostConnectionServer : public ConnectionServer { +class BoostConnectionServer : public ConnectionServer, public EventOwner, public boost::enable_shared_from_this<BoostConnectionServer> { public: BoostConnectionServer(int port, boost::asio::io_service& ioService) : acceptor_(ioService, boost::asio::ip::tcp::endpoint(boost::asio::ip::tcp::v4(), port)) { + } + + void start() { acceptNextConnection(); } @@ -32,12 +37,12 @@ class BoostConnectionServer : public ConnectionServer { void acceptNextConnection() { boost::shared_ptr<BoostConnection> newConnection(new BoostConnection(&acceptor_.io_service())); acceptor_.async_accept(newConnection->getSocket(), - boost::bind(&BoostConnectionServer::handleAccept, this, newConnection, boost::asio::placeholders::error)); + boost::bind(&BoostConnectionServer::handleAccept, shared_from_this(), newConnection, boost::asio::placeholders::error)); } void handleAccept(boost::shared_ptr<BoostConnection> newConnection, const boost::system::error_code& error) { if (!error) { - MainEventLoop::postEvent(boost::bind(boost::ref(onNewConnection), newConnection), this); + MainEventLoop::postEvent(boost::bind(boost::ref(onNewConnection), newConnection), shared_from_this()); newConnection->listen(); acceptNextConnection(); } @@ -49,12 +54,9 @@ class BoostConnectionServer : public ConnectionServer { class Server { public: Server(UserRegistry* userRegistry) : userRegistry_(userRegistry) { - serverFromClientConnectionServer_ = new BoostConnectionServer(5222, boostIOServiceThread_.getIOService()); + serverFromClientConnectionServer_ = boost::shared_ptr<BoostConnectionServer>(new BoostConnectionServer(5224, boostIOServiceThread_.getIOService())); serverFromClientConnectionServer_->onNewConnection.connect(boost::bind(&Server::handleNewConnection, this, _1)); - } - - ~Server() { - delete serverFromClientConnectionServer_; + serverFromClientConnectionServer_->start(); } private: @@ -101,7 +103,7 @@ class Server { IDGenerator idGenerator_; UserRegistry* userRegistry_; BoostIOServiceThread boostIOServiceThread_; - BoostConnectionServer* serverFromClientConnectionServer_; + boost::shared_ptr<BoostConnectionServer> serverFromClientConnectionServer_; std::vector<ServerFromClientSession*> serverFromClientSessions_; FullPayloadParserFactoryCollection payloadParserFactories_; FullPayloadSerializerCollection payloadSerializers_; @@ -110,7 +112,10 @@ class Server { int main() { SimpleEventLoop eventLoop; SimpleUserRegistry userRegistry; - userRegistry.addUser(JID("remko@limber"), "pass"); + userRegistry.addUser(JID("remko@localhost"), "remko"); + userRegistry.addUser(JID("kevin@localhost"), "kevin"); + userRegistry.addUser(JID("remko@limber.swift.im"), "remko"); + userRegistry.addUser(JID("kevin@limber.swift.im"), "kevin"); Server server(&userRegistry); eventLoop.run(); return 0; |