break;
String filename = seg.getDlFile().getFilename();
logger.msg("Downloading next segment of file: " + filename, MyLogger.SEV_DEBUG);
// create new response handler
RspHandler newHandler = new RspHandler(seg, false);
synchronized(activeRspHandlers)
{
activeRspHandlers.add(newHandler);
}
// map the new response handler to the download file
mapHandlerToFile(newHandler, seg);
// start data download and increase thread counter
nioClient.fetchArticleData(seg.getGroups().firstElement(), seg.getArticleId(), newHandler);
runningThreads++;
}
// Add finished response handlers from backup downloader to this.activeRspHandlers
if(backupFileDownloader != null && backupFileDownloader.hasFinishedHandlers())
{
for(RspHandler handler : backupFileDownloader.getFinishedHandlers())
{
activeRspHandlers.add(handler);
DownloadFileSegment seg = handler.dlFileSeg();
dlFileRspHandlerMap.get(seg.getDlFile()).put(seg.getIndex(), handler);
}
}
// check if the next element of the result set is already finished
int actRspHdlsSize;
synchronized(activeRspHandlers)
{
actRspHdlsSize = activeRspHandlers.size();
}
ArrayList<RspHandler> toRemoveVector = new ArrayList<>();
for(int i = 0; i < actRspHdlsSize; i++)
{
RspHandler handler;
synchronized(activeRspHandlers)
{
handler = activeRspHandlers.get(i);
}
// handle error response from NNTP server
if(handler.getError() == RspHandler.ERR_NONE)
{
// no error, do nothing
}
else if(handler.getError() == RspHandler.ERR_AUTH)
{
// do nothing for this error (?)
}
else if(handler.getError() == RspHandler.ERR_FETCH_430)
{
// 430 No such article
if(backupFileDownloader != null && !handler.isFromBackupDownloader())
{
// try to download this failed segment via the backup connection
dlFileRspHandlerMap.get(handler.dlFileSeg().getDlFile()).remove(handler.dlFileSeg().getIndex());
backupFileDownloader.addSegment(handler.dlFileSeg());
toRemoveVector.add(handler);
continue;
}
else
{
// no backup downloader, or already tried there...
String msg = "Article not found on server (" + handler.getErrorMsg().trim() + ")";
logger.msg(msg, MyLogger.SEV_WARNING);
if(!tooMany430ErrorsSent && fetch430count++ > MAX_430_ERRORS)
{
EventQueue.invokeLater(new Runnable()
{
@Override
public void run()
{
mainApp.tooMany430Errors();
}
});
tooMany430ErrorsSent = true;
logger.msg("At least " + MAX_430_ERRORS + " articles have not been found on server(s).", MyLogger.SEV_WARNING);
}
}
}
else if(handler.getError() == RspHandler.ERR_FETCH)
{
// failed to fetch article (non-430)
String msg = "Failed to fetch article <"
+ handler.dlFileSeg().getArticleId() + "> ("
+ handler.getErrorMsg() + ")";
logger.msg(msg, MyLogger.SEV_WARNING);
}
else
{
// all other errors
String msg = "Failed to fetch article <"
+ handler.dlFileSeg().getArticleId() + "> ("
+ handler.getErrorMsg() + ")";
logger.msg(msg, MyLogger.SEV_WARNING);
shutdown = true;
}
// update downloaded byte counter ...
DownloadFile dlFile = handler.dlFileSeg().getDlFile();
String filename = dlFile.getFilename();
int bytes = 0;
Integer bytesInt = downloadedBytes.get(filename);
if(bytesInt != null)
bytes = bytesInt;
bytes += handler.newByteCount();
downloadedBytes.put(filename, bytes);
// ... and progres bar in main window
int last = 0;
Integer lastInt = lastProgBarUpdate.get(filename);
if(lastInt != null)
last = lastInt;
last = updateProgressBar(bytes, last, dlFile);
lastProgBarUpdate.put(filename, last);
// all data downloaded?
if(handler.isFinished())
{
if(handler.getError() == RspHandler.ERR_NONE)
fetch430count = 0;
toRemoveVector.add(handler);
runningThreads--;
decrSegCount(filename); // decrease main window segment counter
// segment done, so check if whole download file is finished now
dlFile.removeSegment(handler.dlFileSeg().getIndex());
if(!dlFile.hasMoreSegments())
{
try
{
handleFinishedDlFile(dlFile);