quickfix.mina.AbstractIoHandler Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of quickfixj-core Show documentation
Show all versions of quickfixj-core Show documentation
The core QuickFIX/J engine
/*******************************************************************************
* Copyright (c) quickfixengine.org All rights reserved.
*
* This file is part of the QuickFIX FIX Engine
*
* This file may be distributed under the terms of the quickfixengine.org
* license as defined by quickfixengine.org and appearing in the file
* LICENSE included in the packaging of this file.
*
* This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING
* THE WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A
* PARTICULAR PURPOSE.
*
* See http://www.quickfixengine.org/LICENSE for licensing information.
*
* Contact [email protected] if any conditions of this licensing
* are not clear to you.
******************************************************************************/
package quickfix.mina;
import static quickfix.MessageUtils.parse;
import java.io.IOException;
import org.apache.mina.core.service.IoHandlerAdapter;
import org.apache.mina.core.session.IoSession;
import org.apache.mina.filter.codec.ProtocolCodecException;
import org.apache.mina.filter.codec.ProtocolDecoderException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import quickfix.InvalidMessage;
import quickfix.Message;
import quickfix.MessageUtils;
import quickfix.Session;
import quickfix.SessionID;
import quickfix.field.MsgType;
/**
* Abstract class used for acceptor and initiator IO handlers.
*/
public abstract class AbstractIoHandler extends IoHandlerAdapter {
protected final Logger log = LoggerFactory.getLogger(getClass());
private final NetworkingOptions networkingOptions;
private final EventHandlingStrategy eventHandlingStrategy;
public AbstractIoHandler(NetworkingOptions options, EventHandlingStrategy eventHandlingStrategy) {
networkingOptions = options;
this.eventHandlingStrategy = eventHandlingStrategy;
}
@Override
public void exceptionCaught(IoSession ioSession, Throwable cause) throws Exception {
boolean disconnectNeeded = false;
Session quickFixSession = findQFSession(ioSession);
Throwable realCause = cause;
if (cause instanceof ProtocolDecoderException && cause.getCause() != null) {
realCause = cause.getCause();
}
String reason;
if (realCause instanceof IOException) {
if (quickFixSession != null && quickFixSession.isEnabled()) {
reason = "Socket exception (" + ioSession.getRemoteAddress() + "): " + cause;
} else {
reason = "Socket (" + ioSession.getRemoteAddress() + "): " + cause;
}
disconnectNeeded = true;
} else if (realCause instanceof CriticalProtocolCodecException) {
reason = "Critical protocol codec error: " + cause;
disconnectNeeded = true;
} else if (realCause instanceof ProtocolCodecException) {
reason = "Protocol handler exception: " + cause;
} else {
reason = cause.toString();
}
if (disconnectNeeded) {
if (quickFixSession != null) {
quickFixSession.disconnect(reason, true);
} else {
log.error(reason, cause);
ioSession.close(true);
}
} else {
log.error(reason, cause);
}
}
@Override
public void sessionCreated(IoSession ioSession) throws Exception {
super.sessionCreated(ioSession);
networkingOptions.apply(ioSession);
}
@Override
public void sessionClosed(IoSession ioSession) throws Exception {
try {
Session quickFixSession = findQFSession(ioSession);
if (quickFixSession != null) {
eventHandlingStrategy.onMessage(quickFixSession, EventHandlingStrategy.END_OF_STREAM );
ioSession.removeAttribute(SessionConnector.QF_SESSION);
}
ioSession.close(true);
} catch (Exception e) {
ioSession.close(true);
throw e;
}
}
@Override
public void messageReceived(IoSession ioSession, Object message) throws Exception {
String messageString = (String) message;
SessionID remoteSessionID = MessageUtils.getReverseSessionID(messageString);
Session quickFixSession = findQFSession(ioSession, remoteSessionID);
if (quickFixSession != null) {
quickFixSession.getLog().onIncoming(messageString);
try {
Message fixMessage = parse(quickFixSession, messageString);
processMessage(ioSession, fixMessage);
} catch (InvalidMessage e) {
if (MsgType.LOGON.equals(MessageUtils.getMessageType(messageString))) {
log.error("Invalid LOGON message, disconnecting: " + e.getMessage());
ioSession.close(true);
} else {
log.error("Invalid message: " + e.getMessage());
}
}
} else {
log.error("Disconnecting; received message for unknown session: " + messageString);
ioSession.close(true);
}
}
protected Session findQFSession(IoSession ioSession, SessionID sessionID) {
Session quickfixSession = findQFSession(ioSession);
if (quickfixSession == null) {
quickfixSession = Session.lookupSession(sessionID);
}
return quickfixSession;
}
private Session findQFSession(IoSession ioSession) {
return (Session) ioSession.getAttribute(SessionConnector.QF_SESSION);
}
protected NetworkingOptions getNetworkingOptions() {
return networkingOptions;
}
protected abstract void processMessage(IoSession ioSession, Message message) throws Exception;
}