Package org.jgroups.stack
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
,FORK
,ForkProtocol
,Fragmentation
,GMS
,HDRS
,INJECT_VIEW
,KeyExchange
,MAKE_BATCH
,MERGE3
,MessageDispatcher.ProtocolAdapter
,NAKACK2
,NON_BLOCKING_SENDS
,PDC
,PERF
,ProtocolStack
,RATE
,RATE_LIMITER
,RATE_LIMITER2
,RED
,RELAY
,ReliableMulticast
,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 java.lang.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 justup()
,down()
andgetName()
. 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 !
- Author:
- Bela Ban
-
-
Field Summary
Fields Modifier and Type Field Description protected java.lang.String
after_creation_hook
protected Protocol
down_prot
protected boolean
ergonomics
protected short
id
protected Address
local_addr
protected Log
log
protected java.util.List<Policy>
policies
protected ProtocolStack
stack
protected boolean
stats
protected Protocol
up_prot
-
Constructor Summary
Constructors Constructor Description Protocol()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected boolean
accept(Message msg)
Called by the default implementation ofup(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.<T extends Protocol>
TaddPolicy(Policy p)
Address
addr()
<T extends Protocol>
Taddr(Address addr)
java.lang.String
afterCreationHook()
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.void
enableStats(boolean flag)
Address
getAddress()
java.util.List<java.lang.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.<T extends Protocol>
TgetDownProtocol()
java.util.List<java.lang.Integer>
getDownServices()
Returns all services provided by protocols below the current protocolshort
getId()
short[]
getIdsAbove()
Returns the protocol IDs of all protocols above this one (excluding the current protocol)java.lang.String
getLevel()
Log
getLog()
java.lang.String
getName()
java.util.List<? extends Policy>
getPolicies()
ProtocolStack
getProtocolStack()
SocketFactory
getSocketFactory()
Returns the SocketFactory associated with this protocol, if overridden in a subclass, or passes the call downThreadFactory
getThreadFactory()
Supposed to be overwritten by subclasses.TP
getTransport()
<T extends Protocol>
TgetUpProtocol()
java.util.List<java.lang.Integer>
getUpServices()
Returns all services provided by the protocols above the current protocoljava.lang.Object
getValue(java.lang.String name)
void
init()
Called after a protocol has been created and before the protocol is started.boolean
isErgonomics()
<T extends Protocol>
Tlevel(java.lang.String level)
void
parse(XmlNode node)
Called by the XML parser when subelements are found in the configuration of a protocol.java.lang.String
policies()
java.util.List<java.lang.Integer>
providedDownServices()
List of events that are provided to layers below (they will be handled when sent from down below)java.util.List<java.lang.Integer>
providedUpServices()
List of events that are provided to layers above (they will be handled when sent down from above)<T extends Protocol>
TremovePolicy(Policy p)
java.util.List<java.lang.Integer>
requiredDownServices()
List of events that are required to be answered by some layer belowjava.util.List<java.lang.Integer>
requiredUpServices()
List of events that are required to be answered by some layer abovevoid
resetStatistics()
void
resetStats()
<T extends Protocol>
TsetAddress(Address addr)
<T extends Protocol>
TsetDownProtocol(Protocol prot)
<T extends Protocol>
TsetErgonomics(boolean ergonomics)
<T extends Protocol>
TsetId(short id)
<T extends Protocol>
TsetLevel(java.lang.String level)
Sets the level of a logger.<T extends Protocol>
TsetPolicies(java.util.List<Policy> l)
<T extends Protocol>
TsetProtocolStack(ProtocolStack s)
void
setSocketFactory(SocketFactory factory)
Sets a SocketFactory.<T extends Protocol>
TsetUpProtocol(Protocol prot)
<T extends Protocol>
TsetValue(java.lang.String name, java.lang.Object value)
void
start()
This method is called on aJChannel.connect(String)
; starts work.boolean
statsEnabled()
void
stop()
Called on aJChannel.disconnect()
; stops work (e.g.java.lang.String
toString()
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
.
-
-
-
Field Detail
-
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 java.lang.String after_creation_hook
-
id
protected short id
-
local_addr
protected Address local_addr
-
log
protected final Log log
-
policies
protected java.util.List<Policy> policies
-
-
Method Detail
-
setLevel
public <T extends Protocol> T setLevel(java.lang.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 java.lang.String getLevel()
-
level
public <T extends Protocol> T level(java.lang.String level)
-
getAddress
public Address getAddress()
-
addr
public 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 java.lang.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()
-
setProtocolStack
public <T extends Protocol> T setProtocolStack(ProtocolStack s)
-
afterCreationHook
public java.lang.String afterCreationHook()
-
getLog
public Log getLog()
-
getPolicies
public java.util.List<? extends Policy> getPolicies()
-
policies
public java.lang.String policies()
-
getValue
public java.lang.Object getValue(java.lang.String name)
-
setValue
public <T extends Protocol> T setValue(java.lang.String name, java.lang.Object value)
-
getComponents
public java.util.List<java.lang.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 java.lang.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:
java.lang.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 java.lang.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.
-
start
public void start() throws java.lang.Exception
This method is called on aJChannel.connect(String)
; starts work. Protocols are connected ready to receive events. Will be called from bottom to top.- Specified by:
start
in interfaceLifecycle
- Throws:
java.lang.Exception
- Thrown if protocol cannot be started successfully. This will cause the ProtocolStack to fail, soJChannel.connect(String)
will throw an exception
-
stop
public void stop()
Called on aJChannel.disconnect()
; stops work (e.g. by closing multicast socket). Will be called from top to bottom.
-
destroy
public void destroy()
This method is called on aJChannel.close()
. Does some cleanup; after the call, the VM will terminate
-
requiredUpServices
public java.util.List<java.lang.Integer> requiredUpServices()
List of events that are required to be answered by some layer above
-
requiredDownServices
public java.util.List<java.lang.Integer> requiredDownServices()
List of events that are required to be answered by some layer below
-
providedUpServices
public java.util.List<java.lang.Integer> providedUpServices()
List of events that are provided to layers above (they will be handled when sent down from above)
-
providedDownServices
public java.util.List<java.lang.Integer> providedDownServices()
List of events that are provided to layers below (they will be handled when sent from down below)
-
getDownServices
public final java.util.List<java.lang.Integer> getDownServices()
Returns all services provided by protocols below the current protocol
-
getUpServices
public final java.util.List<java.lang.Integer> getUpServices()
Returns all services provided by the protocols above the current protocol
-
down
public java.lang.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 usingdown_prot.down()
.
-
down
public java.lang.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 java.util.concurrent.CompletableFuture<java.lang.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 asMessage.TransientFlag.DONT_BLOCK
, then it will be dropped, otherwise it will be sent on the caller's thread.- Parameters:
msg
- The message to be sentasync
- Whether to send the message asynchronously- Returns:
- A CompletableFuture of the result (or exception)
-
up
public java.lang.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 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)
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 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 (callingaccept(Message)
), and - if true - callsup(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.- Parameters:
batch
- The message batch
-
toString
public java.lang.String toString()
- Overrides:
toString
in classjava.lang.Object
-
accept
protected boolean accept(Message msg)
Called by the default implementation ofup(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
-
-