Imported Upstream version 1.57.0
[platform/upstream/boost.git] / libs / asio / example / cpp03 / serialization / server.cpp
1 //
2 // server.cpp
3 // ~~~~~~~~~~
4 //
5 // Copyright (c) 2003-2014 Christopher M. Kohlhoff (chris at kohlhoff dot com)
6 //
7 // Distributed under the Boost Software License, Version 1.0. (See accompanying
8 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
9 //
10
11 #include <boost/asio.hpp>
12 #include <boost/bind.hpp>
13 #include <boost/lexical_cast.hpp>
14 #include <iostream>
15 #include <vector>
16 #include "connection.hpp" // Must come before boost/serialization headers.
17 #include <boost/serialization/vector.hpp>
18 #include "stock.hpp"
19
20 namespace s11n_example {
21
22 /// Serves stock quote information to any client that connects to it.
23 class server
24 {
25 public:
26   /// Constructor opens the acceptor and starts waiting for the first incoming
27   /// connection.
28   server(boost::asio::io_service& io_service, unsigned short port)
29     : acceptor_(io_service,
30         boost::asio::ip::tcp::endpoint(boost::asio::ip::tcp::v4(), port))
31   {
32     // Create the data to be sent to each client.
33     stock s;
34     s.code = "ABC";
35     s.name = "A Big Company";
36     s.open_price = 4.56;
37     s.high_price = 5.12;
38     s.low_price = 4.33;
39     s.last_price = 4.98;
40     s.buy_price = 4.96;
41     s.buy_quantity = 1000;
42     s.sell_price = 4.99;
43     s.sell_quantity = 2000;
44     stocks_.push_back(s);
45     s.code = "DEF";
46     s.name = "Developer Entertainment Firm";
47     s.open_price = 20.24;
48     s.high_price = 22.88;
49     s.low_price = 19.50;
50     s.last_price = 19.76;
51     s.buy_price = 19.72;
52     s.buy_quantity = 34000;
53     s.sell_price = 19.85;
54     s.sell_quantity = 45000;
55     stocks_.push_back(s);
56
57     // Start an accept operation for a new connection.
58     connection_ptr new_conn(new connection(acceptor_.get_io_service()));
59     acceptor_.async_accept(new_conn->socket(),
60         boost::bind(&server::handle_accept, this,
61           boost::asio::placeholders::error, new_conn));
62   }
63
64   /// Handle completion of a accept operation.
65   void handle_accept(const boost::system::error_code& e, connection_ptr conn)
66   {
67     if (!e)
68     {
69       // Successfully accepted a new connection. Send the list of stocks to the
70       // client. The connection::async_write() function will automatically
71       // serialize the data structure for us.
72       conn->async_write(stocks_,
73           boost::bind(&server::handle_write, this,
74             boost::asio::placeholders::error, conn));
75     }
76
77     // Start an accept operation for a new connection.
78     connection_ptr new_conn(new connection(acceptor_.get_io_service()));
79     acceptor_.async_accept(new_conn->socket(),
80         boost::bind(&server::handle_accept, this,
81           boost::asio::placeholders::error, new_conn));
82   }
83
84   /// Handle completion of a write operation.
85   void handle_write(const boost::system::error_code& e, connection_ptr conn)
86   {
87     // Nothing to do. The socket will be closed automatically when the last
88     // reference to the connection object goes away.
89   }
90
91 private:
92   /// The acceptor object used to accept incoming socket connections.
93   boost::asio::ip::tcp::acceptor acceptor_;
94
95   /// The data to be sent to each client.
96   std::vector<stock> stocks_;
97 };
98
99 } // namespace s11n_example
100
101 int main(int argc, char* argv[])
102 {
103   try
104   {
105     // Check command line arguments.
106     if (argc != 2)
107     {
108       std::cerr << "Usage: server <port>" << std::endl;
109       return 1;
110     }
111     unsigned short port = boost::lexical_cast<unsigned short>(argv[1]);
112
113     boost::asio::io_service io_service;
114     s11n_example::server server(io_service, port);
115     io_service.run();
116   }
117   catch (std::exception& e)
118   {
119     std::cerr << e.what() << std::endl;
120   }
121
122   return 0;
123 }