Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update QueueModerator.java #41

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,10 @@ void DownloadManagerPro.StartQueueDownload(int downloadTaskPerTime, int sortType

Example:
``` java
int taskTokenOne = dm.addTask("save_name", "http://www.site.com/video/ss.mp4", false, false);
int taskTokenTwo = dm.addTask("save_name", "http://www.site.com/video/ss_1.mp4", false, false);
int taskTokenThree = dm.addTask("save_name", "http://www.site.com/video/ss_2.mp4", false, false);

try {
dm.startQueueDownload(3, QueueSort.oldestFirst);

Expand Down
18 changes: 18 additions & 0 deletions com/golshadi/majid/core/DownloadManagerPro.java
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,24 @@ public boolean delete(int token, boolean deleteTaskFile){
return false;
}

/**
* delete all uncompleted downloads tasks from db and files
*/
public void deleteAllUnCompleteds() {
List<Task> task = tasksDataSource.allUnCompleteds();
if (task != null) {
for (Task tsk : task) {

List<Chunk> taskChunks =
chunksDataSource.chunksRelatedTask(tsk.id);
for (Chunk chunk : taskChunks) {
FileUtils.delete(tsk.save_address, String.valueOf(chunk.id));
chunksDataSource.delete(chunk.id);
}
delete(tsk.id, true);
}
}
}

/**
* close db connection
Expand Down
51 changes: 29 additions & 22 deletions com/golshadi/majid/core/mainWorker/QueueModerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,36 +37,43 @@ public QueueModerator(TasksDataSource tasksDataSource, ChunksDataSource chunksDa
this.moderator = localModerator;
this.moderator.setQueueObserver(this);
this.listener = downloadManagerListener;

this.downloadTaskPerTime = downloadPerTime;
if (tasks.size() < downloadPerTime) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe It's better to write something like this

 this.downloadTaskPerTime = downloadPerTime;
 if (tasks.size() < downloadPerTime) {
       this.downloadTaskPerTime = tasks.size();
}

But why?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes you have right, its better than my code

this.downloadTaskPerTime = tasks.size();
}

this.uncompletedTasks = tasks;

downloaderList =
new HashMap<Integer, Thread>(downloadTaskPerTime);
downloaderList =new HashMap<>(downloadTaskPerTime);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Revert

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

downloadTaskPerTime variable still final ?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Alamusitl are you familiar with this library?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not much

}


public void startQueue() {

if (uncompletedTasks != null) {

int location = 0;
while (uncompletedTasks.size() > 0 &&
!pauseFlag &&
downloadTaskPerTime >= downloaderList.size()) {
Task task = uncompletedTasks.get(location);
Thread downloader =
new AsyncStartDownload(tasksDataSource, chunksDataSource, moderator, listener, task);

downloaderList.put(task.id, downloader);
uncompletedTasks.remove(location);

downloader.start();


location++;
}

}
if (uncompletedTasks != null) {

int location = 0;
while (uncompletedTasks.size() > 0 &&
!pauseFlag &&
downloadTaskPerTime >= downloaderList.size()) {
try {
Task task = uncompletedTasks.get(location);
Thread downloader =
new AsyncStartDownload(tasksDataSource, chunksDataSource, moderator, listener, task);

downloaderList.put(task.id, downloader);
uncompletedTasks.remove(location);

downloader.start();

location++;
} catch (IndexOutOfBoundsException e) {
e.printStackTrace();
Log.e("QueueModerator", e.getMessage());
}
}
}
}

public void wakeUp(int taskID){
Expand Down
25 changes: 25 additions & 0 deletions com/golshadi/majid/database/TasksDataSource.java
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,31 @@ public Task getTaskInfo(int id) {
return task;
}

public List<Task> allUnCompleteds() {
List<Task> unCompleted = new ArrayList<>();

String query = "SELECT * FROM " + TABLES.TASKS
+ " WHERE " + TASKS.COLUMN_STATE + "!=" + SqlString.Int(TaskStates.END);

Cursor cr = database.rawQuery(query, null);

if (cr != null) {
cr.moveToFirst();

while (!cr.isAfterLast()) {
Task task = new Task();
task.cursorToTask(cr);
unCompleted.add(task);

cr.moveToNext();
}

cr.close();
}

return unCompleted;
}

public Task getTaskInfoWithName(String name) {
String query = "SELECT * FROM "+TABLES.TASKS+" WHERE "+TASKS.COLUMN_NAME+"="+SqlString.String(name);
Cursor cr = database.rawQuery(query, null);
Expand Down