Skip to content

Commit e790370

Browse files
committed
update
1 parent d31d30f commit e790370

File tree

4 files changed

+129
-0
lines changed

4 files changed

+129
-0
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package container;
2+
3+
import java.util.concurrent.BlockingQueue;
4+
import java.util.concurrent.PriorityBlockingQueue;
5+
6+
import scheduler.ParserTask;
7+
8+
public class Container {
9+
private static BlockingQueue<ParserTask> priorityQueue = new PriorityBlockingQueue<ParserTask>(200);
10+
public static boolean isEmpty() {
11+
return priorityQueue.isEmpty();
12+
}
13+
public static BlockingQueue<ParserTask> getQueue() {
14+
return priorityQueue;
15+
}
16+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package scheduler;
2+
3+
import java.util.List;
4+
import java.util.concurrent.BlockingQueue;
5+
6+
import container.Container;
7+
import downloader.HttpClientDownloader;
8+
9+
public class DispatchScheduler implements Scheduler {
10+
private volatile boolean stoped;
11+
private ParserTask task;
12+
private HttpClientDownloader downloader = HttpClientDownloader.getInstance();;
13+
14+
private static BlockingQueue<ParserTask> queue = Container.getQueue();
15+
public void startProcess() {
16+
if (queue.isEmpty()) {
17+
try {
18+
List<String> list = downloader.problemListDownloader();
19+
for (String value: list) {
20+
task = new ParserTask(value);
21+
task.isType();
22+
queue.put(task);
23+
}
24+
} catch (InterruptedException e) {
25+
// TODO Auto-generated catch block
26+
e.printStackTrace();
27+
}
28+
} else {
29+
try {
30+
task = queue.take();
31+
String url = task.getUrl();
32+
task = new ParserTask(url);
33+
task.isType();
34+
queue.put(task);
35+
} catch (InterruptedException e) {
36+
// TODO Auto-generated catch block
37+
e.printStackTrace();
38+
}
39+
}
40+
}
41+
public void taskProcceed() {
42+
while (!queue.isEmpty()) {
43+
try {
44+
task = queue.take();
45+
} catch (InterruptedException e1) {
46+
// TODO Auto-generated catch block
47+
e1.printStackTrace();
48+
}
49+
String url = task.getUrl();
50+
task = new ParserTask(url);
51+
task.isType();
52+
try {
53+
queue.put(task);
54+
} catch (InterruptedException e) {
55+
// TODO Auto-generated catch block
56+
e.printStackTrace();
57+
}
58+
}
59+
}
60+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package scheduler;
2+
3+
import java.util.regex.Matcher;
4+
import java.util.regex.Pattern;
5+
6+
public class ParserTask implements Comparable<ParserTask>{
7+
private static final String submissionListURL = ".*/submissions/$"; // 检验是否是题目提交列表页面
8+
private static final String problemCodeURL = ".*/submissions/detail/.*"; // 检验是否是题目代码页面
9+
private static final String problemListURL = ".*/problemset/algorithms/$"; // 检验是否是题目列表页面
10+
11+
static enum TaskType {
12+
GET_PROBLEM_LIST_URL, GET_SUBMISION_URL, GET_CODE_URL, GET_CODE
13+
}
14+
15+
private String url;
16+
private TaskType type;
17+
18+
public ParserTask(String url) {
19+
this.url = url;
20+
}
21+
22+
public String getUrl() {
23+
return this.url;
24+
}
25+
26+
public void isType() {
27+
if (checkPattern(url, problemListURL)) {
28+
type = TaskType.GET_PROBLEM_LIST_URL;
29+
} else if (checkPattern(url, problemCodeURL)) {
30+
type = TaskType.GET_CODE_URL;
31+
} else if (checkPattern(url, submissionListURL)) {
32+
type = TaskType.GET_SUBMISION_URL;
33+
} else {
34+
type = TaskType.GET_CODE;
35+
}
36+
}
37+
38+
public boolean checkPattern(String url, String patternString) {
39+
Pattern pattern = Pattern.compile(patternString);
40+
Matcher matcher = pattern.matcher(url);
41+
boolean res = matcher.matches();
42+
return res;
43+
}
44+
45+
public int compareTo(ParserTask o) {
46+
return o.type.ordinal()-this.type.ordinal();
47+
}
48+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package scheduler;
2+
3+
public interface Scheduler {
4+
public void taskProcceed();
5+
}

0 commit comments

Comments
 (0)