📄 taskinfo.java
字号:
package com.sinpool.rivercrescent.Task;
import net.jxta.peergroup.*;
import java.io.*;
import java.util.*;
import javax.swing.*;
import javax.swing.table.*;
import net.jxta.pipe.PipeService;
import net.jxta.peergroup.*;
import net.jxta.protocol.*;
import net.jxta.discovery.DiscoveryService;
import com.sinpool.rivercrescent.*;
import com.sinpool.rivercrescent.io.*;
import com.sinpool.rivercrescent.myutil.MyUtil;
public class TaskInfo implements Serializable{
//不用串行化的属性
private transient PeerGroup currentGroup = null;
private transient PipeService taskPipes = null;
private transient PipeAdvertisement taskPipeadv = null;
//需要读取对象时,初始化
private String taskPercentage = null;
private String taskDownloadSpeed = null; //需要作为过渡属性
private String taskUploadSpeed = null; //需要作为过渡属性
private String riverscentFileName = null;
private String taskType = null; //任务类型(join or publish)
private String taskFileName = null;
private String taskGroupName = null;
private String taskFileLength = null;
private String taskFinishedLength = null;
private String taskUsedTime = null;
private String taskRequireTime = null;
private String taskPieceLength = null;
private String taskRiverscentFileName = null;
private String taskRiverFileType = null;
private String taskGroupID = null;
private String taskPipeID = null;
private Hashtable dictionary = null;
private Vector filePieceQueue = new Vector(10,5); //每个文件的累加和
private boolean[] finishedPieces;
private int taskIndex; //任务索引号
private transient JTable taskTable;
private transient DefaultTableModel taskStatusTableModel;
/** "创建"任务的构造函数,<br>
*
*@param currentGroup 被创建组,或加入组的对象,<br>
* 是一个PeerGroup对象。<br>
*
*@param riverscentFileName 是此任务元文件的文件名,<br>
* 用来创建TaskInfo对象的属性。<br>
*
*@param FileAbsolutePath<br>
* 创建共享文件时,手动设置此参数<br>
* 单文件时:源文件具体文件名;多文件时:文件夹顶级目录<br>
*<br>
* 或者是join的文件,这时不用设置此参数,使用另一个构造函数<br>
* 在初始化时,自动设置。<br>
* 单文件时:保存文件的目的文件具体文件名。<br>
* 多文件时:保存文件的目的文件顶级文件夹名(包括 name )。<br>
*
*/
public TaskInfo(PeerGroup currentGroup,String riverscentFileName,String FileAbsolutePath,
PipeService ps,PipeAdvertisement padv,String type){
this.currentGroup = currentGroup;
this.riverscentFileName = riverscentFileName;
this.taskFileName = FileAbsolutePath;
this.taskPipes = ps;
this.taskPipeadv = padv;
this.taskType = type;
this.taskRiverscentFileName = riverscentFileName;
this.taskPipeID = padv.getPipeID().toString();
this.initAllProperties();
}
/**给JoinedDownloadManager使用的构造函数,
*只有PeerGroup参数,
*其他两个参数,随后再设置。
*
*@param pg 当前任务所在的组
*
*/
public TaskInfo(PeerGroup currentGroup,PipeService ps,PipeAdvertisement padv,String type,JTable taskTable){
this.currentGroup = currentGroup;
this.taskPipes = ps;
this.taskPipeadv = padv;
this.taskType = type;
this.taskTable = taskTable;
this.taskGroupName = currentGroup.getPeerGroupName();
this.taskGroupID = MyUtil.getGroupID_34(currentGroup.getPeerGroupID().toString());
this.taskPipeID = padv.getPipeID().toString();
}
/** "创建"任务的构造函数,<br>
*
*@param currentGroup 被创建组,或加入组的对象,<br>
* 是一个PeerGroup对象。<br>
*
*@param riverscentFileName 是此任务元文件的文件名,<br>
* 用来创建TaskInfo对象的属性。<br>
*
*@param FileAbsolutePath<br>
* 创建共享文件时,手动设置此参数<br>
* 单文件时:源文件具体文件名;多文件时:文件夹顶级目录<br>
*<br>
* 或者是join的文件,这时不用设置此参数,使用另一个构造函数<br>
* 在初始化时,自动设置。<br>
* 单文件时:保存文件的目的文件具体文件名。<br>
* 多文件时:保存文件的目的文件顶级文件夹名(包括 name )。<br>
*
*/
public TaskInfo(PeerGroup currentGroup,String riverscentFileName,String FileAbsolutePath,
PipeService ps,PipeAdvertisement padv,String type,JTable taskTable){
this(currentGroup,riverscentFileName,FileAbsolutePath,ps,padv,type);
this.taskTable = taskTable;
}
/**将taskInfo写入磁盘,<br>
*
*@return 一个boolean型,<br>
* 指出此操作是否成功。<br>
*/
public boolean taskInfoToDisk(){
String fileName = "taskobj\\" + this.getTaskGroupID();
try{
File file = new File(fileName);
File f = new File("taskobj");
f.mkdir() ;
FileOutputStream fos = new FileOutputStream(file);
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(this);
oos.flush();
oos.close();
fos.close();
}catch (IOException ioe) {ioe.printStackTrace();System.out.println("将taskinfo写入磁盘时,出错。");return false;}
return true;
}
/**从磁盘中读取一个taskInfo对象。<br>
* 此方法为static方法,
* 用来在非第一次启动时生成一个对象。<br>
*
*@param fileName 指出所读取的taskInfo对象的位置,<br>
* 包括"taskobj\\" + 此任务(PeerGroup对象).getPeerGroupID().toString()<br>
*
*@return 一个Object对象,<br>
* 可以转换成其他对象,<br>
* 这里要转换成TaskInfo对象。<br>
*/
public static TaskInfo taskInfoFromDisk(DiscoveryService ds , PeerGroup pg , String fileName){
TaskInfo result = null;
PeerGroupAdvertisement tempAdv = null;
try{
File file = new File("taskobj\\" + fileName);
FileInputStream fis = new FileInputStream(file);
ObjectInputStream ois = new ObjectInputStream(fis);
result = (TaskInfo)ois.readObject();
ois.close();
fis.close();
}catch (IOException ioe) {ioe.printStackTrace();System.out.println("出现IO错误!");return null;}
catch (ClassNotFoundException cfe) {System.out.println("从流中检索的对象没有找到所属的类!");return null;}
//初始化组信息,只能先初始化PeerGroup,其他的元素(PipeAdv和PipeService)只能在RiverScresentFrame中
//初始化了,因为这个从磁盘读信息的方法是静态的,不能使用TaskInfo对象的方法。所以,只能
//RiverScresentFrame对象里,使用set方法设置其他元素(PipeAdv和PipeService)。
Enumeration group_ae = null ;
for ( int i=0;i<3;i++){
try{
group_ae = ds.getLocalAdvertisements(DiscoveryService.GROUP , "GID" , "urn:jxta:uuid-" + result.getTaskGroupID() );
if ( (group_ae != null) && group_ae.hasMoreElements() ){
tempAdv = (PeerGroupAdvertisement) group_ae.nextElement();
result.currentGroup = pg.newGroup(tempAdv);
break;
}
Thread.sleep(300);
}
catch (Exception e) {
System.out.println("TaskInfo 类中读入的对象 创建currentGroup时出错。");
}
}
if ( tempAdv == null )
System.out.println("在缓存中没有发现组ID是" + "urn:jxta:uuid-" + result.getTaskGroupID() + "的组。");
result.setTaskDownloadSpeed("0");
result.setTaskUploadSpeed("0");
return result;
}
public boolean initJoinedProperties(String pgid){
String filename = pgid + ".river";
DecoderBencoding db = new DecoderBencoding();
try{
db.openFile(filename);
}catch (IOException ioe) {System.out.println("在\\riverscent文件夹没有找到"+riverscentFileName+"元文件!");return false;}
try{
db.decoderBencoding();
}catch (IOException ioe) {System.out.println("对元文件解码过程出现异常!");return false;}
dictionary = (Hashtable)db.getDictionary();
this.taskPieceLength = String.valueOf(((Integer)dictionary.get("piece length")).intValue());
//单文件时
if ( ((String)dictionary.get("type")).equals("single") ){
this.taskFileLength = String.valueOf(((Integer)dictionary.get("length")).intValue());
}
//多文件时
else{
int pieceNumTotal = 0,pl;
int length_index = ((Integer)dictionary.get("length_index")).intValue(),eachFileLength = 0;
long totalFileLength = 0;
for (int i=1;i<=length_index;i++){
eachFileLength = ((Integer)dictionary.get("length"+ i)).intValue();
totalFileLength += eachFileLength;
pl = Integer.valueOf(this.getTaskPieceLength()).intValue();
pieceNumTotal += ((eachFileLength%pl == 0) ?eachFileLength/pl:(eachFileLength/pl + 1));
Integer pnt = new Integer(pieceNumTotal);
filePieceQueue.addElement( pnt );
}
this.taskFileLength = String.valueOf(totalFileLength);
}
//重新设置river元文件的名字。
String riverFileName_qian = "riverfile/" + filename;
String riverFileName_hou = "riverfile/" + (String)dictionary.get("name") + ".river";
File riverFile_qian = new File(riverFileName_qian);
File riverFile_hou = new File(riverFileName_hou);
boolean b = riverFile_qian.renameTo(riverFile_hou);
//设置river元文件名字。
this.taskRiverscentFileName = riverFile_hou.getName();
this.taskRiverFileType = (String)dictionary.get("type");
this.taskGroupName = currentGroup.getPeerGroupName();
this.taskFinishedLength = "0";
this.taskPercentage = "0%";
this.taskDownloadSpeed = "0";
this.taskUploadSpeed = "0";
this.taskUsedTime = "0";
this.taskRequireTime = "0";
//初始化已经下载的文件块
int a = ((Integer)dictionary.get("pieces")).intValue()/20;
//将第0块的下载情况也加入这个数组。即,元文件的下载情况。
//第0个块,即元文件此时已经下载完了,所以从1开始初始化。
a += 1;
finishedPieces = new boolean[a];
finishedPieces[0] = true;
for (int i=1;i<a;i++){
finishedPieces[i] = false;
}
//根据单、多文件,出现保存对话框
if ( !this.saveFile() ) {return false;}
//this.setTaskFileName("D:/javawork/TEMP/BetBeans/bysj/"+ (String)dictionary.get("name"));
try {if ( !this.createDir() ) return false; } catch (IOException ioe){ System.out.println("TaskInfo 303 创建保存文件夹时出现IO错误!");}
return true;
}
private void initAllProperties(){
DecoderBencoding db = new DecoderBencoding();
try{
db.openFile(riverscentFileName);
}catch (IOException ioe) {System.out.println("在\\riverscent文件夹没有找到"+riverscentFileName+"元文件!");return;}
try{
db.decoderBencoding();
}catch (IOException ioe) {System.out.println("对元文件解码过程出现异常!");return;}
dictionary = (Hashtable)db.getDictionary();
this.taskPieceLength = String.valueOf(((Integer)dictionary.get("piece length")).intValue());
//单文件时
if ( ((String)dictionary.get("type")).equals("single") ){
this.taskFileLength = String.valueOf(((Integer)dictionary.get("length")).intValue());
}
//多文件时
else{
int pieceNumTotal = 0,pl;
int length_index = ((Integer)dictionary.get("length_index")).intValue(),eachFileLength = 0;
long totalFileLength = 0;
for (int i=1;i<=length_index;i++){
eachFileLength = ((Integer)dictionary.get("length"+i)).intValue();
totalFileLength += eachFileLength;
pl = Integer.valueOf(this.getTaskPieceLength()).intValue();
pieceNumTotal += ((eachFileLength%pl == 0) ?eachFileLength/pl:(eachFileLength/pl + 1));
Integer pnt = new Integer(pieceNumTotal);
filePieceQueue.addElement( pnt );
}
this.taskFileLength = String.valueOf(totalFileLength);
}
this.taskGroupID = MyUtil.getGroupID_34(currentGroup.getPeerGroupID().toString());
this.taskRiverFileType = (String)dictionary.get("type");
this.taskGroupName = currentGroup.getPeerGroupName();
this.taskFinishedLength = this.taskFileLength;
this.taskPercentage = "100%";
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -