コード例 #1
0
ファイル: RequestServer.hpp プロジェクト: hamarpaul/GnuRadar
         ///////////////////////////////////////////////////////////////////////
         // Starts Request Server in Thread.
         ///////////////////////////////////////////////////////////////////////
         void Run()
         {
            active_ = true;
            std::string reply_msg;
            std::string data;
            gnuradar::ControlMessage request_msg;
            gnuradar::ResponseMessage response_msg;
            zmq::message_t request;

            while( active_ )
            {
               // Wait for next request from client.
               socket_->recv (&request);

               // Parse message from wire.
               request_msg.ParseFromString( zmq_helper::FormatString(request) );

               // Execute client request
               response_msg = this->ExecuteRequest( request_msg );

               // Send reply
               this->SendResponse( response_msg );
            }

            std::cout << "CLOSING REQUEST SERVER SOCKET" << std::endl;
            socket_->close();
         }
コード例 #2
0
void TileDownloader::downloadPictureTile(unsigned int pictureIndex, unsigned int tileIndex)
{
	TileInfo& tileInfo = mPictures[pictureIndex].tiles[tileIndex];
	std::string url      = tileInfo.url;
	std::string filepath = tileInfo.filePath;

	std::string host = DownloadHelper::extractHost(url);
	SocketPtr sock = DownloadHelper::connect(mService, host);

	std::string header = DownloadHelper::createGETHeader(DownloadHelper::removeHost(url, host), host);

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

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

	std::istream response_stream(&response);
	int length = DownloadHelper::getContentLength(response_stream);
	tileInfo.size = (unsigned int) length;

	DownloadHelper::saveBinFile(filepath, response_stream, length);			

	sock->close();
}
コード例 #3
0
ファイル: telnetappender.cpp プロジェクト: Amakata/wajima
void TelnetAppender::SocketHandler::run()
{
	while(!done)
	{
		try 
		{
			SocketPtr newClient = serverSocket.accept();
			SocketOutputStreamPtr os = newClient->getOutputStream();
			if(connections.size() < MAX_CONNECTIONS)
			{
				connections.push_back(newClient);
				writers.push_back(os);

				StringBuffer oss;
				oss << _T("TelnetAppender v1.0 (") << connections.size()
					<< _T(" active connections)\r\n\r\n");
				print(os, oss.str());
				os->flush();
			} 
			else
			{
				print(os, _T("Too many connections.\r\n"));
				os->flush();
				newClient->close();
			}
		} 
		catch(Exception& e) 
		{
			LogLog::error(_T("Encountered error while in SocketHandler loop."), e);
		}
	}
}
コード例 #4
0
ファイル: StatusServer.hpp プロジェクト: hamarpaul/GnuRadar
         void Run()
         {
            std::cout << "StatusServer: START " << std::endl;
            active_ = true;
            gnuradar::StatusMessage status_msg;

            while( active_ )
            {
               status_msg.set_name("status");
               status_msg.set_head(pcModel_->Head() );
               status_msg.set_tail(pcModel_->Tail() );
               status_msg.set_depth( pcModel_->Depth());
               status_msg.set_over_flow(pcModel_->OverFlow() );
               status_msg.set_bytes_per_buffer( pcModel_->BytesPerBuffer() );

					std::string data;
					status_msg.SerializeToString(&data);
					zmq::message_t zmq_msg(data.size());
					memcpy ((void *) zmq_msg.data(), data.c_str(), data.size());
					socket_->send (zmq_msg);

					Sleep(thread::MSEC, gnuradar::constants::STATUS_REFRESH_RATE_MSEC);
				}

				std::cout << "StatusServer: STOP " << std::endl;
				socket_->close();
			}
コード例 #5
0
void Downloader::downloadAllThumbFiles(const std::string& guid, const JsonInfo& info)
{
	for (unsigned int i=0; i<info.thumbs.size(); ++i)
	{
		char buf[10];
		sprintf(buf, "%08d", i);

		std::stringstream filename;
		filename << "thumbs/" << buf << ".jpg";

		std::string filepath = Parser::createFilePath(guid, filename.str());
		if (!bf::exists(filepath))
		{
			std::string host = extractHost(info.thumbs[i].url);
			SocketPtr sock = connect(host);

			std::string header = createGETHeader(removeHost(info.thumbs[i].url, host), host);

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

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

			std::istream response_stream(&response);
			int length = getContentLength(response_stream);

			saveBinFile(filepath, response_stream, length);

			sock->close();
		}
	}
}
コード例 #6
0
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;
}
コード例 #7
0
void Downloader::downloadAllBinFiles(const std::string& guid, Parser* parser)
{
	std::string host = extractHost(parser->getSoapInfo().collectionRoot);

	SocketPtr sock = connect(host);
	boost::asio::socket_base::keep_alive keepAlive(true);
	sock->set_option(keepAlive);

	std::stringstream headers;
	std::vector<std::string> filenames;

	int nbCoorSystem = parser->getNbCoordSystem();

	for (unsigned int i=0; i<parser->getNbCoordSystem(); ++i)
	{
		int nbPointCloud = parser->getNbPointCloud(i);
		for (unsigned int j=0; j<parser->getNbPointCloud(i); ++j)
		{
			std::stringstream filename;
			filename << "bin/points_"<< i << "_" << j << ".bin";
			if (!bf::exists(Parser::createFilePath(guid, filename.str())))
			{
				filenames.push_back(filename.str());
				std::stringstream url;
				url << parser->getSoapInfo().collectionRoot << filename.str().substr(4);
				headers << createGETHeader(removeHost(url.str(), host), host);
			}
		}
	}

	if (filenames.size() > 0)
	{
		//send GET request
		boost::system::error_code error;
		boost::asio::write(*sock, boost::asio::buffer(headers.str().c_str()), boost::asio::transfer_all(), error);

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

		//read all response header and content
		std::istream response_stream(&response);
		for (unsigned int i=0; i<filenames.size(); ++i)
		{
			int length = getContentLength(response_stream);
			saveBinFile(Parser::createFilePath(guid, filenames[i]), response_stream, length);
		}
	}

	sock->close();
}
コード例 #8
0
SocketPtr Downloader::connect(const std::string& url)
{
	tcp::resolver resolver(*(mService));
	tcp::resolver::query query(url, "http");

	tcp::resolver::iterator endpoint_iterator = resolver.resolve(query);
	tcp::resolver::iterator end;

	SocketPtr socket = SocketPtr(new tcp::socket(*(mService)));
	boost::system::error_code error = boost::asio::error::host_not_found;
	while (error && endpoint_iterator != end)
	{
		socket->close();
		socket->connect(*endpoint_iterator++, error);
	}
	if (error)
		throw boost::system::system_error(error);

	return socket;
}
コード例 #9
0
bool TileDownloader::downloadCollection(const std::string& collectionFilePath, const std::string& collectionUrl)
{
	try
	{
		std::string host = DownloadHelper::extractHost(collectionUrl);
		SocketPtr sock = DownloadHelper::connect(mService, host);

		std::string header = DownloadHelper::createGETHeader(DownloadHelper::removeHost(collectionUrl, host), host);

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

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

		sock->close();

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

		//put collection into collectionText
		std::stringstream collectionText;
		while (std::getline(response_stream, line))
			collectionText << line << std::endl;

		//save collection
		if (!DownloadHelper::saveAsciiFile(collectionFilePath, collectionText.str()))
			return false;
	}
	catch (std::exception& e)
	{		
		std::cout << e.what() << std::endl;
		return false;
	}
	return true;
}
コード例 #10
0
bool Downloader::downloadJson(const std::string& guid, const std::string& jsonUrl)
{
	try
	{
		std::string host = extractHost(jsonUrl);
		SocketPtr sock = connect(host);

		std::string header = createGETHeader(removeHost(jsonUrl, host), host);

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

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

		sock->close();

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

		//put json into jsonText
		std::stringstream jsonText;
		while (std::getline(response_stream, line))
			jsonText << line << std::endl;

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