103 lines
2.4 KiB
Java
103 lines
2.4 KiB
Java
package com.nms.server.util.socket;
|
||
|
||
import java.io.IOException;
|
||
import java.net.Socket;
|
||
import java.net.SocketException;
|
||
import java.util.Calendar;
|
||
import java.util.concurrent.Callable;
|
||
|
||
import javax.net.ssl.SSLServerSocket;
|
||
|
||
import org.apache.log4j.Logger;
|
||
|
||
import com.nms.server.common.Common;
|
||
import com.nms.server.common.Constants;
|
||
|
||
/**
|
||
* SSL 通讯 工具类
|
||
* @date Feb 29, 2012 10:05:50 AM
|
||
* @author ZhangGang
|
||
*
|
||
*/
|
||
public abstract class SSLSocketCallable extends SocketUtils implements Callable<Object>{
|
||
|
||
private static Logger logger = Logger.getLogger(SSLSocketCallable.class);
|
||
//ServerSocket 通讯服务
|
||
protected SSLServerSocket ss = null;
|
||
private boolean clientFlag = false;// 是否dc server 接收的client
|
||
/**
|
||
* 通讯创建
|
||
* @param ip 目标主机IP
|
||
* @param port 目标主机端口
|
||
* @throws Exception
|
||
*/
|
||
public SSLSocketCallable(String ip,Integer port){
|
||
super(ip, port);
|
||
}
|
||
|
||
/**
|
||
* 通讯创建
|
||
* @param client 目标通讯实例
|
||
*/
|
||
public SSLSocketCallable(Socket client) {
|
||
super(client);
|
||
clientFlag = true;
|
||
}
|
||
|
||
/**
|
||
* 通讯线程执行方法 默认格式
|
||
* @return
|
||
* @throws Exception
|
||
*/
|
||
public Object call(){
|
||
Thread.currentThread().setName("Create communication"+ip+":"+port);
|
||
Object obj = null; //返回对象
|
||
try {
|
||
try {
|
||
//- 校验 是否创建新通讯连接
|
||
if(socket==null){ //客户端通讯创建
|
||
createClientSocket();
|
||
}else{ //服务端通讯创建
|
||
createServerSocket();
|
||
}
|
||
|
||
} catch (SocketException e) {
|
||
logger.error("Create failed:"+e.getMessage(),e);
|
||
Common.addErrorInfo(Constants.ERROR_CODE_CREATE_SOCKET,ip, Calendar.getInstance().getTime(), Constants.ERROR_INFO_SATAE_ERROR,"");
|
||
close();
|
||
return obj;
|
||
} catch (Exception e) {
|
||
close();
|
||
return obj;
|
||
}
|
||
|
||
//-- 自定义通讯操作
|
||
try {
|
||
obj = toDo();
|
||
}catch (Exception e) {
|
||
logger.error("Communicate contents exception"+e.getMessage(),e);
|
||
Common.addErrorInfo(Constants.ERROR_CODE_SOCKET_RUNTIME,ip, Calendar.getInstance().getTime(), Constants.ERROR_INFO_SATAE_ERROR,"");
|
||
} finally {
|
||
close();
|
||
}
|
||
} finally {
|
||
if(clientFlag){
|
||
Common.releaseServerSemaphore();//释放server信号量
|
||
}
|
||
}
|
||
return obj;
|
||
}
|
||
|
||
/**
|
||
* 待实现的通信操作
|
||
* @time Aug 28, 2011-9:04:46 PM
|
||
* @param outl
|
||
* @param inl
|
||
* @throws Exception
|
||
*/
|
||
|
||
protected abstract Object toDo()throws Exception;
|
||
|
||
|
||
}
|