ftpserver中的业务逻辑同样封装在FtpHandler中,类似于MINA中的IoHandler,可以看他的源代码:
FtpHandler 写道
public interface FtpHandler {
void init(FtpServerContext context, Listener listener) throws Exception;
void sessionCreated(FtpIoSession session) throws Exception;
void sessionOpened(FtpIoSession session) throws Exception;
void sessionIdle(FtpIoSession session, IdleStatus status) throws Exception;
void exceptionCaught(FtpIoSession session, Throwable cause)
throws Exception;
void messageReceived(FtpIoSession session, FtpRequest request)
throws Exception;
void messageSent(FtpIoSession session, FtpReply reply) throws Exception;
}
void init(FtpServerContext context, Listener listener) throws Exception;
void sessionCreated(FtpIoSession session) throws Exception;
void sessionOpened(FtpIoSession session) throws Exception;
void sessionIdle(FtpIoSession session, IdleStatus status) throws Exception;
void exceptionCaught(FtpIoSession session, Throwable cause)
throws Exception;
void messageReceived(FtpIoSession session, FtpRequest request)
throws Exception;
void messageSent(FtpIoSession session, FtpReply reply) throws Exception;
}
可以看到,FtpHandler的接口定义几乎和IoHandler一模一样,同样的sessionCreated等方法,调用的模式也是和IoHandler一样的。而和MINA所要求的IoHandler的交互就是需要FtpHandlerAdapter,将底层的消息等封装成FtpIoSession和FtpRequest。具体实现参见FtpHandlerAdapter。其实FtpHandlerAdapter什么都没做,只是单纯的new一个FtpIoSession或FtpRequest。
具体的ftp协议的解析,就是在DefaultFtpHandler#public void messageReceived(final FtpIoSession session, final FtpRequest request) throws Exception 中实现,
写道
String commandName = request.getCommand();
CommandFactory commandFactory = context.getCommandFactory();
Command command = commandFactory.getCommand(commandName);
CommandFactory commandFactory = context.getCommandFactory();
Command command = commandFactory.getCommand(commandName);
实现ftp命令的解析,好简单啊,比其他协议简单多了
然后就是从session中查找用于是否登录,类似于http中的session,不再废话