Search in sources :

Example 16 with ChannelBufferOutputStream

use of org.jboss.netty.buffer.ChannelBufferOutputStream in project MSEC by Tencent.

the class NettyClient method sendRequestAndWaitResponse.

// ��RPC��
public RpcResponse sendRequestAndWaitResponse(CustomPackageCodec packageCodec, RpcRequest request, int timeoutMillis) {
    RpcResponse response = null;
    ChannelBufferOutputStream bout = new ChannelBufferOutputStream(ChannelBuffers.dynamicBuffer(1024));
    long sequence = request.getSeq();
    int ret = 0;
    try {
        ret = packageCodec.encode(sequence, bout);
    } catch (IOException ex) {
        response = new RpcResponse();
        response.setErrno(-1);
        response.setError(ex);
        return response;
    }
    if (ret != 0) {
        response = new RpcResponse();
        response.setErrno(-1);
        response.setError(new Exception("Encode request failed."));
        return response;
    }
    Object[] objects = new Object[3];
    objects[0] = bout.buffer();
    objects[1] = packageCodec;
    objects[2] = null;
    if (sendRequest(objects) == null)
        return null;
    CallbackFuture callback = new CallbackFuture(request, this);
    sessions.put(request.getSeq(), callback);
    try {
        response = callback.getResponse(timeoutMillis);
    } catch (Exception ex) {
        // TODO: set exception
        System.out.println("Exception occurs: " + ex);
        response = new RpcResponse();
        response.setErrno(-1);
        response.setError(ex);
    } finally {
        sessions.remove(request.getSeq());
    }
    return response;
}
Also used : ChannelBufferOutputStream(org.jboss.netty.buffer.ChannelBufferOutputStream) IOException(java.io.IOException) IOException(java.io.IOException)

Example 17 with ChannelBufferOutputStream

use of org.jboss.netty.buffer.ChannelBufferOutputStream in project NabAlive by jcheype.

the class Response method writeJSON.

public void writeJSON(Object object) throws IOException {
    ChannelBuffer channelBuffer = ChannelBuffers.dynamicBuffer();
    ChannelBufferOutputStream outputStream = new ChannelBufferOutputStream(channelBuffer);
    mapper.writeValue(outputStream, object);
    outputStream.close();
    write(channelBuffer);
}
Also used : ChannelBufferOutputStream(org.jboss.netty.buffer.ChannelBufferOutputStream) ChannelBuffer(org.jboss.netty.buffer.ChannelBuffer)

Example 18 with ChannelBufferOutputStream

use of org.jboss.netty.buffer.ChannelBufferOutputStream in project NabAlive by jcheype.

the class Response method writeXML.

public void writeXML(Object object) {
    ChannelBuffer channelBuffer = ChannelBuffers.dynamicBuffer();
    ChannelBufferOutputStream outputStream = new ChannelBufferOutputStream(channelBuffer);
    xstreamXML.toXML(object, outputStream);
    write(channelBuffer);
}
Also used : ChannelBufferOutputStream(org.jboss.netty.buffer.ChannelBufferOutputStream) ChannelBuffer(org.jboss.netty.buffer.ChannelBuffer)

Example 19 with ChannelBufferOutputStream

use of org.jboss.netty.buffer.ChannelBufferOutputStream in project NabAlive by jcheype.

the class DataUtil method compress.

public static ChannelBuffer compress(ChannelBuffer in) throws IOException {
    ChannelBufferInputStream channelBufferInputStream = new ChannelBufferInputStream(in);
    ChannelBuffer out = ChannelBuffers.dynamicBuffer(in.readableBytes());
    ChannelBufferOutputStream channelBufferOutputStream = new ChannelBufferOutputStream(out);
    compress(ByteStreams.toByteArray(channelBufferInputStream), channelBufferOutputStream);
    return out;
}
Also used : ChannelBufferOutputStream(org.jboss.netty.buffer.ChannelBufferOutputStream) ChannelBufferInputStream(org.jboss.netty.buffer.ChannelBufferInputStream) ChannelBuffer(org.jboss.netty.buffer.ChannelBuffer)

Aggregations

ChannelBufferOutputStream (org.jboss.netty.buffer.ChannelBufferOutputStream)19 ChannelBuffer (org.jboss.netty.buffer.ChannelBuffer)12 IOException (java.io.IOException)5 OutputStream (java.io.OutputStream)3 TaskMessage (backtype.storm.messaging.TaskMessage)2 JsonGenerator (com.fasterxml.jackson.core.JsonGenerator)2 Callback (com.stumbleupon.async.Callback)2 Deferred (com.stumbleupon.async.Deferred)2 PrintWriter (java.io.PrintWriter)2 ClosedChannelException (java.nio.channels.ClosedChannelException)2 ArrayList (java.util.ArrayList)2 TSSubQuery (net.opentsdb.core.TSSubQuery)2 MessageEvent (org.jboss.netty.channel.MessageEvent)2 DbusEventV1Factory (com.linkedin.databus.core.DbusEventV1Factory)1 StreamEventsArgs (com.linkedin.databus.core.StreamEventsArgs)1 HttpException (com.nabalive.framework.web.exception.HttpException)1 WritableByteChannel (java.nio.channels.WritableByteChannel)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1