Esempio n. 1
0
  /**
   * Parse TE List from the channel buffer.
   *
   * @param cb of type channel buffer
   * @throws PcepParseException if mandatory fields are missing
   */
  public void parseTEList(ChannelBuffer cb) throws PcepParseException {
    byte yObjClass;
    byte yObjType;

    llTEObjList = new LinkedList<PcepTEObject>();

    // caller should verify for TE object
    if (cb.readableBytes() < OBJECT_HEADER_LENGTH) {
      log.debug("Unable to find TE Object");
      return;
    }

    cb.markReaderIndex();
    PcepObjectHeader tempObjHeader = PcepObjectHeader.read(cb);
    cb.resetReaderIndex();
    yObjClass = tempObjHeader.getObjClass();
    yObjType = tempObjHeader.getObjType();
    PcepTEObject teObj;
    while ((yObjClass == PcepTEObjectVer1.TE_OBJ_CLASS)
        && ((yObjType == PcepTEObjectVer1.TE_OBJ_TYPE_NODE_VALUE)
            || (yObjType == PcepTEObjectVer1.TE_OBJ_TYPE_LINK_VALUE))) {
      teObj = PcepTEObjectVer1.read(cb);
      llTEObjList.add(teObj);

      if (cb.readableBytes() > OBJECT_HEADER_LENGTH) {
        cb.markReaderIndex();
        tempObjHeader = PcepObjectHeader.read(cb);
        cb.resetReaderIndex();
        yObjClass = tempObjHeader.getObjClass();
        yObjType = tempObjHeader.getObjType();
      } else {
        break;
      }
    }
  }
  @Override
  protected Object decode(ChannelHandlerContext ctx, Channel channel, ChannelBuffer buffer)
      throws Exception {
    buffer.markReaderIndex();
    final byte[] buf = new byte[5];
    for (int i = 0; i < buf.length; i++) {
      if (!buffer.readable()) {
        buffer.resetReaderIndex();
        return null;
      }

      buf[i] = buffer.readByte();
      if (buf[i] >= 0) {
        int length = CodedInputStream.newInstance(buf, 0, i + 1).readRawVarint32();
        if (length < 0) {
          throw new CorruptedFrameException("negative length: " + length);
        }

        if (buffer.readableBytes() < length) {
          buffer.resetReaderIndex();
          return null;
        } else {
          return buffer.readBytes(length);
        }
      }
    }

    // Couldn't find the byte whose MSB is off.
    throw new CorruptedFrameException("length wider than 32-bit");
  }
Esempio n. 3
0
 private static boolean maybeSsl(final ChannelBuffer buffer) {
   buffer.markReaderIndex();
   final StringBuffer sb = new StringBuffer();
   for (int lineLength = 0; lineLength++ < 3; sb.append((char) buffer.readByte())) ;
   buffer.resetReaderIndex();
   return !httpVerbPrefix.contains(sb.toString());
 }
Esempio n. 4
0
  /**
   * Reads the byte stream of PcepError from channel buffer.
   *
   * @param cb of type channel buffer
   * @return PcepError error part of PCEP-ERROR
   * @throws PcepParseException if mandatory fields are missing
   */
  public static PcepErrorVer1 read(ChannelBuffer cb) throws PcepParseException {
    if (cb.readableBytes() < OBJECT_HEADER_LENGTH) {
      throw new PcepParseException("Unknown Object");
    }

    PcepErrorVer1 pcepError = new PcepErrorVer1();
    // check whether any PCEP Error Info is present
    cb.markReaderIndex();
    PcepObjectHeader tempObjHeader = PcepObjectHeader.read(cb);
    cb.resetReaderIndex();
    byte yObjClass = tempObjHeader.getObjClass();

    // If RPlist present then store it.RPList and TEList are optional
    if (yObjClass == PcepRPObjectVer1.RP_OBJ_CLASS) {
      log.debug("RP_LIST");
      pcepError.parseRPList(cb);
      yObjClass = checkNextObject(cb);
    } else if (yObjClass == PcepTEObjectVer1.TE_OBJ_CLASS) {
      log.debug("TE_LIST");
      pcepError.parseTEList(cb);
      yObjClass = checkNextObject(cb);
    }

    if (yObjClass == PcepErrorObjectVer1.ERROR_OBJ_CLASS) {
      log.debug("PCEP-ERROR obj list");
      pcepError.parseErrObjList(cb);
      yObjClass = checkNextObject(cb);
    }

    return pcepError;
  }
Esempio n. 5
0
 /**
  * Checks Next Object.
  *
  * @param cb of type channel buffer.
  * @return object type class.
  */
 private static byte checkNextObject(ChannelBuffer cb) {
   if (cb.readableBytes() < OBJECT_HEADER_LENGTH) {
     return 0;
   }
   cb.markReaderIndex();
   PcepObjectHeader tempObjHeader = PcepObjectHeader.read(cb);
   cb.resetReaderIndex();
   return tempObjHeader.getObjClass();
 }
Esempio n. 6
0
  /**
   * parseErrObjList from the channel buffer.
   *
   * @param cb of type channel buffer
   * @throws PcepParseException if mandatory fields are missing
   */
  public void parseErrObjList(ChannelBuffer cb) throws PcepParseException {
    byte yObjClass;
    byte yObjType;
    boolean bIsErrorObjFound = false;

    llErrObjList = new LinkedList<PcepErrorObject>();

    // caller should verify for RP object
    if (cb.readableBytes() < OBJECT_HEADER_LENGTH) {
      throw new PcepParseException("Unable to find PCEP-ERROR Object");
    }

    cb.markReaderIndex();
    PcepObjectHeader tempObjHeader = PcepObjectHeader.read(cb);
    cb.resetReaderIndex();
    yObjClass = tempObjHeader.getObjClass();
    yObjType = tempObjHeader.getObjType();
    PcepErrorObject errorObject;
    while ((yObjClass == PcepErrorObjectVer1.ERROR_OBJ_CLASS)
        && (yObjType == PcepErrorObjectVer1.ERROR_OBJ_TYPE)) {
      errorObject = PcepErrorObjectVer1.read(cb);
      llErrObjList.add(errorObject);
      bIsErrorObjFound = true;

      if (cb.readableBytes() > OBJECT_HEADER_LENGTH) {
        cb.markReaderIndex();
        tempObjHeader = PcepObjectHeader.read(cb);
        cb.resetReaderIndex();
        yObjClass = tempObjHeader.getObjClass();
        yObjType = tempObjHeader.getObjType();
      } else {
        break;
      }
    }

    if (!bIsErrorObjFound) {
      throw new PcepParseException("At least one PCEP-ERROR Object should be present.");
    }
  }
Esempio n. 7
0
  /*
   * 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
    if (buf.readableBytes() < 2) {
      // need more data
      return null;
    }

    // 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();

    // case 1: Control message
    ControlMessage ctrl_msg = ControlMessage.mkMessage(code);
    if (ctrl_msg != null) return ctrl_msg;

    // case 2: task Message
    short task = code;

    // 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 TaskMessage(task, 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);

    // Successfully decoded a frame.
    // Return a TaskMessage object
    return new TaskMessage(task, payload.array());
  }
  protected void applyDataMask(byte[] mask, ChannelBuffer data) {
    if (!shouldMask()) {
      return;
    }

    int dataLen = data.readableBytes();
    data.markReaderIndex();
    for (int i = 0; i < dataLen; ++i) {
      byte cur = data.getByte(i);
      cur = (byte) (cur ^ mask[i % 4]);
      data.setByte(i, cur);
    }
    data.resetReaderIndex();
  }
 @Override
 protected Object decode(
     ChannelHandlerContext ctx, Channel channel, ChannelBuffer buffer, State state)
     throws Exception {
   if (buffer.readableBytes() < 4) {
     return null;
   }
   buffer.markReaderIndex();
   int length = buffer.readInt();
   if (buffer.readableBytes() < length) {
     buffer.resetReaderIndex();
     return null;
   }
   return parseMessage(buffer, length);
 }
Esempio n. 10
0
 @Override
 public boolean checkAccepts(final HttpRequest message) {
   if (message instanceof MappingHttpRequest) {
     final MappingHttpRequest httpRequest = (MappingHttpRequest) message;
     if (httpRequest.getMethod().equals(HttpMethod.POST)) {
       final Map<String, String> parameters =
           new HashMap<String, String>(httpRequest.getParameters());
       final ChannelBuffer buffer = httpRequest.getContent();
       buffer.markReaderIndex();
       final byte[] read = new byte[buffer.readableBytes()];
       buffer.readBytes(read);
       final String query = new String(read);
       buffer.resetReaderIndex();
       for (final String p : query.split("&")) {
         final String[] splitParam = p.split("=");
         String lhs = splitParam[0];
         String rhs = splitParam.length == 2 ? splitParam[1] : null;
         try {
           if (lhs != null) lhs = new URLCodec().decode(lhs);
         } catch (final DecoderException e) {
         }
         try {
           if (rhs != null) rhs = new URLCodec().decode(rhs);
         } catch (final DecoderException e) {
         }
         parameters.put(lhs, rhs);
       }
       for (final RequiredQueryParams p : RequiredQueryParams.values()) {
         if (!parameters.containsKey(p.toString())) {
           return false;
         }
       }
       httpRequest.getParameters().putAll(parameters);
     } else {
       for (final RequiredQueryParams p : RequiredQueryParams.values()) {
         if (!httpRequest.getParameters().containsKey(p.toString())) {
           return false;
         }
       }
     }
     return (message.getUri().startsWith(this.servicePath)
         || message.getUri().startsWith(this.internalServicePath));
   }
   return false;
 }
 /**
  * Overriden super class method. Uses the {@link CommandInterpreter#readCommand(MessageEvent)} to
  * interpret the Command and thereby check if all bytes have been received, returns a null
  * otherwise.
  *
  * @see
  *     org.jboss.netty.handler.codec.frame.FrameDecoder#decode(org.jboss.netty.channel.ChannelHandlerContext,
  *     org.jboss.netty.channel.Channel, org.jboss.netty.buffer.ChannelBuffer)
  */
 protected Object decode(ChannelHandlerContext ctx, Channel channel, ChannelBuffer buffer)
     throws Exception {
   int beginIndex = buffer.readerIndex();
   buffer.markReaderIndex();
   CommandInterpreter.ProxyCommand proxyCommand =
       new CommandInterpreter().interpretCommand(buffer);
   if (proxyCommand.getReadFailure() != null
       && proxyCommand.getReadFailure() == ReadFailure.INSUFFICIENT_DATA) {
     LOGGER.debug(
         "Frame decode failed due to insufficient data. Message is : "
             + proxyCommand.getReadFailureDescription());
     buffer.resetReaderIndex();
     return null; // we return null here and Netty will call this decoder again when more data is
                  // available
   }
   int endIndex = buffer.readerIndex();
   buffer.resetReaderIndex();
   return buffer.readSlice(endIndex - beginIndex);
 }
  @Override
  protected Object decode(ChannelHandlerContext ctx, Channel channel, Object msg) throws Exception {
    if (msg instanceof ChannelBuffer) {
      ChannelBuffer buffer = (ChannelBuffer) msg;
      buffer.markReaderIndex();
      byte clientId = buffer.readByte();
      byte opcode = buffer.readByte();
      if (Opcode.fromByte(opcode) == Opcode.CHAT_MESSAGE) {
        int messageSize = buffer.readInt();
        byte userId = buffer.readByte();
        String message = readString(buffer);

        ChatMessage chatMessage = new ChatMessage(userId, message);
        chatMessage.setClientId((byte) clientId);
        return chatMessage;
      }
      buffer.resetReaderIndex();
    }
    return msg;
  }
Esempio n. 13
0
  /** 解析数据包,主要负责解析数据包前8个字节统一格式的头部信息,生成Packet对象, 剩余的数据部分的解析在后面具体的action处理 */
  @Override
  protected Object decode(ChannelHandlerContext ctx, Channel channel, ChannelBuffer buffer)
      throws Exception {

    if (buffer.readableBytes() < SysConstants.PROTOCOL_HEADER_LENGTH) {
      return null;
    }

    buffer.markReaderIndex(); // 标记一下
    int length = buffer.readInt(); // 消息长度
    char serviceId = buffer.readChar(); // 服务号, 后端这边固定1000
    char commandId = buffer.readChar(); // 命令号
    char version = buffer.readChar(); // 消息version, 1个字节
    char reserved = buffer.readChar(); // 保留,可用户如序列号等
    int contentLength = length - SysConstants.PROTOCOL_HEADER_LENGTH; // 计算数据包中业务数据部分的长度(去除头部长度16)

    if (buffer.readableBytes() < contentLength) {
      logger.info("数据长度:" + contentLength);
      buffer.resetReaderIndex(); // 返回到上面标记的位置
      return null;
    }

    ChannelBuffer cBuffer = ChannelBuffers.buffer(contentLength);
    buffer.readBytes(cBuffer, contentLength); // 转移所有业务部分的数据到新的byte

    Packet packet = new Packet();
    packet.setLength(contentLength);
    packet.setServiceId(serviceId);
    packet.setCommandId(commandId);
    packet.setVersion(version);
    packet.setReserved(reserved);
    DataBuffer dataBuffer = new DataBuffer(cBuffer); // 数据部分
    packet.setContentBuffer(dataBuffer);

    // logger.info("decode packet serviceId : " + packet.getServiceId()
    // + " commandId: " + packet.getCommandId());
    return packet;
  }
  @Override
  protected Object decode(ChannelHandlerContext ctx, Channel channel, ChannelBuffer buffer)
      throws Exception {
    buffer.markReaderIndex();
    byte[] buf = new byte[5];
    for (int i = 0; i < 5; i++) {
      if (!buffer.readable()) {
        break;
      }

      buf[i] = buffer.readByte();
      if (buf[i] >= 0) {
        int messageSize = CodedInputStream.newInstance(buf, 0, i + 1).readRawVarint32();
        if (buffer.readableBytes() < messageSize) {
          break;
        }

        return buffer.readBytes(messageSize);
      }
    }

    buffer.resetReaderIndex();
    return null;
  }
Esempio n. 15
0
  @Override
  protected Object decode(ChannelHandlerContext ctx, Channel channel, ChannelBuffer buffer)
      throws Exception {
    MemcachedRestRequest request = this.request;
    if (request == null) {
      buffer.markReaderIndex();

      if (buffer.readableBytes() < 1) {
        return null;
      }
      short magic = buffer.readUnsignedByte();
      if (magic == 0x80) {
        if (buffer.readableBytes() < 23) return null;
        short opcode = buffer.readUnsignedByte();
        short keyLength = buffer.readShort();
        short extraLength = buffer.readUnsignedByte();
        short dataType = buffer.readUnsignedByte(); // unused
        short reserved = buffer.readShort(); // unused
        int totalBodyLength = buffer.readInt();
        int opaque = buffer.readInt();
        long cas = buffer.readLong();

        // we want the whole of totalBodyLength; otherwise, keep waiting.
        if (buffer.readableBytes() < totalBodyLength) {
          buffer.resetReaderIndex();
          return null;
        }

        buffer.skipBytes(extraLength); // get extras, can be empty

        if (keyLength != 0) {
          byte[] key = new byte[keyLength];
          buffer.readBytes(key);
          String uri = Unicode.fromBytes(key);
          if (opcode == 0x00) { // GET
            request = new MemcachedRestRequest(RestRequest.Method.GET, uri, key, -1, true);
            request.setOpaque(opaque);
            return request;
          } else if (opcode == 0x04) { // DELETE
            request = new MemcachedRestRequest(RestRequest.Method.DELETE, uri, key, -1, true);
            request.setOpaque(opaque);
            return request;
          } else if (opcode == 0x01) { // SET
            // the remainder of the message -- that is, totalLength - (keyLength + extraLength)
            // should be the payload
            int size = totalBodyLength - keyLength - extraLength;
            request = new MemcachedRestRequest(RestRequest.Method.POST, uri, key, size, true);
            request.setOpaque(opaque);
            byte[] data = new byte[size];
            buffer.readBytes(data, 0, size);
            request.setData(data);
            return request;
          }
        } else if (opcode == 0x07) { // QUIT
          channel.disconnect();
        }
      } else {
        buffer.resetReaderIndex(); // reset to get to the first byte
        // need to read a header
        boolean done = false;
        StringBuffer sb = this.sb;
        int readableBytes = buffer.readableBytes();
        for (int i = 0; i < readableBytes; i++) {
          byte next = buffer.readByte();
          if (next == CR) {
            next = buffer.readByte();
            if (next == LF) {
              done = true;
              break;
            }
          } else if (next == LF) {
            done = true;
            break;
          } else {
            sb.append((char) next);
          }
        }
        if (!done) {
          buffer.resetReaderIndex();
          return null;
        }

        String[] args = lineSplit.split(sb);
        // we read the text, clear it
        sb.setLength(0);

        String cmd = args[0];
        String uri = args[1];
        if ("get".equals(cmd)) {
          request = new MemcachedRestRequest(RestRequest.Method.GET, uri, null, -1, false);
          if (args.length > 3) {
            request.setData(Unicode.fromStringAsBytes(args[2]));
          }
          return request;
        } else if ("delete".equals(cmd)) {
          request = new MemcachedRestRequest(RestRequest.Method.DELETE, uri, null, -1, false);
          //                if (args.length > 3) {
          //                    request.setData(Unicode.fromStringAsBytes(args[2]));
          //                }
          return request;
        } else if ("set".equals(cmd)) {
          this.request =
              new MemcachedRestRequest(
                  RestRequest.Method.POST, uri, null, Integer.parseInt(args[4]), false);
          buffer.markReaderIndex();
        } else if ("quit".equals(cmd)) {
          channel.disconnect();
        }
      }
    } else {
      if (buffer.readableBytes() < (request.getDataSize() + 2)) {
        return null;
      }
      byte[] data = new byte[request.getDataSize()];
      buffer.readBytes(data, 0, data.length);
      byte next = buffer.readByte();
      if (next == CR) {
        next = buffer.readByte();
        if (next == LF) {
          request.setData(data);
          // reset
          this.request = null;
          return request;
        } else {
          this.request = null;
          throw new StreamCorruptedException("Expecting \r\n after data block");
        }
      } else {
        this.request = null;
        throw new StreamCorruptedException("Expecting \r\n after data block");
      }
    }
    return null;
  }
Esempio n. 16
0
 /**
  * Ensures that at least a {@code nbytes} are readable from the given buffer. If there aren't
  * enough bytes in the buffer this will raise an exception and cause the {@link ReplayingDecoder}
  * to undo whatever we did thus far so we can wait until we read more from the socket.
  *
  * @param buf Buffer to check.
  * @param nbytes Number of bytes desired.
  */
 static void ensureReadable(final ChannelBuffer buf, final int nbytes) {
   buf.markReaderIndex();
   buf.skipBytes(nbytes); // can puke with Throwable
   buf.resetReaderIndex();
 }
Esempio n. 17
0
  /*
   * 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();
    // Length of control message is 10.
    // Minimum length of a task message is 6(short taskId, int length).
    if (available < 10) {
      // need more data
      return null;
    }

    Long startTime = null;
    if (isServer) {
      startTime = System.nanoTime();
    }
    try {
      // 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 (available < 12) {
          // The time stamp bytes were not received yet - return null.
          buf.resetReaderIndex();
          return null;
        }
        long timeStamp = buf.readLong();
        int clientPort = buf.readInt();
        available -= 12;
        if (ctrl_msg == ControlMessage.EOB_MESSAGE) {

          long interval = System.currentTimeMillis() - timeStamp;
          if (interval > 0) {

            Histogram netTransTime = getTransmitHistogram(channel, clientPort);
            if (netTransTime != null) {
              netTransTime.update(interval);
            }
          }

          recvSpeed.update(Double.valueOf(ControlMessage.encodeLength()));
        }

        return ctrl_msg;
      }

      // case 2: task Message
      short task = code;

      // Make sure that we have received at least an integer (length)
      if (available < 8) {
        // need more data
        buf.resetReaderIndex();

        return null;
      }

      // Read the length field.
      int length = buf.readInt();
      if (length <= 0) {
        LOG.info("Receive one message whose TaskMessage's message length is {}", length);
        return new TaskMessage(task, null);
      }
      int headerLength = buf.readInt();
      if (headerLength <= 0) {
        LOG.info("Receive one message whose TaskMessage's message header length is {}", length);
      }
      // Make sure if there's enough bytes in the buffer.
      available -= 8;
      if (available < length + headerLength) {
        // The whole bytes were not received yet - return null.
        buf.resetReaderIndex();

        return null;
      }

      String component = null;
      String stream = null;
      if (headerLength > 0) {
        ChannelBuffer header = buf.readBytes(headerLength);
        String headerValue = new String(header.array());
        String splits[] = headerValue.split(" ");
        stream = splits[0];
        component = splits[1];
      }

      // There's enough bytes in the buffer. Read it.
      ChannelBuffer payload = buf.readBytes(length);

      // Successfully decoded a frame.
      // Return a TaskMessage object

      byte[] rawBytes = payload.array();
      // @@@ TESTING CODE
      // LOG.info("Receive task:{}, length: {}, data:{}",
      // task, length, JStormUtils.toPrintableString(rawBytes));

      TaskMessage ret = new TaskMessage(task, rawBytes, component, stream);
      recvSpeed.update(Double.valueOf(rawBytes.length + 6));
      return ret;
    } finally {
      if (isServer) {
        Long endTime = System.nanoTime();
        timer.update((endTime - startTime) / 1000000.0d);
      }
    }
  }