查看本类的 API文档回源码主页即时通讯网 - 即时通讯开发者社区!
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  
27  /**
28   * Handles all I/O events fired by MINA.
29   *
30   * @author <a href="http://mina.apache.org">Apache MINA Project</a>
31   *
32   * @see IoHandlerAdapter
33   */
34  public interface IoHandler {
35      /**
36       * Invoked from an I/O processor thread when a new connection has been created.
37       * Because this method is supposed to be called from the same thread that
38       * handles I/O of multiple sessions, please implement this method to perform
39       * tasks that consumes minimal amount of time such as socket parameter
40       * and user-defined session attribute initialization.
41       * 
42       * @param session The session being created
43       * @throws Exception If we get an exception while processing the create event
44       */
45      void sessionCreated(IoSession session) throws Exception;
46  
47      /**
48       * Invoked when a connection has been opened.  This method is invoked after
49       * {@link #sessionCreated(IoSession)}.  The biggest difference from
50       * {@link #sessionCreated(IoSession)} is that it's invoked from other thread
51       * than an I/O processor thread once thread model is configured properly.
52       * 
53       * @param session The session being opened
54       * @throws Exception If we get an exception while processing the open event
55       */
56      void sessionOpened(IoSession session) throws Exception;
57  
58      /**
59       * Invoked when a connection is closed.
60       * 
61       * @param session The session being closed
62       * @throws Exception If we get an exception while processing the close event
63       */
64      void sessionClosed(IoSession session) throws Exception;
65  
66      /**
67       * Invoked with the related {@link IdleStatus} when a connection becomes idle.
68       * This method is not invoked if the transport type is UDP; it's a known bug,
69       * and will be fixed in 2.0.
70       * 
71       * @param session The idling session 
72       * @param status The session's status
73       * @throws Exception If we get an exception while processing the idle event
74       */
75      void sessionIdle(IoSession session, IdleStatus status) throws Exception;
76  
77      /**
78       * Invoked when any exception is thrown by user {@link IoHandler}
79       * implementation or by MINA.  If <code>cause</code> is an instance of
80       * {@link IOException}, MINA will close the connection automatically.
81       * 
82       * @param session The session for which we have got an exception
83       * @param cause The exception that has been caught
84       * @throws Exception If we get an exception while processing the caught exception
85       */
86      void exceptionCaught(IoSession session, Throwable cause) throws Exception;
87  
88      /**
89       * Invoked when a message is received.
90       * 
91       * @param session The session that is receiving a message
92       * @param message The received message
93       * @throws Exception If we get an exception while processing the received message
94       */
95      void messageReceived(IoSession session, Object message) throws Exception;
96  
97      /**
98       * Invoked when a message written by {@link IoSession#write(Object)} is
99       * sent out.
100      * 
101      * @param session The session that has sent a full message
102      * @param message The sent message
103      * @throws Exception If we get an exception while processing the sent message 
104      */
105     void messageSent(IoSession session, Object message) throws Exception;
106 
107     /**
108      * Handle the closure of an half-duplex TCP channel
109      * 
110      * @param session The session which input is being closed
111      * @throws Exception If we get an exception while closing the input
112      */
113     void inputClosed(IoSession session) throws Exception;
114 }