303
|
1 package alice.topology.manager.keeparive;
|
|
2
|
|
3 import alice.codesegment.CodeSegment;
|
309
|
4 import alice.datasegment.CommandType;
|
321
|
5 import alice.datasegment.DataSegment;
|
309
|
6 import alice.datasegment.Receiver;
|
303
|
7
|
319
|
8 public class TaskExecuter extends CodeSegment{
|
310
|
9 private Receiver info = ids.create(CommandType.TAKE);
|
|
10 private TaskInfo nowTask;
|
323
|
11 private boolean skipFlag = false;
|
|
12 private long startTime = 0;
|
319
|
13 private static TaskExecuter instance = new TaskExecuter();
|
310
|
14
|
319
|
15 private TaskExecuter(){}
|
322
|
16 public static TaskExecuter getInstance(){
|
315
|
17 return instance;
|
|
18 }
|
|
19
|
309
|
20 public void setKey(){
|
314
|
21 ids.init();
|
309
|
22 info.setKey("_WAITINGLIST");
|
|
23 }
|
315
|
24
|
309
|
25 @Override
|
322
|
26 public synchronized void run(){
|
324
|
27 ListManager list = info.asClass(ListManager.class);
|
314
|
28 if (list.getTaskList().size() == 0){
|
|
29 ods.update("_WAITINGLIST", list);
|
316
|
30 setKey();
|
314
|
31 return;
|
320
|
32 }
|
|
33 nowTask = list.getTaskList().poll();
|
|
34 ods.update("_WAITINGLIST", list);
|
323
|
35 if (skipFlag){
|
324
|
36 skipFlag = false;
|
323
|
37 }
|
|
38 startTime = System.currentTimeMillis();
|
324
|
39 System.out.println(nowTask.getSleepTime()+" "+nowTask.getType());
|
320
|
40 if (nowTask.getSleepTime()!=0){
|
315
|
41 try {
|
320
|
42 this.wait(nowTask.getSleepTime());
|
322
|
43 } catch (InterruptedException e){}
|
320
|
44 }
|
323
|
45 if (!skipFlag){
|
322
|
46 if (nowTask.getType() == TaskType.PING){
|
321
|
47 ods.ping(nowTask.getManagerKey(), nowTask.getReturnKey());
|
322
|
48 TaskInfo task = new TaskInfo(TaskType.CLOSE);
|
|
49 task.setInfo(nowTask.getManagerKey(), 60 * 1000);
|
|
50 ods.put("_TASKINFO", task);
|
321
|
51 new RespondPing(nowTask.getReturnKey());
|
|
52 } else {
|
323
|
53 // no response from the Remote DataSegment. So close this connection.
|
321
|
54 DataSegment.get(nowTask.getManagerKey()).close();
|
323
|
55 }
|
309
|
56 }
|
321
|
57 nowTask = null;
|
316
|
58 setKey();
|
309
|
59 }
|
323
|
60
|
326
|
61 public synchronized long skip(){
|
323
|
62 skipFlag = true;
|
326
|
63 long remainingTime = nowTask.getSleepTime() - (System.currentTimeMillis() - startTime);
|
323
|
64 nowTask = null;
|
303
|
65 notify();
|
326
|
66 return remainingTime;
|
309
|
67 }
|
315
|
68
|
322
|
69 public TaskInfo getNowTask(){
|
310
|
70 return nowTask;
|
|
71 }
|
315
|
72
|
325
|
73 public long getStartTime(){
|
323
|
74 return startTime;
|
310
|
75 }
|
323
|
76
|
|
77 public boolean compareNowTask(TaskInfo task){
|
|
78 if (nowTask != null){
|
|
79 if (nowTask.getType().equals(task.getType())
|
|
80 && nowTask.getManagerKey().equals(task.getManagerKey())){
|
|
81 return true;
|
|
82 }
|
|
83 }
|
|
84 return false;
|
|
85 }
|
303
|
86
|
323
|
87 } |