Class ProtocolStack

java.lang.Object
org.jgroups.stack.Protocol
org.jgroups.stack.ProtocolStack
All Implemented Interfaces:
Lifecycle
Direct Known Subclasses:
ForkProtocolStack

public class ProtocolStack extends Protocol
A ProtocolStack manages a number of protocols layered above each other. It creates all protocol classes, initializes them and, when ready, starts all of them, beginning with the bottom most protocol. It also dispatches messages received from the stack to registered objects (e.g. channel, GMP) and sends messages sent by those objects down the stack.

The ProtocolStack makes use of the Configurator to setup and initialize stacks, and to destroy them again when not needed anymore

  • Field Details

  • Constructor Details

    • ProtocolStack

      public ProtocolStack(JChannel channel) throws Exception
      Throws:
      Exception
    • ProtocolStack

      public ProtocolStack()
      Used for programmatic creation of ProtocolStack
  • Method Details

    • topProtocol

      public ProtocolStack topProtocol(Protocol top)
    • bottomProtocol

      public ProtocolStack bottomProtocol(Protocol bottom)
    • getChannel

      public JChannel getChannel()
    • setChannel

      public ProtocolStack setChannel(JChannel ch)
    • getProtocols

      public List<Protocol> getProtocols()
      Returns all protocols in a list, from top to bottom. These are not copies of protocols, so modifications will affect the actual instances !
    • getTransport

      public TP getTransport()
      Returns the bottom most protocol
      Overrides:
      getTransport in class Protocol
    • dumpStats

      public Map<String,Map<String,Object>> dumpStats()
    • dumpStats

      public Map<String,Map<String,Object>> dumpStats(String protocol_name, List<String> attrs)
    • printProtocolSpec

      public String printProtocolSpec(boolean include_properties)
      Prints the names of the protocols, from the bottom to top. If include_properties is true, the properties for each protocol will also be printed.
    • printProtocolSpecAsXML

      public String printProtocolSpecAsXML()
    • printProtocolSpecAsPlainString

      public String printProtocolSpecAsPlainString()
    • printProtocolSpecAsPlainString

      private String printProtocolSpecAsPlainString(boolean print_props)
    • getProps

      private static Map<String,String> getProps(Protocol prot)
    • setup

      public void setup(List<ProtocolConfiguration> configs) throws Exception
      Throws:
      Exception
    • setup

      public void setup(List<ProtocolConfiguration> configs, ProtocolHook afterCreationHook) throws Exception
      Throws:
      Exception
    • addProtocol

      public ProtocolStack addProtocol(Protocol prot)
      Adds a protocol at the tail of the protocol list
      Parameters:
      prot -
      Returns:
      Since:
      2.11
    • addProtocols

      public ProtocolStack addProtocols(Protocol... prots)
      Adds a list of protocols
      Parameters:
      prots -
      Returns:
      Since:
      2.11
    • addProtocols

      public ProtocolStack addProtocols(List<Protocol> prots)
      Adds a list of protocols
      Parameters:
      prots -
      Returns:
      Since:
      2.1
    • insertProtocol

      public void insertProtocol(Protocol prot, ProtocolStack.Position position, String neighbor_prot) throws Exception
      Inserts an already created (and initialized) protocol into the protocol list. Sets the links to the protocols above and below correctly and adjusts the linked list of protocols accordingly. Note that this method may change the value of top_prot or bottom_prot.
      Parameters:
      prot - The protocol to be inserted. Before insertion, a sanity check will ensure that none of the existing protocols have the same name as the new protocol.
      position - Where to place the protocol with respect to the neighbor_prot (ABOVE, BELOW)
      neighbor_prot - The name of the neighbor protocol. An exception will be thrown if this name is not found
      Throws:
      Exception - Will be thrown when the new protocol cannot be created, or inserted.
    • insertProtocolInStack

      public void insertProtocolInStack(Protocol prot, Protocol neighbor, ProtocolStack.Position position)
    • checkAndSwitchTop

      private void checkAndSwitchTop(Protocol oldTop, Protocol newTop)
    • insertProtocol

      public void insertProtocol(Protocol prot, ProtocolStack.Position position, Class<? extends Protocol> neighbor_prot) throws Exception
      Throws:
      Exception
    • insertProtocol

      @SafeVarargs public final void insertProtocol(Protocol prot, ProtocolStack.Position position, Class<? extends Protocol>... neighbor_prots) throws Exception
      Throws:
      Exception
    • insertProtocolAtTop

      public void insertProtocolAtTop(Protocol prot)
    • removeProtocol

      public <T extends Protocol> T removeProtocol(String prot_name)
      Removes a protocol from the stack. Stops the protocol and readjusts the linked lists of protocols.
      Parameters:
      prot_name - The name of the protocol. Since all protocol names in a stack have to be unique (otherwise the stack won't be created), the name refers to just 1 protocol.
    • removeProtocols

      public ProtocolStack removeProtocols(String... protocols)
    • removeProtocols

      @SafeVarargs public final ProtocolStack removeProtocols(Class<? extends Protocol>... protocols)
    • removeProtocol

      @SafeVarargs public final <T extends Protocol> T removeProtocol(Class<? extends Protocol>... protocols)
    • removeProtocol

      public <T extends Protocol> T removeProtocol(Class<? extends Protocol> prot)
    • removeProtocol

      public <T extends Protocol> T removeProtocol(T prot)
    • findProtocol

      public <T extends Protocol> T findProtocol(String name)
      Returns a given protocol or null if not found
    • findProtocols

      public <T extends Protocol> List<T> findProtocols(String regexp)
    • getBottomProtocol

      public <T extends Protocol> T getBottomProtocol()
    • getTopProtocol

      public Protocol getTopProtocol()
    • findProtocol

      public <T extends Protocol> T findProtocol(Class<? extends Protocol> clazz)
    • findProtocol

      public static <T extends Protocol> T findProtocol(Protocol start, boolean down, Class<? extends Protocol> clazz)
    • findProtocol

      @SafeVarargs public final <T extends Protocol> T findProtocol(Class<? extends Protocol>... classes)
      Finds the first protocol of a list and returns it. Returns null if no protocol can be found
      Parameters:
      classes - A list of protocol classes to find
      Returns:
      Protocol The protocol found
    • replaceProtocol

      public void replaceProtocol(Protocol existing_prot, Protocol new_prot) throws Exception
      Replaces one protocol instance with another. Should be done before the stack is connected
      Parameters:
      existing_prot -
      new_prot -
      Throws:
      Exception
    • createProtocol

      protected Protocol createProtocol(String classname) throws Exception
      Throws:
      Exception
    • 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
    • initProtocolStack

      public void initProtocolStack(List<ProtocolConfiguration> configs) throws Exception
      Calls @link{Protocol.init()} in all protocols, from bottom to top
      Throws:
      Exception
    • initProtocolStack

      public void initProtocolStack(List<ProtocolConfiguration> configs, ProtocolHook afterCreationHook) throws Exception
      Throws:
      Exception
    • initComponents

      public static void initComponents(Protocol p, ProtocolConfiguration cfg) throws Exception
      Throws:
      Exception
    • destroy

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

      public void startStack() throws Exception
      Start all protocols. The Protocol.start() method is called in each protocol, from bottom to top. Each protocol can perform some initialization, e.g. create a multicast socket
      Throws:
      Exception
    • stopStack

      public void stopStack(String cluster)
      Iterates through all the protocols from top to bottom and does the following:
      1. Waits until all messages in the down queue have been flushed (ie., size is 0)
      2. Calls stop() on the protocol
    • getName

      public String getName()
      Overrides:
      getName in class Protocol
    • up

      public Object up(Event evt)
      Description copied from class: Protocol
      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().
      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
    • down

      public Object down(Event evt)
      Description copied from class: Protocol
      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().
      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
    • down

      public CompletableFuture<Object> down(Message msg, boolean async)
      Description copied from class: Protocol
      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.
      Overrides:
      down in class Protocol
      Parameters:
      msg - The message to be sent
      async - Whether to send the message asynchronously
      Returns:
      A CompletableFuture of the result (or exception)
    • callAfterCreationHook

      protected static void callAfterCreationHook(Protocol prot, String classname) throws Exception
      Throws:
      Exception