Example usage for org.apache.thrift.transport TFramedTransport open

List of usage examples for org.apache.thrift.transport TFramedTransport open

Introduction

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

Prototype

public void open() throws TTransportException 

Source Link

Usage

From source file:com.devwebsphere.TestAPIs.java

License:Open Source License

@BeforeClass
static public void initialize() throws TTransportException {
    TTransport port = new TSocket("localhost", 9100);
    TFramedTransport transport = new TFramedTransport(port);
    TCompactProtocol protocol = new TCompactProtocol(transport);
    client = new WxsGatewayService.Client(protocol);
    transport.open();
}

From source file:com.intel.hibench.streambench.storm.metrics.StatFacade.java

License:Apache License

public StatFacade(String host, int port, String topologyName, int interval) {
    COLLECT_INTERVAL = interval * 1000;/*from w  w  w. j a v  a2 s.c  o m*/
    TSocket tsocket = new TSocket(host, port);
    TFramedTransport tTransport = new TFramedTransport(tsocket);
    TBinaryProtocol tBinaryProtocol = new TBinaryProtocol(tTransport);
    client = new Nimbus.Client(tBinaryProtocol);
    try {
        tTransport.open();
        topologyId = getTopologyId(topologyName);
    } catch (TTransportException e) {
        e.printStackTrace();
    }
}

From source file:com.ning.metrics.collector.TestPerformance.java

License:Apache License

private static TFramedTransport createScribeClient() throws TTransportException {
    final TSocket sock = new TSocket("127.0.0.1", 7911);
    final TFramedTransport transport = new TFramedTransport(sock);
    TBinaryProtocol protocol = new TBinaryProtocol(transport, false, false);
    client = new scribe.Client(protocol, protocol);
    transport.open();
    return transport;
}

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

License:Apache License

@VisibleForTesting
TTransport createTransport(int timeoutMillis) throws TTransportException, IOException {
    TSocket socket = null;/*from w ww  . j a v a  2s  . 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:net.floodlightcontroller.core.OFMessageFilterManager.java

License:Apache License

@LogMessageDoc(level = "ERROR", message = "Failed to establish connection with the "
        + "packetstreamer server.", explanation = "The message tracing server is not running "
                + "or otherwise unavailable.", recommendation = LogMessageDoc.CHECK_CONTROLLER)
public boolean connectToPSServer() {
    int numRetries = 0;
    if (transport != null && transport.isOpen()) {
        return true;
    }/*from w  w w. j  a va2  s. c om*/

    while (numRetries++ < MaxRetry) {
        try {
            TFramedTransport t = new TFramedTransport(new TSocket("localhost", serverPort));
            t.open();

            TProtocol protocol = new TBinaryProtocol(t);
            packetClient = new PacketStreamer.Client(protocol);

            log.debug("Have a connection to packetstreamer server " + "localhost:{}", serverPort);
            transport = t;
            break;
        } catch (TException x) {
            try {
                // Wait for 1 second before retry
                if (numRetries < MaxRetry) {
                    Thread.sleep(1000);
                }
            } catch (Exception e) {
            }
        }
    }

    if (numRetries > MaxRetry) {
        log.error("Failed to establish connection with the " + "packetstreamer server.");
        return false;
    }
    return true;
}

From source file:org.apache.cassandra.http.HTTPCassandra.java

License:Apache License

public String[] handleGET(String[] query) {
    String keyspace = query[1];/*www  . java  2 s .  c o  m*/
    String colFam = query[2];
    String key = query[3];
    String superColumn = query[4];
    String startColumn = query[5];
    String endColumn = query[6];

    String[] consistencyLevelReturnType = StringUtils.split(query[7], ".");
    String type = consistencyLevelReturnType[1];
    ConsistencyLevel CL = ConsistencyLevel.valueOf(consistencyLevelReturnType[0]);

    TTransport tr = new TSocket(HOST, PORT);
    TFramedTransport tf = new TFramedTransport(tr);
    TProtocol proto = new TBinaryProtocol(tf);
    Cassandra.Client client = new Cassandra.Client(proto);
    String retData = "";

    try {
        tf.open();
        client.set_keyspace(keyspace);
        byte[] userIDKey = key.getBytes();
        ColumnParent parent = new ColumnParent(colFam);

        SlicePredicate predicate = new SlicePredicate();
        SliceRange sliceRange = new SliceRange();
        sliceRange.setStart(startColumn.getBytes(UTF8));
        sliceRange.setFinish(endColumn.getBytes(UTF8));
        predicate.setSlice_range(sliceRange);
        List<ColumnOrSuperColumn> results = client.get_slice(ByteBuffer.wrap(userIDKey), parent, predicate, CL);

        //create return data
        if ((type.equalsIgnoreCase("json"))) {

            retData = toJSON(results, key);
            type = "application/json";
        } else if ((type.equalsIgnoreCase("xml"))) {

            retData = toXML(results, key);
            type = "text/xml";
        } else if ((type.equalsIgnoreCase("plain"))) {

            CharBuffer cbuf;
            for (ColumnOrSuperColumn result : results) {
                Column column = result.column;
                Charset charset = Charset.forName("UTF8");
                CharsetDecoder decoder = charset.newDecoder();
                cbuf = decoder.decode(column.name);
                retData += "Key: " + cbuf.toString() + " ";
                cbuf = decoder.decode(column.value);
                retData += "Value: " + cbuf.toString() + " \n";
            }
            type = "text/plain";
        }

        tf.close();
    } catch (Exception e) {
        String[] eRet = new String[2];
        if (e instanceof InvalidRequestException) {
            //keyspace or col fam might not exist
            eRet[0] = "400";
            eRet[1] = "Client Request error"; //todo: query inspection
        } else if (e instanceof UnavailableException) {
            eRet[0] = "503";
            eRet[1] = "Service Unavailable";
        } else if (e instanceof TimedOutException) {
            //not the best fit as 4xx codes are client errors, but this isnt a strict http implementation
            eRet[0] = "408";
            eRet[1] = "Request Timeout - Valid query try again later";
        } else {
            eRet[0] = "500";
            eRet[1] = "Internal Server Error";
        }
        return eRet;
    }
    //send return headers
    if (retData.equals("")) {
        String[] eRet = new String[2];
        eRet[0] = "404";
        eRet[1] = "Row Not Found";
        return eRet;
    } else {
        String[] wholeRowRet = new String[3];
        wholeRowRet[0] = "200";
        wholeRowRet[1] = retData;
        wholeRowRet[2] = type;
        return wholeRowRet;
    }
}

From source file:org.apache.cassandra.http.HTTPCassandra.java

License:Apache License

public String[] handlePOST(String[] query, String[] vars) {

    try {//from w  w  w. j a  va  2  s.  c  o  m
        TTransport tr = new TSocket(HOST, PORT);
        TFramedTransport tf = new TFramedTransport(tr);
        TProtocol proto = new TBinaryProtocol(tf);
        Cassandra.Client client = new Cassandra.Client(proto);
        tf.open();

        client.set_keyspace(query[1]);
        ColumnParent cp = new ColumnParent(query[2]);
        byte[] userIDKey = query[3].getBytes();
        ConsistencyLevel CL = ConsistencyLevel.valueOf(query[7]);

        Long clock;

        for (String var : vars) {
            var = java.net.URLDecoder.decode(var, "ISO-8859-1");
            String[] keyval = var.split("=");
            clock = now.getTimeInMillis();
            client.insert(ByteBuffer.wrap(userIDKey), cp, new Column(ByteBuffer.wrap(keyval[0].getBytes(UTF8)),
                    ByteBuffer.wrap(keyval[1].getBytes()), clock), CL);
        }
        System.out.println("");
        tf.close();
    } catch (Exception e) {
        String[] eRet = new String[2];
        if (e instanceof InvalidRequestException) {
            //keyspace or col fam might not exist
            //see wiki discussion
            eRet[0] = "400";
            eRet[1] = "Client Request error"; //todo: query inspection
        } else if (e instanceof UnavailableException) {
            eRet[0] = "503";
            eRet[1] = "Service Unavailable";
        } else if (e instanceof TimedOutException) {
            //not the best fit as 4xx codes are client errors, but this isnt a strict http implementation
            eRet[0] = "408";
            eRet[1] = "Request Timeout - Valid query try again later";
        } else {
            eRet[0] = "500";
            eRet[1] = "Internal Server Error";
        }
        return eRet;
    }

    String[] ret = new String[2];
    ret[0] = "201";
    ret[1] = "Created";
    return ret;
}

From source file:org.apache.cassandra.http.HTTPCassandra.java

License:Apache License

public String[] handleDELETE(String[] query) {
    String keyspace = query[1];/*w  w  w .j av a  2  s .  c o m*/
    String colFam = query[2];
    String key = query[3];
    String superColumn = query[4];
    String startColumn = query[5];
    String endColumn = query[6];
    ConsistencyLevel CL = ConsistencyLevel.valueOf(query[7]);

    TTransport tr = new TSocket(HOST, PORT);
    TFramedTransport tf = new TFramedTransport(tr);
    TProtocol proto = new TBinaryProtocol(tf);
    Cassandra.Client client = new Cassandra.Client(proto);
    String retData = "";
    long timestamp = 0;

    try {
        tf.open();
        client.set_keyspace(keyspace);
        byte[] userIDKey = key.getBytes();
        ColumnParent parent = new ColumnParent(colFam);

        SlicePredicate predicate = new SlicePredicate();
        SliceRange sliceRange = new SliceRange();
        sliceRange.setStart(startColumn.getBytes(UTF8));
        sliceRange.setFinish(endColumn.getBytes(UTF8));
        predicate.setSlice_range(sliceRange);
        List<ColumnOrSuperColumn> results = client.get_slice(ByteBuffer.wrap(userIDKey), parent, predicate, CL);
        //doesnt need to loop over all
        for (ColumnOrSuperColumn result : results) {
            timestamp = result.column.getTimestamp();
        }

        ColumnPath colPath = new ColumnPath(colFam);
        client.remove(ByteBuffer.wrap(userIDKey), colPath, timestamp, ConsistencyLevel.ONE);
        System.out.println("del");
        tf.close();
    } catch (Exception e) {
        String[] eRet = new String[2];
        if (e instanceof InvalidRequestException) {
            //keyspace or col fam might not exist
            eRet[0] = "400";
            eRet[1] = "Client Request error"; //todo: query inspection
        } else if (e instanceof UnavailableException) {
            eRet[0] = "503";
            eRet[1] = "Service Unavailable";
        } else if (e instanceof TimedOutException) {
            //not the best fit as 4xx codes are client errors, but this isnt a strict http implementation
            eRet[0] = "408";
            eRet[1] = "Request Timeout - Valid query try again later";
        } else {
            eRet[0] = "500";
            eRet[1] = "Internal Server Error";
        }
        return eRet;
    }
    String[] ret = new String[2];
    ret[0] = "201";
    ret[1] = "Created";
    return ret;

}

From source file:org.apache.hadoop.corona.ClusterManagerAvailabilityChecker.java

License:Apache License

/**
 * Used for getting a client to the CoronaProxyJobTracker
 * @param conf/*w  w w  . j  ava 2s  . co m*/
 * @return Returns a client to the CPJT
 * @throws IOException
 */
public static CoronaProxyJobTrackerService.Client getPJTClient(CoronaConf conf) throws IOException {
    InetSocketAddress address = NetUtils.createSocketAddr(conf.getProxyJobTrackerThriftAddress());
    TFramedTransport transport = new TFramedTransport(new TSocket(address.getHostName(), address.getPort()));
    CoronaProxyJobTrackerService.Client client = new CoronaProxyJobTrackerService.Client(
            new TBinaryProtocol(transport));
    try {
        transport.open();
    } catch (TException e) {
        LOG.info("Transport Exception: ", e);
    }
    return client;
}

From source file:org.apache.hadoop.corona.CoronaAdmin.java

License:Apache License

/**
 * Command to ask the Cluster Manager to reread the hosts and excluded hosts
 * file./*from  w  ww .ja  va 2s .c  om*/
 *
 * @exception IOException
 * @return Returns 0 where no exception is thrown.
 */
private int refreshNodes() throws IOException {
    // Get the current configuration
    CoronaConf conf = new CoronaConf(getConf());

    InetSocketAddress address = NetUtils.createSocketAddr(conf.getClusterManagerAddress());
    TFramedTransport transport = new TFramedTransport(new TSocket(address.getHostName(), address.getPort()));
    ClusterManagerService.Client client = new ClusterManagerService.Client(new TBinaryProtocol(transport));

    try {
        transport.open();
        client.refreshNodes();
    } catch (SafeModeException e) {
        System.err.println("ClusterManager is in Safe Mode");
    } catch (TException e) {
        throw new IOException(e);
    }

    return 0;
}