Package org.jgroups.stack
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
- Author:
- Bela Ban
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
ProtocolStack.Position
-
Field Summary
Fields Modifier and Type Field Description protected Protocol
bottom_prot
protected JChannel
channel
protected static java.lang.String
max_list_print_size
protected DiagnosticsHandler.ProbeHandler
props_handler
protected boolean
stopped
protected Protocol
top_prot
-
Fields inherited from class org.jgroups.stack.Protocol
after_creation_hook, down_prot, ergonomics, id, local_addr, log, policies, stack, stats, up_prot
-
-
Constructor Summary
Constructors Constructor Description ProtocolStack()
Used for programmatic creation of ProtocolStackProtocolStack(JChannel channel)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description ProtocolStack
addProtocol(Protocol prot)
Adds a protocol at the tail of the protocol listProtocolStack
addProtocols(java.util.List<Protocol> prots)
Adds a list of protocolsProtocolStack
addProtocols(Protocol... prots)
Adds a list of protocolsProtocolStack
bottomProtocol(Protocol bottom)
protected static void
callAfterCreationHook(Protocol prot, java.lang.String classname)
protected Protocol
createProtocol(java.lang.String classname)
void
destroy()
This method is called on aJChannel.close()
.java.lang.Object
down(Event evt)
An event is to be sent down the stack.java.lang.Object
down(Message msg)
A message is sent down the stack.java.util.concurrent.CompletableFuture<java.lang.Object>
down(Message msg, boolean async)
Passes a message down asynchronously.java.util.Map<java.lang.String,java.util.Map<java.lang.String,java.lang.Object>>
dumpStats()
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)
<T extends Protocol>
TfindProtocol(java.lang.Class<? extends Protocol> clazz)
<T extends Protocol>
TfindProtocol(java.lang.Class<? extends Protocol>... classes)
Finds the first protocol of a list and returns it.<T extends Protocol>
TfindProtocol(java.lang.String name)
Returns a given protocol or null if not foundstatic <T extends Protocol>
TfindProtocol(Protocol start, boolean down, java.lang.Class<? extends Protocol> clazz)
<T extends Protocol>
java.util.List<T>findProtocols(java.lang.String regexp)
<T extends Protocol>
TgetBottomProtocol()
JChannel
getChannel()
java.lang.String
getName()
java.util.List<Protocol>
getProtocols()
Returns all protocols in a list, from top to bottom.Protocol
getTopProtocol()
TP
getTransport()
Returns the bottom most protocolvoid
init()
Called after a protocol has been created and before the protocol is started.static void
initComponents(Protocol p, ProtocolConfiguration cfg)
void
initProtocolStack(java.util.List<ProtocolConfiguration> configs)
Calls @link{Protocol.init()
} in all protocols, from bottom to topvoid
initProtocolStack(java.util.List<ProtocolConfiguration> configs, ProtocolHook afterCreationHook)
void
insertProtocol(Protocol prot, ProtocolStack.Position position, java.lang.Class<? extends Protocol> neighbor_prot)
void
insertProtocol(Protocol prot, ProtocolStack.Position position, java.lang.Class<? extends Protocol>... neighbor_prots)
void
insertProtocol(Protocol prot, ProtocolStack.Position 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, ProtocolStack.Position 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()
<T extends Protocol>
TremoveProtocol(java.lang.Class<? extends Protocol> prot)
<T extends Protocol>
TremoveProtocol(java.lang.Class<? extends Protocol>... protocols)
<T extends Protocol>
TremoveProtocol(java.lang.String prot_name)
Removes a protocol from the stack.<T extends Protocol>
TremoveProtocol(T prot)
ProtocolStack
removeProtocols(java.lang.Class<? extends Protocol>... protocols)
ProtocolStack
removeProtocols(java.lang.String... protocols)
void
replaceProtocol(Protocol existing_prot, Protocol new_prot)
Replaces one protocol instance with another.ProtocolStack
setChannel(JChannel ch)
void
setup(java.util.List<ProtocolConfiguration> configs)
void
setup(java.util.List<ProtocolConfiguration> configs, ProtocolHook afterCreationHook)
void
startStack()
Start all protocols.void
stopStack(java.lang.String cluster)
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 protocolProtocolStack
topProtocol(Protocol top)
java.lang.Object
up(Event evt)
An event was received from the protocol below.java.lang.Object
up(Message msg)
A single message was received.void
up(MessageBatch batch)
Sends up a multiple messages in aMessageBatch
.-
Methods inherited from class org.jgroups.stack.Protocol
accept, addPolicy, addr, addr, afterCreationHook, enableStats, getAddress, getComponents, getDownProtocol, getDownServices, getId, getIdsAbove, getLevel, getLog, getPolicies, getProtocolStack, getSocketFactory, getThreadFactory, getUpProtocol, getUpServices, getValue, isErgonomics, level, parse, policies, providedDownServices, providedUpServices, removePolicy, requiredDownServices, requiredUpServices, resetStatistics, resetStats, setAddress, setDownProtocol, setErgonomics, setId, setLevel, setPolicies, setProtocolStack, setSocketFactory, setUpProtocol, setValue, start, statsEnabled, stop, toString
-
-
-
-
Field Detail
-
max_list_print_size
protected static final java.lang.String max_list_print_size
- See Also:
- Constant Field Values
-
top_prot
protected Protocol top_prot
-
bottom_prot
protected Protocol bottom_prot
-
channel
protected JChannel channel
-
stopped
protected volatile boolean stopped
-
props_handler
protected final DiagnosticsHandler.ProbeHandler props_handler
-
-
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
-
topProtocol
public ProtocolStack topProtocol(Protocol top)
-
bottomProtocol
public ProtocolStack bottomProtocol(Protocol bottom)
-
getChannel
public JChannel getChannel()
-
setChannel
public ProtocolStack setChannel(JChannel ch)
-
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 classProtocol
-
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.
-
insertProtocolInStack
public void insertProtocolInStack(Protocol prot, Protocol neighbor, ProtocolStack.Position position)
-
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 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.
-
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
-
destroy
public void destroy()
Description copied from class:Protocol
This method is called on aJChannel.close()
. Does some cleanup; after the call, the VM will terminate
-
startStack
public void startStack() throws java.lang.Exception
Start all protocols. TheProtocol.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:- Waits until all messages in the down queue have been flushed (ie., size is 0)
- Calls stop() on the 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 usingdown_prot.down()
or c) the event (or another event) is sent up the stack usingup_prot.up()
.
-
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.
-
up
public void up(MessageBatch batch)
Description copied from class:Protocol
Sends up a multiple messages in aMessageBatch
. 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 (callingProtocol.accept(Message)
), and - if true - callsProtocol.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. usingMessageBatch.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.
-
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 usingdown_prot.down()
.
-
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.
-
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 asMessage.TransientFlag.DONT_BLOCK
, then it will be dropped, otherwise it will be sent on the caller's thread.
-
callAfterCreationHook
protected static void callAfterCreationHook(Protocol prot, java.lang.String classname) throws java.lang.Exception
- Throws:
java.lang.Exception
-
-