void SocketConnectorAcceptorTest::testSpawnedAcceptor ()
{
#ifdef VPR_OS_Windows
    long rand_num(rand());
#else
    long rand_num(random());
#endif
    mRendevousPort = 47000 + (rand_num % 71);     // Get a partially random port
    mNumItersA = 5;
    mMessageValue = std::string("The Data");
    mMessageLen = mMessageValue.length();

    mState = NOT_READY;                        // Initialize

    // Spawn acceptor thread
    vpr::Thread acceptor_thread(
        boost::bind(&SocketConnectorAcceptorTest::testSpawnedAcceptor_acceptor,
                    this)
    );

    // Spawn connector thread
    vpr::Thread connector_thread(
        boost::bind(&SocketConnectorAcceptorTest::testSpawnedAcceptor_connector,
                    this)
    );

    CPPUNIT_ASSERT( acceptor_thread.valid() && "Invalid acceptor thread");
    CPPUNIT_ASSERT( connector_thread.valid() && "Invalid connector thread");

    if(!acceptor_thread.valid())
        std::cerr << "Invalid acceptor thread\n";
    if(!connector_thread.valid())
        std::cerr << "Invalid connector_thread\n";

    // Wait for threads
    acceptor_thread.join();
    connector_thread.join();
}
void SocketBandwidthIOStatsTest::testBandwidth()
{
#ifdef VPR_OS_Windows
   long rand_num(rand());
#else
   long rand_num(random());
#endif
   mRendevousPort = 47000 + (rand_num % 71);     // Get a partially random port
   mNumItersA = 1;
   mNumItersB = 5000;
   mMessageValue = std::vector<vpr::Uint8>(1000, 21);      // 10000 bytes of data
   mMessageLen = mMessageValue.size();

   mState = NOT_READY;                        // Initialize

   // Spawn acceptor thread
   vpr::Thread acceptor_thread(
      boost::bind(&SocketBandwidthIOStatsTest::testBandwidth_acceptor, this)
   );

   // Spawn connector thread
   vpr::Thread connector_thread(
      boost::bind(&SocketBandwidthIOStatsTest::testBandwidth_connector, this)
   );

   CPPUNIT_ASSERT( acceptor_thread.valid() && "Invalid acceptor thread");
   CPPUNIT_ASSERT( connector_thread.valid() && "Invalid connector thread");

   if(!acceptor_thread.valid())
      std::cerr << "Invalid acceptor thread\n";
   if(!connector_thread.valid())
      std::cerr << "Invalid connector_thread\n";

   // Wait for threads
   acceptor_thread.join();
   connector_thread.join();
}
Exemple #3
0
static int retrieve_url(AbstractClient *const client, const std::wstring &url_string, const http_verb_t &http_verb, const URL &url, const std::wstring &post_data, const std::wstring &referrer, const std::wstring &outFileName, const bool &set_ftime, const bool &update_mode, const bool &alert, const bool &keep_failed)
{
	//Initialize the post data string
	const std::string post_data_encoded = post_data.empty() ? std::string() : ((post_data.compare(L"-") != 0) ? URL::urlEncode(Utils::wide_str_to_utf8(post_data)) : URL::urlEncode(stdin_get_line()));

	//Detect filestamp of existing file
	const uint64_t timestamp_existing = update_mode ? Utils::get_file_time(outFileName) : AbstractClient::TIME_UNKNOWN;
	if(update_mode && (timestamp_existing == AbstractClient::TIME_UNKNOWN))
	{
		std::wcerr << L"WARNING: Local file does not exist yet, going to download unconditionally!\n" << std::endl;
	}

	//Create the HTTPS connection/request
	std::wcerr << L"Connecting to " << url.getHostName() << L':' << url.getPortNo() << L", please wait..." << std::endl;
	std::unique_ptr<ConnectorThread> connector_thread (new ConnectorThread(client, http_verb, url, post_data_encoded, referrer, timestamp_existing));
	if(!connector_thread->start())
	{
		TRIGGER_SYSTEM_SOUND(alert, false);
		std::wcerr << L"ERROR: Failed to start the file connector thread!\n" << std::endl;
		return EXIT_FAILURE;
	}

	//Wait for connection
	while(!connector_thread->join(Zero::g_sigUserAbort))
	{
		//Check for user abort
		if(ABORTED_BY_USER)
		{
			std::wcerr << L"--> Aborting!\n"<< std::endl;
			connector_thread->stop(1250, true);
			std::wcerr << L"SIGINT: Operation aborted by the user !!!\n" << std::endl;
			return EXIT_FAILURE;
		}
	}

	//Add extra space
	std::wcerr << std::endl;

	//Check thread result
	const uint32_t thread_result = connector_thread->get_result();
	if(thread_result != ConnectorThread::CONNECTION_COMPLETE)
	{
		const std::wstring error_text = connector_thread->get_error_text();
		switch(thread_result)
		{
		case ConnectorThread::CONNECTION_ERR_INET:
			if(!error_text.empty())
			{
				std::wcerr << error_text << L'\n' << std::endl;
			}
			std::wcerr << "ERROR: Connection could not be established!\n" << std::endl;
			break;
		case ConnectorThread::CONNECTION_ERR_ABRT:
			std::wcerr << L"ERROR: The operation has been aborted !!!\n" << std::endl;
			break;
		default:
			std::wcerr << L"ERROR: The operation failed for an unknwon reason!\n" << std::endl;
			break;
		}
		TRIGGER_SYSTEM_SOUND(alert, false);
		return EXIT_FAILURE;
	}

	//Check for user abort
	if(ABORTED_BY_USER)
	{
		std::wcerr << L"SIGINT: Operation aborted by the user !!!\n" << std::endl;
		return EXIT_FAILURE;
	}

	//Initialize local variables
	bool success;
	uint32_t status_code;
	std::wstring content_type, content_encd;
	uint64_t file_size, timestamp;

	//Query result information
	if(!client->result(success, status_code, file_size, timestamp, content_type, content_encd))
	{
		TRIGGER_SYSTEM_SOUND(alert, false);
		const std::wstring error_text = client->get_error_text();
		if(!error_text.empty())
		{
			std::wcerr << error_text << L'\n' << std::endl;
		}
		std::wcerr << "ERROR: Failed to query the response status!\n" << std::endl;
		return EXIT_FAILURE;
	}

	//Skip download this time?
	if(update_mode && (status_code == 304))
	{
		TRIGGER_SYSTEM_SOUND(alert, true);
		std::wcerr << L"SKIPPED: Server currently does *not* provide a newer version of the file." << std::endl;
		std::wcerr << L"         Version created at '" << Utils::timestamp_to_str(timestamp_existing) << L"' was retained.\n" << std::endl;
		return EXIT_SUCCESS;
	}

	//Print some status information
	std::wcerr << L"HTTP response successfully received from server:\n";
	print_response_info(status_code, file_size, timestamp, content_type, content_encd);

	//Request successful?
	if(!success)
	{
		TRIGGER_SYSTEM_SOUND(alert, false);
		std::wcerr << "ERROR: The server failed to handle this request! [Status " << status_code << "]\n" << std::endl;
		return EXIT_FAILURE;
	}

	//Check for user abort
	if(ABORTED_BY_USER)
	{
		std::wcerr << L"SIGINT: Operation aborted by the user !!!\n" << std::endl;
		return EXIT_FAILURE;
	}

	return transfer_file(client, url_string, file_size, (set_ftime ? timestamp : 0), outFileName, alert, keep_failed);
}