Java Code Examples for org.jboss.netty.buffer.ChannelBuffers#wrappedBuffer()

The following examples show how to use org.jboss.netty.buffer.ChannelBuffers#wrappedBuffer() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: EventClient.java    From hadoop-arch-book with Apache License 2.0 6 votes vote down vote up
public Action submitUserEvent(UserEvent userEvent) throws Exception {
  Channel channel = channelStateEvent.getChannel();

  if (channel.isWritable()) {
    String json = userEvent.getJSONObject().toString();
    ChannelBuffer m = ChannelBuffers.wrappedBuffer(Bytes.toBytes(json));

    LOG.info("EventClient:sending " + json);

    channel.write(m);
  } else {
    throw new RuntimeException("Channel is not writable");
  }

  synchronized (this) {
    long startTime = System.currentTimeMillis();
    LOG.info("-- EventClient:Waiting for response " + startTime);
    this.wait();
    LOG.info("-- EventClient:Got response " + (System.currentTimeMillis() - startTime));
  }

  LOG.info("EventClient:action " + action.alert);
  return action;
}
 
Example 2
Source File: TestFrameDecoder.java    From hadoop with Apache License 2.0 6 votes vote down vote up
@Override
protected void handleInternal(ChannelHandlerContext ctx, RpcInfo info) {
  // This is just like what's done in RpcProgramMountd#handleInternal and
  // RpcProgramNfs3#handleInternal.
  RpcCall rpcCall = (RpcCall) info.header();
  final int procedure = rpcCall.getProcedure();
  if (procedure != 0) {
    boolean portMonitorSuccess = doPortMonitoring(info.remoteAddress());
    if (!portMonitorSuccess) {
      sendRejectedReply(rpcCall, info.remoteAddress(), ctx);
      return;
    }
  }
  
  resultSize = info.data().readableBytes();
  RpcAcceptedReply reply = RpcAcceptedReply.getAcceptInstance(1234,
      new VerifierNone());
  XDR out = new XDR();
  reply.write(out);
  ChannelBuffer b = ChannelBuffers.wrappedBuffer(out.asReadOnlyWrap().buffer());
  RpcResponse rsp = new RpcResponse(b, info.remoteAddress());
  RpcUtil.sendRpcResponse(ctx, rsp);
}
 
Example 3
Source File: OFStatisticsRequestTest.java    From floodlight_with_topoguard with Apache License 2.0 6 votes vote down vote up
public void testOFStatisticsRequestVendor() throws Exception {
    byte[] packet = new byte[] { 0x01, 0x10, 0x00, 0x50, 0x00, 0x00, 0x00,
            0x63, (byte) 0xff, (byte) 0xff, 0x00, 0x00, 0x00, 0x00, 0x00,
            0x4c, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
            0x01, 0x00, 0x38, 0x00, 0x00, 0x00, 0x00, 0x00, 0x30, 0x20,
            (byte) 0xe0, 0x00, 0x11, 0x00, 0x0c, 0x29, (byte) 0xc5,
            (byte) 0x95, 0x57, 0x02, 0x25, 0x5c, (byte) 0xca, 0x00, 0x02,
            (byte) 0xff, (byte) 0xff, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00,
            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x2d, 0x00, 0x50, 0x04,
            0x00, 0x00, 0x00, 0x00, (byte) 0xff, 0x00, 0x00, 0x00,
            (byte) 0xff, (byte) 0xff, 0x4e, 0x20 };

    OFMessageFactory factory = BasicFactory.getInstance();
    ChannelBuffer packetBuf = ChannelBuffers.wrappedBuffer(packet);
    List<OFMessage> msg = factory.parseMessage(packetBuf);
    TestCase.assertNotNull(msg);
    TestCase.assertEquals(msg.size(), 1);
    TestCase.assertTrue(msg.get(0) instanceof OFStatisticsRequest);
    OFStatisticsRequest sr = (OFStatisticsRequest) msg.get(0);
    TestCase.assertEquals(OFStatisticsType.VENDOR, sr.getStatisticType());
    TestCase.assertEquals(1, sr.getStatistics().size());
    TestCase.assertTrue(sr.getStatistics().get(0) instanceof OFVendorStatistics);
    TestCase.assertEquals(68, ((OFVendorStatistics)sr.getStatistics().get(0)).getLength());
}
 
Example 4
Source File: RpcUtil.java    From hadoop with Apache License 2.0 6 votes vote down vote up
@Override
public void messageReceived(ChannelHandlerContext ctx, MessageEvent e)
    throws Exception {
  ChannelBuffer buf = (ChannelBuffer) e.getMessage();
  ByteBuffer b = buf.toByteBuffer().asReadOnlyBuffer();
  XDR in = new XDR(b, XDR.State.READING);

  RpcInfo info = null;
  try {
    RpcCall callHeader = RpcCall.read(in);
    ChannelBuffer dataBuffer = ChannelBuffers.wrappedBuffer(in.buffer()
        .slice());
    info = new RpcInfo(callHeader, dataBuffer, ctx, e.getChannel(),
        e.getRemoteAddress());
  } catch (Exception exc) {
    LOG.info("Malformed RPC request from " + e.getRemoteAddress());
  }

  if (info != null) {
    Channels.fireMessageReceived(ctx, info);
  }
}
 
Example 5
Source File: TestFrameDecoder.java    From big-c with Apache License 2.0 6 votes vote down vote up
@Override
protected void handleInternal(ChannelHandlerContext ctx, RpcInfo info) {
  // This is just like what's done in RpcProgramMountd#handleInternal and
  // RpcProgramNfs3#handleInternal.
  RpcCall rpcCall = (RpcCall) info.header();
  final int procedure = rpcCall.getProcedure();
  if (procedure != 0) {
    boolean portMonitorSuccess = doPortMonitoring(info.remoteAddress());
    if (!portMonitorSuccess) {
      sendRejectedReply(rpcCall, info.remoteAddress(), ctx);
      return;
    }
  }
  
  resultSize = info.data().readableBytes();
  RpcAcceptedReply reply = RpcAcceptedReply.getAcceptInstance(1234,
      new VerifierNone());
  XDR out = new XDR();
  reply.write(out);
  ChannelBuffer b = ChannelBuffers.wrappedBuffer(out.asReadOnlyWrap().buffer());
  RpcResponse rsp = new RpcResponse(b, info.remoteAddress());
  RpcUtil.sendRpcResponse(ctx, rsp);
}
 
Example 6
Source File: NettyCodecAdapter.java    From dubbox with Apache License 2.0 5 votes vote down vote up
@Override
protected Object encode(ChannelHandlerContext ctx, Channel ch, Object msg) throws Exception {
    com.alibaba.dubbo.remoting.buffer.ChannelBuffer buffer =
        com.alibaba.dubbo.remoting.buffer.ChannelBuffers.dynamicBuffer(1024);
    NettyChannel channel = NettyChannel.getOrAddChannel(ch, url, handler);
    try {
    	codec.encode(channel, buffer, msg);
    } finally {
        NettyChannel.removeChannelIfDisconnected(ch);
    }
    return ChannelBuffers.wrappedBuffer(buffer.toByteBuffer());
}
 
Example 7
Source File: NettyCodecAdapter.java    From dubbox-hystrix with Apache License 2.0 5 votes vote down vote up
@Override
protected Object encode(ChannelHandlerContext ctx, Channel ch, Object msg) throws Exception {
    com.alibaba.dubbo.remoting.buffer.ChannelBuffer buffer =
        com.alibaba.dubbo.remoting.buffer.ChannelBuffers.dynamicBuffer(1024);
    NettyChannel channel = NettyChannel.getOrAddChannel(ch, url, handler);
    try {
    	codec.encode(channel, buffer, msg);
    } finally {
        NettyChannel.removeChannelIfDisconnected(ch);
    }
    return ChannelBuffers.wrappedBuffer(buffer.toByteBuffer());
}
 
Example 8
Source File: MainActivity.java    From astrobee_android with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unused")
protected final void onNewData(final ByteBuffer data) {
    if (!mExposureSet) {
        mUiHandler.post(new Runnable() {
            @Override
            public void run() {
                if (mExposureSet)
                    return;
                setExposure();
                mExposureSet = true;
            }
        });
    }

    if (mPublisher == null)
        return;
    if (!mPublisher.hasSubscribers())
        return;

    data.order(ByteOrder.nativeOrder());
    data.rewind();

    if (mChannelBuffer == null) {
        mChannelBuffer = ChannelBuffers.wrappedBuffer(data);
    }

    mChannelBuffer.setIndex(0, mChannelBuffer.capacity());

    mCloud.getHeader().setStamp(mConnectedNode.getCurrentTime());
    mCloud.getHeader().setSeq(mSeqNum.incrementAndGet());
    mCloud.setData(mChannelBuffer);

    mPublisher.publish(mCloud);
}
 
Example 9
Source File: PlayHandler.java    From restcommander with Apache License 2.0 5 votes vote down vote up
private void upgradeResponseHixie76(HttpRequest req, HttpResponse res) {
    res.setStatus(new HttpResponseStatus(101, "Web Socket Protocol Handshake"));
    res.addHeader(UPGRADE, WEBSOCKET);
    res.addHeader(CONNECTION, UPGRADE);
    res.addHeader(SEC_WEBSOCKET_ORIGIN, req.getHeader(ORIGIN));
    res.addHeader(SEC_WEBSOCKET_LOCATION, getWebSocketLocation(req));
    String protocol = req.getHeader(SEC_WEBSOCKET_PROTOCOL);
    if (protocol != null) {
        res.addHeader(SEC_WEBSOCKET_PROTOCOL, protocol);
    }

    // Calculate the answer of the challenge.
    String key1 = req.getHeader(SEC_WEBSOCKET_KEY1);
    String key2 = req.getHeader(SEC_WEBSOCKET_KEY2);
    int a = (int) (Long.parseLong(key1.replaceAll("[^0-9]", "")) / key1.replaceAll("[^ ]", "").length());
    int b = (int) (Long.parseLong(key2.replaceAll("[^0-9]", "")) / key2.replaceAll("[^ ]", "").length());
    long c = req.getContent().readLong();
    ChannelBuffer input = ChannelBuffers.buffer(16);
    input.writeInt(a);
    input.writeInt(b);
    input.writeLong(c);
    try {
        ChannelBuffer output = ChannelBuffers.wrappedBuffer(
                MessageDigest.getInstance("MD5").digest(input.array()));
        res.setContent(output);
    } catch (NoSuchAlgorithmException e) {
        throw new RuntimeException(e);
    }
}
 
Example 10
Source File: RpcUtil.java    From hadoop with Apache License 2.0 5 votes vote down vote up
@Override
public void messageReceived(ChannelHandlerContext ctx, MessageEvent e)
    throws Exception {
  RpcResponse r = (RpcResponse) e.getMessage();
  byte[] fragmentHeader = XDR.recordMark(r.data().readableBytes(), true);
  ChannelBuffer header = ChannelBuffers.wrappedBuffer(fragmentHeader);
  ChannelBuffer d = ChannelBuffers.wrappedBuffer(header, r.data());
  e.getChannel().write(d);
}
 
Example 11
Source File: ChannelBufferHelper.java    From canal with Apache License 2.0 4 votes vote down vote up
public final ChannelBuffer buildChannelBufferFromCommandPacket(IPacket packet) throws IOException {
    byte[] bodyBytes = packet.toBytes();
    ChannelBuffer header = createHeader(bodyBytes.length, (byte) 0);
    return ChannelBuffers.wrappedBuffer(header, ChannelBuffers.wrappedBuffer(bodyBytes));
}
 
Example 12
Source File: NettyBackedChannelBufferFactory.java    From dubbo3 with Apache License 2.0 4 votes vote down vote up
public ChannelBuffer getBuffer(ByteBuffer nioBuffer) {
    return new NettyBackedChannelBuffer(ChannelBuffers.wrappedBuffer(nioBuffer));
}
 
Example 13
Source File: BinlogDumpCommandBuilder.java    From canal with Apache License 2.0 4 votes vote down vote up
public ChannelBuffer toChannelBuffer(BinlogDumpCommandPacket command) throws IOException {
    byte[] commandBytes = command.toBytes();
    byte[] headerBytes = assembleHeaderBytes(commandBytes.length);
    ChannelBuffer buffer = ChannelBuffers.wrappedBuffer(headerBytes, commandBytes);
    return buffer;
}
 
Example 14
Source File: RpcProgramMountd.java    From hadoop with Apache License 2.0 4 votes vote down vote up
@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);
}
 
Example 15
Source File: ManagerNode.java    From astrobee_android with Apache License 2.0 4 votes vote down vote up
public void onGuestScienceData(Message msg) {
     String apkFullName = "", topic = "";
     // Check to make sure the message contains the full name of the guest science apk
     if (!msg.getData().containsKey("apkFullName")) {
         mLogger.error(LOG_TAG, "Guest science message doesn't contain the full apk name and " +
                 "will not be processed!");
         return;
     }

     apkFullName = msg.getData().getString("apkFullName");

     if (msg.getData().containsKey("topic")) {
         topic = msg.getData().getString("topic");
         if (topic.length() > 32) {
             mLogger.error(LOG_TAG, "The topic string in the guest science message is too " +
                     "big to send to the ground so the message will not be sent. Length must " +
                     " be no more than 32 characters not " + topic.length() + ".");
             return;
         }
     }

     byte[] data = null;
     if (!msg.getData().containsKey("data")) {
         mLogger.error(LOG_TAG, "Guest science message doesn't contain data and will not be " +
                 "processed.");
         return;
     } else {
         data = msg.getData().getByteArray("data");
         if (data.length > 2048) {
             mLogger.error(LOG_TAG, "The data in the guest science message is too big to send " +
                     "to the ground so the message will not be sent. Length of data must be no" +
                     " more than 2048 bytes not " + data.length + ".");
             return;
         }
     }

     GuestScienceData dataMsg = mMessageFactory.newFromType(GuestScienceData._TYPE);
     Header hdr = mMessageFactory.newFromType(Header._TYPE);

     hdr.setStamp(mNodeConfig.getTimeProvider().getCurrentTime());
     dataMsg.setHeader(hdr);

     dataMsg.setApkName(apkFullName);

     if (msg.what == MessageType.STRING.toInt()) {
         dataMsg.setDataType(GuestScienceData.STRING);
     } else if (msg.what == MessageType.JSON.toInt()) {
         dataMsg.setDataType(GuestScienceData.JSON);
     } else if (msg.what == MessageType.BINARY.toInt()) {
         dataMsg.setDataType(GuestScienceData.BINARY);
     } else {
         mLogger.error(LOG_TAG, "Message type in guest science message is unknown so the message " +
                 "will not be sent to the ground.");
         return;
     }

     dataMsg.setTopic(topic);

     // If there isn't data, don't copy it over as it will crash
     if (data.length != 0) {
         ChannelBuffer dataBuff = ChannelBuffers.wrappedBuffer(ByteOrder.LITTLE_ENDIAN, data);
         dataMsg.setData(dataBuff);
     }
     mDataPublisher.publish(dataMsg);
}
 
Example 16
Source File: NettyBackedChannelBufferFactory.java    From dubbox with Apache License 2.0 4 votes vote down vote up
@Override
public ChannelBuffer getBuffer(ByteBuffer nioBuffer) {
    return new NettyBackedChannelBuffer(ChannelBuffers.wrappedBuffer(nioBuffer));
}
 
Example 17
Source File: PingSimplePacket.java    From pinpoint with Apache License 2.0 4 votes vote down vote up
@Override
public ChannelBuffer toBuffer() {
    return ChannelBuffers.wrappedBuffer(PING_BYTE);
}
 
Example 18
Source File: ChannelBufferHelper.java    From canal-1.1.3 with Apache License 2.0 4 votes vote down vote up
public final ChannelBuffer createHeader(int bodyLength, byte packetNumber) {
    HeaderPacket header = new HeaderPacket();
    header.setPacketBodyLength(bodyLength);
    header.setPacketSequenceNumber(packetNumber);
    return ChannelBuffers.wrappedBuffer(header.toBytes());
}
 
Example 19
Source File: ChannelBufferHelper.java    From canal-1.1.3 with Apache License 2.0 4 votes vote down vote up
public final ChannelBuffer createHeaderWithPacketNumberPlusOne(int bodyLength, byte packetNumber) {
    HeaderPacket header = new HeaderPacket();
    header.setPacketBodyLength(bodyLength);
    header.setPacketSequenceNumber((byte) (packetNumber + 1));
    return ChannelBuffers.wrappedBuffer(header.toBytes());
}
 
Example 20
Source File: OFStatisticsReplyTest.java    From floodlight_with_topoguard with Apache License 2.0 4 votes vote down vote up
public void testOFFlowStatisticsReply() throws Exception {
    byte[] packet = new byte[] { 0x01, 0x11, 0x01, 0x2c, 0x00, 0x00, 0x00,
            0x01, 0x00, 0x01, 0x00, 0x00, 0x00, 0x60, 0x00, 0x00, 0x00,
            0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00,
            0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, (byte) 0xff,
            (byte) 0xff, 0x00, 0x00, 0x08, 0x00, 0x00, 0x01, 0x00, 0x00,
            0x0a, 0x00, 0x00, 0x03, 0x0a, 0x00, 0x00, 0x02, 0x00, 0x00,
            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3a, (byte) 0xa6,
            (byte) 0xa6, 0x00, (byte) 0xff, (byte) 0xff, 0x00, 0x05, 0x00,
            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
            0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
            (byte) 0xc4, 0x00, 0x00, 0x00, 0x08, 0x00, 0x01, 0x00, 0x00,
            0x00, 0x60, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02,
            0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x00,
            0x00, 0x02, (byte) 0xff, (byte) 0xff, 0x00, 0x00, 0x08, 0x06,
            0x00, 0x02, 0x00, 0x00, 0x0a, 0x00, 0x00, 0x03, 0x0a, 0x00,
            0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
            0x3b, 0x2f, (byte) 0xfa, 0x40, (byte) 0xff, (byte) 0xff, 0x00,
            0x05, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
            0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00,
            0x00, 0x00, 0x3c, 0x00, 0x00, 0x00, 0x08, 0x00, 0x01, 0x00,
            0x00, 0x00, 0x60, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
            0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00,
            0x00, 0x00, 0x03, (byte) 0xff, (byte) 0xff, 0x00, 0x62, 0x08,
            0x00, 0x00, 0x01, 0x62, 0x37, 0x0a, 0x00, 0x00, 0x02, 0x0a,
            0x00, 0x00, 0x03, 0x00, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00,
            0x00, 0x3a, (byte) 0xc5, 0x2a, (byte) 0x80, (byte) 0xff,
            (byte) 0xff, 0x00, 0x05, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00,
            0x00, 0x00, 0x00, 0x00, 0x00, (byte) 0xc4, 0x00, 0x00, 0x00,
            0x08, 0x00, 0x02, 0x00, 0x00 };

    OFMessageFactory factory = BasicFactory.getInstance();
    ChannelBuffer packetBuf = ChannelBuffers.wrappedBuffer(packet);
    List<OFMessage> msg = factory.parseMessage(packetBuf);
    TestCase.assertNotNull(msg);
    TestCase.assertEquals(msg.size(), 1);
    TestCase.assertTrue(msg.get(0) instanceof OFStatisticsReply);
    OFStatisticsReply sr = (OFStatisticsReply) msg.get(0);
    TestCase.assertEquals(OFStatisticsType.FLOW, sr.getStatisticType());
    TestCase.assertEquals(3, sr.getStatistics().size());
}