org.jgroups.stack

## Class ProtocolStack

- java.lang.Object
- - org.jgroups.stack.Protocol
  - - org.jgroups.stack.ProtocolStack

All Implemented Interfaces:
  Transport

    public class ProtocolStack
    extends Protocol
    implements Transport

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

### Nested Class Summary

Nested Classes
`static class` `ProtocolStack.RefCounter` Keeps track of the number os times init()/destroy() and start()/stop have been called.

### Field Summary

Fields
`static int` `ABOVE`
`static int` `BELOW`

### Fields inherited from class org.jgroups.stack.Protocol

`down_prot, ergonomics, id, log, name, stack, stats, up_prot`

### Constructor Summary

Constructors
Constructor and Description
`ProtocolStack()` Used for programmatic creation of ProtocolStack
`ProtocolStack(JChannel channel)`

### Method Summary

Methods
`ProtocolStack` `addProtocol(Protocol prot)` Adds a protocol at the tail of the protocol list
`ProtocolStack` `addProtocols(java.util.List<Protocol> prots)` Adds a list of protocols
`ProtocolStack` `addProtocols(Protocol... prots)` Adds a list of protocols
`java.util.List<Protocol>` `copyProtocols(ProtocolStack targetStack)`
`protected Protocol` `createProtocol(java.lang.String classname)`
`void` `destroy()` This method is called on a `Channel.close()`.
`java.lang.Object` `down(Event evt)` An event is to be sent down the stack.
`java.util.Map<java.lang.String,java.lang.Object>` `dumpStats()`
`java.util.Map<java.lang.String,java.lang.Object>` `dumpStats(java.lang.String protocol_name)`
`java.util.Map<java.lang.String,java.lang.Object>` `dumpStats(java.lang.String protocol_name, java.util.List<java.lang.String> attrs)`
`java.lang.String` `dumpTimerQueue()` Deprecated. Use `TP.getTimer()` instead to fetch the timer from the transport and then invoke the method on it
`Protocol` `findProtocol(java.lang.Class<?>... classes)` Finds the first protocol of a list and returns it.
`Protocol` `findProtocol(java.lang.Class<?> clazz)`
`Protocol` `findProtocol(java.lang.String name)` Returns a given protocol or null if not found
`void` `flushEvents()` Deprecated.
`Protocol` `getBottomProtocol()`
`JChannel` `getChannel()`
`java.lang.String` `getName()` All protocol names have to be unique !
`java.util.List<Protocol>` `getProtocols()` Returns all protocols in a list, from top to bottom.
`static java.util.concurrent.ConcurrentMap<java.lang.String,Tuple<TP,ProtocolStack.RefCounter>>` `getSingletonTransports()`
`ThreadFactory` `getThreadFactory()` Deprecated. Use `Protocol.getThreadFactory()` instead
`static ThreadFactory` `getTimerThreadFactory()` Deprecated.
`int` `getTimerThreads()` Deprecated. Use `TP.getTimer()` to fetch the timer and call getCorePoolSize() directly
`Protocol` `getTopProtocol()`
`TP` `getTransport()` Returns the bottom most protocol
`void` `init()` Called after instance has been created (null constructor) and before protocol is started.
`void` `initProtocolStack()`
`void` `insertProtocol(Protocol prot, int position, java.lang.Class<? extends Protocol>... neighbor_prots)`
`void` `insertProtocol(Protocol prot, int position, java.lang.Class<? extends Protocol> neighbor_prot)`
`void` `insertProtocol(Protocol prot, int position, java.lang.String neighbor_prot)` Inserts an already created (and initialized) protocol into the protocol list.
`void` `insertProtocolAtTop(Protocol prot)`
`void` `insertProtocolInStack(Protocol prot, Protocol neighbor, int position)`
`java.lang.String` `printProtocolSpec(boolean include_properties)` Prints the names of the protocols, from the bottom to top.
`java.lang.String` `printProtocolSpecAsPlainString()`
`java.lang.String` `printProtocolSpecAsXML()`
`java.lang.Object` `receive(long timeout)`
`Protocol` `removeProtocol(java.lang.Class... protocols)`
`Protocol` `removeProtocol(java.lang.Class prot)`
`Protocol` `removeProtocol(java.lang.String prot_name)` Removes a protocol from the stack.
`void` `send(Message msg)`
`void` `setChannel(JChannel ch)`
`void` `setThreadFactory(ThreadFactory f)` Deprecated. Use `Protocol.getThreadFactory()` instead
`static void` `setTimerThreadFactory(ThreadFactory f)` Deprecated. Use `TP.setTimerThreadFactory(org.jgroups.util.ThreadFactory)` instead
`void` `setup(java.util.List<ProtocolConfiguration> configs)`
`void` `setup(ProtocolStack stack)`
`void` `startStack(java.lang.String cluster_name, Address local_addr)` Start all layers.
`void` `stopStack(java.lang.String cluster_name)` Iterates through all the protocols from top to bottom and does the following: Waits until all messages in the down queue have been flushed (ie., size is 0) Calls stop() on the protocol
`java.lang.Object` `up(Event evt)` An event was received from the layer below.

### Methods inherited from class org.jgroups.stack.Protocol

`downThreadEnabled, enableStats, getConfigurableObjects, getDownProtocol, getId, getLevel, getProperties, getProtocolStack, getSocketFactory, getUpProtocol, getValue, isErgonomics, printStats, providedDownServices, providedUpServices, requiredDownServices, requiredUpServices, resetStats, setDownProtocol, setErgonomics, setId, setLevel, setProperties, setPropertiesInternal, setProperty, setProtocolStack, setSocketFactory, setUpProtocol, setValue, setValues, start, statsEnabled, stop, upThreadEnabled`

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

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

### Field Detail

#### ABOVE

    public static final int ABOVE

See Also:
  Constant Field Values

#### BELOW

    public static final int BELOW

See Also:
  Constant Field Values

### Constructor Detail

#### ProtocolStack

    public ProtocolStack(JChannel channel)
                  throws ChannelException

Throws:
  `ChannelException`

#### ProtocolStack

    public ProtocolStack()

Used for programmatic creation of ProtocolStack

### Method Detail

#### getChannel

    public JChannel getChannel()

#### setChannel

    public void setChannel(JChannel ch)

#### getThreadFactory

    public ThreadFactory getThreadFactory()

Deprecated. Use `Protocol.getThreadFactory()` instead
Description copied from class: `Protocol`
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

Overrides:
  `getThreadFactory` in class `Protocol`
Returns:

#### getTimerThreadFactory

    @Deprecated
    public static ThreadFactory getTimerThreadFactory()

Deprecated.

#### setThreadFactory

    public void setThreadFactory(ThreadFactory f)

Deprecated. Use `Protocol.getThreadFactory()` instead

Parameters:
  `f` -

#### setTimerThreadFactory

    public static void setTimerThreadFactory(ThreadFactory f)

Deprecated. Use `TP.setTimerThreadFactory(org.jgroups.util.ThreadFactory)` instead

Parameters:
  `f` -

#### getTimerThreads

    public int getTimerThreads()

Deprecated. Use `TP.getTimer()` to fetch the timer and call getCorePoolSize() directly

Returns:

#### 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 !

#### copyProtocols

    public java.util.List<Protocol> copyProtocols(ProtocolStack targetStack)
                                           throws java.lang.IllegalAccessException,
                                                  java.lang.InstantiationException

Throws:
  `java.lang.IllegalAccessException`
  `java.lang.InstantiationException`

#### getTransport

    public TP getTransport()

Returns the bottom most protocol

Overrides:
  `getTransport` in class `Protocol`

#### getSingletonTransports

    public static java.util.concurrent.ConcurrentMap<java.lang.String,Tuple<TP,ProtocolStack.RefCounter>> getSingletonTransports()

#### dumpStats

    public java.util.Map<java.lang.String,java.lang.Object> dumpStats()

Overrides:
  `dumpStats` in class `Protocol`
Returns:
  Map<String,Map<key,val>>

#### dumpStats

    public java.util.Map<java.lang.String,java.lang.Object> dumpStats(java.lang.String protocol_name)

#### dumpStats

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

#### dumpTimerQueue

    public java.lang.String dumpTimerQueue()

Deprecated. Use `TP.getTimer()` instead to fetch the timer from the transport and then invoke the method on it

Returns:

#### 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(ProtocolStack stack)
               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,
                      int 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.

#### insertProtocolInStack

    public void insertProtocolInStack(Protocol prot,
                             Protocol neighbor,
                             int position)

#### insertProtocol

    public void insertProtocol(Protocol prot,
                      int position,
                      java.lang.Class<? extends Protocol> neighbor_prot)
                        throws java.lang.Exception

Throws:
  `java.lang.Exception`

#### insertProtocol

    public void insertProtocol(Protocol prot,
                      int position,
                      java.lang.Class<? extends Protocol>... neighbor_prots)
                        throws java.lang.Exception

Throws:
  `java.lang.Exception`

#### insertProtocolAtTop

    public void insertProtocolAtTop(Protocol prot)

#### removeProtocol

    public Protocol removeProtocol(java.lang.String prot_name)
                            throws java.lang.Exception

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.

#### removeProtocol

    public Protocol removeProtocol(java.lang.Class... protocols)

#### removeProtocol

    public Protocol removeProtocol(java.lang.Class prot)

#### findProtocol

    public Protocol findProtocol(java.lang.String name)

Returns a given protocol or null if not found

#### getBottomProtocol

    public Protocol getBottomProtocol()

#### getTopProtocol

    public Protocol getTopProtocol()

#### findProtocol

    public Protocol findProtocol(java.lang.Class<?> clazz)

#### findProtocol

    public Protocol findProtocol(java.lang.Class<?>... 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

#### 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 instance has been created (null constructor) and before protocol is started. Properties are already set. Other protocols are not yet connected and events cannot yet be sent.

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 channel constructor will throw an exception

#### initProtocolStack

    public void initProtocolStack()
                           throws java.lang.Exception

Throws:
  `java.lang.Exception`

#### destroy

    public void destroy()

Description copied from class: `Protocol`
This method is called on a `Channel.close()`. Does some cleanup; after the call the VM will terminate

Overrides:
  `destroy` in class `Protocol`

#### startStack

    public void startStack(java.lang.String cluster_name,
                  Address local_addr)
                    throws java.lang.Exception

Start all layers. The `Protocol.start()` method is called in each protocol, from top to bottom. Each layer can perform some initialization, e.g. create a multicast socket

Throws:
  `java.lang.Exception`

#### stopStack

    public void stopStack(java.lang.String cluster_name)

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

#### flushEvents

    public void flushEvents()

Deprecated.
Not needed anymore, just left in here for backwards compatibility with JBoss AS

#### send

    public void send(Message msg)
              throws java.lang.Exception

Specified by:
  `send` in interface `Transport`
Throws:
  `java.lang.Exception`

#### receive

    public java.lang.Object receive(long timeout)
                             throws java.lang.Exception

Specified by:
  `receive` in interface `Transport`
Throws:
  `java.lang.Exception`

#### getName

    public java.lang.String getName()

Description copied from class: `Protocol`
All protocol names have to be unique !

Overrides:
  `getName` in class `Protocol`

#### up

    public java.lang.Object up(Event evt)

Description copied from class: `Protocol`
An event was received from the layer below. Usually the current layer 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`

#### down

    public java.lang.Object down(Event evt)

Description copied from class: `Protocol`
An event is to be sent down the stack. The layer 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 layer may need to add a header to it (or do nothing at all) before sending it down the stack using `down_prot.down()`. In case of a GET_ADDRESS event (which tries to retrieve the stack's address from one of the bottom layers), the layer may need to send a new response event back up the stack using `up_prot.up()`.

Overrides:
  `down` in class `Protocol`