MinaPacketDecoder.java 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. package com.persagy.iottransfer.communication.mina.codec;
  2. import com.persagy.iottransfer.communication.entity.Packet;
  3. import com.persagy.iottransfer.communication.util.MyDecoder;
  4. import org.apache.mina.core.buffer.IoBuffer;
  5. import org.apache.mina.core.session.IoSession;
  6. import org.apache.mina.filter.codec.CumulativeProtocolDecoder;
  7. import org.apache.mina.filter.codec.ProtocolDecoderOutput;
  8. import java.util.HashMap;
  9. import java.util.List;
  10. import java.util.Map;
  11. public class MinaPacketDecoder extends CumulativeProtocolDecoder {
  12. String ip;
  13. int port;
  14. String encoding;
  15. String aes_password;
  16. boolean compress = false;
  17. boolean separate = false;
  18. boolean separateBytes = false;
  19. byte prefix;
  20. byte suffix;
  21. byte[] prefixBytes;
  22. byte[] suffixBytes;
  23. MyDecoder decoder;
  24. Map<String, List<Byte>> tmpByteListMap = new HashMap<String, List<Byte>>();
  25. public MinaPacketDecoder(String ip, int port, String encoding, String aes_password, boolean compress) {
  26. this.ip = ip;
  27. this.port = port;
  28. this.encoding = encoding;
  29. this.aes_password = aes_password;
  30. this.compress = compress;
  31. this.separate = false;
  32. this.decoder = new MyDecoder(this.ip, this.port, this.encoding, this.aes_password, this.compress, this.separate,
  33. this.separateBytes, this.prefix, this.suffix, this.prefixBytes, this.suffixBytes);
  34. }
  35. public MinaPacketDecoder(String ip, int port, String encoding, String aes_password, boolean compress, byte prefix,
  36. byte suffix) {
  37. this.ip = ip;
  38. this.port = port;
  39. this.encoding = encoding;
  40. this.aes_password = aes_password;
  41. this.compress = compress;
  42. this.separate = true;
  43. this.separateBytes = false;
  44. this.prefix = prefix;
  45. this.suffix = suffix;
  46. this.decoder = new MyDecoder(this.ip, this.port, this.encoding, this.aes_password, this.compress, this.separate,
  47. this.separateBytes, this.prefix, this.suffix, this.prefixBytes, this.suffixBytes);
  48. }
  49. public MinaPacketDecoder(String ip, int port, String encoding, String aes_password, boolean compress,
  50. byte[] prefixBytes, byte[] suffixBytes) {
  51. this.ip = ip;
  52. this.port = port;
  53. this.encoding = encoding;
  54. this.aes_password = aes_password;
  55. this.compress = compress;
  56. this.separate = true;
  57. this.separateBytes = true;
  58. this.prefixBytes = prefixBytes;
  59. this.suffixBytes = suffixBytes;
  60. this.decoder = new MyDecoder(this.ip, this.port, this.encoding, this.aes_password, this.compress, this.separate,
  61. this.separateBytes, this.prefix, this.suffix, this.prefixBytes, this.suffixBytes);
  62. }
  63. public boolean doDecode(IoSession session, IoBuffer in, ProtocolDecoderOutput out) throws Exception {
  64. int remainingLength = in.remaining();
  65. byte[] remainingBytes = new byte[remainingLength];
  66. for (int i = 0; i < remainingLength; i++) {
  67. remainingBytes[i] = in.get();
  68. }
  69. String remoteAddress = session.getRemoteAddress().toString();
  70. List<Packet> packetList = this.decoder.decode(remoteAddress, remainingBytes);
  71. for (int i = 0; i < packetList.size(); i++) {
  72. Packet packet = packetList.get(i);
  73. out.write(packet);
  74. }
  75. return true;
  76. }
  77. }