Package org.jgroups.stack
Class Protocol
- java.lang.Object
-
- org.jgroups.stack.Protocol
-
- Direct Known Subclasses:
ABP
,AUTH
,BARRIER
,CLEAR_FLAGS
,COMPRESS
,COUNTER
,DAISYCHAIN
,DELAY
,DELIVERY_TIME
,DETECT_LOOPBACKS
,DISCARD
,DISCARD_PAYLOAD
,Discovery
,DROP
,DUPL
,Encrypt
,EXAMPLE
,Executing
,FailureDetection
,FD
,FD_HOST
,FD_SOCK
,FlowControl
,FLUSH
,FORK
,ForkProtocol
,FORWARD_TO_COORD
,FRAG
,FRAG2
,FRAG3
,GMS
,HDRS
,INJECT_VIEW
,KeyExchange
,Locking
,MAKE_BATCH
,MERGE3
,NAKACK2
,NAMING
,PDC
,PERF
,ProtocolStack
,RATE
,RATE_LIMITER
,RED
,RELAY
,RELAY2
,REVERSE
,RSVP
,SASL
,SEQUENCER
,SEQUENCER2
,SERIALIZE
,SHUFFLE
,SIZE
,SNIFF
,SOS
,STABLE
,STATE_TRANSFER
,STATS
,STOMP
,StreamingStateTransfer
,SUPERVISOR
,TOA
,TP
,TRACE
,UNICAST3
,VERIFY_SUSPECT
public abstract class Protocol extends java.lang.Object
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 Log
log
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.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.void
enableStats(boolean flag)
java.util.List<java.lang.Object>
getConfigurableObjects()
After configuring the protocol itself from the properties defined in the XML config, a protocol might have additional 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()
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.protected 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 instance has been created (null constructor) and before 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.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)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>
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>
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)
.boolean
statsEnabled()
void
stop()
This method is called on aJChannel.disconnect()
.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
-
log
protected final Log log
-
-
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)
-
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()
-
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)
-
getConfigurableObjects
public java.util.List<java.lang.Object> getConfigurableObjects()
After configuring the protocol itself from the properties defined in the XML config, a protocol might have additional 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
protected 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 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.- 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
-
start
public void start() throws java.lang.Exception
This method is called on aJChannel.connect(String)
. Starts work. Protocols are connected and queues are ready to receive events. Will be called from bottom to top. This call will replace the START and START_OK events.- 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()
This method is called on aJChannel.disconnect()
. Stops work (e.g. by closing multicast socket). Will be called from top to bottom. This means that at the time of the method invocation the neighbor protocol below is still working. This method will replace the STOP, STOP_OK, CLEANUP and CLEANUP_OK events. The ProtocolStack guarantees that when this method is called all messages in the down queue will have been flushed
-
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
-
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(org.jgroups.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.getMatchingMessages(short,boolean)
), 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
-
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
-
-