`
Donald_Draper
  • 浏览: 980033 次
社区版块
存档分类
最新评论

Mina Io会话接口定义

    博客分类:
  • Mina
阅读更多
Mina Nio处理器:http://donald-draper.iteye.com/blog/2377725
引言:
     前面的文章我们看了Nio处理器,先来回顾一下:
     NioProcessor内部有一个选择器Selector,一个可重入读写锁用于控制选择器相关的操作,构造主要是初始化线程执行器和选择器。Nio处理器的选择操作,唤醒等操作,实际通过内部的选择器完成。初始化会话,主要是配置会话通道为非阻塞模式,注册会话通道读事件到选择器。注册新选择器,主要是注册旧选择器的选择key(集合)关联的会话,通道,及通道兴趣事件集到新的选择器;会话时附加在通道选择key的Attachment上。处理器处理会话读写操作,主要是通过会话关联的通道完成。关闭会话主要是关闭会话关联的字节通道和取消会话关联选择key。
今天我们来看一下Io会话的定义:
/**
 * A handle which represents connection between two endpoints regardless of 
 * transport types.
 * 
 * {@link IoSession} provides user-defined attributes.  User-defined attributes
 * are application-specific data which is associated with a session.
 * It often contains objects that represents the state of a higher-level protocol
 * and becomes a way to exchange data between filters and handlers.
 * 
 * <h3>Adjusting Transport Type Specific Properties</h3>
 * <p>
 * You can simply downcast the session to an appropriate subclass.
 * 

 * 
 * <h3>Thread Safety</h3>
 * 
 * {@link IoSession} is thread-safe.  But please note that performing
 * more than one {@link #write(Object)} calls at the same time will
 * cause the {@link IoFilter#filterWrite(IoFilter.NextFilter, IoSession, IoFilter.WriteRequest)}
 * is executed simnutaneously, and therefore you have to make sure the
 * {@link IoFilter} implementations you're using are thread-safe, too. 
 * 

 *   
 * @author The Apache Directory Project (mina-dev@directory.apache.org)
 * @version $Rev$, $Date$
 */
public interface IoSession {

    /**
     * Returns the {@link IoService} which provides I/O service to this session.
     获取会话关联的IoService
     */
    IoService getService();

    /**
     * Returns the {@link IoServiceConfig} of this session.
     获取会话关联的IoService配置
     */
    IoServiceConfig getServiceConfig();

    /**
     * Returns the {@link IoHandler} which handles this session.
     获取会话Iohandler
     */
    IoHandler getHandler();

    /**
     * Returns the configuration of this session.
     获取会话配置
     */
    IoSessionConfig getConfig();

    /**
     * Returns the filter chain that only affects this session.
     获取会话过滤链
     */
    IoFilterChain getFilterChain();

    /**
     * Writes the specified <code>message</code> to remote peer.  This
     * operation is asynchronous; {@link IoHandler#messageSent(IoSession, Object)}
     * will be invoked when the message is actually sent to remote peer.
     * You can also wait for the returned {@link WriteFuture} if you want
     * to wait for the message actually written.
     发送消息给远端的peer。此操作是异步的,当消息实际发送到远端peer时,会调用
     IoHandler#messageSent方法法。如果想等待消息实际发送完,可以等待WriteFuture。
     */
    WriteFuture write(Object message);

    /**
     * Closes this session immediately.  This operation is asynthronous.
     * Wait for the returned {@link CloseFuture} if you want to wait for
     * the session actually closed.
     立刻关闭会话。此操作为异步的。如果想要等待会话实际完成关闭,可以等待CloseFuture
     */
    CloseFuture close();

    /**
     * Returns an attachment of this session.
     * This method is identical with <tt>getAttribute( "" )</tt>.
     返回会话附加物
     */
    Object getAttachment();

    /**
     * Sets an attachment of this session.
     * This method is identical with <tt>setAttribute( "", attachment )</tt>.
     * 设置会话附加物
     * @return Old attachment.  <tt>null</tt> if it is new.
     */
    Object setAttachment(Object attachment);

    /**
     * Returns the value of user-defined attribute of this session.
     * 获取会话属性key对一个的属性值
     * @param key the key of the attribute
     * @return <tt>null</tt> if there is no attribute with the specified key
     */
    Object getAttribute(String key);

    /**
     * Sets a user-defined attribute.
     * 设置会话属性
     * @param key the key of the attribute
     * @param value the value of the attribute
     * @return The old value of the attribute.  <tt>null</tt> if it is new.
     */
    Object setAttribute(String key, Object value);

    /**
     * Sets a user defined attribute without a value.  This is useful when
     * you just want to put a 'mark' attribute.  Its value is set to
     * {@link Boolean#TRUE}.
     * 设置会话无值属性
     * @param key the key of the attribute
     * @return The old value of the attribute.  <tt>null</tt> if it is new.
     */
    Object setAttribute(String key);

    /**
     * Removes a user-defined attribute with the specified key.
     * 移除会话属性
     * @return The old value of the attribute.  <tt>null</tt> if not found.
     */
    Object removeAttribute(String key);

    /**
     * Returns <tt>true</tt> if this session contains the attribute with
     * the specified <tt>key</tt>.
     判断是否包含属性key
     */
    boolean containsAttribute(String key);

    /**
     * Returns the set of keys of all user-defined attributes.
     获取会话所有属性
     */
    Set getAttributeKeys();

    /**
     * Returns transport type of this session.
     获取会话transport类型,socket,Datagram,vmpipe
     */
    TransportType getTransportType();

    /**
     * Returns <code>true</code> if this session is connected with remote peer.
     会话是否连接
     */
    boolean isConnected();

    /**
     * Returns <code>true</tt> if and only if this session is being closed
     * (but not disconnected yet) or is closed.
     会话是否关闭
     */
    boolean isClosing();

    /**
     * Returns the {@link CloseFuture} of this session.  This method returns
     * the same instance whenever user calls it.
     获取会话关闭结果
     */
    CloseFuture getCloseFuture();

    /**
     * Returns the socket address of remote peer. 
     获取会话远端peer地址
     */
    SocketAddress getRemoteAddress();

    /**
     * Returns the socket address of local machine which is associated with this
     * session.
     获取会话本地地址
     */
    SocketAddress getLocalAddress();

    /**
     * Returns the socket address of the {@link IoService} listens to to manage
     * this session.  If this session is managed by {@link IoAcceptor}, it
     * returns the {@link SocketAddress} which is specified as a parameter of
     * {@link IoAcceptor#bind(SocketAddress, IoHandler)}.  If this session is
     * managed by {@link IoConnector}, this method returns the same address with
     * that of {@link #getRemoteAddress()}.  
     获取IoService监听管理会话的地址。如果会话被IoAcceptor管理,返回的为IoAcceptor#bind(SocketAddress, IoHandler)
     的参数地址。如果会话为IoConnector,则返回的远端peer地址。
     */
    SocketAddress getServiceAddress();

    /**
     * Returns idle time for the specified type of idleness in seconds.
     返回空闲状态的空闲时间s
     */
    int getIdleTime(IdleStatus status);

    /**
     * Returns idle time for the specified type of idleness in milliseconds.
     返回空闲状态的空闲时间ms
     */
    long getIdleTimeInMillis(IdleStatus status);

    /**
     * Sets idle time for the specified type of idleness in seconds.
     设置空闲状态的空闲时间
     */
    void setIdleTime(IdleStatus status, int idleTime);

    /**
     * Returns write timeout in seconds.
     获取写超时时间s
     */
    int getWriteTimeout();

    /**
     * Returns write timeout in milliseconds.
     获取写超时时间ms
     */
    long getWriteTimeoutInMillis();

    /**
     * Sets write timeout in seconds.
     设置写超时时间
     */
    void setWriteTimeout(int writeTimeout);

    /**
     * Returns the current {@link TrafficMask} of this session.
     返回会话传输状态(读写)
     */
    TrafficMask getTrafficMask();

    /**
     * Sets the {@link TrafficMask} of this session which will result
     * the parent {@link IoService} to start to control the traffic
     * of this session immediately.
     设置会话传输状态,这将引起Io服务,开始控制会话传输
     */
    void setTrafficMask(TrafficMask trafficMask);

    /**
     * A shortcut method for {@link #setTrafficMask(TrafficMask)} that
     * suspends read operations for this session.
     暂定会话读操作
     */
    void suspendRead();

    /**
     * A shortcut method for {@link #setTrafficMask(TrafficMask)} that
     * suspends write operations for this session.
     暂定会话写操作
     */
    void suspendWrite();

    /**
     * A shortcut method for {@link #setTrafficMask(TrafficMask)} that
     * resumes read operations for this session.
     恢复会话读操作
     */
    void resumeRead();

    /**
     * A shortcut method for {@link #setTrafficMask(TrafficMask)} that
     * resumes write operations for this session.
     恢复会话写操作
     */
    void resumeWrite();

    /**
     * Returns the total number of bytes which were read from this session.
     获取从会话读取的字节数
     */
    long getReadBytes();

    /**
     * Returns the total number of bytes which were written to this session.
     获取会话写的字节数
     */
    long getWrittenBytes();

    /**
     * Returns the total number of messages which were read and decoded from this session. 
     获取从会话读取或解码的消息数
     */
    long getReadMessages();

    /**
     * Returns the total number of messages which were written and encoded by this session.
     获取会话发送消息和编码消息的数量
     */
    long getWrittenMessages();

    /**
     * Returns the total number of write requests which were written to this session.
     获取会话写请求的数量
     */
    long getWrittenWriteRequests();

    /**
     * Returns the number of write requests which are scheduled to be written
     * to this session.
     获取会话写请求实际被调度的数量,即实际发送数量
     */
    int getScheduledWriteRequests();

    /**
     * Returns the number of bytes which are scheduled to be written to this
     * session.
     获取会话实际发送字节数
     */
    int getScheduledWriteBytes();

    /**
     * Returns the time in millis when this session is created.
     获取会话创建时间
     */
    long getCreationTime();

    /**
     * Returns the time in millis when I/O occurred lastly.
     获取会话上一次发送IO操作的时间
     */
    long getLastIoTime();

    /**
     * Returns the time in millis when read operation occurred lastly.
    获取会话上一次读操作的时间
     */
    long getLastReadTime();

    /**
     * Returns the time in millis when write operation occurred lastly.
     获取会话上一次写操作的时间
     */
    long getLastWriteTime();

    /**
     * Returns <code>true</code> if this session is idle for the specified 
     * {@link IdleStatus}.
     判断会话是否处理空闲状态status
     */
    boolean isIdle(IdleStatus status);

    /**
     * Returns the number of the fired continuous <tt>sessionIdle</tt> events
     * for the specified {@link IdleStatus}.
     获取会话处于空闲状态status的次数
     * <p>
     * If <tt>sessionIdle</tt> event is fired first after some time after I/O,
     * <tt>idleCount</tt> becomes <tt>1</tt>.  <tt>idleCount</tt> resets to
     * <tt>0</tt> if any I/O occurs again, otherwise it increases to
     * <tt>2</tt> and so on if <tt>sessionIdle</tt> event is fired again without
     * any I/O between two (or more) <tt>sessionIdle</tt> events.
     */
    int getIdleCount(IdleStatus status);

    /**
     * Returns the time in millis when the last <tt>sessionIdle</tt> event
     * is fired for the specified {@link IdleStatus}.
     获取会话上次处于空闲状态status的时间
     */
    long getLastIdleTime(IdleStatus status);
}

附:
//TransportType
/**
 * Represents network transport types.
 * MINA provides three transport types by default:
 * [list]
 *   [*]{@link #SOCKET} - TCP/IP

 *   [*]{@link #DATAGRAM} - UDP/IP

 *   <li>{@link #VM_PIPE} - in-VM pipe support (only available in protocol
 *       layer</li>
 * [/list]
 * <p>
 * You can also create your own transport type.  Please refer to
 * {@link #TransportType(String[], boolean)}.
 * 
 * @author The Apache Directory Project (mina-dev@directory.apache.org)
 * @version $Rev$, $Date$
 */
public final class TransportType implements Serializable {
    private static final long serialVersionUID = 3258132470497883447L;

    private static final Map name2type = new HashMap();   
    private final String[] names;
    private final transient boolean connectionless;
    private final transient Class envelopeType;
    /**
     * Transport type: TCP/IP (Registry name: <tt>"SOCKET"</tt> or <tt>"TCP"</tt>)
     */
    public static final TransportType SOCKET = new TransportType(new String[] {
            "SOCKET", "TCP" }, false);

    /**
     * Transport type: UDP/IP (Registry name: <tt>"DATAGRAM"</tt> or <tt>"UDP"</tt>)
     */
    public static final TransportType DATAGRAM = new TransportType(
            new String[] { "DATAGRAM", "UDP" }, true);

    /**
     * Transport type: in-VM pipe (Registry name: <tt>"VM_PIPE"</tt>) 
     * Please refer to
     * [url=../protocol/vmpipe/package-summary.htm]<tt>org.apache.mina.protocol.vmpipe</tt>[/url]
     * package.
     */
    public static final TransportType VM_PIPE = new TransportType(
            new String[] { "VM_PIPE" }, Object.class, false);
    ...
 }


//TrafficMask
/**
 * A type-safe mask that is used to control the traffic of {@link IoSession}
 * with {@link IoSession#setTrafficMask(TrafficMask)}.
 *
 * @author The Apache Directory Project (mina-dev@directory.apache.org)
 * @version $Rev$, $Date$
 */
public class TrafficMask {
    private final int interestOps;
    private final String name;
    /**
     * This mask suspends both reads and writes.
     */
    public static final TrafficMask NONE = new TrafficMask(0, "none");

    /**
     * This mask suspends writes, and resumes reads if reads were suspended.
     */
    public static final TrafficMask READ = new TrafficMask(
            SelectionKey.OP_READ, "read");

    /**
     * This mask suspends reads, and resumes writes if writes were suspended.
     */
    public static final TrafficMask WRITE = new TrafficMask(
            SelectionKey.OP_WRITE, "write");

    /**
     * This mask resumes both reads and writes if any of them were suspended.
     */
    public static final TrafficMask ALL = new TrafficMask(SelectionKey.OP_READ
            | SelectionKey.OP_WRITE, "all");
    ...
 }
0
1
分享到:
评论

相关推荐

    Mina 2.0快速入门与源码解析

    `IdleStatus` 枚举定义了会话的三种空闲状态:读取空闲、写入空闲以及两者同时空闲。通过设置空闲时间,可以帮助我们更好地管理会话资源。 综上所述,Mina 2.0 为开发者提供了丰富的功能和灵活性,使得构建复杂网络...

    Mina 使用DEMO

    4. **会话事件**:Mina 会触发各种会话事件,如打开、关闭、异常等,开发者可以通过监听这些事件来执行相应的操作。 5. **数据传输**:数据的读写操作通过IoBuffer对象进行,它提供了一种高效、灵活的数据缓冲机制...

    Mina2.0阅读源码笔记(很值得一看)

    - IoService 接口定义了 Mina 中用于处理网络连接的服务,它包括 IoAcceptor 和 IoConnector。 - **IoAcceptor**: 负责监听并接受来自客户端的连接请求。 - **IoConnector**: 用于发起连接到远程服务器的请求。 ...

    Mina2.0框架源码剖析

    Mina使用IoBuffer作为数据缓冲区,它提供了一种高效的字节操作方式。同时,Mina还提供了Codec接口,用于数据的编码和解码。通过实现Codec接口,开发者可以自定义数据格式,实现与其他系统的通信。 五、异步I/O模型 ...

    Apache_Mina_Server_2.0中文参考手册

    3. IoFilter:这个接口定义了一组拦截器,用于处理日志输出、黑名单过滤等,以及数据的编码(write方向)和解码(read方向)。编码和解码操作是Mina框架中非常核心的部分,因为它涉及到网络中数据传输的格式转换。 ...

    Apache Mina Server 2.0 中文参考手册(带目录)

    3. IoFilter:这个接口定义了一系列可以进行数据拦截的组件,包括日志输出、数据过滤、编码和解码等操作。这些拦截器可以配置为一个链式结构,对流经的数据进行预处理或后处理。 4. IoHandler:这是业务逻辑处理的...

    MINA2与Netty4比较分析

    线程模型主要用来描述网络框架如何使用线程去处理连接、会话、以及请求和响应的逻辑。Mina2与Netty4都使用了Reactor模式的线程模型,但具体实现细节上有所不同。 Mina2使用了一个IoAcceptor线程来监听客户端连接,...

    NIO_MINA学习例子_源代码

    3. **MINA会话管理**:了解Session对象如何维护客户端连接的状态,包括打开、关闭、读写事件的处理。 4. **MINA服务端口**:查看服务端口的绑定和监听代码,理解如何启动一个MINA服务器,并接受客户端连接。 5. **...

    MINA、Netty、Twisted放在一起学

    通过“Mina、Netty、Twisted一起学(九):异步IO和回调函数.docx”,我们可以深入了解Twisted的异步I/O机制和回调函数的使用。 这些文档还涵盖了特定主题,如“Mina、Netty、Twisted一起学(三):TCP消息固定大小...

    Mina 2.0.7 demo

    首先,Mina的核心在于它的事件驱动模型,它使用了IO多路复用技术,如Java的NIO(Non-blocking I/O)库,这使得程序能够同时处理多个连接,提高了并发性能。在Mina中,我们通常会定义一个`FilterChain`,这个链中的每...

    myhttp:提供基于mina 的不依赖web容器对http协的支持

    Mina提供了一种事件驱动的异步模型,通过IO多路复用技术(如NIO)提高了网络通信的效率。在Mina中,网络连接被抽象为Session,数据交换则通过Filter Chain进行处理,这种设计模式允许开发者灵活地添加、修改或移除...

    一个基于Java的RPC工具.zip

    - **Spring Cloud**: 提供了一整套微服务解决方案,包括服务注册、服务发现、配置中心、熔断器、路由、微代理、控制总线、一次性令牌、全局锁、领导选举、分布式会话、集群状态等。 7. **Java并发与多线程**: - ...

    J2EE武功秘籍

    - **Factory Method**:定义一个创建产品对象的接口,让子类决定实例化哪一个类。 - **Adapter**:将一个类的接口转换成客户希望的另一个接口。 **3. 经典线程设计模式** - **生产者消费者模型**:通过缓冲区实现...

    JAVA上百实例源码以及开源项目源代码

    Java二进制IO类与文件复制操作实例 16个目标文件 内容索引:Java源码,初学实例,二进制,文件复制 Java二进制IO类与文件复制操作实例,好像是一本书的例子,源代码有的是独立运行的,与同目录下的其它代码文件互不联系...

Global site tag (gtag.js) - Google Analytics