org.jgroups.blocks.mux.MuxUpHandler Maven / Gradle / Ivy
package org.jgroups.blocks.mux;
import org.jgroups.Event;
import org.jgroups.Message;
import org.jgroups.UpHandler;
import org.jgroups.logging.Log;
import org.jgroups.logging.LogFactory;
import org.jgroups.util.ImmutableReference;
import org.jgroups.util.Util;
import java.util.Map;
/**
* Allows up handler multiplexing.
*
* @author Bela Ban
* @author Paul Ferraro
*/
public class MuxUpHandler implements UpHandler, Muxer {
protected final Log log=LogFactory.getLog(getClass());
private final Map handlers=Util.createConcurrentMap();
private volatile UpHandler defaultHandler;
private volatile Event lastFlushEvent;
private final Object flushMutex = new Object();
/**
* Creates a multiplexing up handler, with no default handler.
*/
public MuxUpHandler() {
this.defaultHandler = null;
}
/**
* Creates a multiplexing up handler using the specified default handler.
* @param defaultHandler a default up handler to handle messages with no {@link MuxHeader}
*/
public MuxUpHandler(UpHandler defaultHandler) {
this.defaultHandler = defaultHandler;
}
/**
* {@inheritDoc}
* @see org.jgroups.blocks.mux.Muxer#add(short, java.lang.Object)
*/
@Override
public void add(short id, UpHandler handler) {
synchronized (flushMutex)
{
if (lastFlushEvent != null)
{
handler.up(lastFlushEvent);
}
handlers.put(id, handler);
}
}
/**
* {@inheritDoc}
* @see org.jgroups.blocks.mux.Muxer#get(short)
*/
@Override
public UpHandler get(short id) {
return handlers.get(id);
}
/**
* {@inheritDoc}
* @see org.jgroups.blocks.mux.Muxer#remove(short)
*/
@Override
public void remove(short id) {
handlers.remove(id);
}
@Override
public UpHandler getDefaultHandler() {
return defaultHandler;
}
@Override
public void setDefaultHandler(UpHandler handler) {
this.defaultHandler = handler;
}
/**
* {@inheritDoc}
* @see org.jgroups.UpHandler#up(org.jgroups.Event)
*/
@Override
public Object up(Event evt) {
switch (evt.getType()) {
case Event.MSG:
Message msg = (Message) evt.getArg();
MuxHeader hdr = (MuxHeader) msg.getHeader(MuxRequestCorrelator.MUX_ID);
if (hdr != null) {
short id = hdr.getId();
UpHandler handler = handlers.get(id);
return (handler != null) ? handler.up(evt) : new NoMuxHandler(id);
}
break;
case Event.GET_APPLSTATE:
case Event.GET_STATE_OK:
case Event.STATE_TRANSFER_OUTPUTSTREAM:
case Event.STATE_TRANSFER_INPUTSTREAM:
ImmutableReference
© 2015 - 2025 Weber Informatics LLC | Privacy Policy