package com.nms.server.util.socket;

import java.io.IOException;
import java.net.Socket;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.Calendar;
import java.util.concurrent.Callable;
import javax.net.ssl.SSLServerSocket;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import com.nms.server.bean.DetectInfo;
import com.nms.server.common.Common;
import com.nms.server.common.Constants;
import com.nms.server.thread.monitor.util.MonitorUtil;
import com.nms.server.util.ExceptionPrintUtils;

/**
 * 	SSL 通讯 工具类
 *  @date Feb 29, 2012 10:05:50 AM
 *	@author ZhangGang
 *
 */
public abstract class SSLSocketCallableForDateCollection extends SocketUtils implements Callable<Object>{
	
	private static Logger logger = Logger.getLogger(SSLSocketCallable.class);
	//ServerSocket 通讯服务
	protected SSLServerSocket ss = null;
	
    /**
     * 通讯创建
	 * @param ip		目标主机IP
	 * @param port		目标主机端口
	 * @throws Exception
	 */
	public SSLSocketCallableForDateCollection(String ip,Integer port){
		super(ip, port);
	}
	
    /**
     * 通讯创建
     * @param client	目标通讯实例
	 */
    public SSLSocketCallableForDateCollection(Socket client) {
    	super(client);
    }
	
	/**
	 * 通讯线程执行方法 默认格式
	 * @return
	 * @throws Exception
	 */
	public Object call(){
		Thread.currentThread().setName("Create communication"+ip+":"+port);
		Object obj = null;		//返回对象
		Long checkTime = System.currentTimeMillis();	//用于产生数据中的开始时间
		try {	
			//- 校验 是否创建新通讯连接
			if(socket==null){	//客户端通讯创建
				createClientSocket();
			}else{	//服务端通讯创建
				createServerSocket();
			}
			
		} catch (SocketException e) {		
			logger.error("Create communication exception", e);
			Common.addErrorInfo(Constants.ERROR_CODE_CREATE_SOCKET,ip, Calendar.getInstance().getTime(), Constants.ERROR_INFO_SATAE_ERROR,"");
//			obj = errorNodeToDo(checkTime);
			close();
			return obj;
		} catch (IOException e) {
			logger.error("Create communication exception", e);
			close();
//			obj = errorNodeToDo(checkTime);
			return obj;
		}catch (InterruptedException e) {
			logger.error("Create communication exception,thread is interrupted",e);//线程被中断，不再进行握手失败具体原因的判断
			return null;
		} catch (Exception e) {
			logger.error("Create communication exception", e);
			close();
//			obj = errorNodeToDo(checkTime);
			return obj;
		}
		
		//-- 自定义通讯操作
		try {
			obj = toDo();
		}catch (Exception e) {
			logger.error("Communicate content exception"+e.getMessage(),e);
			Common.addErrorInfo(Constants.ERROR_CODE_SOCKET_RUNTIME,ip, Calendar.getInstance().getTime(), Constants.ERROR_INFO_SATAE_ERROR,"");
		} finally {
			close();
		}

		return obj;
	}
	
//	//创建握手监测信息
//	protected abstract void createNmsClientDetectData(DetectInfo  detectInfo);

	
	/**
	 * 待实现的通信操作
	 * @time Aug 28, 2011-9:04:46 PM
	 * @param outl
	 * @param inl
	 * @throws Exception
	 */

	protected abstract Object toDo()throws Exception;
	
	/**
	 * 节点连接出现异常
	 * @return
	 * @throws Exception
	 */
//	protected abstract Object errorNodeToDo(Long checkTime);
	
	
}
