package com.eactive.eai.rms.client;

import java.util.List;

import org.apache.mina.common.IoSession;
import org.apache.mina.handler.demux.DemuxingIoHandler;
import org.apache.mina.handler.demux.MessageHandler;
import org.apache.mina.util.SessionLog;

import org.springframework.beans.factory.InitializingBean;

import com.eactive.eai.rms.client.handler.IMessageHandler;

/**
 * The Class UdpClientSessionHandler.
 * <ul>
 * <li>1. Function :EAI Framework으로 부터 송신하는 UDP/IP의 SessionHandler입니다. </li>
 * <li>2. Summary :</li>
 * <li>3. Notification :</li>
 * </ul>
 * 
 * @author  : $Author: 박기섭 $
 * @version : $Revision: 1.6 $
 */
public class UdpClientSessionHandler extends DemuxingIoHandler implements
        InitializingBean {

    public UdpClientSessionHandler(List<IMessageHandler> handlers) {
        for (IMessageHandler handler : handlers) {
            addMessageHandler(handler.targetClass(), (MessageHandler) handler);
        }
    }

    /* (non-Javadoc)
     * @see org.apache.mina.common.IoHandlerAdapter#sessionOpened(org.apache.mina.common.IoSession)
     */
    @Override
    public void sessionOpened(IoSession session) {
    }

    /* (non-Javadoc)
     * @see org.apache.mina.common.IoHandlerAdapter#messageSent(org.apache.mina.common.IoSession, java.lang.Object)
     */
    @Override
    public void messageSent(IoSession session, Object message) throws Exception {
        if (SessionLog.isDebugEnabled(session)) {
            SessionLog.debug(session, "message : " + message + "\n" + session);
        }
    }

    /* (non-Javadoc)
     * @see org.apache.mina.common.IoHandlerAdapter#exceptionCaught(org.apache.mina.common.IoSession, java.lang.Throwable)
     */
    @Override
    public void exceptionCaught(IoSession session, Throwable cause) {
        if (SessionLog.isDebugEnabled(session)) {
            SessionLog.debug(session, "exceptionCaught", cause);
        }
    }

    /* (non-Javadoc)
     * @see org.apache.mina.common.IoHandlerAdapter#sessionClosed(org.apache.mina.common.IoSession)
     */
    @Override
    public void sessionClosed(IoSession session) throws Exception {
    }

    public void afterPropertiesSet() throws Exception {
    }

}