Search in sources :

Example 1 with ChannelBuffer

use of org.jboss.netty.buffer.ChannelBuffer in project hadoop by apache.

the class RpcProgram method sendRejectedReply.

protected static void sendRejectedReply(RpcCall call, SocketAddress remoteAddress, ChannelHandlerContext ctx) {
    XDR out = new XDR();
    RpcDeniedReply reply = new RpcDeniedReply(call.getXid(), RpcReply.ReplyState.MSG_DENIED, RpcDeniedReply.RejectState.AUTH_ERROR, new VerifierNone());
    reply.write(out);
    ChannelBuffer buf = ChannelBuffers.wrappedBuffer(out.asReadOnlyWrap().buffer());
    RpcResponse rsp = new RpcResponse(buf, remoteAddress);
    RpcUtil.sendRpcResponse(ctx, rsp);
}
Also used : VerifierNone(org.apache.hadoop.oncrpc.security.VerifierNone) ChannelBuffer(org.jboss.netty.buffer.ChannelBuffer)

Example 2 with ChannelBuffer

use of org.jboss.netty.buffer.ChannelBuffer in project hadoop by apache.

the class RpcProgramMountd method handleInternal.

@Override
public void handleInternal(ChannelHandlerContext ctx, RpcInfo info) {
    RpcCall rpcCall = (RpcCall) info.header();
    final MNTPROC mntproc = MNTPROC.fromValue(rpcCall.getProcedure());
    int xid = rpcCall.getXid();
    byte[] data = new byte[info.data().readableBytes()];
    info.data().readBytes(data);
    XDR xdr = new XDR(data);
    XDR out = new XDR();
    InetAddress client = ((InetSocketAddress) info.remoteAddress()).getAddress();
    if (mntproc == MNTPROC.NULL) {
        out = nullOp(out, xid, client);
    } else if (mntproc == MNTPROC.MNT) {
        // Only do port monitoring for MNT
        if (!doPortMonitoring(info.remoteAddress())) {
            out = MountResponse.writeMNTResponse(Nfs3Status.NFS3ERR_ACCES, out, xid, null);
        } else {
            out = mnt(xdr, out, xid, client);
        }
    } else if (mntproc == MNTPROC.DUMP) {
        out = dump(out, xid, client);
    } else if (mntproc == MNTPROC.UMNT) {
        out = umnt(xdr, out, xid, client);
    } else if (mntproc == MNTPROC.UMNTALL) {
        umntall(out, xid, client);
    } else if (mntproc == MNTPROC.EXPORT) {
        // Currently only support one NFS export
        List<NfsExports> hostsMatchers = new ArrayList<NfsExports>();
        if (hostsMatcher != null) {
            hostsMatchers.add(hostsMatcher);
            out = MountResponse.writeExportList(out, xid, exports, hostsMatchers);
        } else {
            // This means there are no valid exports provided.
            RpcAcceptedReply.getInstance(xid, RpcAcceptedReply.AcceptState.PROC_UNAVAIL, new VerifierNone()).write(out);
        }
    } else {
        // Invalid procedure
        RpcAcceptedReply.getInstance(xid, RpcAcceptedReply.AcceptState.PROC_UNAVAIL, new VerifierNone()).write(out);
    }
    ChannelBuffer buf = ChannelBuffers.wrappedBuffer(out.asReadOnlyWrap().buffer());
    RpcResponse rsp = new RpcResponse(buf, info.remoteAddress());
    RpcUtil.sendRpcResponse(ctx, rsp);
}
Also used : InetSocketAddress(java.net.InetSocketAddress) XDR(org.apache.hadoop.oncrpc.XDR) RpcResponse(org.apache.hadoop.oncrpc.RpcResponse) ChannelBuffer(org.jboss.netty.buffer.ChannelBuffer) RpcCall(org.apache.hadoop.oncrpc.RpcCall) VerifierNone(org.apache.hadoop.oncrpc.security.VerifierNone) NfsExports(org.apache.hadoop.nfs.NfsExports) ArrayList(java.util.ArrayList) List(java.util.List) InetAddress(java.net.InetAddress)

Example 3 with ChannelBuffer

use of org.jboss.netty.buffer.ChannelBuffer in project hadoop by apache.

the class Nfs3Utils method writeChannelCommit.

public static void writeChannelCommit(Channel channel, XDR out, int xid) {
    if (RpcProgramNfs3.LOG.isDebugEnabled()) {
        RpcProgramNfs3.LOG.debug("Commit done:" + xid);
    }
    ChannelBuffer outBuf = XDR.writeMessageTcp(out, true);
    channel.write(outBuf);
}
Also used : ChannelBuffer(org.jboss.netty.buffer.ChannelBuffer)

Example 4 with ChannelBuffer

use of org.jboss.netty.buffer.ChannelBuffer in project hadoop by apache.

the class TestFrameDecoder method testSingleFrame.

@Test
public void testSingleFrame() {
    RpcFrameDecoder decoder = new RpcFrameDecoder();
    // Test "Length field is not received yet"
    ByteBuffer buffer = ByteBuffer.allocate(1);
    ChannelBuffer buf = new ByteBufferBackedChannelBuffer(buffer);
    ChannelBuffer channelBuffer = (ChannelBuffer) decoder.decode(Mockito.mock(ChannelHandlerContext.class), Mockito.mock(Channel.class), buf);
    assertTrue(channelBuffer == null);
    // Test all bytes are not received yet
    byte[] fragment = new byte[4 + 9];
    // final fragment
    fragment[0] = (byte) (1 << 7);
    fragment[1] = 0;
    fragment[2] = 0;
    // fragment size = 10 bytes
    fragment[3] = (byte) 10;
    assertTrue(XDR.isLastFragment(fragment));
    assertTrue(XDR.fragmentSize(fragment) == 10);
    buffer = ByteBuffer.allocate(4 + 9);
    buffer.put(fragment);
    buffer.flip();
    buf = new ByteBufferBackedChannelBuffer(buffer);
    channelBuffer = (ChannelBuffer) decoder.decode(Mockito.mock(ChannelHandlerContext.class), Mockito.mock(Channel.class), buf);
    assertTrue(channelBuffer == null);
}
Also used : RpcFrameDecoder(org.apache.hadoop.oncrpc.RpcUtil.RpcFrameDecoder) Channel(org.jboss.netty.channel.Channel) ChannelHandlerContext(org.jboss.netty.channel.ChannelHandlerContext) ByteBuffer(java.nio.ByteBuffer) ChannelBuffer(org.jboss.netty.buffer.ChannelBuffer) ByteBufferBackedChannelBuffer(org.jboss.netty.buffer.ByteBufferBackedChannelBuffer) ByteBufferBackedChannelBuffer(org.jboss.netty.buffer.ByteBufferBackedChannelBuffer) Test(org.junit.Test)

Example 5 with ChannelBuffer

use of org.jboss.netty.buffer.ChannelBuffer in project storm by apache.

the class MessageDecoder method decode.

/*
     * Each ControlMessage is encoded as:
     *  code (<0) ... short(2)
     * Each TaskMessage is encoded as:
     *  task (>=0) ... short(2)
     *  len ... int(4)
     *  payload ... byte[]     *  
     */
protected Object decode(ChannelHandlerContext ctx, Channel channel, ChannelBuffer buf) throws Exception {
    // Make sure that we have received at least a short 
    long available = buf.readableBytes();
    if (available < 2) {
        //need more data
        return null;
    }
    List<Object> ret = new ArrayList<>();
    // Use while loop, try to decode as more messages as possible in single call
    while (available >= 2) {
        // Mark the current buffer position before reading task/len field
        // because the whole frame might not be in the buffer yet.
        // We will reset the buffer position to the marked position if
        // there's not enough bytes in the buffer.
        buf.markReaderIndex();
        // read the short field
        short code = buf.readShort();
        available -= 2;
        // case 1: Control message
        ControlMessage ctrl_msg = ControlMessage.mkMessage(code);
        if (ctrl_msg != null) {
            if (ctrl_msg == ControlMessage.EOB_MESSAGE) {
                continue;
            } else {
                return ctrl_msg;
            }
        }
        //case 2: SaslTokenMessageRequest
        if (code == SaslMessageToken.IDENTIFIER) {
            // Make sure that we have received at least an integer (length) 
            if (buf.readableBytes() < 4) {
                //need more data
                buf.resetReaderIndex();
                return null;
            }
            // Read the length field.
            int length = buf.readInt();
            if (length <= 0) {
                return new SaslMessageToken(null);
            }
            // Make sure if there's enough bytes in the buffer.
            if (buf.readableBytes() < length) {
                // The whole bytes were not received yet - return null.
                buf.resetReaderIndex();
                return null;
            }
            // There's enough bytes in the buffer. Read it.  
            ChannelBuffer payload = buf.readBytes(length);
            // Return a SaslTokenMessageRequest object
            return new SaslMessageToken(payload.array());
        }
        // Make sure that we have received at least an integer (length)
        if (available < 4) {
            // need more data
            buf.resetReaderIndex();
            break;
        }
        // Read the length field.
        int length = buf.readInt();
        available -= 4;
        if (length <= 0) {
            ret.add(new TaskMessage(code, null));
            break;
        }
        // Make sure if there's enough bytes in the buffer.
        if (available < length) {
            // The whole bytes were not received yet - return null.
            buf.resetReaderIndex();
            break;
        }
        available -= length;
        // There's enough bytes in the buffer. Read it.
        ChannelBuffer payload = buf.readBytes(length);
        // Successfully decoded a frame.
        // Return a TaskMessage object
        ret.add(new TaskMessage(code, payload.array()));
    }
    if (ret.size() == 0) {
        return null;
    } else {
        return ret;
    }
}
Also used : ArrayList(java.util.ArrayList) ChannelBuffer(org.jboss.netty.buffer.ChannelBuffer) TaskMessage(org.apache.storm.messaging.TaskMessage)

Aggregations

ChannelBuffer (org.jboss.netty.buffer.ChannelBuffer)312 Test (org.junit.Test)63 DefaultHttpResponse (org.jboss.netty.handler.codec.http.DefaultHttpResponse)59 HttpResponse (org.jboss.netty.handler.codec.http.HttpResponse)49 Test (org.testng.annotations.Test)49 HttpChunk (org.jboss.netty.handler.codec.http.HttpChunk)46 DefaultHttpChunk (org.jboss.netty.handler.codec.http.DefaultHttpChunk)43 HttpChunkTrailer (org.jboss.netty.handler.codec.http.HttpChunkTrailer)37 DefaultHttpChunkTrailer (org.jboss.netty.handler.codec.http.DefaultHttpChunkTrailer)34 Checkpoint (com.linkedin.databus.core.Checkpoint)27 ByteBuffer (java.nio.ByteBuffer)27 BootstrapDatabaseTooOldException (com.linkedin.databus2.core.container.request.BootstrapDatabaseTooOldException)25 IOException (java.io.IOException)23 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)23 ArrayList (java.util.ArrayList)21 Channel (org.jboss.netty.channel.Channel)19 ChannelFuture (org.jboss.netty.channel.ChannelFuture)18 ChannelPipeline (org.jboss.netty.channel.ChannelPipeline)17 BlockLogBuffer (org.neo4j.com.BlockLogBuffer)16 Map (java.util.Map)14