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;
}
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);
}
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);
}
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;
}
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);
}
Aggregations