#include <iostream>
#include <boost/asio.hpp>
#include <boost/config/compiler/visualc.hpp>
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/json_parser.hpp>
#include <boost/foreach.hpp>
#include <boost/filesystem.hpp>
#include <boost/filesystem/fstream.hpp>
#include <boost/iostreams/device/mapped_file.hpp>
#include <cassert>
#include <exception>
#include <sstream>
#include <string>
#include <boost/thread.hpp> void test_http()
{
using namespace boost::asio;
ip::tcp::iostream stream; stream.expires_from_now(boost::posix_time::seconds());
stream.connect("www.boost.org", "http");
stream << "GET /LICENSE_1_0.txt HTTP/1.0\r\n";
stream << "Host: www.boost.org\r\n";
stream << "Accept: */*\r\n";
stream << "Connection: close\r\n\r\n"; stream.flush();
std::cout << stream.rdbuf();
} void udp_echo_client()
{ using boost::asio::ip::udp;
boost::asio::io_service my_io_service;
udp::socket s(my_io_service, udp::endpoint(udp::v4(), )); udp::resolver resolver(my_io_service);
} int read_file_content(std::string fileName, std::stringstream& ss)
{
namespace fs = boost::filesystem;
fs::path file(fileName);
if (!fs::exists(file))
{
std::cerr << "file does not exist." << std::endl;
return EXIT_FAILURE;
} //std::ifstream t(file) >> ss;
fs::ifstream t(file);
ss << t.rdbuf();
// boost::iostreams::mapped_file mf(file);
// mf.data() >> ss; return EXIT_SUCCESS;
} void print_json_data(boost::property_tree::ptree const& pt)
{
using boost::property_tree::ptree;
ptree::const_iterator end = pt.end();
for (ptree::const_iterator it = pt.begin(); it != end; it++)
{
std::cout << it->first << ": " << it->second.get_value<std::string>() << std::endl;
print_json_data(it->second);
}
} int parse_json_data()
{
try
{
std::stringstream ss; read_file_content("data.json",ss);
//ss << "{ \"root\": { \"values\": [1, 2, 3, 4, 5 ] } }"; std::cout << ss.str() << std::endl; boost::property_tree::ptree pt;
boost::property_tree::read_json(ss, pt); BOOST_FOREACH(boost::property_tree::ptree::value_type& v, pt.get_child("editorialMarket"))
{
assert(v.first.empty());
std::cout << v.second.data() << std::endl;
} print_json_data(pt);
}
catch (std::exception& e)
{
std::cerr << "error: " << e.what() << std::endl;
} return EXIT_SUCCESS;
} /*
函数 main() 首先定义了一个 I/O 服务 io_service,用于初始化 I/O 对象 timer。
就象 boost::asio::deadline_timer 那样,所有 I/O 对象通常都需要一个 I/O 服务作为它们的构造函数的第一个参数。
由于 timer 的作用类似于一个闹钟,所以 boost::asio::deadline_timer 的构造函数可以传入第二个参数,
用于表示在某个时间点或是在某段时长之后闹钟停止。 以上例子指定了五秒的时长,该闹钟在 timer 被定义之后立即开始计时
*/ void handler1(const boost::system::error_code& ec)
{
std::cout << "5 s" << std::endl;
for (int i = ; i < ; i++)
{
printf("%s: %d\n",__FUNCTION__, i);
boost::this_thread::sleep(boost::posix_time::seconds());
}
} void handler2(const boost::system::error_code& ec)
{
std::cout << "5 s" << std::endl;
for (int i = ; i < ;i++)
{
printf("%s: %d\n", __FUNCTION__, i);
boost::this_thread::sleep(boost::posix_time::seconds());
}
} boost::asio::io_service io_service1;
boost::asio::io_service io_service2;
void run1()
{
printf("in %s\n", __FUNCTION__);
io_service1.run();
} void run2()
{
printf("in %s\n", __FUNCTION__);
io_service2.run();
} void test_io_srv()
{
//boost::asio::io_service io_service;
//创建5s定时器
boost::asio::deadline_timer timer1(io_service1, boost::posix_time::seconds());
timer1.async_wait(handler1); //创建5s定时器
boost::asio::deadline_timer timer2(io_service2, boost::posix_time::seconds());
timer2.async_wait(handler2); //io_service.run();
boost::thread thread1(run1);
boost::thread thread2(run2); thread1.join();
thread2.join();
} int main()
{
//parse_json_data();
test_io_srv();
std::cout << "test_io_srv..." << std::endl;
}