bboss.org.jgroups.blocks.mux.MuxUpHandler Maven / Gradle / Ivy
The newest version!
package bboss.org.jgroups.blocks.mux;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicReference;
import bboss.org.jgroups.Event;
import bboss.org.jgroups.Message;
import bboss.org.jgroups.UpHandler;
import bboss.org.jgroups.logging.Log;
import bboss.org.jgroups.logging.LogFactory;
/**
* Allows up handler multiplexing.
*
* @author Bela Ban
* @author Paul Ferraro
* @version $Id: MuxUpHandler.java,v 1.3 2010/06/09 03:24:51 bstansberry Exp $
*/
public class MuxUpHandler implements UpHandler, Muxer {
protected final Log log=LogFactory.getLog(getClass());
private final Map handlers = new ConcurrentHashMap();
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 bboss.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 bboss.org.jgroups.blocks.mux.Muxer#get(short)
*/
// @Override
public UpHandler get(short id) {
return handlers.get(id);
}
/**
* {@inheritDoc}
* @see bboss.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 bboss.org.jgroups.UpHandler#up(bboss.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: {
AtomicReference