示例#1
0
文件: Master.cpp 项目: yvt/Merlion
    Master::Master(const std::shared_ptr<Library>& library, const MasterParameters& parameters):
	_library(library),
	_parameters(parameters),
	nodeAcceptor(library->ioService(), parseTcpEndpoint(parameters.nodeEndpoint)),
	clientAcceptor(library->ioService(), parseTcpEndpoint(parameters.clientEndpoint)),
	heartbeatTimer(library->ioService()),
	heartbeatRunning(true),
	_sslContext(ssl::context::tlsv1),
	disposed(false),
	nodeAcceptorRunning(true),
	clientAcceptorRunning(true)
	{
		// Setup SSL
		std::string pw = parameters.sslPassword;
		sslContext().set_password_callback([pw](std::size_t, ssl::context::password_purpose) { return pw; });
		
		try {
			BOOST_LOG_SEV(log, LogLevel::Info) <<
			format("Using %s as the certificate chain file.") % parameters.sslCertificateFile;
			
			sslContext().use_certificate_chain_file(parameters.sslCertificateFile);
		} catch (const std::exception& ex) {
			MSCThrow(InvalidDataException
			(str(format("Error occured while attempting to use '%s' as the "
						"certificate chain file: %s") %
				 parameters.sslCertificateFile % ex.what())));
		}
		try {
			BOOST_LOG_SEV(log, LogLevel::Info) <<
			format("Using %s as the private key file.") % parameters.sslPrivateKeyFile;
			
			sslContext().use_private_key_file(parameters.sslPrivateKeyFile, boost::asio::ssl::context::pem);
		} catch (const std::exception& ex) {
			MSCThrow(InvalidDataException
			(str(format("Error occured while attempting to use '%s' as the "
						"private key file: %s") %
				 parameters.sslPrivateKeyFile % ex.what())));
		}
		
		BOOST_LOG_SEV(log, LogLevel::Debug) << "SSL is ready.";
		
		// Prepare to accept the first client and node
		BOOST_LOG_SEV(log, LogLevel::Debug) << "Preparing to accept clients and nodes.";
		waitingNodeConnection = std::make_shared<MasterNodeConnection>(*this);
		waitingClient = std::make_shared<MasterClient>(*this, 1,
													   _parameters.allowVersionSpecification);
		
        acceptNodeConnectionAsync(true);
        acceptClientAsync(true);

		// Start heartbeat
		BOOST_LOG_SEV(log, LogLevel::Debug) << "Starting heartbeat.";
        doHeartbeat(boost::system::error_code());
		
		BOOST_LOG_SEV(log, LogLevel::Info) <<
		format("Merlion Master Server Core (%s) running.") % MSC_VERSION_STRING;
    }
static void testSSLConnect()
{
    //
    //  Create WSMANExportClient
    //
    Monitor monitor;
    HTTPConnector httpConnector(&monitor);
    WSMANExportClient client(&httpConnector,&monitor);

    //
    //  Create SSLContext
    //
    const char* pegasusHome = getenv("PEGASUS_HOME");
    String trustStore = FileSystem::getAbsolutePath(pegasusHome,
        "client.pem");
    String certPath = FileSystem::getAbsolutePath(pegasusHome,
        "cert.pem");
    String keyPath = FileSystem::getAbsolutePath(pegasusHome,
        "file.pem");
    String randPath;
#ifdef PEGASUS_SSL_RANDOMFILE
    randPath = FileSystem::getAbsolutePath(pegasusHome,
        PEGASUS_SSLCLIENT_RANDOMFILE);
#endif

    AutoPtr<SSLContext> sslContext(
        new SSLContext(trustStore, certPath, keyPath, 0, randPath));

    //
    //  Look up port number
    //
    Uint32 port =
        System::lookupPort(WBEM_HTTPS_SERVICE_NAME, WBEM_DEFAULT_HTTPS_PORT);

    //
    //  Test SSL connect with empty string host
    //
    Boolean exceptionCaught = false;
    try
    {
        client.connect("", port, *sslContext);
    }
    catch(const Exception&)
    {
        exceptionCaught = true;
    }
    PEGASUS_TEST_ASSERT(!exceptionCaught);

    //
    //  Test SSL connect when already connected
    //
    Boolean alreadyConnectedCaught = false;
    try
    {
        client.connect("localhost", port, *sslContext);
    }
    catch(const AlreadyConnectedException&)
    {
        alreadyConnectedCaught = true;
    }
    PEGASUS_TEST_ASSERT(alreadyConnectedCaught);
    client.disconnect();

    //
    //  Test SSL connect with bad hostname to cause exception
    //
    Boolean invalidLocatorCaught = false;
    try
    {
        client.connect("nonexistant-zxcvqw3r", port, *sslContext);
    }
    catch(const InvalidLocatorException&)
    {
        invalidLocatorCaught = true;
    }
    PEGASUS_TEST_ASSERT(invalidLocatorCaught);
}