Search in sources :

Example 36 with TTransport

use of org.apache.thrift.transport.TTransport in project brisk by riptano.

the class CassandraStorage method createConnection.

private static Cassandra.Client createConnection(String host, Integer port, boolean framed) throws IOException {
    TSocket socket = new TSocket(host, port);
    TTransport trans = framed ? new TFramedTransport(socket) : socket;
    try {
        trans.open();
    } catch (TTransportException e) {
        throw new IOException("unable to connect to server", e);
    }
    return new Cassandra.Client(new TBinaryProtocol(trans));
}
Also used : TFramedTransport(org.apache.thrift.transport.TFramedTransport) TTransportException(org.apache.thrift.transport.TTransportException) TTransport(org.apache.thrift.transport.TTransport) IOException(java.io.IOException) TSocket(org.apache.thrift.transport.TSocket)

Example 37 with TTransport

use of org.apache.thrift.transport.TTransport in project simba-os by cegeka.

the class ThriftServlet method doService.

@Override
protected void doService(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    TTransport inTransport;
    TTransport outTransport;
    try {
        response.setContentType("application/x-thrift");
        InputStream in = request.getInputStream();
        OutputStream out = response.getOutputStream();
        TTransport transport = new TIOStreamTransport(in, out);
        inTransport = transport;
        outTransport = transport;
        TProtocol inProtocol = protocolFactory.getProtocol(inTransport);
        TProtocol outProtocol = protocolFactory.getProtocol(outTransport);
        getProcessor(getRequestedServiceName(request)).process(inProtocol, outProtocol);
        out.flush();
    } catch (TException te) {
        throw new ServletException(te);
    }
}
Also used : TException(org.apache.thrift.TException) ServletException(javax.servlet.ServletException) TProtocol(org.apache.thrift.protocol.TProtocol) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) TIOStreamTransport(org.apache.thrift.transport.TIOStreamTransport) TTransport(org.apache.thrift.transport.TTransport)

Example 38 with TTransport

use of org.apache.thrift.transport.TTransport in project cogcomp-nlp by CogComp.

the class CuratorClient method addRecordViewFromCurator.

/**
     * Does the network call to the Curator and fetches a record that has a particular view.
     *
     * @param text The raw text (this will be used if {@link #respectTokenization} is false.
     * @param sentences The list of tokenized sentences (will be {@code null} if
     *        {@link #respectTokenization} is true.
     * @param viewName The view to get (according to the Curator lingo.)
     * @return A {@link edu.illinois.cs.cogcomp.thrift.curator.Record} with the requested view
     */
private Record addRecordViewFromCurator(String text, List<String> sentences, String viewName) throws ServiceUnavailableException, AnnotationFailedException, TException, SocketException {
    viewName = convertCuratorViewName(viewName);
    TTransport transport = new TSocket(this.curatorHost, this.curatorPort);
    logger.debug("Calling curator on host '" + curatorHost + "', port '" + curatorPort + "' for view '" + viewName + "'...");
    try {
        ((TSocket) transport).getSocket().setReuseAddress(true);
    } catch (SocketException e) {
        logger.error("Unable to setReuseAddress!", e);
        throw e;
    }
    transport = new TFramedTransport(transport);
    TProtocol protocol = new TBinaryProtocol(transport);
    transport.open();
    Curator.Client client = new Curator.Client(protocol);
    Record newRecord;
    if (respectTokenization) {
        newRecord = client.wsprovide(viewName, sentences, forceUpdate);
    } else {
        newRecord = client.provide(viewName, text, forceUpdate);
    }
    transport.close();
    return newRecord;
}
Also used : SocketException(java.net.SocketException) TBinaryProtocol(org.apache.thrift.protocol.TBinaryProtocol) TProtocol(org.apache.thrift.protocol.TProtocol) TFramedTransport(org.apache.thrift.transport.TFramedTransport) Curator(edu.illinois.cs.cogcomp.thrift.curator.Curator) Record(edu.illinois.cs.cogcomp.thrift.curator.Record) TTransport(org.apache.thrift.transport.TTransport) TSocket(org.apache.thrift.transport.TSocket)

Example 39 with TTransport

use of org.apache.thrift.transport.TTransport in project dubbo by alibaba.

the class ThriftCodecTest method testEncodeRequest.

@Test
public void testEncodeRequest() throws Exception {
    Request request = createRequest();
    ChannelBuffer output = ChannelBuffers.dynamicBuffer(1024);
    codec.encode(channel, output, request);
    byte[] bytes = new byte[output.readableBytes()];
    output.readBytes(bytes);
    ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
    TTransport transport = new TIOStreamTransport(bis);
    TBinaryProtocol protocol = new TBinaryProtocol(transport);
    // frame
    byte[] length = new byte[4];
    transport.read(length, 0, 4);
    if (bis.markSupported()) {
        bis.mark(0);
    }
    // magic
    Assert.assertEquals(ThriftCodec.MAGIC, protocol.readI16());
    // message length
    int messageLength = protocol.readI32();
    Assert.assertEquals(messageLength + 4, bytes.length);
    // header length
    short headerLength = protocol.readI16();
    // version
    Assert.assertEquals(ThriftCodec.VERSION, protocol.readByte());
    // service name
    Assert.assertEquals(Demo.Iface.class.getName(), protocol.readString());
    // dubbo request id
    Assert.assertEquals(request.getId(), protocol.readI64());
    // test message header length
    if (bis.markSupported()) {
        bis.reset();
        bis.skip(headerLength);
    }
    TMessage message = protocol.readMessageBegin();
    Demo.echoString_args args = new Demo.echoString_args();
    args.read(protocol);
    protocol.readMessageEnd();
    Assert.assertEquals("echoString", message.name);
    Assert.assertEquals(TMessageType.CALL, message.type);
    Assert.assertEquals("Hello, World!", args.getArg());
}
Also used : Demo(com.alibaba.dubbo.rpc.gen.thrift.Demo) Request(com.alibaba.dubbo.remoting.exchange.Request) TIOStreamTransport(org.apache.thrift.transport.TIOStreamTransport) ChannelBuffer(com.alibaba.dubbo.remoting.buffer.ChannelBuffer) TBinaryProtocol(org.apache.thrift.protocol.TBinaryProtocol) ByteArrayInputStream(java.io.ByteArrayInputStream) TMessage(org.apache.thrift.protocol.TMessage) TTransport(org.apache.thrift.transport.TTransport) Test(org.junit.Test)

Example 40 with TTransport

use of org.apache.thrift.transport.TTransport in project brisk by riptano.

the class ThriftUtils method createNamenodeClient.

/**
 * Creates a Thrift name node client.
 *
 * @param conf the HDFS instance
 * @return a Thrift name node client.
 */
public static Namenode.Client createNamenodeClient(Configuration conf) throws Exception {
    String s = conf.get(NamenodePlugin.THRIFT_ADDRESS_PROPERTY, NamenodePlugin.DEFAULT_THRIFT_ADDRESS);
    // TODO(todd) use fs.default.name here if set to 0.0.0.0 - but share this with the code in
    // SecondaryNameNode that does the same
    InetSocketAddress addr = NetUtils.createSocketAddr(s);
    // in the thrift config.
    if (addr.getAddress().isAnyLocalAddress()) {
        InetSocketAddress nnAddr = NameNode.getAddress(conf);
        addr = new InetSocketAddress(nnAddr.getAddress(), addr.getPort());
    }
    TTransport t = new TSocket(addr.getHostName(), addr.getPort());
    if (UserGroupInformation.isSecurityEnabled()) {
        t = new HadoopThriftAuthBridge.Client().createClientTransport(conf.get(DFSConfigKeys.DFS_NAMENODE_USER_NAME_KEY), addr.getHostName(), "KERBEROS", t);
    }
    t.open();
    TProtocol p = new TBinaryProtocol(t);
    return new Namenode.Client(p);
}
Also used : TBinaryProtocol(org.apache.thrift.protocol.TBinaryProtocol) TProtocol(org.apache.thrift.protocol.TProtocol) InetSocketAddress(java.net.InetSocketAddress) TTransport(org.apache.thrift.transport.TTransport) TSocket(org.apache.thrift.transport.TSocket)

Aggregations

TTransport (org.apache.thrift.transport.TTransport)99 TSocket (org.apache.thrift.transport.TSocket)43 TProtocol (org.apache.thrift.protocol.TProtocol)42 TBinaryProtocol (org.apache.thrift.protocol.TBinaryProtocol)36 TFramedTransport (org.apache.thrift.transport.TFramedTransport)28 TTransportException (org.apache.thrift.transport.TTransportException)20 Test (org.junit.Test)20 TException (org.apache.thrift.TException)18 IOException (java.io.IOException)12 TCompactProtocol (org.apache.thrift.protocol.TCompactProtocol)10 ArrayList (java.util.ArrayList)9 TIOStreamTransport (org.apache.thrift.transport.TIOStreamTransport)8 HashMap (java.util.HashMap)6 Socket (java.net.Socket)5 TCLIService (org.apache.hive.service.rpc.thrift.TCLIService)5 THttpClient (org.apache.thrift.transport.THttpClient)4 TSaslClientTransport (org.apache.thrift.transport.TSaslClientTransport)4 ChannelBuffer (com.alibaba.dubbo.remoting.buffer.ChannelBuffer)3 Request (com.alibaba.dubbo.remoting.exchange.Request)3 Demo (com.alibaba.dubbo.rpc.gen.thrift.Demo)3