Ads2PayloadProtocol extends MessageToMessageCodec<ByteBuf, AmsPacket> { @Override protected void decode(ChannelHandlerContext channelHandlerContext, ByteBuf byteBuf, List<Object> out) { if (byteBuf == Unpooled.EMPTY_BUFFER) { reset(); return; } LOGGER.trace("(-->IN): {}, {}, {}", channelHandlerContext, byteBuf, out); AmsNetId targetAmsNetId = AmsNetId.of(byteBuf); AmsPort targetAmsPort = AmsPort.of(byteBuf); AmsNetId sourceAmsNetId = AmsNetId.of(byteBuf); AmsPort sourceAmsPort = AmsPort.of(byteBuf); Command commandId = Command.of(byteBuf); State stateId = State.of(byteBuf); DataLength dataLength = DataLength.of(byteBuf); AmsError errorCode = AmsError.of(byteBuf); Invoke invoke = Invoke.of(byteBuf); AmsPacket correlatedAmsPacket = requests.remove(invoke); if (correlatedAmsPacket != null) { LOGGER.debug("Correlated packet received {}", correlatedAmsPacket); } if (dataLength.getAsLong() > Integer.MAX_VALUE) { throw new AdsProtocolOverflowException(Integer.class, dataLength.getAsLong()); } ByteBuf commandBuffer = byteBuf.readBytes((int) dataLength.getAsLong()); try { AmsHeader amsHeader = AmsHeader.of(targetAmsNetId, targetAmsPort, sourceAmsNetId, sourceAmsPort, commandId, stateId, dataLength, errorCode, invoke); final AmsPacket amsPacket; switch (commandId) { case INVALID: amsPacket = handleInvalidCommand(commandBuffer, amsHeader); break; case ADS_READ_DEVICE_INFO: amsPacket = handleADSReadDeviceInfoCommand(stateId, commandBuffer, amsHeader); break; case ADS_READ: amsPacket = handleADSReadCommand(stateId, commandBuffer, amsHeader); break; case ADS_WRITE: amsPacket = handleADSWriteCommand(stateId, commandBuffer, amsHeader); break; case ADS_READ_STATE: amsPacket = handleADSReadStateCommand(stateId, commandBuffer, amsHeader); break; case ADS_WRITE_CONTROL: amsPacket = handleADSWriteControlCommand(stateId, commandBuffer, amsHeader); break; case ADS_ADD_DEVICE_NOTIFICATION: amsPacket = handleADSAddDeviceNotificationCommand(stateId, commandBuffer, amsHeader); break; case ADS_DELETE_DEVICE_NOTIFICATION: amsPacket = handADSDeleteDeviceNotificationCommand(stateId, commandBuffer, amsHeader); break; case ADS_DEVICE_NOTIFICATION: amsPacket = handleADSDeviceNotificationCommand(stateId, commandBuffer, amsHeader); break; case ADS_READ_WRITE: amsPacket = handleADSReadWriteCommand(stateId, commandBuffer, amsHeader); break; case UNKNOWN: default: amsPacket = handleUnknownCommand(commandBuffer, amsHeader); } LOGGER.debug("Received amsPacket {}", amsPacket); out.add(amsPacket); if (commandBuffer.readableBytes() > 0) { throw new IllegalStateException("Unread bytes left: " + commandBuffer.readableBytes()); } } finally { commandBuffer.release(); } } Ads2PayloadProtocol(); void reset();  }
@Test public void decode() throws Exception { ArrayList<Object> out = new ArrayList<>(); SUT.decode(null, amsPacket.getByteBuf(), out); assertThat(out, hasSize(1)); }