Class FRAG2

All Implemented Interfaces:
Lifecycle
Direct Known Subclasses:
FRAG4

public class FRAG2 extends Fragmentation
Fragmentation layer. Fragments messages larger than frag_size into smaller packets. Reassembles fragmented packets into bigger ones. The fragmentation number is prepended to the messages as a header (and removed at the receiving side).

Each fragment is identified by (a) the sender (part of the message to which the header is appended), (b) the fragmentation ID (which is unique per FRAG2 layer (monotonically increasing) and (c) the fragement ID which ranges from 0 to number_of_fragments-1.

Requirement: lossless delivery (e.g. NAK, ACK). No requirement on ordering. Works for both unicast and multicast messages.

Compared to FRAG, this protocol does not need to serialize the message in order to break it into smaller fragments: it looks only at the message's buffer, which is a byte[] array anyway. We assume that the size addition for headers and src and dest address is minimal when the transport finally has to serialize the message, so we add a constant (200 bytes).

Note that this protocol only works with BytesMessage types

  • Field Details

  • Constructor Details

    • FRAG2

      public FRAG2()
  • Method Details

    • getAvgSizeDown

      public String getAvgSizeDown()
    • getAvgSizeUp

      public String getAvgSizeUp()
    • getNextId

      protected long getNextId()
    • init

      public void init() throws Exception
      Description copied from class: Protocol
      Called after a protocol has been created and before the protocol is started. Attributes are already set. Other protocols are not yet connected and events cannot yet be sent.
      Specified by:
      init in interface Lifecycle
      Overrides:
      init in class Protocol
      Throws:
      Exception - Thrown if protocol cannot be initialized successfully. This will cause the ProtocolStack to fail, so the the channel constructor will throw an exception
    • resetStats

      public void resetStats()
      Overrides:
      resetStats in class Fragmentation
    • down

      public Object down(Event evt)
      Fragment a packet if larger than frag_size (add a header). Otherwise just pass down. Only add a header if fragmentation is needed !
      Overrides:
      down in class Protocol
    • down

      public Object down(Message msg)
      Description copied from class: Protocol
      A message is sent down the stack. Protocols may examine the message and do something (e.g. add a header) with it, before passing it down.
      Overrides:
      down in class Protocol
    • up

      public Object up(Event evt)
      If event is a message, if it is fragmented, re-assemble fragments into big message and pass up the stack.
      Overrides:
      up in class Protocol
    • up

      public Object up(Message msg)
      Description copied from class: Protocol
      A single message was received. Protocols may examine the message and do something (e.g. add a header) with it before passing it up.
      Overrides:
      up in class Protocol
    • up

      public void up(MessageBatch batch)
      Description copied from class: Protocol
      Sends up a multiple messages in a MessageBatch. The sender of the batch is always the same, and so is the destination (null == multicast messages). Messages in a batch can be OOB messages, regular messages, or mixed messages, although the transport itself will create initial MessageBatches that contain only either OOB or regular messages.

      The default processing below sends messages up the stack individually, based on a matching criteria (calling Protocol.accept(Message)), and - if true - calls Protocol.up(org.jgroups.Event) for that message and removes the message. If the batch is not empty, it is passed up, or else it is dropped.

      Subclasses should check if there are any messages destined for them (e.g. using MessageBatch.iterator(Predicate)), then possibly remove and process them and finally pass the batch up to the next protocol. Protocols can also modify messages in place, e.g. ENCRYPT could decrypt all encrypted messages in the batch, not remove them, and pass the batch up when done.

      Overrides:
      up in class Protocol
      Parameters:
      batch - The message batch
    • handleViewChange

      protected void handleViewChange(View view)
    • clearFragmentsFor

      public void clearFragmentsFor(Address mbr)
    • clearAllFragments

      public void clearAllFragments()
    • fragment

      protected void fragment(Message msg)
      Send all fragments as separate messages (with same ID !). Example:
           Given the generated ID is 2344, number of fragments=3, message {dst,src,buf}
           would be fragmented into:
      
           [2344,3,0]{dst,src,buf1},
           [2344,3,1]{dst,src,buf2} and
           [2344,3,2]{dst,src,buf3}
           
    • unfragment

      protected Message unfragment(Message msg, FragHeader hdr)
      1. Get all the fragment buffers 2. When all are received: assemble them into one big buffer 3. Read headers and byte buffer from big buffer 4. Set headers and buffer in msg 5. Return the message
    • assembleMessage

      protected Message assembleMessage(Message[] fragments, boolean needs_deserialization, FragHeader hdr) throws Exception
      Assembles all the message fragments into one message. This method does not check if the fragmentation is complete (use FRAG2.FragEntry.isComplete() to verify) before calling this method)
      Returns:
      the complete message
      Throws:
      Exception