asio 0.3.8rc3 Home | Reference | Tutorial | Examples | Design
Examples

s11n_example::client Class Reference

Collaboration diagram for s11n_example::client:

Collaboration graph

List of all members.


Detailed Description

Downloads stock quote information from a server.

Definition at line 22 of file client.cpp.


Public Member Functions

 client (asio::io_service &io_service, const std::string &host, const std::string &service)
 Constructor starts the asynchronous connect operation.
void handle_connect (const asio::error_code &e, asio::ip::tcp::resolver::iterator endpoint_iterator)
 Handle completion of a connect operation.
void handle_read (const asio::error_code &e)
 Handle completion of a read operation.

Private Attributes

connection connection_
 The connection to the server.
std::vector< stockstocks_
 The data received from the server.

Constructor & Destructor Documentation

s11n_example::client::client ( asio::io_service io_service,
const std::string &  host,
const std::string &  service 
)

Constructor starts the asynchronous connect operation.

Definition at line 26 of file client.cpp.

00028     : connection_(io_service)
00029   {
00030     // Resolve the host name into an IP address.
00031     asio::ip::tcp::resolver resolver(io_service);
00032     asio::ip::tcp::resolver::query query(host, service);
00033     asio::ip::tcp::resolver::iterator endpoint_iterator =
00034       resolver.resolve(query);
00035     asio::ip::tcp::endpoint endpoint = *endpoint_iterator;
00036 
00037     // Start an asynchronous connect operation.
00038     connection_.socket().async_connect(endpoint,
00039         boost::bind(&client::handle_connect, this,
00040           asio::placeholders::error, ++endpoint_iterator));
00041   }


Member Function Documentation

void s11n_example::client::handle_connect ( const asio::error_code e,
asio::ip::tcp::resolver::iterator  endpoint_iterator 
)

Handle completion of a connect operation.

Definition at line 44 of file client.cpp.

Referenced by client().

00046   {
00047     if (!e)
00048     {
00049       // Successfully established connection. Start operation to read the list
00050       // of stocks. The connection::async_read() function will automatically
00051       // decode the data that is read from the underlying socket.
00052       connection_.async_read(stocks_,
00053           boost::bind(&client::handle_read, this,
00054             asio::placeholders::error));
00055     }
00056     else if (endpoint_iterator != asio::ip::tcp::resolver::iterator())
00057     {
00058       // Try the next endpoint.
00059       connection_.socket().close();
00060       asio::ip::tcp::endpoint endpoint = *endpoint_iterator;
00061       connection_.socket().async_connect(endpoint,
00062           boost::bind(&client::handle_connect, this,
00063             asio::placeholders::error, ++endpoint_iterator));
00064     }
00065     else
00066     {
00067       // An error occurred. Log it and return. Since we are not starting a new
00068       // operation the io_service will run out of work to do and the client will
00069       // exit.
00070       std::cerr << e.message() << std::endl;
00071     }
00072   }

void s11n_example::client::handle_read ( const asio::error_code e  ) 

Handle completion of a read operation.

Definition at line 75 of file client.cpp.

Referenced by handle_connect().

00076   {
00077     if (!e)
00078     {
00079       // Print out the data that was received.
00080       for (std::size_t i = 0; i < stocks_.size(); ++i)
00081       {
00082         std::cout << "Stock number " << i << "\n";
00083         std::cout << "  code: " << stocks_[i].code << "\n";
00084         std::cout << "  name: " << stocks_[i].name << "\n";
00085         std::cout << "  open_price: " << stocks_[i].open_price << "\n";
00086         std::cout << "  high_price: " << stocks_[i].high_price << "\n";
00087         std::cout << "  low_price: " << stocks_[i].low_price << "\n";
00088         std::cout << "  last_price: " << stocks_[i].last_price << "\n";
00089         std::cout << "  buy_price: " << stocks_[i].buy_price << "\n";
00090         std::cout << "  buy_quantity: " << stocks_[i].buy_quantity << "\n";
00091         std::cout << "  sell_price: " << stocks_[i].sell_price << "\n";
00092         std::cout << "  sell_quantity: " << stocks_[i].sell_quantity << "\n";
00093       }
00094     }
00095     else
00096     {
00097       // An error occurred.
00098       std::cerr << e.message() << std::endl;
00099     }
00100 
00101     // Since we are not starting a new operation the io_service will run out of
00102     // work to do and the client will exit.
00103   }


Member Data Documentation

connection s11n_example::client::connection_ [private]

The connection to the server.

Definition at line 107 of file client.cpp.

Referenced by client(), and handle_connect().

std::vector<stock> s11n_example::client::stocks_ [private]

The data received from the server.

Definition at line 110 of file client.cpp.

Referenced by handle_connect(), and handle_read().


The documentation for this class was generated from the following file:
asio 0.3.8rc3 Home | Reference | Tutorial | Examples | Design