summaryrefslogtreecommitdiffstats
blob: e68e03ff1353a64e23273660e09cba483b82cd9d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146

#include <iostream>
#include <string>
#include <boost/asio.hpp>
#include <boost/signal.hpp>
#include <boost/bind.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/enable_shared_from_this.hpp>
#include <boost/thread.hpp>

#include "Swiften/Base/ByteArray.h"
#include "Swiften/EventLoop/MainEventLoop.h"
#include "Swiften/EventLoop/SimpleEventLoop.h"
#include "Swiften/Network/IncomingConnection.h"
#include "Swiften/Network/ConnectionServer.h"
#include "Swiften/Network/BoostIOServiceThread.h"

using namespace Swift;

class ServerFromClientSession {
	public:
		ServerFromClientSession(boost::shared_ptr<IncomingConnection> connection) : connection_(connection) {
		}

		void start() {
			connection_->write("Hello\n");
			onSessionFinished();
		}
	
		boost::signal<void()> onSessionFinished;

	private:
		boost::shared_ptr<IncomingConnection> connection_;
};

// A reference-counted non-modifiable buffer class.
class SharedBuffer {
	public:
		SharedBuffer(const ByteArray& data) : 
				data_(new std::vector<char>(data.begin(), data.end())),
				buffer_(boost::asio::buffer(*data_)) {
		}

		// ConstBufferSequence requirements.
		typedef boost::asio::const_buffer value_type;
		typedef const boost::asio::const_buffer* const_iterator;
		const boost::asio::const_buffer* begin() const { return &buffer_; }
		const boost::asio::const_buffer* end() const { return &buffer_ + 1; }

	private:
		boost::shared_ptr< std::vector<char> > data_;
		boost::asio::const_buffer buffer_;
};

class IncomingBoostConnection : public IncomingConnection, public boost::enable_shared_from_this<IncomingBoostConnection> {
	public:
		typedef boost::shared_ptr<IncomingBoostConnection> pointer;

		static pointer create(boost::asio::io_service& ioService) {
			return pointer(new IncomingBoostConnection(ioService));
		}

		boost::asio::ip::tcp::socket& getSocket() {
			return socket_;
		}

		void write(const ByteArray& data) {
			boost::asio::async_write(socket_, SharedBuffer(data),
					boost::bind(
						&IncomingBoostConnection::handleDataWritten, 
						shared_from_this(),
						boost::asio::placeholders::error));
		}

	private:
		IncomingBoostConnection(boost::asio::io_service& ioService) : socket_(ioService) {
		}

		void handleDataWritten(const boost::system::error_code& error) {
			if (error) {
				std::cerr << "ERROR: Unable to write data to socket" << std::endl;
			}
		}

		boost::asio::ip::tcp::socket socket_;
};

class BoostConnectionServer : public ConnectionServer {
	public:
		BoostConnectionServer(int port, boost::asio::io_service& ioService) : acceptor_(ioService, boost::asio::ip::tcp::endpoint(boost::asio::ip::tcp::v4(), port)) {
			acceptNextConnection();
		}

	private:
		void acceptNextConnection() {
			IncomingBoostConnection::pointer newConnection = IncomingBoostConnection::create(acceptor_.io_service());
			acceptor_.async_accept(newConnection->getSocket(), 
				boost::bind(&BoostConnectionServer::handleAccept, this, newConnection, boost::asio::placeholders::error));
		}

		void handleAccept(IncomingBoostConnection::pointer newConnection, const boost::system::error_code& error) {
			if (!error) {
				MainEventLoop::postEvent(boost::bind(boost::ref(onNewConnection), newConnection), this);
				acceptNextConnection();
			}
		}

		boost::asio::ip::tcp::acceptor acceptor_;
};

class Server {
	public:
		Server() {
			serverFromClientConnectionServer_ = new BoostConnectionServer(5222, boostIOServiceThread_.getIOService());
			serverFromClientConnectionServer_->onNewConnection.connect(boost::bind(&Server::handleNewConnection, this, _1));
		}

		~Server() {
			delete serverFromClientConnectionServer_;
		}

	private:
		void handleNewConnection(boost::shared_ptr<IncomingConnection> c) {
			ServerFromClientSession* session = new ServerFromClientSession(c);
			serverFromClientSessions_.push_back(session);
			session->onSessionFinished.connect(boost::bind(&Server::handleSessionFinished, this, session));
			session->start();
		}

		void handleSessionFinished(ServerFromClientSession* session) {
			serverFromClientSessions_.erase(std::remove(serverFromClientSessions_.begin(), serverFromClientSessions_.end(), session), serverFromClientSessions_.end());
			delete session;
		}

	private:
		BoostIOServiceThread boostIOServiceThread_;
		BoostConnectionServer* serverFromClientConnectionServer_;
		std::vector<ServerFromClientSession*> serverFromClientSessions_;
};

int main() {
	SimpleEventLoop eventLoop;
	Server server;
	eventLoop.run();
  return 0;
}