asio 0.3.7 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 12 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 &e, asio::ip::tcp::resolver::iterator endpoint_iterator)
 Handle completion of a connect operation.
void handle_read (const asio::error &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 16 of file client.cpp.

00018     : connection_(io_service)
00019   {
00020     // Resolve the host name into an IP address.
00021     asio::ip::tcp::resolver resolver(io_service);
00022     asio::ip::tcp::resolver::query query(host, service);
00023     asio::ip::tcp::resolver::iterator endpoint_iterator =
00024       resolver.resolve(query);
00025     asio::ip::tcp::endpoint endpoint = *endpoint_iterator;
00026 
00027     // Start an asynchronous connect operation.
00028     connection_.socket().async_connect(endpoint,
00029         boost::bind(&client::handle_connect, this,
00030           asio::placeholders::error, ++endpoint_iterator));
00031   }


Member Function Documentation

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

Handle completion of a connect operation.

Definition at line 34 of file client.cpp.

Referenced by client().

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

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

Handle completion of a read operation.

Definition at line 65 of file client.cpp.

Referenced by handle_connect().

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


Member Data Documentation

connection s11n_example::client::connection_ [private]

The connection to the server.

Definition at line 97 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 100 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.7 Home | Reference | Tutorial | Examples | Design