com.h3c.util.pkt

## Class Packet

- java.lang.Object
- - com.h3c.util.pkt.Packet

-     public class Packet
      extends Object

  Packet (immutable) contains a list of protocol layers.

  An onion metaphor is used to describe the layers of a packet. The layer index corresponds to the depth inside the packet. For example, if the following packet is constructed:

       Packet p = new Packet(eth, ip, udp, dhcp)

  The layers are of the form:
  - layers[0] = eth (outermost)
  - layers[1] = ip
  - layers[2] = udp
  - layers[3] = dhcp (innermost)
  In which case, a call to `innermostId()` would return `ProtocolId.DHCP`.

- - ### Constructor Summary

    Constructors
    Constructor and Description
    `Packet(List<Protocol> layers)` Constructor creates a packet of protocol layers.
    `Packet(Protocol... layers)` Constructor creates a packet of protocol layers.

  - ### Method Summary

    Methods
    Modifier and Type | Method and Description
    --- | ---
    `static long` | `computeProtocolMask(ProtocolId... protocols)` Computes the mask of all protocol IDs.
    `Protocol` | `get(int i)` Returns the protocol layer at the given index.
    `<P extends Protocol> P` | `get(ProtocolId id)` Returns the protocol with the given protocol ID.
    `boolean` | `has(ProtocolId id)` Returns true if the protocols corresponding to the given ID is contained in the packet.
    `boolean` | `hasAll(ProtocolId... ids)` Returns true if all the protocols corresponding to the IDs are contained in the packet.
    `boolean` | `hasAny(ProtocolId... ids)` Returns true if any of the protocols corresponding to the IDs are contained in the packet.
    `Protocol` | `innermost()` Returns the innermost layer protocol (highest in the network stack).
    `ProtocolId` | `innermostId()` Returns the innermost layer protocol ID (highest in the the network stack).
    `List<ProtocolId>` | `protocolIds()` Returns a list of protocol IDs from the outermost to innermost layer.
    `long` | `protocolMask()` Returns the bit mask representing present protocol IDs.
    `int` | `size()` Returns the number of protocol layers.
    `String` | `toDebugString()` Returns the formatted debug string for the packet.
    `String` | `toString()`

    - ### Methods inherited from class java.lang.Object

      `clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait`

- - ### Constructor Detail

    - #### Packet

          public Packet(Protocol... layers)

      Constructor creates a packet of protocol layers.
      Parameters:
        `layers` - protocol layers

    - #### Packet

          public Packet(List<Protocol> layers)

      Constructor creates a packet of protocol layers.
      Parameters:
        `layers` - protocol layers

  - ### Method Detail

    - #### computeProtocolMask

          public static long computeProtocolMask(ProtocolId... protocols)

      Computes the mask of all protocol IDs.
      Parameters:
        `protocols` - collection of protocols
      Returns:
        bit mask of all protocol IDs

    - #### size

          public int size()

      Returns the number of protocol layers.
      Returns:
        the number of protocol layers

    - #### get

          public Protocol get(int i)

      Returns the protocol layer at the given index.
      Parameters:
        `i` - layer index
      Returns:
        the protocol layer at the given index or null

    - #### get

          public <P extends Protocol> P get(ProtocolId id)

      Returns the protocol with the given protocol ID.

      Suppress warning is needed here because of the cast from `Protocol` to the generic type. It is safe because each concrete protocol must implement `Protocol.id()` to return its distinct ID.

      Parameters:
        `id` - protocol ID
      Returns:
        the protocol layer at the given index or null

    - #### has

          public boolean has(ProtocolId id)

      Returns true if the protocols corresponding to the given ID is contained in the packet.
      Parameters:
        `id` - protocol ID
      Returns:
        true if the protocol is contained in the packet

    - #### hasAll

          public boolean hasAll(ProtocolId... ids)

      Returns true if all the protocols corresponding to the IDs are contained in the packet.
      Parameters:
        `ids` - protocol IDs
      Returns:
        true if all of the protocols are contained in the packet

    - #### hasAny

          public boolean hasAny(ProtocolId... ids)

      Returns true if any of the protocols corresponding to the IDs are contained in the packet.
      Parameters:
        `ids` - protocol IDs
      Returns:
        true if any of the protocols are contained in the packet

    - #### protocolIds

          public List<ProtocolId> protocolIds()

      Returns a list of protocol IDs from the outermost to innermost layer.
      Returns:
        list of protocol IDs

    - #### innermost

          public Protocol innermost()

      Returns the innermost layer protocol (highest in the network stack).
      Returns:
        the protocol layer or null

    - #### innermostId

          public ProtocolId innermostId()

      Returns the innermost layer protocol ID (highest in the the network stack).
      Returns:
        the protocol layer ID or `UNKNOWN` if there are no known protocols in the packet

    - #### protocolMask

          public long protocolMask()

      Returns the bit mask representing present protocol IDs.
      Returns:
        bit mask of protocol IDs

    - #### toString

          public String toString()

      Overrides:
        `toString` in class `Object`

    - #### toDebugString

          public String toDebugString()

      Returns the formatted debug string for the packet.
      Returns:
        the formatted debug string representing this packet

Copyright © 2014. All Rights Reserved.