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

📄 crbtclient.java

📁 中国联通炫铃业务接口开发
💻 JAVA
字号:
package com.wireless.crbt.gwif.netty2;

import java.io.IOException;
import java.net.InetSocketAddress;

import net.gleamynode.netty2.IoProcessor;
import net.gleamynode.netty2.MessageRecognizer;
import net.gleamynode.netty2.OrderedEventDispatcher;
import net.gleamynode.netty2.Session;
import net.gleamynode.netty2.ThreadPooledEventDispatcher;

import com.wireless.gwif.socketconn.ByteMessageRecognizer;
import com.wireless.gwif.socketconn.ConnManageInterface;

public class CrbtClient implements ConnManageInterface{
	 private String serverHost;
	    private int serverPort;
	    private String localHost;
	    private int localPort;
	    private int CONNECT_TIMEOUT = 180; // seconds
	    private int IDLE_TIME = 15;       // seconds
	    private int DISPATCHER_THREAD_POOL_SIZE = 4;
//	    private int IOPROCESSOR_THREAD_POOL_SIZE = 4;
	    private IoProcessor ioProcessor = null;
	    private ThreadPooledEventDispatcher eventDispatcher = null;
	    private ClientDefaultCrbtConnTemplet templet = null;
	    private Session session = null;
	    private MessageRecognizer recognizer = null;
	    private boolean startFlag = false;

	    public CrbtClient(String host, int port){
	        this.serverHost = host;
	        this.serverPort = port;
	    }

	    public CrbtClient(String host, int port, String localHost, int localPort){
	        this.serverHost = host;
	        this.serverPort = port;
	        this.localHost = localHost;
	        this.localPort = localPort;
	    }
	    public void registerConnTemplet(ClientDefaultCrbtConnTemplet templet){
	        this.templet = templet;
	    }

	    public ClientDefaultCrbtConnTemplet getConnTemplet(){
	      return this.templet;
	    }

	    public void registerMessageRecognizer(MessageRecognizer recognizer){
	        this.recognizer = recognizer;
	    }

	    public void setIDLE_TIME(int spanSend){
	        IDLE_TIME = spanSend;
	    }

	    public void setDispatcherThreadPoolSize(int size){
	      DISPATCHER_THREAD_POOL_SIZE = size;
	    }

//	    public void setIoProcessorThreadPoolSize(int size){
//	      IOPROCESSOR_THREAD_POOL_SIZE = size;
//	    }

	    public void start() {

	        if( startFlag ){
	          return;
	        }

	        if( this.templet == null )
	            throw new IllegalStateException("You must register a Connection Templet (ClientDefaultConnTemplet)");

	        if( this.IDLE_TIME < 0 )
	          throw new IllegalStateException("idle time error for input " + IDLE_TIME);

	        // initialize I/O processor and event dispatcher
	        ioProcessor = new IoProcessor();
	        eventDispatcher = new OrderedEventDispatcher();

	        // start with the default number of I/O worker threads
	        try {
	            ioProcessor.start();
	        } catch (IOException e) {
	            throw new IllegalStateException("The method start of the IOProcessor not ready to execute.\n" + e.getMessage());
	        }

	        // start with a few event dispatcher threads
	        eventDispatcher.setThreadPoolSize(DISPATCHER_THREAD_POOL_SIZE);
	        eventDispatcher.start();

	        // prepare message recognizer
	        if( recognizer == null )
	          recognizer = new ByteMessageRecognizer();

	        // create a client session
	        session = null;
	        if( this.localHost != null && (this.localHost.split("[.]").length==4) && this.localPort != 0){
	          session = new Session(ioProcessor,
	                                new InetSocketAddress(serverHost, serverPort),
	                                new InetSocketAddress(localHost, localPort),
	                                recognizer, eventDispatcher);
	        }
	        else{
	          session = new Session(ioProcessor,
	                                new InetSocketAddress(serverHost, serverPort),
	                                recognizer, eventDispatcher);
	        }

	        // set configuration
	        session.getConfig().setConnectTimeout(CONNECT_TIMEOUT);

	        // set idle time (use to send active test package)
	        session.getConfig().setIdleTime(IDLE_TIME);

	        templet.initialize();
	        // suscribe and start communication
	        session.addSessionListener(templet);
	        templet.setSession(session);

//	        System.out.println("Connecting to " + session.getSocketAddress());
	        session.start();

	        startFlag = true;
	    }

	    public void close(){
	      if( this.session != null && !this.session.isClosed()){
	        this.session.close();
	      }
	    }

	    public void restart(){
	      if( this.session != null && this.session.isClosed()){
	        this.session.start();
	      }
	    }

	    public boolean isFinishStop() {
	        return !startFlag;
	    }

	    public void stop() {
	        templet.setIdleFlag(false);
	        templet.stopSendT();

	        if( session != null && !session.isClosed() ){
	          try {
	              templet.processSendTermate();
	              Thread.sleep(100);
	              session.close();
	              Thread.sleep(100);
	          } catch (Exception ex) {
	          }
	        }

	        templet.finalize();
	        // stop I/O processor and event dispatcher
	        if( eventDispatcher != null ){
	          eventDispatcher.stop();
	          eventDispatcher = null;
	        }

	        if( ioProcessor != null ){
	          ioProcessor.stop();
	          ioProcessor = null;
	        }

	        startFlag = false;
	    }
}

⌨️ 快捷键说明

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