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

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
分享到:
评论

相关推荐

    spring mina 封装rest接口服务器

    基于spring mina 封装 rest 形式接口服务器,摆脱对tomcat,resin等服务器的依赖,基于spring,mina本身可提供tcp/ip接口,同时封装rest可方面提供http形式rest接口访问服务,方便接入

    spring+mina实现http接口服务端通信客户端

    此demo利用springmvc整合mina,实现客户端主动发送消息到服务端,并且以http接口的方式实现,亲测可用。

    mina core 包

    org.apache.mina.core.buffer.IoBuffer mina core 包

    mina自定义编码器-自行做会话累积

    mina自定义编码器-自行做会话累积。apache mina编码器

    C++异步网络IO库,仿java的mina实现

    前段时间整理一下代码,仿照java的mina自己做了一套C++的异步socket IO 框架。 编译环境: fedora 10 / cenos 5.4 / cygwin gcc version 4.3.2 其他linux环境没试过,不过应该也没啥问题。 使用到的库: 如果光...

    mina原理[定义].pdf

    mina原理[定义].pdf

    Java学习之IO总结及mina和netty

    NULL 博文链接:https://410063005.iteye.com/blog/1724491

    mina连接 mina心跳连接 mina断线重连

    mina连接,mina心跳连接,mina断线重连。其中客户端可直接用在android上。根据各方参考资料,经过自己的理解弄出来的。CSDN的资源分太难得了。

    mina使用mina使用mina使用

    mina的使用初步入门mina的使用初步入门mina的使用初步入门

    MINA_API+MINA_DOC+mina

    里面包含mina2.0的api(英文)和mina自学手册,还有mina的开发指导

    mina2.0 含11个jar包

    mina-core-2.0.0-M6.jar mina-example-2.0.0-M6.jar mina-filter-codec-netty-2.0.0-M6.jar mina-filter-compression-2.0.0-M6.jar mina-integration-beans-2.0.0-M6.jar mina-integration-jmx-2.0.0-M6.jar mina-...

    Mina 2.0 User Guide(Mina 2.0 用户指南)

    Chapter 10 - IoBuffer Chapter 11 - Codec Filter Chapter 12 - Executor Filter Chapter 13 - SSL Filter Chapter 14 - Logging Filter Part III - MINA Advanced Chapter 15 - Debugging Chapter 16 - State...

    mina开发手册

    Apache Mina Server 2.0 中文参考手册 Apache Mina Server 是一个网络通信应用框架,也就是说,它主要是对基于TCP/IP、UDP/IP ...步(Mina 的异步IO 默认使用的是JAVA NIO 作为底层支持)操作的编程模型。

    Apache MINA 2.0 用户指南中英文对照阅读版[带书签]

    第三章:IO 服务 第四章:会话 第五章:过滤器 第六章:传输 第七章:事件处理器 第八章:字节缓存 第九章:编解码器过滤器 第十章:执行者过滤器 第十一章:SSL 过滤器 第十二章:日志过滤器 第十三章:调试 第十四...

    mina 2.0 api

    mina的api接口文档,mina 2.0的所有类说明都在其中,对使用mina的人是很好的工具书

    mina 中文参考手册 各个接口都做了详尽的描述

    对mina的各个接口都做了详尽的描述,这可是做移动飞信朋友做的pdf哦!免费让大家下载吧!希望广传天下

    mina的高级使用,mina文件图片传送,mina发送文件,mina报文处理,mina发送xml和json

    mina的高级使用,mina文件图片传送,

    mina网络编程框架

    mina网络编程框架,mina网络编程框架是你的网络编程更简单,我使用mina写了一个接口,还可以。如果有不明白的可以给我发邮件3864913429@163.com. qq:270445938

    Apache_Mina2.0学习笔记

    最近使用Mina开发一个Java的NIO服务端程序,因此也特意学习了Apache的这个Mina框架。 引言 1 一. Mina入门 2 第一步.下载使用的Jar包 2 第二步.工程创建配置 2 第三步.服务端程序 3 第四步.客户端程序 6 第五步.长...

    关于apache Mina Server

    深入理解Apache_Mina_(1)----_Mina的几个类 深入理解Apache_Mina_(2)----_与IoFilter相关的几个类 深入理解Apache_Mina_(3)----_与IoHandler相关的几个类 深入理解Apache_Mina_(4)----_IoFilter和IoHandler的区别和...

Global site tag (gtag.js) - Google Analytics