Example usage for org.apache.cassandra.transport.messages StartupMessage StartupMessage

List of usage examples for org.apache.cassandra.transport.messages StartupMessage StartupMessage

Introduction

In this page you can find the example usage for org.apache.cassandra.transport.messages StartupMessage StartupMessage.

Prototype

public StartupMessage(Map<String, String> options) 

Source Link

Usage

From source file:com.datastax.driver.core.Connection.java

License:Apache License

private void initializeTransport() throws ConnectionException, InterruptedException {

    // TODO: we will need to get fancy about handling protocol version at
    // some point, but keep it simple for now.
    ImmutableMap.Builder<String, String> options = new ImmutableMap.Builder<String, String>();
    options.put(StartupMessage.CQL_VERSION, CQL_VERSION);
    ProtocolOptions.Compression compression = factory.configuration.getProtocolOptions().getCompression();
    if (compression != ProtocolOptions.Compression.NONE) {
        options.put(StartupMessage.COMPRESSION, compression.toString());
        setCompressor(compression.compressor());
    }/*from w  w  w . j  ava 2s . c  o m*/
    StartupMessage startup = new StartupMessage(options.build());
    try {
        Message.Response response = write(startup).get();
        switch (response.type) {
        case READY:
            break;
        case ERROR:
            throw defunct(new TransportException(address, String.format("Error initializing connection: %s",
                    ((ErrorMessage) response).error.getMessage())));
        case AUTHENTICATE:
            CredentialsMessage creds = new CredentialsMessage();
            creds.credentials.putAll(factory.authProvider.getAuthInfo(address));
            Message.Response authResponse = write(creds).get();
            switch (authResponse.type) {
            case READY:
                break;
            case ERROR:
                throw new AuthenticationException(address, (((ErrorMessage) authResponse).error).getMessage());
            default:
                throw defunct(new TransportException(address,
                        String.format("Unexpected %s response message from server to a CREDENTIALS message",
                                authResponse.type)));
            }
            break;
        default:
            throw defunct(new TransportException(address, String
                    .format("Unexpected %s response message from server to a STARTUP message", response.type)));
        }
    } catch (BusyConnectionException e) {
        throw new DriverInternalError("Newly created connection should not be busy");
    } catch (ExecutionException e) {
        throw defunct(new ConnectionException(address,
                String.format("Unexpected error during transport initialization (%s)", e.getCause()),
                e.getCause()));
    }
}