⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ftpstatistics.java

📁 Ftp服务1.0
💻 JAVA
字号:
package ranab.server.ftp;

import java.util.Date;
import java.io.File;
import ranab.util.Message;
import ranab.server.ftp.FtpUser;

/**
 * This class encapsulates all the global statistics.
 *
 * @author <a href="mailto:rana_b@yahoo.com">Rana Bhattacharyya</a>
 */
public 
class FtpStatistics {

    private FtpStatisticsListener mListener = null;
    private FtpFileListener mFileListener   = null;               
    private FtpConfig mConfig               = null;
    
    private Date mStartTime        = new Date();
    
    private int miNbrUpload        = 0;
    private int miNbrDownload      = 0;
    private int miNbrDelete        = 0;
    
    private int miLogins           = 0;
    private int miTotalLogins      = 0;
    
    private int miAnonLogins       = 0;
    private int miTotalAnonLogins  = 0;
    
    private int miConnections      = 0;
    private int miTotalConnections = 0;
    
    private long mlBytesUpload     = 0L;
    private long mlBytesDownload   = 0L;
    
    /**
     * Default constructor. 
     */
    public FtpStatistics(FtpConfig cfg) {
        mConfig = cfg;
    }
    
    
    /////////////////  All get methods  /////////////////
    /**
     * Get server start time.
     */
    public Date getStartTime() {
        return mStartTime;
    }
     
    /**
     * Get number of files uploaded.
     */
    public int getFileUploadNbr() {
        return miNbrUpload;
    }
    
    /**
     * Get number of files downloaded.
     */
    public int getFileDownloadNbr() {
        return miNbrDownload;
    }
    
    /**
     * Get number of files deleted.
     */
    public int getFileDeleteNbr() {
        return miNbrDelete;
    }
     
    /**
     * Get total number of bytes uploaded.
     */
    public long getFileUploadSize() {
        return mlBytesUpload;
    }
     
    /**
     * Get total number of bytes downloaded.
     */
    public long getFileDownloadSize() {
        return mlBytesDownload;
    }
    
    /**
     * Get current number of connections.
     */
    public int getConnectionNbr() {
        return miConnections;
    }
     
    /**
     * Get total connection number
     */ 
    public int getTotalConnectionNbr() {
        return miTotalConnections;
    } 
     
    /**
     * Get current number of logins
     */ 
    public int getLoginNbr() {
        return miLogins;
    } 
     
    /**
     * Get total number of logins
     */ 
    public int getTotalLoginNbr() {
        return miTotalLogins;
    } 
     
    /**
     * Get current anonymous logins.
     */
    public int getAnonLoginNbr() {
        return miAnonLogins;
    }
     
    /**
     * Get total anonymous logins
     */ 
    public int getTotalAnonLoginNbr() {
        return miTotalAnonLogins;
    } 
     
     
    /////////////////  All set methods  ///////////////////
    /**
     * Increment upload count.
     */
    void setUpload(File fl, FtpUser user, long sz) {
        ++miNbrUpload;
        mlBytesUpload += sz;
        mConfig.getLogger().info("File upload : " + user.getName() + " - " + fl.getAbsolutePath());
        notifyUpload(fl, user);
    }
     
    /**
     * Increment download count.
     */
    void setDownload(File fl, FtpUser user, long sz) {
        ++miNbrDownload;
        mlBytesDownload += sz;
        mConfig.getLogger().info("File download : " + user.getName() + " - " + fl.getAbsolutePath());
        notifyDownload(fl, user);
    }
     
    /**
     * Increment delete count.
     */
    void setDelete(File fl, FtpUser user) {
        ++miNbrDelete;
        mConfig.getLogger().info("File delete : " + user.getName() + " - " + fl.getAbsolutePath());
        notifyDelete(fl, user);
    }
     
    /**
     * New login.
     */
    void setLogin(boolean anonymous) {
        ++miLogins;
        ++miTotalLogins;
        if(anonymous) {
            ++miAnonLogins;
            ++miTotalAnonLogins;
        }
        notifyLogin();
    }
     
    /**
     * User logout
     */
    void setLogout(boolean anonymous) {
        --miLogins;
        if(anonymous) {
            --miAnonLogins;
        }
        notifyLogout();
    }
    
    /**
     * New connection
     */
    void setOpenConnection() {
        ++miConnections;
        ++miTotalConnections;
        notifyConnection();
    }
    
    /**
     * Close connection
     */
    void setCloseConnection() {
        --miConnections;
        notifyConnection();
    }
    
    ////////////////////////////////////////////////////////////
    //                Event listener methods                  // 
    ////////////////////////////////////////////////////////////
    /**
     * Add a listener object.
     */
    public void setListener(FtpStatisticsListener listener) {
        mListener = listener;
    }
    
    /**
     * Get listener object.
     */
    public FtpStatisticsListener getListener() {
        return mListener;
    }
     
    /**
     * Get file listener
     */ 
    public void setFileListener(FtpFileListener listener) {
        mFileListener = listener;
    } 
     
    /**
     * Set file listener
     */ 
    public FtpFileListener getFileListener() {
        return mFileListener;
    } 
     
    /**               
     * Listener upload notification.
     */
    private void notifyUpload(final File fl, final FtpUser user) {
        final FtpStatisticsListener listener = mListener;
        if (listener != null) {
            Message msg = new Message() {
                public void execute() {
                    listener.notifyUpload();
                }    
            };
            mConfig.getMessageQueue().add(msg);
        }

        final FtpFileListener fileListener = mFileListener;
        if (fileListener != null) {
            Message msg = new Message() {
                public void execute() {
                    fileListener.notifyUpload(fl, user);
                }    
            };
            mConfig.getMessageQueue().add(msg);
        }
    }
    
    /**
     * Listener download notification.
     */
    private void notifyDownload(final File fl, final FtpUser user) {
        final FtpStatisticsListener listener = mListener;
        if (listener != null) {
            Message msg = new Message() {
                public void execute() {
                    listener.notifyDownload();
                }    
            };
            mConfig.getMessageQueue().add(msg);
        }
    
        final FtpFileListener fileListener = mFileListener;
        if (fileListener != null) {
            Message msg = new Message() {
                public void execute() {
                    fileListener.notifyDownload(fl, user);
                }    
            };
            mConfig.getMessageQueue().add(msg);
        }
    } 
    
    /**
     * Listener delete notification.
     */
    private void notifyDelete(final File fl, final FtpUser user) {
        final FtpStatisticsListener listener = mListener;
        if (listener != null) {
            Message msg = new Message() {
                public void execute() {
                    listener.notifyDelete();
                }    
            };
            mConfig.getMessageQueue().add(msg);
        }
        
        final FtpFileListener fileListener = mFileListener;
        if (fileListener != null) {
            Message msg = new Message() {
                public void execute() {
                    fileListener.notifyDelete(fl, user);
                }    
            };
            mConfig.getMessageQueue().add(msg);
        }
    }
     
    /**
     * Listener user login notification.
     */
    private void notifyLogin() {
        final FtpStatisticsListener listener = mListener;
        if (listener != null) {
            Message msg = new Message() {
                public void execute() {
                    listener.notifyLogin();
                }
            };
            mConfig.getMessageQueue().add(msg);
        }
    }
    
    /**
     * Listener user logout notification.
     */
    private void notifyLogout() {
        final FtpStatisticsListener listener = mListener;
        if (listener != null) {
            Message msg = new Message() {
                public void execute() {
                    listener.notifyLogout();
                }
            };
            mConfig.getMessageQueue().add(msg);
        }
    } 
    
    /**
     * Listener user connection open/close notification.
     */
    private void notifyConnection() {
        final FtpStatisticsListener listener = mListener;
        if (listener != null) {
            Message msg = new Message() {
                public void execute() {
                    listener.notifyConnection();
                }
            };
            mConfig.getMessageQueue().add(msg);
        }
    } 
}    

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -