package com.nms.server.thread.file.upload;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Calendar;

import org.apache.log4j.Logger;

import com.nms.server.common.Constants;
import com.nms.server.util.ExceptionPrintUtils;
import com.nms.server.util.socket.SSLSocketCallable;

/**
 * XML解析线程
 * 
 * @author ZGGG3
 * 
 */
public class FileUploadThread3 extends SSLSocketCallable {
	Logger logger = Logger.getLogger(FileUploadThread3.class);
	private static Integer port = Constants.WEB_SOCKET_PORT;

	private String name; // 自定义Thread Name
	private File[] files;
	public FileUploadThread3(String name,String ip,File[] files) throws Exception {
		super(ip, port);
		this.name = name;
		this.files = files;
	}

	/*
	 * 线程操作
	 * 
	 * 依次解析urlList中仍存在的Files
	 * 
	 * 实现了依次解析Files，由于后期实现多线程解析操作
	 */
	protected Object toDo() throws Exception {
		// 为当前线程命名 ，用与开发阶段友好输出。
		Thread.currentThread().setName(name);
		
		try {
			
			//循环遍历目录子文件
				logger.debug("上传文件数量:>  "  + files.length + "个文件");
				
				
				long l = Calendar.getInstance().getTimeInMillis();
				if (files != null && files.length > 0) {
					
					sendMessage("byte:bpUploadFiles");
					String msg = receiveMessage();
					
					bpSendFileByBath(Arrays.asList(files), null);
					String params = this.receiveMessage();	//接收需要上传的文件列表
					System.out.println("bpSendFileByBath "+params);
					
				}
				
				for(File file :files){
					file.delete();
					logger.debug(file.getAbsoluteFile()+"上传完成，已删除！");
				}
			logger.debug("上传文件 "+files.length+"个 上传耗时："+(Calendar.getInstance().getTimeInMillis()-l)+" ms");
			logger.info("上传完毕！");
		} catch (Exception e) {
			logger.error(ExceptionPrintUtils.printExceptionStack(e));
		}
		finally{
//			dao.close();
		}
		return null;
	}

//	public static void main(String[] args) {
//		long l = Calendar.getInstance().getTimeInMillis();
//		try {
//			Future<?> future = Common.service.submit(new FileUploadThread3("文件上传线程", Constants.MISSION_FILE_UPLOAD_DIR));
//			future.get();
//			System.exit(0);
//		} catch (Exception e) {
//			e.printStackTrace();
//		}
//		System.out.println(Calendar.getInstance().getTimeInMillis()-l);
//	}
}
