package com.dy.pmsTest.tcpClient; 
 | 
  
 | 
import com.dy.common.util.Callback; 
 | 
import com.dy.pmsTest.common.UnitAdapterInterface; 
 | 
import com.dy.pmsTest.common.UnitInterface; 
 | 
import com.dy.pmsTest.common.UnitStartedCallbackInterface; 
 | 
import org.apache.logging.log4j.LogManager; 
 | 
import org.apache.logging.log4j.Logger; 
 | 
import org.apache.mina.core.session.IoSession; 
 | 
  
 | 
public class TcpClUnit implements UnitInterface { 
 | 
  
 | 
    private static final Logger log = LogManager.getLogger(TcpClUnit.class) ; 
 | 
  
 | 
    private static TcpClUnit instance = new TcpClUnit() ; 
 | 
  
 | 
    public static TcpClUnitAdapter adapter ; 
 | 
    public static TcpClUnitConfigVo confVo ; 
 | 
  
 | 
    public static IoSession session ; 
 | 
  
 | 
    private TcpClUnit(){} ; 
 | 
  
 | 
    public static TcpClUnit getInstance(){ 
 | 
        return instance ; 
 | 
    } 
 | 
  
 | 
    @Override 
 | 
    public void setAdapter(UnitAdapterInterface adapter) throws Exception { 
 | 
        if(adapter == null){ 
 | 
            throw new Exception("Tcp Client模块适配器对象不能为空!") ; 
 | 
        } 
 | 
        TcpClUnit.adapter = (TcpClUnitAdapter)adapter ; 
 | 
        TcpClUnit.confVo = TcpClUnit.adapter.getConfig() ; 
 | 
        if(TcpClUnit.confVo == null){ 
 | 
            throw new Exception("Tcp Client模块配置对象不能为空!") ; 
 | 
        } 
 | 
    } 
 | 
  
 | 
    @Override 
 | 
    public void start(UnitStartedCallbackInterface callback) throws Exception { 
 | 
        System.out.println("Tcp Client starting"); 
 | 
        this.doStart(); 
 | 
        callback.call(null) ; 
 | 
    } 
 | 
  
 | 
    @Override 
 | 
    public void stop(UnitStartedCallbackInterface callback) throws Exception { 
 | 
        callback.call(null); 
 | 
    } 
 | 
  
 | 
    private void doStart(){ 
 | 
        new Thread(new Runnable(){ 
 | 
            @Override 
 | 
            public void run() { 
 | 
                Exception ex = null ; 
 | 
                while(true){ 
 | 
                    ex = null ; 
 | 
                    try { 
 | 
                        new TcpConnect().createSession( 
 | 
                                confVo.mwServerIp, 
 | 
                                confVo.mwServerPort, 
 | 
                                confVo.connectTimeout, 
 | 
                                new TcpHandler(), 
 | 
                                new Callback() { 
 | 
                                    @Override 
 | 
                                    public void call(Object obj) { 
 | 
                                        if (obj == null) { 
 | 
                                            log.error("创建网络会话返回为null"); 
 | 
                                        } else { 
 | 
                                            TcpClUnit.session = (IoSession) obj; 
 | 
                                            log.info("成功创建与网串中间件的TCP连接"); 
 | 
                                        } 
 | 
                                    } 
 | 
  
 | 
                                    @Override 
 | 
                                    public void call(Object... objs) { 
 | 
                                    } 
 | 
  
 | 
                                    @Override 
 | 
                                    public void exception(Exception e) { 
 | 
                                    } 
 | 
                                }); 
 | 
                    }catch (Exception e){ 
 | 
                       ex = e ; 
 | 
                    } 
 | 
                    if(ex == null){ 
 | 
                        break ; 
 | 
                    }else{ 
 | 
                        try{ 
 | 
                            Thread.sleep(100); 
 | 
                        }catch (Exception e){ 
 | 
                        } 
 | 
                    } 
 | 
                } 
 | 
            } 
 | 
        }).start(); 
 | 
    } 
 | 
  
 | 
} 
 |