import org.apache.mina.core.session.IoSession; //导入方法依赖的package包/类
@Override
public void messageReceived ( final NextFilter nextFilter, final IoSession session, final Object message ) throws Exception
{
logger.debug ( "messageReceived - message: {}, session: {}", message, session ); //$NON-NLS-1$
final boolean opened = isOpened ( session );
if ( message instanceof HelloMessage && !opened && !this.clientMode )
{
// received hello when we need it
final Object reply = createSession ( session, nextFilter, (HelloMessage)message );
logger.info ( "Reply to session creation: {}", reply ); //$NON-NLS-1$
if ( reply instanceof AcceptMessage )
{
this.acceptedProperties = Collections.unmodifiableMap ( ( (AcceptMessage)reply ).getProperties () );
// configure the session, enable filters
configureSession ( session, nextFilter, this.acceptedProperties );
/*
* send reply - this must be the first message after the configureSession call
* so that filter can reset their initial flag. The first message must still be
* un-filtered since the other side needs to adapt its filter chain first. However
* this must be an atomic operation since sending and changing the filter in two
* separate steps could mean that we already receive the reply to our ACCEPT
* before we had a chance to change our filter chain.
*/
nextFilter.filterWrite ( session, new DefaultWriteRequest ( reply ) );
// post configure the session
postConfigureSession ( session, nextFilter, this.acceptedProperties );
if ( isOpened ( session ) )
{
// if the session is marked "open" by the StartSessionHandshake, then notify the open state immediately
startSession ( session, nextFilter );
}
}
else
{
// send reply
nextFilter.filterWrite ( session, new DefaultWriteRequest ( reply ) );
// close - after message was sent
session.close ( false );
}
dumpFilterChain ( session );
logger.debug ( "Done handling HelloMessage" );
}
else if ( message instanceof AcceptMessage && !opened && this.clientMode )
{
this.acceptedProperties = Collections.unmodifiableMap ( ( (AcceptMessage)message ).getProperties () );
configureSession ( session, nextFilter, this.acceptedProperties );
postConfigureSession ( session, nextFilter, this.acceptedProperties );
markOpened ( session );
startSession ( session, nextFilter );
dumpFilterChain ( session );
}
else if ( message instanceof CloseMessage )
{
// remote will close
session.setAttribute ( "closeReason.message", ( (CloseMessage)message ).getMessage () ); //$NON-NLS-1$
session.setAttribute ( "closeReason.code", ( (CloseMessage)message ).getCode () ); //$NON-NLS-1$
session.close ( true );
}
else if ( message instanceof DataMessage && opened )
{
nextFilter.messageReceived ( session, message );
}
else if ( message instanceof StartMessage && !opened )
{
logger.debug ( "Starting session" );
markOpened ( session );
startSession ( session, nextFilter );
dumpFilterChain ( session );
}
else
{
throw new IllegalStateException ( String.format ( "(opened: %s) Message type: %s", opened, message.getClass () ) ); //$NON-NLS-1$
}
}