Example usage for org.apache.thrift.transport TSocket TSocket

List of usage examples for org.apache.thrift.transport TSocket TSocket

Introduction

In this page you can find the example usage for org.apache.thrift.transport TSocket TSocket.

Prototype

public TSocket(Socket socket) throws TTransportException 

Source Link

Document

Constructor that takes an already created socket.

Usage

From source file:com.cottsoft.thrift.framework.client.BaseClientFactory.java

License:Apache License

/**
 * ?TTransport//from  w  ww.  ja  v a2 s. c  o  m
 * 
 * @throws ThriftException
 */
public void init() throws ThriftException {
    try {
        Socket s = new Socket(hostIp, port);
        s.setReuseAddress(true);
        s.setSoTimeout(timeout);
        transport = new TSocket(s);
        if (!transport.isOpen()) {
            transport.open();
        }
    } catch (TTransportException e) {
        throw new ThriftException(e);
    } catch (SocketException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (UnknownHostException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

From source file:com.facebook.swift.service.TestThriftSslService.java

License:Apache License

private ResultCode logThrift(int port, List<LogEntry> messages)
        throws TException, IOException, NoSuchAlgorithmException, KeyManagementException {
    SSLContext ctx = SSLContext.getInstance("TLS");
    ctx.init(null, InsecureTrustManagerFactory.INSTANCE.getTrustManagers(), null);
    Socket sslSocket = ctx.getSocketFactory().createSocket("localhost", port);
    TSocket socket = new TSocket(sslSocket);
    try {/*from w w w. j  a v a  2  s.  c  o  m*/
        TBinaryProtocol tp = new TBinaryProtocol(new TFramedTransport(socket));
        return new scribe.Client(tp).Log(messages);
    } finally {
        socket.close();
    }
}

From source file:com.flipkart.phantom.thrift.impl.ThriftProxy.java

License:Apache License

/**
 * Gets a pooled TSocket instance//  w w  w  .ja  va  2 s .  c  o m
 * @return a TSocket instance
 */
public TSocket getPooledSocket() {
    try {
        return new TSocket(this.socketPool.borrowObject());
    } catch (Exception e) {
        LOGGER.error("Error while borrowing TSocket : " + e.getMessage(), e);
        throw new RuntimeException("Error while borrowing TSocket : " + e.getMessage(), e);
    }
}

From source file:com.nearinfinity.blur.thrift.BlurClientManager.java

License:Apache License

public static Client newClient(Connection connection) throws TTransportException, IOException {
    String host = connection.getHost();
    int port = connection.getPort();
    TSocket trans;//from  w ww  .j a v  a2 s .  c o m
    Socket socket;
    if (connection.isProxy()) {
        Proxy proxy = new Proxy(Type.SOCKS,
                new InetSocketAddress(connection.getProxyHost(), connection.getProxyPort()));
        socket = new Socket(proxy);
    } else {
        socket = new Socket();
    }
    socket.setTcpNoDelay(true);
    socket.connect(new InetSocketAddress(host, port));
    trans = new TSocket(socket);

    TProtocol proto = new TBinaryProtocol(new TFramedTransport(trans));
    Client client = new Client(proto);
    return client;
}

From source file:com.twitter.common.thrift.monitoring.TMonitoredServerSocket.java

License:Apache License

@Override
protected TSocket acceptImpl() throws TTransportException {
    final TSocket socket = super.acceptImpl();
    final InetSocketAddress remoteAddress = (InetSocketAddress) socket.getSocket().getRemoteSocketAddress();

    TSocket monitoredSocket = new TSocket(socket.getSocket()) {
        boolean closed = false;

        @Override/*  w w  w  .  java 2 s . c  o m*/
        public void close() {
            try {
                super.close();
            } finally {
                if (!closed) {
                    monitor.released(remoteAddress);
                    addressMap.remove(this);
                }
                closed = true;
            }
        }
    };

    addressMap.put(monitoredSocket, remoteAddress);

    monitor.connected(remoteAddress);
    return monitoredSocket;
}

From source file:com.twitter.common.thrift.ThriftConnectionFactory.java

License:Apache License

@VisibleForTesting
TTransport createTransport(int timeoutMillis) throws TTransportException, IOException {
    TSocket socket = null;// w  ww.  jav a2 s . c  o m
    if (transportType != TransportType.NONBLOCKING) {
        // can't do a nonblocking create on a blocking transport
        if (timeoutMillis <= 0) {
            return null;
        }

        if (sslTransport) {
            SSLSocketFactory factory = (SSLSocketFactory) SSLSocketFactory.getDefault();
            SSLSocket ssl_socket = (SSLSocket) factory.createSocket(endpoint.getHostName(), endpoint.getPort());
            ssl_socket.setSoTimeout(timeoutMillis);
            return new TSocket(ssl_socket);
        } else {
            socket = new TSocket(endpoint.getHostName(), endpoint.getPort(), timeoutMillis);
        }
    }

    try {
        switch (transportType) {
        case BLOCKING:
            socket.open();
            setSocketTimeout(socket);
            return socket;
        case FRAMED:
            TFramedTransport transport = new TFramedTransport(socket);
            transport.open();
            setSocketTimeout(socket);
            return transport;
        case NONBLOCKING:
            try {
                return new TNonblockingSocket(endpoint.getHostName(), endpoint.getPort());
            } catch (IOException e) {
                throw new IOException("Failed to create non-blocking transport to " + endpoint, e);
            }
        }
    } catch (TTransportException e) {
        throw new TTransportException("Failed to create transport to " + endpoint, e);
    }

    throw new IllegalArgumentException("unknown transport type " + transportType);
}

From source file:com.weibo.dip.flume.extension.sink.scribe.ScribeSink.java

License:Apache License

@Override
public void configure(Context context) {
    String name = context.getString(ScribeSinkConfigurationConstants.CONFIG_SINK_NAME, "sink-" + hashCode());

    setName(name);//  w  w  w. j  a va2s  . c  o m

    sinkCounter = new SinkCounter(name);

    batchSize = context.getLong(ScribeSinkConfigurationConstants.CONFIG_BATCHSIZE, 1L);

    String clazz = context.getString(ScribeSinkConfigurationConstants.CONFIG_SERIALIZER,
            EventToLogEntrySerializer.class.getName());

    try {
        serializer = (FlumeEventSerializer) Class.forName(clazz).newInstance();
    } catch (Exception ex) {
        LOGGER.warn("Defaulting to EventToLogEntrySerializer", ex);

        serializer = new EventToLogEntrySerializer();
    } finally {
        serializer.configure(context);
    }

    String host = context.getString(ScribeSinkConfigurationConstants.CONFIG_SCRIBE_HOST);

    int port = context.getInteger(ScribeSinkConfigurationConstants.CONFIG_SCRIBE_PORT);

    try {
        transport = new TFramedTransport(new TSocket(new Socket(host, port)));
    } catch (Exception ex) {
        LOGGER.error("Unable to create Thrift Transport", ex);

        throw new RuntimeException(ex);
    }
}

From source file:de.bitzeche.logback.scribe.ScribeAppender.java

License:Apache License

public void start() {
    int errorCount = 0;
    try {// ww w. j  a v a 2 s .c  om
        synchronized (this) {
            if (hostname == null) {
                try {
                    hostname = InetAddress.getLocalHost().getCanonicalHostName();
                } catch (UnknownHostException e) {
                    // can't get hostname
                    addError("The Hostanme option is mandatory");
                    errorCount++;
                }
            }

            // Thrift boilerplate code
            logEntries = new ArrayList<LogEntry>(1);
            TSocket sock = new TSocket(new Socket(scribeHost, scribePort));
            transport = new TFramedTransport(sock);
            TBinaryProtocol protocol = new TBinaryProtocol(transport, false, false);
            client = new Client(protocol, protocol);

            if (errorCount == 0) {
                super.start();
            }
        }
    } catch (TTransportException e) {
        System.err.println(e);
    } catch (UnknownHostException e) {
        System.err.println(e);
    } catch (IOException e) {
        System.err.println(e);
    } catch (Exception e) {
        System.err.println(e);
    }
}

From source file:edu.washington.cs.cse490h.donut.service.RemoteLocatorClientFactory.java

License:Apache License

@Override
public synchronized KeyLocator.Iface tryOne(TNode node) throws Exception {
    TProtocol protocol;//from   w w w.jav  a 2 s  .  c  o m
    while (socketMap.containsKey(node)) {
        wait();
    }

    Socket socket = new Socket(node.getName(), node.getPort());
    socketMap.put(node, socket);
    protocol = new TBinaryProtocol(new TSocket(socket));
    return new KeyLocator.Client(protocol);
}

From source file:ezbake.thrift.transport.EzSSLTransportFactory.java

License:Apache License

private static TSocket createClient(SSLSocketFactory factory, String host, int port, int timeout)
        throws TTransportException {
    try {//from w  w  w .j  ava  2 s .  com
        SSLSocket socket = (SSLSocket) factory.createSocket(host, port);
        socket.setSoTimeout(timeout);
        return new TSocket(socket);
    } catch (Exception e) {
        throw new TTransportException("Could not connect to " + host + " on port " + port, e);
    }
}