`
1998a
  • 浏览: 114086 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

FtpServer 中的Adapter模式

    博客分类:
  • MINA
阅读更多

FtpServer是基于MINA的一个微型的FTP Server的实现,据说可以很好的集成到其他程序中,提供ftp服务

最近在看mina,顺便想找个大一点儿的例子来看看MINA是如何使用的。就找到了这个FTP实现,看了下代码,感觉还是不错的,决定写些东西记录自己的想法。先从最简单的开始。

不愧是MINA下的项目,很多地方都可以看到MINA的气息

和MINA 其他程序一样,ftp业务逻辑封装到FtpHandler中。代码如下:

FtpHandler

public interface FtpHandler {

void init(FtpServerContext context, Listener listener) throws Exception;

/**
* Invoked from an I/O processor thread when a new connection has been
* created. Because this method is supposed to be called from the same
* thread that handles I/O of multiple sessions, please implement this
* method to perform tasks that consumes minimal amount of time such as
* socket parameter and user-defined session attribute initialization.
*/
void sessionCreated(FtpIoSession session) throws Exception;

/**
* Invoked when a connection has been opened. This method is invoked after
* {@link #sessionCreated(IoSession)}. The biggest difference from
* {@link #sessionCreated(IoSession)} is that it's invoked from other thread
* than an I/O processor thread once thread modesl is configured properly.
*/
void sessionOpened(FtpIoSession session) throws Exception;

/**
* Invoked when a connection is closed.
*/
void sessionClosed(FtpIoSession session) throws Exception;

/**
* Invoked with the related {@link IdleStatus} when a connection becomes
* idle. This method is not invoked if the transport type is UDP; it's a
* known bug, and will be fixed in 2.0.
*/
void sessionIdle(FtpIoSession session, IdleStatus status) throws Exception;

/**
* Invoked when any exception is thrown by user {@link IoHandler}
* implementation or by MINA. If <code>cause</code> is instanceof
* {@link IOException}, MINA will close the connection automatically.
*/
void exceptionCaught(FtpIoSession session, Throwable cause)
throws Exception;

/**
* Invoked when a message is received.
*/
void messageReceived(FtpIoSession session, FtpRequest request)
throws Exception;

/**
* Invoked when a message written by {@link IoSession#write(Object)} is sent
* out.
*/
void messageSent(FtpIoSession session, FtpReply reply) throws Exception;
}
 

简直和MINA 中的IoHandler一模一样,就是把原来的session换成了FtpIoSession,可能作者不想让MINA太多的影响自己,所以把自己完全包装了一遍,必要的时候就Adapter一下,代码如下

写道
public class FtpHandlerAdapter implements IoHandler {
private FtpServerContext context;

private FtpHandler ftpHandler;

public FtpHandlerAdapter(FtpServerContext context, FtpHandler ftpHandler) {
this.context = context;
this.ftpHandler = ftpHandler;
}

public void exceptionCaught(IoSession session, Throwable cause)
throws Exception {
FtpIoSession ftpSession = new FtpIoSession(session, context);
ftpHandler.exceptionCaught(ftpSession, cause);
}

public void messageReceived(IoSession session, Object message)
throws Exception {
FtpIoSession ftpSession = new FtpIoSession(session, context);
FtpRequest request = new FtpRequestImpl(message.toString());

ftpHandler.messageReceived(ftpSession, request);
}

public void messageSent(IoSession session, Object message) throws Exception {
FtpIoSession ftpSession = new FtpIoSession(session, context);
ftpHandler.messageSent(ftpSession, (FtpReply) message);
}

public void sessionClosed(IoSession session) throws Exception {
FtpIoSession ftpSession = new FtpIoSession(session, context);
ftpHandler.sessionClosed(ftpSession);
}

public void sessionCreated(IoSession session) throws Exception {
FtpIoSession ftpSession = new FtpIoSession(session, context);
ftpHandler.sessionCreated(ftpSession);
}

public void sessionIdle(IoSession session, IdleStatus status)
throws Exception {
FtpIoSession ftpSession = new FtpIoSession(session, context);
ftpHandler.sessionIdle(ftpSession, status);
}

public void sessionOpened(IoSession session) throws Exception {
FtpIoSession ftpSession = new FtpIoSession(session, context);
ftpHandler.sessionOpened(ftpSession);
}

public FtpHandler getFtpHandler() {
return ftpHandler;
}

public void setFtpHandler(FtpHandler handler) {
this.ftpHandler = handler;

}

}

 在FtpHandlerAdapter中,不仅adapt了FtpHandler,还完成了IoSession到FtpSession 的封装。

 

 

未完待续

 

分享到:
评论

相关推荐

    ftpserver配置方式

    -- FTP主动模式 --&gt; &lt;property name="fileType" value="ASCII" /&gt; &lt;!-- 文件类型,默认为ASCII --&gt; ``` 接着,创建FTP outbound gateway,用于执行FTP命令: ```xml &lt;int-ftp:outbound-gateway id=...

    Android端连接ftp服务器浏览资源目录

    FTP有两种工作模式:主动模式和被动模式,主动模式中服务器主动建立数据连接,被动模式则由客户端建立。 2. Android网络访问权限:在Android中,任何网络相关的操作都需要在`AndroidManifest.xml`中声明相应的权限...

    FTP C#

    - `BizTalk Sftp Adapter`是用于BizTalk Server的适配器,支持SSH File Transfer Protocol(SFTP)。 - `FTP Client Demo for .NET C# and VB.NET`可能是一个示例项目,展示了如何在C#和VB.NET中实现FTP客户端功能...

    Biztalk Server 2009 视频 demo source code

    1. **BizTalk Adapter Framework**:BizTalk Server 2009 支持多种适配器,允许与各种系统和应用程序进行通信,如SQL Server、Oracle数据库、Web服务、FTP服务器等。适配器框架使得集成变得更加灵活和简便。 2. **...

    BizTalk 2004 教程

    BizTalk 2004基于一个强大的服务导向架构(SOA),由多个组件组成,包括BizTalk Server、BizTalk Adapter Pack、BizTalk Orchestration、BizTalk Messaging Engine和BizTalk BAM(Business Activity Monitoring)等...

    LVS实现Windows IIS负载均衡解决方案.doc

    3. VS/DR(Virtual Server via Direct Routing):直接路由方法,负载能力最强,也是本文中采用的模式。在这种模式下,Realserver会直接将响应返回给客户端,无需经过调度器。 在Windows IIS服务器上实现LVS负载...

    微软软件外包项目案例分析.pptx

    FTP Adapter for BizTalk 2002 是微软软件外包项目的一个典型案例。该项目的开发部门是 E-Business Server Division,外包的原因是原有人力资源转移到另外一个更重要的项目的开发。 Satyam Computer Service Ltd. 是...

    微软软件外包项目-案例分析.pptx

    以FTP Adapter for BizTalk 2002为例,该项目原本由E-Business Server Division开发,但由于人力资源转移,决定外包给Satyam Computer Service Ltd.,采用虚拟团队模式进行合作。项目启动和签约过程包括了初步接触、...

    09年最新网络术语解释

    Adapter则是安装在计算机内用于增加网络功能的硬件设备,对于无线网络来说,就是无线网卡。 Ad-hoc模式是指无线设备之间直接通信,无需通过AP,形成一个临时的网络。AES(Advanced Encryption Standard)是高级加密...

    嵌入式Linux开发平台搭建

    - 服务器组件:如FTP Server、MySQL Database等。 - 语言支持:安装“Chinese Support”。 #### 五、系统配置与优化 1. **重启系统**:完成安装后重启系统。 2. **用户设置**:建立新用户,并设置密码。 3. **...

    webMethods学习笔记

    - **集成服务器(Integration Server)**:作为ESB的核心,负责处理所有进出的消息流。 - **消息路由**:根据预定义的规则自动将消息路由到正确的目的地。 - **转换服务**:支持多种数据格式之间的转换,如XML到CSV...

    计算机英语词汇.doc

    12. **FTP (File Transfer Protocol)**: 文件传输协议,用于在网络上进行文件传输的标准协议。 13. **HTTP (Hypertext Transfer Protocol)**: 超文本传输协议,用于Web浏览器和服务器之间传输网页的标准协议。 14....

    计算机 英语 词汇 大全

    #### Adapter cards(适配卡) - **定义**:用来连接不同接口或扩展功能的硬件卡,如网络适配卡、显卡等。 - **应用场景**:在PC中扩展功能,如增加更多的USB接口或者提高图形处理能力。 #### Advanced application...

Global site tag (gtag.js) - Google Analytics