001/*
002 *  Licensed to the Apache Software Foundation (ASF) under one
003 *  or more contributor license agreements.  See the NOTICE file
004 *  distributed with this work for additional information
005 *  regarding copyright ownership.  The ASF licenses this file
006 *  to you under the Apache License, Version 2.0 (the
007 *  "License"); you may not use this file except in compliance
008 *  with the License.  You may obtain a copy of the License at
009 *
010 *    http://www.apache.org/licenses/LICENSE-2.0
011 *
012 *  Unless required by applicable law or agreed to in writing,
013 *  software distributed under the License is distributed on an
014 *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 *  KIND, either express or implied.  See the License for the
016 *  specific language governing permissions and limitations
017 *  under the License.
018 *
019 */
020package org.apache.mina.core.session;
021
022import org.apache.mina.core.write.WriteRequest;
023
024/**
025 * An I/O event or an I/O request that MINA provides.
026 * Most users won't need to use this class.  It is usually used by internal
027 * components to store I/O events.
028 *
029 * @author <a href="http://mina.apache.org">Apache MINA Project</a>
030 */
031public class IoEvent implements Runnable {
032    private final IoEventType type;
033
034    private final IoSession session;
035
036    private final Object parameter;
037
038    public IoEvent(IoEventType type, IoSession session, Object parameter) {
039        if (type == null) {
040            throw new IllegalArgumentException("type");
041        }
042        if (session == null) {
043            throw new IllegalArgumentException("session");
044        }
045        this.type = type;
046        this.session = session;
047        this.parameter = parameter;
048    }
049
050    public IoEventType getType() {
051        return type;
052    }
053
054    public IoSession getSession() {
055        return session;
056    }
057
058    public Object getParameter() {
059        return parameter;
060    }
061
062    public void run() {
063        fire();
064    }
065
066    public void fire() {
067        switch (getType()) {
068        case MESSAGE_RECEIVED:
069            getSession().getFilterChain().fireMessageReceived(getParameter());
070            break;
071        case MESSAGE_SENT:
072            getSession().getFilterChain().fireMessageSent((WriteRequest) getParameter());
073            break;
074        case WRITE:
075            getSession().getFilterChain().fireFilterWrite((WriteRequest) getParameter());
076            break;
077        case CLOSE:
078            getSession().getFilterChain().fireFilterClose();
079            break;
080        case EXCEPTION_CAUGHT:
081            getSession().getFilterChain().fireExceptionCaught((Throwable) getParameter());
082            break;
083        case SESSION_IDLE:
084            getSession().getFilterChain().fireSessionIdle((IdleStatus) getParameter());
085            break;
086        case SESSION_OPENED:
087            getSession().getFilterChain().fireSessionOpened();
088            break;
089        case SESSION_CREATED:
090            getSession().getFilterChain().fireSessionCreated();
091            break;
092        case SESSION_CLOSED:
093            getSession().getFilterChain().fireSessionClosed();
094            break;
095        default:
096            throw new IllegalArgumentException("Unknown event type: " + getType());
097        }
098    }
099
100    @Override
101    public String toString() {
102        if (getParameter() == null) {
103            return "[" + getSession() + "] " + getType().name();
104        }
105
106        return "[" + getSession() + "] " + getType().name() + ": " + getParameter();
107    }
108}