Search in sources :

Example 16 with TBinaryProtocol

use of org.apache.thrift.protocol.TBinaryProtocol in project carat by amplab.

the class ProtocolClient method getInstance.

/**
     * FIXME: this needs to come from a factory, so that connections are not
     * kept open unnecessarily, and that they do not become stale, and that we
     * handle disconnections gracefully.
     * 
     * @param c
     * @return
     * @throws NumberFormatException 
     * @throws TTransportException 
     */
public static CaratService.Client getInstance(Context c) throws NumberFormatException, TTransportException {
    if (SERVER_ADDRESS == null) {
        Properties properties = new Properties();
        try {
            InputStream raw = c.getAssets().open(SERVER_PROPERTIES);
            if (raw != null) {
                properties.load(raw);
                if (properties.containsKey("PORT"))
                    SERVER_PORT = Integer.parseInt(properties.getProperty("PORT", "8080"));
                if (properties.containsKey("ADDRESS"))
                    SERVER_ADDRESS = properties.getProperty("ADDRESS", "server.caratproject.com");
                Log.d(TAG, "Set address=" + SERVER_ADDRESS + " port=" + SERVER_PORT);
            } else
                Log.e(TAG, "Could not open server property file!");
        } catch (IOException e) {
            Log.e(TAG, "Could not open server property file: " + e.toString());
        }
    }
    if (SERVER_ADDRESS == null || SERVER_PORT == 0)
        return null;
    TSocket soc = new TSocket(SERVER_ADDRESS, SERVER_PORT, 60000);
    TProtocol p = new TBinaryProtocol(soc, true, true);
    CaratService.Client instance = new CaratService.Client(p);
    if (soc != null && !soc.isOpen())
        soc.open();
    return instance;
}
Also used : CaratService(edu.berkeley.cs.amplab.carat.thrift.CaratService) TBinaryProtocol(org.apache.thrift.protocol.TBinaryProtocol) TProtocol(org.apache.thrift.protocol.TProtocol) InputStream(java.io.InputStream) IOException(java.io.IOException) Properties(java.util.Properties) TSocket(org.apache.thrift.transport.TSocket)

Example 17 with TBinaryProtocol

use of org.apache.thrift.protocol.TBinaryProtocol in project titan by thinkaurelius.

the class CTConnectionFactory method makeRawConnection.

/**
     * Create a Cassandra-Thrift connection, but do not attempt to
     * set a keyspace on the connection.
     *
     * @return A CTConnection ready to talk to a Cassandra cluster
     * @throws TTransportException on any Thrift transport failure
     */
public CTConnection makeRawConnection() throws TTransportException {
    final Config cfg = cfgRef.get();
    String hostname = cfg.getRandomHost();
    log.debug("Creating TSocket({}, {}, {}, {}, {})", hostname, cfg.port, cfg.username, cfg.password, cfg.timeoutMS);
    TSocket socket;
    if (null != cfg.sslTruststoreLocation && !cfg.sslTruststoreLocation.isEmpty()) {
        TSSLTransportFactory.TSSLTransportParameters params = new TSSLTransportFactory.TSSLTransportParameters() {

            {
                setTrustStore(cfg.sslTruststoreLocation, cfg.sslTruststorePassword);
            }
        };
        socket = TSSLTransportFactory.getClientSocket(hostname, cfg.port, cfg.timeoutMS, params);
    } else {
        socket = new TSocket(hostname, cfg.port, cfg.timeoutMS);
    }
    TTransport transport = new TFramedTransport(socket, cfg.frameSize);
    log.trace("Created transport {}", transport);
    TBinaryProtocol protocol = new TBinaryProtocol(transport);
    Cassandra.Client client = new Cassandra.Client(protocol);
    if (!transport.isOpen()) {
        transport.open();
    }
    if (cfg.username != null) {
        Map<String, String> credentials = new HashMap<String, String>() {

            {
                put(IAuthenticator.USERNAME_KEY, cfg.username);
                put(IAuthenticator.PASSWORD_KEY, cfg.password);
            }
        };
        try {
            client.login(new AuthenticationRequest(credentials));
        } catch (Exception e) {
            // TTransportException will propagate authentication/authorization failure
            throw new TTransportException(e);
        }
    }
    return new CTConnection(transport, client, cfg);
}
Also used : TBinaryProtocol(org.apache.thrift.protocol.TBinaryProtocol)

Example 18 with TBinaryProtocol

use of org.apache.thrift.protocol.TBinaryProtocol in project alluxio by Alluxio.

the class ThriftClientPool method createNewResource.

/**
   * Creates a thrift client instance.
   *
   * @return the thrift client created
   * @throws IOException if it fails to create a thrift client
   */
@Override
protected T createNewResource() throws IOException {
    TTransport transport = mTransportProvider.getClientTransport(mParentSubject, mAddress);
    TProtocol binaryProtocol = new TBinaryProtocol(transport);
    T client = createThriftClient(new TMultiplexedProtocol(binaryProtocol, mServiceName));
    TException exception;
    RetryPolicy retryPolicy = new ExponentialBackoffRetry(BASE_SLEEP_MS, MAX_SLEEP_MS, RPC_MAX_NUM_RETRY);
    do {
        LOG.info("Alluxio client (version {}) is trying to connect with {} {} @ {}", RuntimeConstants.VERSION, mServiceName, mAddress);
        try {
            if (!transport.isOpen()) {
                transport.open();
            }
            if (transport.isOpen()) {
                checkVersion(client);
            }
            LOG.info("Client registered with {} @ {}", mServiceName, mAddress);
            return client;
        } catch (TTransportException e) {
            if (e.getCause() instanceof java.net.SocketTimeoutException) {
                // Do not retry if socket timeout.
                String message = "Thrift transport open times out. Please check whether the " + "authentication types match between client and server. Note that NOSASL client " + "is not able to connect to servers with SIMPLE security mode.";
                throw new IOException(message, e);
            }
            LOG.warn("Failed to connect ({}) to {} @ {}: {}", retryPolicy.getRetryCount(), mServiceName, mAddress, e.getMessage());
            exception = e;
        }
    } while (retryPolicy.attemptRetry());
    LOG.error("Failed after " + retryPolicy.getRetryCount() + " retries.");
    Preconditions.checkNotNull(exception);
    throw new IOException(exception);
}
Also used : TException(org.apache.thrift.TException) ExponentialBackoffRetry(alluxio.retry.ExponentialBackoffRetry) TTransportException(org.apache.thrift.transport.TTransportException) IOException(java.io.IOException) TMultiplexedProtocol(org.apache.thrift.protocol.TMultiplexedProtocol) TBinaryProtocol(org.apache.thrift.protocol.TBinaryProtocol) TProtocol(org.apache.thrift.protocol.TProtocol) TTransport(org.apache.thrift.transport.TTransport) RetryPolicy(alluxio.retry.RetryPolicy)

Example 19 with TBinaryProtocol

use of org.apache.thrift.protocol.TBinaryProtocol in project heron by twitter.

the class ScribeSink method open.

// Open the TTransport connection and client to scribe server
private boolean open() {
    try {
        TSocket socket = new TSocket((String) config.get(KEY_SCRIBE_HOST), TypeUtils.getInteger(config.get(KEY_SCRIBE_PORT)), TypeUtils.getInteger(config.get(KEY_SCRIBE_TIMEOUT_MS)));
        transport = new TFramedTransport(socket);
        transport.open();
    } catch (TException tx) {
        LOG.log(Level.SEVERE, "Failed to open connection to scribe server " + connectionString(), tx);
        return false;
    }
    LOG.info("Opened connection to scribe server " + connectionString());
    TProtocol protocol = new TBinaryProtocol(transport);
    client = new scribe.Client(protocol);
    return true;
}
Also used : TException(org.apache.thrift.TException) TBinaryProtocol(org.apache.thrift.protocol.TBinaryProtocol) TProtocol(org.apache.thrift.protocol.TProtocol) TFramedTransport(org.apache.thrift.transport.TFramedTransport) TSocket(org.apache.thrift.transport.TSocket) org.apache.scribe.scribe(org.apache.scribe.scribe)

Example 20 with TBinaryProtocol

use of org.apache.thrift.protocol.TBinaryProtocol in project eiger by wlloyd.

the class WordCountSetup method createConnection.

private static Cassandra.Client createConnection(String host, Integer port, boolean framed) throws TTransportException {
    TSocket socket = new TSocket(host, port);
    TTransport trans = framed ? new TFramedTransport(socket) : socket;
    trans.open();
    TProtocol protocol = new TBinaryProtocol(trans);
    return new Cassandra.Client(protocol);
}
Also used : TBinaryProtocol(org.apache.thrift.protocol.TBinaryProtocol) TProtocol(org.apache.thrift.protocol.TProtocol) TFramedTransport(org.apache.thrift.transport.TFramedTransport) TTransport(org.apache.thrift.transport.TTransport) TSocket(org.apache.thrift.transport.TSocket)

Aggregations

TBinaryProtocol (org.apache.thrift.protocol.TBinaryProtocol)52 TSocket (org.apache.thrift.transport.TSocket)34 TProtocol (org.apache.thrift.protocol.TProtocol)26 TTransport (org.apache.thrift.transport.TTransport)25 TFramedTransport (org.apache.thrift.transport.TFramedTransport)21 TException (org.apache.thrift.TException)16 TIOStreamTransport (org.apache.thrift.transport.TIOStreamTransport)11 IOException (java.io.IOException)10 TMessage (org.apache.thrift.protocol.TMessage)8 TTransportException (org.apache.thrift.transport.TTransportException)8 Test (org.junit.Test)7 Request (com.alibaba.dubbo.remoting.exchange.Request)6 Demo (com.alibaba.dubbo.rpc.gen.thrift.Demo)6 ChannelBuffer (com.alibaba.dubbo.remoting.buffer.ChannelBuffer)5 RpcResult (com.alibaba.dubbo.rpc.RpcResult)5 RandomAccessByteArrayOutputStream (com.alibaba.dubbo.rpc.protocol.thrift.io.RandomAccessByteArrayOutputStream)5 Hbase (org.apache.hadoop.hbase.thrift.generated.Hbase)5 URL (com.alibaba.dubbo.common.URL)4 Channel (com.alibaba.dubbo.remoting.Channel)4 Response (com.alibaba.dubbo.remoting.exchange.Response)4