Boost C++ Libraries Home Libraries People FAQ More

Home | Reference | Tutorial | Examples | Design

serialization/client.cpp

Go to the documentation of this file.
00001 #include <boost/asio.hpp>
00002 #include <boost/bind.hpp>
00003 #include <iostream>
00004 #include <vector>
00005 #include "connection.hpp" // Must come before boost/serialization headers.
00006 #include <boost/serialization/vector.hpp>
00007 #include "stock.hpp"
00008 
00009 namespace s11n_example {
00010 
00012 class client
00013 {
00014 public:
00016   client(boost::asio::io_service& io_service,
00017       const std::string& host, const std::string& service)
00018     : connection_(io_service)
00019   {
00020     // Resolve the host name into an IP address.
00021     boost::asio::ip::tcp::resolver resolver(io_service);
00022     boost::asio::ip::tcp::resolver::query query(host, service);
00023     boost::asio::ip::tcp::resolver::iterator endpoint_iterator =
00024       resolver.resolve(query);
00025     boost::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           boost::asio::placeholders::error, ++endpoint_iterator));
00031   }
00032 
00034   void handle_connect(const boost::asio::error& e,
00035       boost::asio::ip::tcp::resolver::iterator endpoint_iterator)
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             boost::asio::placeholders::error));
00045     }
00046     else if (endpoint_iterator != boost::asio::ip::tcp::resolver::iterator())
00047     {
00048       // Try the next endpoint.
00049       connection_.socket().close();
00050       boost::asio::ip::tcp::endpoint endpoint = *endpoint_iterator;
00051       connection_.socket().async_connect(endpoint,
00052           boost::bind(&client::handle_connect, this,
00053             boost::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   }
00063 
00065   void handle_read(const boost::asio::error& e)
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   }
00094 
00095 private:
00097   connection connection_;
00098 
00100   std::vector<stock> stocks_;
00101 };
00102 
00103 } // namespace s11n_example
00104 
00105 int main(int argc, char* argv[])
00106 {
00107   try
00108   {
00109     // Check command line arguments.
00110     if (argc != 3)
00111     {
00112       std::cerr << "Usage: client <host> <port>" << std::endl;
00113       return 1;
00114     }
00115 
00116     boost::asio::io_service io_service;
00117     s11n_example::client client(io_service, argv[1], argv[2]);
00118     io_service.run();
00119   }
00120   catch (std::exception& e)
00121   {
00122     std::cerr << e.what() << std::endl;
00123   }
00124 
00125   return 0;
00126 }
Copyright © 2003 - 2006 Christopher M. Kohlhoff

Home | Reference | Tutorial | Examples | Design