This repository has been archived on 2025-09-14. You can view files and clone it, but cannot push or open issues or pull requests.
Files
nms-nmsserver/src/com/nms/server/thread/socket/NMSWebBPDownload.java

136 lines
3.9 KiB
Java
Raw Normal View History

2018-09-27 16:17:06 +08:00
package com.nms.server.thread.socket;
import java.util.List;
import java.util.concurrent.Callable;
import javax.net.ssl.SSLSocket;
import javax.net.ssl.SSLSocketFactory;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import com.nms.server.common.Constants;
import com.nms.server.util.ExceptionPrintUtils;
import com.nms.server.util.socket.SocketUtils;
import com.nms.server.util.socket.limiter.InputStreamLimiter;
import com.nms.server.util.socket.limiter.OutputStreamLimiter;
import com.socket.utils.FileComment;
/**
* 安全通讯的客户端
*/
public class NMSWebBPDownload extends SocketUtils implements Callable<Object>{
private static Logger logger = Logger.getLogger(NMSWebBPDownload.class);
private String cmd = "byte:bpDownloadFile";
private String downDir=null;
private final List<FileComment> fileList; //
public static final String TYPE_MISSION = "1";
public static final String TYPE_SNMP_CLASS = "2";
public static final String TYPE_SNMP_MIB = "3";
/**
*
* @param fileList 需要下载的文件的 信息结构 String[]{fileName,start,end,MD5}
* @param downDir 保存路径 文件的保存路径
* @param downType 下载类型 统一下载接口的子分类 用于确认 服务端配置文件任务文件等非同一目录下保存的文件
* @throws Exception
*/
public NMSWebBPDownload(String IP,Integer PORT,List<FileComment> fileList,String downDir,String downType) throws Exception{
super(IP,PORT);
this.fileList = fileList;
this.downDir = downDir;
cmd +=":"+downType;
}
/**
* 通讯线程执行方法 默认格式
* @return
* @throws Exception
*/
public Object call(){
// Thread.currentThread().setName("任务文件下载线程");
Thread.currentThread().setName("Task File Download Thread");
Object obj = null; //返回对象
//- 校验 是否创建新通讯连接
if(socket==null && (StringUtils.isNotEmpty(ip) && port != null)){
try {
//-- create SocketFactory
SSLSocketFactory ssf = sSLContext.getSocketFactory();
//-- create socket
socket=(SSLSocket)ssf.createSocket(ip,port);
logger.debug("create socket success.");
//2014-1-23 hyx 如果建立socket成功但是startHandshake握手失败且未设置超时时间时则会一直阻塞
socket.setSoTimeout(Constants.SSL_SO_TIMEOUT*1000);
//-- handshake 握手
((SSLSocket) socket).startHandshake();
logger.debug("handshake success.");
} catch (Exception e) {
logger.warn("Target communication:>"+ip+":"+port+" create failure",e);
2018-09-27 16:17:06 +08:00
close();
return -2;
}
}
try {
//-- 获取通讯IO流
outl = new OutputStreamLimiter(socket.getOutputStream());
inl = new InputStreamLimiter(socket.getInputStream());
//增加通讯超时设置
socket.setSoTimeout(Constants.SSL_SO_TIMEOUT*1000);
// logger.info("通信超时时间:"+Constants.SSL_SO_TIMEOUT*1000);
//-- 自定义通讯操作
obj = toDo();
}catch (Exception e) {
logger.error(ExceptionPrintUtils.printExceptionStack(e));
obj = -1;
} finally {
close();
logger.info("--- 通信关闭 ---");
}
return obj;
}
/*
* 返回值描述
* 0 下载成功
* 1 下载失败
* -1 下载失败 可再次尝试
* -2 下载失败 且无需再次尝试
* -3 待下载文件不存在
*/
public Object toDo() throws Exception {
// Thread.currentThread().setName("任务文件下载线程");
Thread.currentThread().setName("Task file download thread");
try {
//存在未下载的文件 开始通讯
if(fileList.size()>0){
//- 通讯命令
sendMessage(cmd);
String str = receiveMessage();
//- 发送下载文件名称
sendObject(fileList);
//- 接收文件长度
return bpReceiveFileByBathMd5(downDir);
}
return 0;
} catch (Exception e) {
logger.error(ExceptionPrintUtils.printExceptionStack(e));
return -1;
}
}
}