Class Protocol

java.lang.Object
org.jgroups.stack.Protocol
All Implemented Interfaces:
Lifecycle
Direct Known Subclasses:
AUTH, BARRIER, BATCH, BATCH2, CLEAR_FLAGS, COMPRESS, COUNTER, DAISYCHAIN, DELAY, DETECT_LOOPBACKS, DISCARD, DISCARD_PAYLOAD, Discovery, DROP, DUPL, Encrypt, EXAMPLE, FailureDetection, FD_HOST, FD_SOCK, FD_SOCK2, FlowControl, FLUSH, FORK, ForkProtocol, Fragmentation, GMS, HDRS, INJECT_VIEW, KeyExchange, Locking, MAKE_BATCH, MERGE3, MessageDispatcher.ProtocolAdapter, NAKACK2, NON_BLOCKING_SENDS, PDC, PERF, ProtocolStack, RATE, RATE_LIMITER, RATE_LIMITER2, RED, RELAY, REVERSE, RSVP, SEQUENCER, SEQUENCER2, SERIALIZE, SHUFFLE, SIZE, SNIFF, SOS, STABLE, STATE_TRANSFER, STATS, STOMP, StreamingStateTransfer, THREAD_COUNT, TIME, TP, TRACE, UNBATCH, UNICAST3, VERIFY_SUSPECT, VERIFY_SUSPECT2

public abstract class Protocol extends Object implements Lifecycle
The Protocol class provides a set of common services for protocol layers. Each layer has to be a subclass of Protocol and override a number of methods (typically just up(), down() and getName(). Layers are stacked in a certain order to form a protocol stack. Events are passed from lower layers to upper ones and vice versa. E.g. a Message received by the UDP layer at the bottom will be passed to its higher layer as an Event. That layer will in turn pass the Event to its layer and so on, until a layer handles the Message and sends a response or discards it, the former resulting in another Event being passed down the stack.

The important thing to bear in mind is that Events have to be passed on between layers in FIFO order which is guaranteed by the Protocol implementation and must be guaranteed by subclasses implementing their on Event queuing.

Note that each class implementing interface Protocol MUST provide an empty, public constructor !

  • Field Details

    • up_prot

      protected Protocol up_prot
    • down_prot

      protected Protocol down_prot
    • stack

      protected ProtocolStack stack
    • stats

      protected boolean stats
    • ergonomics

      protected boolean ergonomics
    • after_creation_hook

      protected String after_creation_hook
    • id

      protected short id
    • local_addr

      protected Address local_addr
    • log

      protected final Log log
    • policies

      protected List<Policy> policies
  • Constructor Details

    • Protocol

      public Protocol()
  • Method Details

    • setLevel

      public <T extends Protocol> T setLevel(String level)
      Sets the level of a logger. This method is used to dynamically change the logging level of a running system, e.g. via JMX. The appender of a level needs to exist.
      Parameters:
      level - The new level. Valid values are "fatal", "error", "warn", "info", "debug", "trace" (capitalization not relevant)
    • getLevel

      public String getLevel()
    • level

      public <T extends Protocol> T level(String level)
    • getAddress

      public Address getAddress()
    • addr

      public Address addr()
    • addr

      public <T extends Protocol> T addr(Address addr)
    • setAddress

      public <T extends Protocol> T setAddress(Address addr)
    • isErgonomics

      public boolean isErgonomics()
    • setErgonomics

      public <T extends Protocol> T setErgonomics(boolean ergonomics)
    • getProtocolStack

      public ProtocolStack getProtocolStack()
    • statsEnabled

      public boolean statsEnabled()
    • enableStats

      public void enableStats(boolean flag)
    • getName

      public String getName()
    • getId

      public short getId()
    • setId

      public <T extends Protocol> T setId(short id)
    • getUpProtocol

      public <T extends Protocol> T getUpProtocol()
    • getDownProtocol

      public <T extends Protocol> T getDownProtocol()
    • setUpProtocol

      public <T extends Protocol> T setUpProtocol(Protocol prot)
    • setDownProtocol

      public <T extends Protocol> T setDownProtocol(Protocol prot)
    • setProtocolStack

      public <T extends Protocol> T setProtocolStack(ProtocolStack s)
    • afterCreationHook

      public String afterCreationHook()
    • getLog

      public Log getLog()
    • getPolicies

      public List<? extends Policy> getPolicies()
    • policies

      public String policies()
    • setPolicies

      public <T extends Protocol> T setPolicies(List<Policy> l)
    • addPolicy

      public <T extends Protocol> T addPolicy(Policy p)
    • removePolicy

      public <T extends Protocol> T removePolicy(Policy p)
    • getValue

      public Object getValue(String name)
    • setValue

      public <T extends Protocol> T setValue(String name, Object value)
    • getComponents

      public List<Object> getComponents()
      After configuring the protocol itself from the properties defined in the XML config, a protocol might have additional component objects which need to be configured. This callback allows a protocol developer to configure those other objects. This call is guaranteed to be invoked after the protocol itself has been configured.

      See AUTH for an example.

    • parse

      public void parse(XmlNode node) throws Exception
      Called by the XML parser when subelements are found in the configuration of a protocol. This allows a protocol to define protocol-specific information and to parse it
      Throws:
      Exception
    • getIdsAbove

      public short[] getIdsAbove()
      Returns the protocol IDs of all protocols above this one (excluding the current protocol)
    • getTransport

      public TP getTransport()
    • getThreadFactory

      public ThreadFactory getThreadFactory()
      Supposed to be overwritten by subclasses. Usually the transport returns a valid non-null thread factory, but thread factories can also be created by individual protocols
      Returns:
    • getSocketFactory

      public SocketFactory getSocketFactory()
      Returns the SocketFactory associated with this protocol, if overridden in a subclass, or passes the call down
      Returns:
      SocketFactory
    • setSocketFactory

      public void setSocketFactory(SocketFactory factory)
      Sets a SocketFactory. Socket factories are typically provided by the transport (TP)
      Parameters:
      factory -
    • resetStatistics

      public void resetStatistics()
    • resetStats

      public void resetStats()
    • init

      public void init() throws Exception
      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
      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
    • start

      public void start() throws Exception
      This method is called on a JChannel.connect(String); starts work. Protocols are connected ready to receive events. Will be called from bottom to top.
      Specified by:
      start in interface Lifecycle
      Throws:
      Exception - Thrown if protocol cannot be started successfully. This will cause the ProtocolStack to fail, so JChannel.connect(String) will throw an exception
    • stop

      public void stop()
      Called on a JChannel.disconnect(); stops work (e.g. by closing multicast socket). Will be called from top to bottom.
      Specified by:
      stop in interface Lifecycle
    • destroy

      public void destroy()
      This method is called on a JChannel.close(). Does some cleanup; after the call, the VM will terminate
      Specified by:
      destroy in interface Lifecycle
    • requiredUpServices

      public List<Integer> requiredUpServices()
      List of events that are required to be answered by some layer above
    • requiredDownServices

      public List<Integer> requiredDownServices()
      List of events that are required to be answered by some layer below
    • providedUpServices

      public List<Integer> providedUpServices()
      List of events that are provided to layers above (they will be handled when sent down from above)
    • providedDownServices

      public List<Integer> providedDownServices()
      List of events that are provided to layers below (they will be handled when sent from down below)
    • getDownServices

      public final List<Integer> getDownServices()
      Returns all services provided by protocols below the current protocol
    • getUpServices

      public final List<Integer> getUpServices()
      Returns all services provided by the protocols above the current protocol
    • down

      public Object down(Event evt)
      An event is to be sent down the stack. A protocol may want to examine its type and perform some action on it, depending on the event's type. If the event is a message MSG, then the protocol may need to add a header to it (or do nothing at all) before sending it down the stack using down_prot.down().
    • down

      public Object down(Message msg)
      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.
      Since:
      4.0
    • down

      public CompletableFuture<Object> down(Message msg, boolean async)
      Passes a message down asynchronously. The sending is executed in the transport's thread pool. If the pool is full and the message is marked as Message.TransientFlag.DONT_BLOCK, then it will be dropped, otherwise it will be sent on the caller's thread.
      Parameters:
      msg - The message to be sent
      async - Whether to send the message asynchronously
      Returns:
      A CompletableFuture of the result (or exception)
    • up

      public Object up(Event evt)
      An event was received from the protocol below. Usually the current protocol will want to examine the event type and - depending on its type - perform some computation (e.g. removing headers from a MSG event type, or updating the internal membership list when receiving a VIEW_CHANGE event). Finally, the event is either a) discarded, or b) an event is sent down the stack using down_prot.down() or c) the event (or another event) is sent up the stack using up_prot.up().
    • up

      public Object up(Message msg)
      A single message was received. Protocols may examine the message and do something (e.g. add a header) with it before passing it up.
      Since:
      4.0
    • up

      public void up(MessageBatch batch)
      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 accept(Message)), and - if true - calls 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.

      Parameters:
      batch - The message batch
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • accept

      protected boolean accept(Message msg)
      Called by the default implementation of up(org.jgroups.util.MessageBatch) for each message to determine if the message should be removed from the message batch (and handled by the current protocol) or not.
      Parameters:
      msg - The message. Guaranteed to be non-null
      Returns:
      True if the message should be handled by this protocol (will be removed from the batch), false if the message should remain in the batch and be passed up.

      The default implementation tries to find a header matching the current protocol's ID and returns true if there is a match, or false otherwise