Example #1
0
int main() 
try {
    SocketFactoryPtr factory = SocketFactory::create_factory();
    SocketPtr socket = factory->create_socket( true );
    socket->signal_data_received().connect( handle_data );
    socket->signal_remote_disconnect().connect( handle_stop );
    socket->connect_to_server( "127.0.0.1", 1024 );
    while( true )
    {
        {
            std::lock_guard<std::mutex> l(g_stop_mutex);
            if( g_stop )
            {
                socket->shutdown();
                break;
            }
        }
        std::string msg;
        std::cout<<"say? ";
        getline( std::cin, msg );
        if( "QUIT" == msg )
        {
            std::lock_guard<std::mutex> l(g_stop_mutex);
            g_stop = true;
            socket->shutdown();
        }
        else if( "STOPLISTENER" == msg )
        {
            send_data( socket, msg );
            socket->shutdown();
            std::lock_guard<std::mutex> l(g_stop_mutex);
            g_stop = true;
        }
        else
        {
            send_data( socket, msg );
        }
    }
    return 0;
}
catch( boost::exception &error ) {
    int const *errorNo
                = boost::get_error_info<boost::errinfo_errno>( error );
    std::cerr<<"Error occurred [" <<strerror(*errorNo) <<"]"<<std::endl;
    return -1;
}
catch( std::exception &e ) {
    std::cerr<<"Error occurred [" <<e.what() <<"]"<<std::endl;
    return -1;
}
bool Downloader::downloadSoap(const std::string& guid)
{
	try
	{
		SocketPtr sock = connect("www.photosynth.net");

		//prepare header + content
		std::stringstream content;
		content << "<?xml version=\"1.0\" encoding=\"utf-8\"?>"<<std::endl;
		content << "<soap12:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap12=\"http://www.w3.org/2003/05/soap-envelope\">"<<std::endl;
		content << "	<soap12:Body>"<<std::endl;
		content << "		<GetCollectionData xmlns=\"http://labs.live.com/\">"<<std::endl;
		content << "		<collectionId>"<<guid<<"</collectionId>"<<std::endl;
		content << "		<incrementEmbedCount>false</incrementEmbedCount>"<<std::endl;
		content << "		</GetCollectionData>"<<std::endl;
		content << "	</soap12:Body>"<<std::endl;
		content << "</soap12:Envelope>"<<std::endl;

		std::stringstream header;
		header << "POST /photosynthws/PhotosynthService.asmx HTTP/1.1"<<std::endl;
		header << "Host: photosynth.net"<<std::endl;
		header << "Content-Type: application/soap+xml; charset=utf-8"<<std::endl;
		header << "Content-Length: "<<content.str().size()<<std::endl;
		header << ""<<std::endl;

		//send header + content
		boost::system::error_code error;
		boost::asio::write(*sock, boost::asio::buffer(header.str().c_str()), boost::asio::transfer_all(), error);
		boost::asio::write(*sock, boost::asio::buffer(content.str().c_str()), boost::asio::transfer_all(), error);

		sock->shutdown(tcp::socket::shutdown_send);

		//read the response
		boost::asio::streambuf response;
		while (boost::asio::read(*sock, response, boost::asio::transfer_at_least(1), error));

		sock->close();

		std::istream response_stream(&response);
		std::string line;

		//skip header from response
		while (std::getline(response_stream, line) && line != "\r");		

		//put soap response into xml
		std::stringstream xml;
		while (std::getline(response_stream, line))
			xml << line << std::endl;

		//save soap response
		if (!saveAsciiFile(Parser::createFilePath(guid, Parser::soapFilename), xml.str()))
			return false;		
	}
	catch (std::exception& e)
	{		
		std::cerr << e.what() << std::endl;
		return false;
	}
	return true;
}