@Test
  public void testEndianness() {
    ChannelBuffer buffer;
    int value = 12;

    // ByteOrder.BIG_ENDIAN
    buffer = ChannelBuffers.buffer(ByteOrder.BIG_ENDIAN, 4);
    buffer.writeInt(value);
    for (int i = 0; i < 4; i++) {
      byte b = buffer.readByte();
      System.out.println(b);
    }

    // ByteOrder.LITTLE_ENDIAN
    buffer = ChannelBuffers.buffer(ByteOrder.LITTLE_ENDIAN, 4);
    buffer.writeInt(value);
    for (int i = 0; i < 4; i++) {
      byte b = buffer.readByte();
      System.out.println(b);
    }

    // ByteOrder.nativeOrder()
    buffer = ChannelBuffers.buffer(ByteOrder.nativeOrder(), 4);
    buffer.writeInt(value);
    for (int i = 0; i < 4; i++) {
      byte b = buffer.readByte();
      System.out.println(b);
    }
  }
  @Test
  public void setmst() {
    String host = "host";
    int port = 1978;
    long timestamp = System.currentTimeMillis();
    int opts = RDB.ROCHKCON;
    Setmst dut = new Setmst(host, port, timestamp, opts);

    ChannelBuffer request = ChannelBuffers.buffer(2 + 4 + 4 + 8 + 4 + host.getBytes().length);
    request.writeBytes(new byte[] {(byte) 0xC8, (byte) 0x78});
    request.writeInt(host.getBytes().length);
    request.writeInt(port);
    request.writeLong(timestamp);
    request.writeInt(opts);
    request.writeBytes(host.getBytes());
    ChannelBuffer actual = ChannelBuffers.buffer(request.capacity());
    dut.encode(actual);
    assertEquals(request, actual);

    ChannelBuffer response = ChannelBuffers.buffer(1);
    assertFalse(dut.decode(response));

    response.writeByte(Command.ESUCCESS);
    assertTrue(dut.decode(response));
    assertTrue(dut.getReturnValue());

    // error
    response.clear();
    response.writeByte(Command.EUNKNOWN);
    assertTrue(dut.decode(response));
    assertFalse(dut.getReturnValue());
  }
  @Test
  public void iternext() {
    Iternext dut = new Iternext(transcoder, transcoder);

    ChannelBuffer request = ChannelBuffers.buffer(2);
    request.writeBytes(new byte[] {(byte) 0xC8, (byte) 0x51});
    ChannelBuffer actual = ChannelBuffers.buffer(request.capacity());
    dut.encode(actual);
    assertEquals(request, actual);

    ChannelBuffer response = ChannelBuffers.buffer(1 + 4 + value.length);
    assertFalse(dut.decode(response));

    response.writeByte(BinaryCommand.ESUCCESS);
    assertFalse(dut.decode(response));
    response.resetReaderIndex();

    response.writeInt(value.length);
    response.writeBytes(value);
    assertTrue(dut.decode(response));
    assertArrayEquals(value, (byte[]) dut.getReturnValue());

    // error
    response.clear();
    response.writeByte(BinaryCommand.EUNKNOWN);
    assertTrue(dut.decode(response));
    assertNull(dut.getReturnValue());
  }
 @Override
 public ChannelBuffer encode(StateChangeMessage message) throws IOException {
   ChannelBuffer buffer = ChannelBuffers.buffer(3);
   buffer.writeByte(message.getState());
   buffer.writeByte(message.getGameMode());
   return buffer;
 }
Exemple #5
0
 @Override
 protected Object decode(ChannelHandlerContext ctx, Channel channel, ChannelBuffer buffer)
     throws Exception {
   if (opcode == -1) {
     if (buffer.readableBytes() >= 1) {
       opcode = buffer.readByte() & 0xFF;
       opcode = (opcode - cipher.getNextValue()) & 0xFF;
       size = Client.PACKET_SIZES[opcode];
     } else {
       return null;
     }
   }
   if (size == -1) {
     if (buffer.readableBytes() >= 1) {
       size = buffer.readByte() & 0xFF;
     } else {
       return null;
     }
   }
   if (buffer.readableBytes() >= size) {
     final byte[] data = new byte[size];
     buffer.readBytes(data);
     final ChannelBuffer payload = ChannelBuffers.buffer(size);
     payload.writeBytes(data);
     try {
       return new Packet(opcode, Type.FIXED, payload);
     } finally {
       opcode = -1;
       size = -1;
     }
   }
   return null;
 }
  @Test
  public void testExtractBadEventLarge() {
    String badData1 = "<10> bad bad data bad bad\n";
    // The minimum size (which is 10) overrides the 5 specified here.
    SyslogUtils util = new SyslogUtils(5, false, false);
    ChannelBuffer buff = ChannelBuffers.buffer(100);
    buff.writeBytes(badData1.getBytes());
    Event e = util.extractEvent(buff);
    if (e == null) {
      throw new NullPointerException("Event is null");
    }
    Map<String, String> headers = e.getHeaders();
    Assert.assertEquals("1", headers.get(SyslogUtils.SYSLOG_FACILITY));
    Assert.assertEquals("2", headers.get(SyslogUtils.SYSLOG_SEVERITY));
    Assert.assertEquals(
        SyslogUtils.SyslogStatus.INCOMPLETE.getSyslogStatus(),
        headers.get(SyslogUtils.EVENT_STATUS));
    Assert.assertEquals("<10> bad b".trim(), new String(e.getBody()).trim());

    Event e2 = util.extractEvent(buff);

    if (e2 == null) {
      throw new NullPointerException("Event is null");
    }
    Map<String, String> headers2 = e2.getHeaders();
    Assert.assertEquals("0", headers2.get(SyslogUtils.SYSLOG_FACILITY));
    Assert.assertEquals("0", headers2.get(SyslogUtils.SYSLOG_SEVERITY));
    Assert.assertEquals(
        SyslogUtils.SyslogStatus.INVALID.getSyslogStatus(), headers2.get(SyslogUtils.EVENT_STATUS));
    Assert.assertEquals("ad data ba".trim(), new String(e2.getBody()).trim());
  }
  @Test
  public void testGoodEventGoodEvent() {

    String priority = "<10>";
    String goodData1 = "Good good good data\n";
    SyslogUtils util = new SyslogUtils(false);
    ChannelBuffer buff = ChannelBuffers.buffer(100);
    buff.writeBytes((priority + goodData1).getBytes());
    String priority2 = "<20>";
    String goodData2 = "Good really good data\n";
    buff.writeBytes((priority2 + goodData2).getBytes());
    Event e = util.extractEvent(buff);
    if (e == null) {
      throw new NullPointerException("Event is null");
    }
    Map<String, String> headers = e.getHeaders();
    Assert.assertEquals("1", headers.get(SyslogUtils.SYSLOG_FACILITY));
    Assert.assertEquals("2", headers.get(SyslogUtils.SYSLOG_SEVERITY));
    Assert.assertEquals(null, headers.get(SyslogUtils.EVENT_STATUS));
    Assert.assertEquals(priority + goodData1.trim(), new String(e.getBody()).trim());

    Event e2 = util.extractEvent(buff);
    if (e2 == null) {
      throw new NullPointerException("Event is null");
    }
    Map<String, String> headers2 = e2.getHeaders();
    Assert.assertEquals("2", headers2.get(SyslogUtils.SYSLOG_FACILITY));
    Assert.assertEquals("4", headers2.get(SyslogUtils.SYSLOG_SEVERITY));
    Assert.assertEquals(null, headers.get(SyslogUtils.EVENT_STATUS));
    Assert.assertEquals(priority2 + goodData2.trim(), new String(e2.getBody()).trim());
  }
  @Test
  public void testBadEventBadEvent() {
    String badData1 = "hi guys! <10F> bad bad data\n";
    SyslogUtils util = new SyslogUtils(false);
    ChannelBuffer buff = ChannelBuffers.buffer(100);
    buff.writeBytes(badData1.getBytes());
    String badData2 = "hi guys! <20> bad bad data\n";
    buff.writeBytes((badData2).getBytes());
    Event e = util.extractEvent(buff);

    if (e == null) {
      throw new NullPointerException("Event is null");
    }
    Map<String, String> headers = e.getHeaders();
    Assert.assertEquals("0", headers.get(SyslogUtils.SYSLOG_FACILITY));
    Assert.assertEquals("0", headers.get(SyslogUtils.SYSLOG_SEVERITY));
    Assert.assertEquals(
        SyslogUtils.SyslogStatus.INVALID.getSyslogStatus(), headers.get(SyslogUtils.EVENT_STATUS));
    Assert.assertEquals(badData1.trim(), new String(e.getBody()).trim());

    Event e2 = util.extractEvent(buff);

    if (e2 == null) {
      throw new NullPointerException("Event is null");
    }
    Map<String, String> headers2 = e2.getHeaders();
    Assert.assertEquals("0", headers2.get(SyslogUtils.SYSLOG_FACILITY));
    Assert.assertEquals("0", headers2.get(SyslogUtils.SYSLOG_SEVERITY));
    Assert.assertEquals(
        SyslogUtils.SyslogStatus.INVALID.getSyslogStatus(), headers2.get(SyslogUtils.EVENT_STATUS));
    Assert.assertEquals(badData2.trim(), new String(e2.getBody()).trim());
  }
  public void checkHeader(String msg1, String stamp1, String format1, String host1, String data1)
      throws ParseException {
    SyslogUtils util = new SyslogUtils(false);
    ChannelBuffer buff = ChannelBuffers.buffer(200);

    buff.writeBytes(msg1.getBytes());
    Event e = util.extractEvent(buff);
    if (e == null) {
      throw new NullPointerException("Event is null");
    }
    Map<String, String> headers2 = e.getHeaders();
    if (stamp1 == null) {
      Assert.assertFalse(headers2.containsKey("timestamp"));
    } else {
      SimpleDateFormat formater = new SimpleDateFormat(format1);
      Assert.assertEquals(
          String.valueOf(formater.parse(stamp1).getTime()), headers2.get("timestamp"));
    }
    if (host1 == null) {
      Assert.assertFalse(headers2.containsKey("host"));
    } else {
      String host2 = headers2.get("host");
      Assert.assertEquals(host2, host1);
    }
    Assert.assertEquals(data1, new String(e.getBody()));
  }
  /*
   * (non-Javadoc)
   *
   * @see org.jboss.netty.handler.codec.oneone.OneToOneEncoder#encode(org.jboss
   * .netty.channel.ChannelHandlerContext, org.jboss.netty.channel.Channel, java.lang.Object)
   */
  @Override
  protected Object encode(final ChannelHandlerContext ctx, final Channel channel, final Object msg)
      throws Exception {

    final ChannelBuffer message = (ChannelBuffer) msg;

    final AuthToClientChannelHandler channelHandler =
        (AuthToClientChannelHandler) ctx.getPipeline().getLast();
    final int opcode = message.readUnsignedByte();
    final int size = message.readableBytes();

    final ChannelBuffer frame = ChannelBuffers.buffer(ByteOrder.LITTLE_ENDIAN, (size + 3));
    frame.writeByte(opcode);
    frame.writeShort(size);

    final byte[] tmpa = new byte[message.readableBytes()];
    message.readBytes(tmpa);
    frame.writeBytes(channelHandler.getCrypt().encrypt(tmpa));

    log.debug(String.format("[SEND PACKET] :  0x%02X", opcode));
    final List<String> d =
        breakStringInChunks(new BigInteger(1, tmpa).toString(16).toUpperCase(), 16);
    for (final String string : d) {
      log.debug(string);
    }
    return frame;
  }
 @Override
 public ChannelBuffer encode(CollectItemMessage message) throws IOException {
   ChannelBuffer buffer = ChannelBuffers.buffer(8);
   buffer.writeInt(message.getId());
   buffer.writeInt(message.getCollector());
   return buffer;
 }
Exemple #12
0
 @Override
 public ChannelBuffer encode(LoadChunkMessage message) {
   ChannelBuffer buffer = ChannelBuffers.buffer(9);
   buffer.writeInt(message.getX());
   buffer.writeInt(message.getZ());
   buffer.writeByte(message.isLoaded() ? 1 : 0);
   return buffer;
 }
 @Override
 public ChannelBuffer encode(EntityRotationMessage message) throws IOException {
   ChannelBuffer buffer = ChannelBuffers.buffer(6);
   buffer.writeInt(message.getId());
   buffer.writeByte(message.getRotation());
   buffer.writeByte(message.getPitch());
   return buffer;
 }
  @Override
  public ChannelBuffer encode(WorldEditCUIMessage message) {
    byte[] data = message.getMessage().getBytes(UTF_8_CHARSET);

    ChannelBuffer buffer = ChannelBuffers.buffer(data.length);
    buffer.writeBytes(data);
    return buffer;
  }
 @Override
 public void createEnd(LocalChannelReference lcr) throws OpenR66ProtocolPacketException {
   end = ChannelBuffers.buffer(5 + (version != null ? version.getBytes().length : 0));
   end.writeInt(localId);
   end.writeByte(way);
   if (version != null) {
     end.writeBytes(version.getBytes());
   }
 }
 @Override
 protected Object encode(ChannelHandlerContext ctx, Channel channel, Object msg) throws Exception {
   ChannelBuffer buffer = null;
   if (msg instanceof SocksMessage) {
     buffer = ChannelBuffers.buffer(DEFAULT_ENCODER_BUFFER_SIZE);
     ((SocksMessage) msg).encodeAsByteBuf(buffer);
   }
   return buffer;
 }
Exemple #17
0
	@Override
	public ChannelBuffer encode(PositionMessage message) throws IOException {
		ChannelBuffer buffer = ChannelBuffers.buffer(33);
		buffer.writeDouble(message.getX());
		buffer.writeDouble(message.getY());
		buffer.writeDouble(message.getStance());
		buffer.writeDouble(message.getZ());
		buffer.writeByte(message.isOnGround() ? 1 : 0);
		return buffer;
	}
 @Override
 public ChannelBuffer encode(ExperienceOrbMessage message) throws IOException {
   ChannelBuffer buffer = ChannelBuffers.buffer(19);
   buffer.writeInt(message.getId());
   buffer.writeInt(message.getX());
   buffer.writeInt(message.getY());
   buffer.writeInt(message.getZ());
   buffer.writeShort(message.getCount());
   return buffer;
 }
  @Override
  public ChannelBuffer encode(PlayerRotationMessage message) throws IOException {
    ChannelBuffer buffer = ChannelBuffers.buffer(3);

    buffer.writeByte(message.getPlayerId());
    buffer.writeByte((byte) ((int) (message.getYaw() * 256 / 360) & 255));
    buffer.writeByte((byte) ((int) (message.getPitch() * 256 / 360) & 255));

    return buffer;
  }
Exemple #20
0
 @Override
 public ChannelBuffer encode(UseBedMessage message) throws IOException {
   ChannelBuffer buffer = ChannelBuffers.buffer(14);
   buffer.writeInt(message.getId());
   buffer.writeByte(message.getUsed());
   buffer.writeInt(message.getX());
   buffer.writeByte(message.getY());
   buffer.writeInt(message.getZ());
   return buffer;
 }
Exemple #21
0
 public ChannelBuffer encode(final RtmpMessage message) {
   final ChannelBuffer in = message.encode();
   final RtmpHeader header = message.getHeader();
   if (header.isChunkSize()) {
     final ChunkSize csMessage = (ChunkSize) message;
     logger.debug("encoder new chunk size: {}", csMessage);
     chunkSize = csMessage.getChunkSize();
   } else if (header.isControl()) {
     final Control control = (Control) message;
     if (control.getType() == Control.Type.STREAM_BEGIN) {
       clearPrevHeaders();
     }
   }
   final int channelId = header.getChannelId();
   header.setSize(in.readableBytes());
   final RtmpHeader prevHeader = channelPrevHeaders[channelId];
   if (prevHeader != null // first stream message is always large
       && header.getStreamId() > 0 // all control messages always large
       && header.getTime() > 0) { // if time is zero, always large
     if (header.getSize() == prevHeader.getSize()) {
       header.setHeaderType(RtmpHeader.Type.SMALL);
     } else {
       header.setHeaderType(RtmpHeader.Type.MEDIUM);
     }
     final int deltaTime = header.getTime() - prevHeader.getTime();
     if (deltaTime < 0) {
       logger.warn("negative time: {}", header);
       header.setDeltaTime(0);
     } else {
       header.setDeltaTime(deltaTime);
     }
   } else {
     // otherwise force to LARGE
     header.setHeaderType(RtmpHeader.Type.LARGE);
   }
   channelPrevHeaders[channelId] = header;
   if (logger.isDebugEnabled()) {
     // logger.debug(">> {}", message);
   }
   final ChannelBuffer out =
       ChannelBuffers.buffer(
           RtmpHeader.MAX_ENCODED_SIZE + header.getSize() + header.getSize() / chunkSize);
   boolean first = true;
   while (in.readable()) {
     final int size = Math.min(chunkSize, in.readableBytes());
     if (first) {
       header.encode(out);
       first = false;
     } else {
       out.writeBytes(header.getTinyHeader());
     }
     in.readBytes(out, size);
   }
   return out;
 }
Exemple #22
0
 @Override
 public ChannelBuffer encode(BlockActionMessage message) throws IOException {
   ChannelBuffer buffer = ChannelBuffers.buffer(14);
   buffer.writeInt(message.getX());
   buffer.writeShort(message.getY());
   buffer.writeInt(message.getZ());
   buffer.writeByte(message.getFirstByte());
   buffer.writeByte(message.getSecondByte());
   buffer.writeShort(message.getBlockId());
   return buffer;
 }
 @Override
 public ChannelBuffer encode(RelativeEntityPositionRotationMessage message) throws IOException {
   ChannelBuffer buffer = ChannelBuffers.buffer(9);
   buffer.writeInt(message.getId());
   buffer.writeByte(message.getDeltaX());
   buffer.writeByte(message.getDeltaY());
   buffer.writeByte(message.getDeltaZ());
   buffer.writeByte(message.getRotation());
   buffer.writeByte(message.getPitch());
   return buffer;
 }
 @Override
 public void channelConnected(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception {
   Channel channel = e.getChannel();
   System.out.println(channel.getRemoteAddress().toString());
   System.out.println("channelConnected...");
   String msg = "welcome rookiefly...";
   ChannelBuffer buffer = ChannelBuffers.buffer(1024);
   buffer.writeBytes(msg.getBytes());
   channel.write(buffer);
   ctx.sendUpstream(e);
 }
Exemple #25
0
 /** Utility for test method. */
 private DdPacket createDdPacket() throws OspfParseException {
   byte[] ddPacket = {
     2, 2, 0, 32, -64, -88, -86, 8, 0, 0, 0, 1, -96, 82, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5, -36, 2,
     7, 65, 119, -87, 126
   };
   DdPacket ddPacket1 = new DdPacket();
   ChannelBuffer buf = ChannelBuffers.buffer(ddPacket.length);
   buf.writeBytes(ddPacket);
   ddPacket1.readFrom(buf);
   return ddPacket1;
 }
 @Override
 public ChannelBuffer encode(EntityTeleportMessage message) throws IOException {
   ChannelBuffer buffer = ChannelBuffers.buffer(18);
   buffer.writeInt(message.getEntityId());
   buffer.writeInt(message.getX());
   buffer.writeInt(message.getY());
   buffer.writeInt(message.getZ());
   buffer.writeByte(message.getRotation());
   buffer.writeByte(message.getPitch());
   return buffer;
 }
 @Override
 public ChannelBuffer encode(PlayerPositionLookMessage message) throws IOException {
   ChannelBuffer buffer = ChannelBuffers.buffer(41);
   buffer.writeDouble(message.getX());
   buffer.writeDouble(message.getY());
   buffer.writeDouble(message.getStance());
   buffer.writeDouble(message.getZ());
   buffer.writeFloat(message.getYaw());
   buffer.writeFloat(message.getPitch());
   buffer.writeByte(message.isOnGround() ? 1 : 0);
   return buffer;
 }
  public static void send(MessageEvent event, Integer transactionId, Long connectionId)
      throws Exception {
    logger.debug("ConnectionResponse::send to " + event.getRemoteAddress());

    ChannelBuffer responseBuffer = ChannelBuffers.buffer(4 + 4 + 8);
    responseBuffer.writeInt(Action.CONNECT.getId());
    responseBuffer.writeInt(transactionId);
    responseBuffer.writeLong(connectionId);

    logger.debug("ConnectionResponse DUMP: " + Utils.getHexString(responseBuffer.array()));

    event.getChannel().write(responseBuffer, event.getRemoteAddress());
  }
  @Test
  public void protocol() {
    double num = 4;
    Adddouble dut = new Adddouble(transcoder, transcoder, key, num);

    ChannelBuffer request = ChannelBuffers.buffer(2 + 4 + 8 + 8 + key.length);
    request.writeBytes(new byte[] {(byte) 0xC8, (byte) 0x61});
    request.writeInt(key.length);
    request.writeLong(dut._integ(num));
    request.writeLong(dut._fract(num));
    request.writeBytes(key);
    ChannelBuffer actual = ChannelBuffers.buffer(request.capacity());
    dut.encode(actual);
    assertEquals(request, actual);

    ChannelBuffer response = ChannelBuffers.buffer(1 + 8 + 8);
    assertFalse(dut.decode(response));

    response.writeByte(Command.ESUCCESS);
    assertFalse(dut.decode(response));
    response.resetReaderIndex();

    response.writeLong(dut._integ(3.0 + num));
    assertFalse(dut.decode(response));
    response.resetReaderIndex();

    response.writeLong(dut._fract(3.0 + num));
    assertTrue(dut.decode(response));
    assertEquals(3.0 + num, (double) dut.getReturnValue(), 0.0);

    // error
    response.clear();
    response.writeByte(Command.EUNKNOWN);
    assertTrue(dut.decode(response));
    assertEquals(Double.NaN, (double) dut.getReturnValue(), 0.0);
  }
Exemple #30
0
    @Override
    public void writeRequested(ChannelHandlerContext ctx, MessageEvent e) {
      PaxosMessage msg;
      ChannelBuffer sendBuf;
      byte[] data;

      msg = (PaxosMessage) e.getMessage(); // The original message.

      // Serialize the message.
      data = msg.serialize();
      sendBuf = ChannelBuffers.buffer(data.length);
      sendBuf.writeBytes(data); // Write the actual msg.

      // Send the message upstream.
      Channels.write(ctx, e.getFuture(), sendBuf);
    }