Class 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

    Author:
    Bela Ban
    • Constructor Detail

      • ProtocolStack

        public ProtocolStack​(JChannel channel)
                      throws java.lang.Exception
        Throws:
        java.lang.Exception
      • ProtocolStack

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

      • getChannel

        public JChannel getChannel()
      • getProtocols

        public java.util.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 java.util.Map<java.lang.String,​java.util.Map<java.lang.String,​java.lang.Object>> dumpStats()
      • dumpStats

        public java.util.Map<java.lang.String,​java.util.Map<java.lang.String,​java.lang.Object>> dumpStats​(java.lang.String protocol_name,
                                                                                                                      java.util.List<java.lang.String> attrs)
      • printProtocolSpec

        public java.lang.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 java.lang.String printProtocolSpecAsXML()
      • printProtocolSpecAsPlainString

        public java.lang.String printProtocolSpecAsPlainString()
      • setup

        public void setup​(java.util.List<ProtocolConfiguration> configs)
                   throws java.lang.Exception
        Throws:
        java.lang.Exception
      • setup

        public void setup​(java.util.List<ProtocolConfiguration> configs,
                          ProtocolHook afterCreationHook)
                   throws java.lang.Exception
        Throws:
        java.lang.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​(java.util.List<Protocol> prots)
        Adds a list of protocols
        Parameters:
        prots -
        Returns:
        Since:
        2.1
      • insertProtocol

        public void insertProtocol​(Protocol prot,
                                   ProtocolStack.Position position,
                                   java.lang.String neighbor_prot)
                            throws java.lang.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:
        java.lang.Exception - Will be thrown when the new protocol cannot be created, or inserted.
      • insertProtocol

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

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

        public void insertProtocolAtTop​(Protocol prot)
      • removeProtocol

        public <T extends Protocol> T removeProtocol​(java.lang.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.
        Throws:
        java.lang.Exception - Thrown if the protocol cannot be stopped correctly.
      • removeProtocols

        public ProtocolStack removeProtocols​(java.lang.String... protocols)
      • removeProtocols

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

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

        public <T extends Protocol> T removeProtocol​(java.lang.Class<? extends Protocol> prot)
      • removeProtocol

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

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

        public <T extends Protocol> java.util.List<T> findProtocols​(java.lang.String regexp)
      • getBottomProtocol

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

        public Protocol getTopProtocol()
      • findProtocol

        public <T extends Protocol> T findProtocol​(java.lang.Class<? extends Protocol> clazz)
      • findProtocol

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

        @SafeVarargs
        public final <T extends Protocol> T findProtocol​(java.lang.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 java.lang.Exception
        Replaces one protocol instance with another. Should be done before the stack is connected
        Parameters:
        existing_prot -
        new_prot -
        Throws:
        java.lang.Exception
      • createProtocol

        protected Protocol createProtocol​(java.lang.String classname)
                                   throws java.lang.Exception
        Throws:
        java.lang.Exception
      • init

        public void init()
                  throws java.lang.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:
        java.lang.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​(java.util.List<ProtocolConfiguration> configs)
                               throws java.lang.Exception
        Calls @link{Protocol.init()} in all protocols, from bottom to top
        Throws:
        java.lang.Exception
      • initProtocolStack

        public void initProtocolStack​(java.util.List<ProtocolConfiguration> configs,
                                      ProtocolHook afterCreationHook)
                               throws java.lang.Exception
        Throws:
        java.lang.Exception
      • initComponents

        public static void initComponents​(Protocol p,
                                          ProtocolConfiguration cfg)
                                   throws java.lang.Exception
        Throws:
        java.lang.Exception
      • startStack

        public void startStack()
                        throws java.lang.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:
        java.lang.Exception
      • stopStack

        public void stopStack​(java.lang.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 java.lang.String getName()
        Overrides:
        getName in class Protocol
      • up

        public java.lang.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 java.lang.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 java.lang.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 java.lang.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 java.util.concurrent.CompletableFuture<java.lang.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,
                                                    java.lang.String classname)
                                             throws java.lang.Exception
        Throws:
        java.lang.Exception