package getFilePkg.download;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import getFilePkg.download.constant.DownloadInfoFile;
import getFilePkg.download.constant.Task;
import getFilePkg.file.DownloadStatus;
import getFilePkg.net.Protocol;
import getFilePkg.net.applicationprotocol.http.ApplicationProtocol;
import getFilePkg.net.applicationprotocol.http.HTTPHandler;
/**
* This class is responsible for creating as well as resuming connection
* threads.
* @author biplap
*
*/
public class ConnectionThread implements Runnable{
private DownloadStat downloadManager;
private int task;
private String filename;
private long startByte;
private long endByte;
private ConnectionItem connectionItem;
/**
* Constructor called in case of new connection
* @param argDownloadManager
* @param argFilename
* @param argStartByte
* @param argEndByte
*/
public ConnectionThread(DownloadStat argDownloadManager,
String argFilename,long argStartByte,long argEndByte){
task=Task.NewDownload;
downloadManager=argDownloadManager;
filename=argFilename;
startByte=argStartByte;
endByte=argEndByte;
}
/**
* Constructor called in case of resuming a connection.
* @param argDownloadManager
* @param argConnectionItem
*/
public ConnectionThread(ConnectionItem argConnectionItem){
task=Task.Resume;
connectionItem=argConnectionItem;
}
synchronized public void wakeup(){
notify();
}
public void run(){
try {
download();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private void download() throws IOException, Exception{
switch(this.task){
case Task.NewDownload:{
ConnectionItem nextConnection=new ConnectionItem(filename,
startByte,endByte);
ApplicationProtocol appProto=null;
switch(downloadManager.protocol){
case Protocol.HTTP:{
appProto=new HTTPHandler();
break;
}
}
nextConnection.setConnectionStatus(DownloadStatus.Downloading);
//nextConnection.connectionThread=this;
downloadManager.connectionList.add(nextConnection);
storeObject();
appProto.connect(downloadManager.host);
appProto.get(downloadManager.url, filename,startByte,endByte);
nextConnection.setConnectionStatus(DownloadStatus.Downloaded);
break;
}
case Task.Resume:{
ApplicationProtocol appProto=null;
switch(downloadManager.protocol){
case Protocol.HTTP:{
appProto=new HTTPHandler();
break;
}
}
if(connectionItem.getConnectionStatus()==DownloadStatus.Downloading){
//downloadManager.connectionList.add(connectionItem);
appProto.connect(downloadManager.host);
appProto.get(downloadManager.url, filename,startByte,endByte);
connectionItem.setConnectionStatus(DownloadStatus.Downloaded);
}
break;
}
}
}
/**
* Serializes the downloadManager object so that it can be resumed later
* @throws IOException
*/
private void storeObject() throws IOException{
FileOutputStream fileOut=new FileOutputStream(DownloadInfoFile.name);
ObjectOutputStream objectOut=new ObjectOutputStream(fileOut);
objectOut.writeObject(this.downloadManager);
objectOut.close();
fileOut.close();
}
}