ServerTransportmodel

字號:

介紹
    Server Transport 即傳輸層,是建立在通信信道建立成功和傳輸指令交互成功的基礎上的一層用于傳送數(shù)據(jù)的層,類似TCP、UDP,它的主要的作用就是輸送我們定義的數(shù)據(jù),數(shù)據(jù)格式的數(shù)據(jù)結構則可以自己進行約定,如字符流、字節(jié)流等,也可以為XML或其他數(shù)據(jù)結構實現(xiàn)的數(shù)據(jù)格式。
    在Transport建立過程中,Server與Client端需要持有一些數(shù)據(jù)成員,如對方的信任信息、連接地址等socket參數(shù)、操作柄的代理工廠。
    這樣建立Transport后則可以實現(xiàn)數(shù)據(jù)傳輸、指令下達、報告提交等功能。
    部分代碼
    package thread;
    import java.util.concurrent.LinkedBlockingQueue;
    import java.util.concurrent.ThreadPoolExecutor;
    import java.util.concurrent.TimeUnit;
    import org.apache.commons.logging.Log;
    import org.apache.commons.logging.LogFactory;
    /**
    * This ServerTransport is work in multi thread env
    *
    *
    */
    public class ServerTransport {
    private static final Log _log = LogFactory.getLog(ServerTransport.class);
    private final Object _lock = new Object();
    private boolean _readyFlag = false;
    private boolean _stopFlag;
    private final AsynchronousCaller _asyncCaller;
    /**
    * Initialization AsynchronousCaller
    * @param asyncThreadPoolSize
    */
    public ServerTransport(int asyncThreadPoolSize) {
    if(asyncThreadPoolSize<0){
    throw new IllegalArgumentException("illegal Server Transport poolSize: "+asyncThreadPoolSize);
    }
    _asyncCaller = new AsynchronousCaller(asyncThreadPoolSize);
    //Do some other things about each transport!
    }
    /*
    * Operate Handler
    */
    //check ready
    public boolean isReady(){
    synchronized(_lock){
    return _readyFlag;
    }
    }
    //start
    public void start() throws Exception {
    if (isStopped()) {
    return;
    }
    _asyncCaller.start();
    setReady(true);
    }
    //stop
    public void stop() {
    _asyncCaller.stop();
    setReady(false);
    setStop(false);
    }
    //set ready
    private void setReady(boolean b) {
    synchronized(_lock){
    _readyFlag = b;
    }
    }
    //set ready
    private void setStop(boolean b) {
    synchronized(_lock){
    _stopFlag = b;
    }
    }