1 | /*
|
---|
2 | * StockServer.cpp
|
---|
3 | *
|
---|
4 | * Created on: 18.11.2011
|
---|
5 | * Author: heber
|
---|
6 | */
|
---|
7 |
|
---|
8 | // include config.h
|
---|
9 | #ifdef HAVE_CONFIG_H
|
---|
10 | #include <config.h>
|
---|
11 | #endif
|
---|
12 |
|
---|
13 |
|
---|
14 | #include <boost/asio.hpp>
|
---|
15 | #include <boost/bind.hpp>
|
---|
16 | #include <boost/lexical_cast.hpp>
|
---|
17 | #include <iostream>
|
---|
18 | #include <vector>
|
---|
19 | #include "connection.hpp" // Must come before boost/serialization headers.
|
---|
20 | #include <boost/serialization/vector.hpp>
|
---|
21 | #include "FragmentJob.hpp"
|
---|
22 |
|
---|
23 | #include "StockServer.hpp"
|
---|
24 |
|
---|
25 | StockServer::StockServer(boost::asio::io_service& io_service, unsigned short port) :
|
---|
26 | acceptor_(io_service,
|
---|
27 | boost::asio::ip::tcp::endpoint(boost::asio::ip::tcp::v4(), port)
|
---|
28 | )
|
---|
29 | {
|
---|
30 | FragmentJob s(std::string("test"), 1);
|
---|
31 | jobs_.push_back(s);
|
---|
32 |
|
---|
33 | // Start an accept operation for a new connection.
|
---|
34 | connection_ptr new_conn(new connection(acceptor_.get_io_service()));
|
---|
35 | acceptor_.async_accept(new_conn->socket(),
|
---|
36 | boost::bind(&StockServer::handle_accept, this,
|
---|
37 | boost::asio::placeholders::error, new_conn));
|
---|
38 | }
|
---|
39 |
|
---|
40 | void StockServer::handle_accept(const boost::system::error_code& e, connection_ptr conn)
|
---|
41 | {
|
---|
42 | std::cout << "handle_accept called." << std::endl;
|
---|
43 | if (!e)
|
---|
44 | {
|
---|
45 | // Successfully accepted a new connection. Send the list of stocks to the
|
---|
46 | // client. The connection::async_write() function will automatically
|
---|
47 | // serialize the data structure for us.
|
---|
48 | conn->async_write(jobs_,
|
---|
49 | boost::bind(&StockServer::handle_write, this,
|
---|
50 | boost::asio::placeholders::error, conn));
|
---|
51 | }
|
---|
52 |
|
---|
53 | connection_ptr new_conn(new connection(acceptor_.get_io_service()));
|
---|
54 | acceptor_.async_accept(new_conn->socket(),
|
---|
55 | boost::bind(&StockServer::handle_accept, this,
|
---|
56 | boost::asio::placeholders::error, new_conn));
|
---|
57 | }
|
---|
58 |
|
---|
59 | void StockServer::handle_write(const boost::system::error_code& e, connection_ptr conn)
|
---|
60 | {
|
---|
61 | // Nothing to do. The socket will be closed automatically when the last
|
---|
62 | // reference to the connection object goes away.
|
---|
63 | }
|
---|