1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one 3 * or more contributor license agreements. See the NOTICE file 4 * distributed with this work for additional information 5 * regarding copyright ownership. The ASF licenses this file 6 * to you under the Apache License, Version 2.0 (the 7 * "License"); you may not use this file except in compliance 8 * with the License. You may obtain a copy of the License at 9 * 10 * http://www.apache.org/licenses/LICENSE-2.0 11 * 12 * Unless required by applicable law or agreed to in writing, 13 * software distributed under the License is distributed on an 14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 * KIND, either express or implied. See the License for the 16 * specific language governing permissions and limitations 17 * under the License. 18 * 19 */ 20 package org.apache.mina.core.service; 21 22 import java.io.IOException; 23 24 import org.apache.mina.core.session.IdleStatus; 25 import org.apache.mina.core.session.IoSession; 26 import org.apache.mina.filter.FilterEvent; 27 28 /** 29 * Handles all I/O events fired by MINA. 30 * 31 * @author <a href="http://mina.apache.org">Apache MINA Project</a> 32 * 33 * @see IoHandlerAdapter 34 */ 35 public interface IoHandler { 36 /** 37 * Invoked from an I/O processor thread when a new connection has been created. 38 * Because this method is supposed to be called from the same thread that 39 * handles I/O of multiple sessions, please implement this method to perform 40 * tasks that consumes minimal amount of time such as socket parameter 41 * and user-defined session attribute initialization. 42 * 43 * @param session The session being created 44 * @throws Exception If we get an exception while processing the create event 45 */ 46 void sessionCreated(IoSession session) throws Exception; 47 48 /** 49 * Invoked when a connection has been opened. This method is invoked after 50 * {@link #sessionCreated(IoSession)}. The biggest difference from 51 * {@link #sessionCreated(IoSession)} is that it's invoked from other thread 52 * than an I/O processor thread once thread model is configured properly. 53 * 54 * @param session The session being opened 55 * @throws Exception If we get an exception while processing the open event 56 */ 57 void sessionOpened(IoSession session) throws Exception; 58 59 /** 60 * Invoked when a connection is closed. 61 * 62 * @param session The session being closed 63 * @throws Exception If we get an exception while processing the close event 64 */ 65 void sessionClosed(IoSession session) throws Exception; 66 67 /** 68 * Invoked with the related {@link IdleStatus} when a connection becomes idle. 69 * This method is not invoked if the transport type is UDP; it's a known bug, 70 * and will be fixed in 2.0. 71 * 72 * @param session The idling session 73 * @param status The session's status 74 * @throws Exception If we get an exception while processing the idle event 75 */ 76 void sessionIdle(IoSession session, IdleStatus status) throws Exception; 77 78 /** 79 * Invoked when any exception is thrown by user {@link IoHandler} 80 * implementation or by MINA. If <code>cause</code> is an instance of 81 * {@link IOException}, MINA will close the connection automatically. 82 * 83 * @param session The session for which we have got an exception 84 * @param cause The exception that has been caught 85 * @throws Exception If we get an exception while processing the caught exception 86 */ 87 void exceptionCaught(IoSession session, Throwable cause) throws Exception; 88 89 /** 90 * Invoked when a message is received. 91 * 92 * @param session The session that is receiving a message 93 * @param message The received message 94 * @throws Exception If we get an exception while processing the received message 95 */ 96 void messageReceived(IoSession session, Object message) throws Exception; 97 98 /** 99 * Invoked when a message written by {@link IoSession#write(Object)} is 100 * sent out. 101 * 102 * @param session The session that has sent a full message 103 * @param message The sent message 104 * @throws Exception If we get an exception while processing the sent message 105 */ 106 void messageSent(IoSession session, Object message) throws Exception; 107 108 /** 109 * Handle the closure of an half-duplex TCP channel 110 * 111 * @param session The session which input is being closed 112 * @throws Exception If we get an exception while closing the input 113 */ 114 void inputClosed(IoSession session) throws Exception; 115 116 /** 117 * Invoked when a filter event is fired. Each filter might sent a different event, 118 * this is very application specific. 119 * 120 * @param session The session for which we have an event to process 121 * @param event The event to process 122 * @throws Exception If we get an exception while processing the event 123 */ 124 void event(IoSession session, FilterEvent event) throws Exception; 125 }