blob: 33c6aa31b34c5c77854a35888ab64542a435e82e [file] [log] [blame]
jaegonkim6a7b5242018-09-12 23:09:42 +09001/*
2 * Copyright 2018-present Open Networking Foundation
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package org.onosproject.workflow.api;
17
18import java.util.Date;
19import java.util.PriorityQueue;
20import java.util.Timer;
21import java.util.TimerTask;
22
23/**
24 * Class for time chain timer.
25 */
26public class TimerChain {
27
28 private PriorityQueue<TimerChainTask> taskQueue = new PriorityQueue<>();
29
30 private Timer impendingTimer;
31 private TimerChainTask impendingTask;
32
33 /**
34 * Schedules timer event.
35 * @param afterMs millisecond which time event happens.
36 * @param runnable runnable to be executed after 'afterMs'
37 */
38 public void schedule(long afterMs, Runnable runnable) {
39 schedule(new Date((new Date()).getTime() + afterMs), runnable);
40 }
41
42 /**
43 * Schedules timer event.
44 * @param date date which timer event happens.
45 * @param runnable runnable to be executed on 'date'
46 */
47 public void schedule(Date date, Runnable runnable) {
48 schedule(new TimerChainTask(this, date, runnable));
49 }
50
51 /**
52 * Schedule timer chain task.
53 * @param task task to be scheduled.
54 */
55 private void schedule(TimerChainTask task) {
56 synchronized (this) {
57 if (taskQueue.size() == 0) {
58 scheduleImpending(task);
59 return;
60 }
61
62 if (task.date().getTime() < head().date().getTime()) {
63 impendingTimer.cancel();
64 impendingTask.cancel();
65 TimerChainTask prevImpendingTask = pop().copy();
66 taskQueue.offer(prevImpendingTask);
67
68 scheduleImpending(task);
69 } else {
70 taskQueue.offer(task);
71 }
72 }
73 }
74
75 /**
76 * Schedule impending timer task.
77 * @param task impending timer chain task
78 * @return timer chain task
79 */
80 private TimerChainTask scheduleImpending(TimerChainTask task) {
81 taskQueue.offer(task);
82 Timer timer = new Timer();
83 this.setImpendingTask(task);
84 this.setImpendingTimer(timer);
85 timer.schedule(task, task.date());
86 return task;
87 }
88
89 /**
90 * Gets impending timer.
91 * @return impending timer
92 */
93 public Timer implendingTimer() {
94 return impendingTimer;
95 }
96
97 /**
98 * Sets impending timer.
99 * @param impendingTimer impending timer
100 */
101 public void setImpendingTimer(Timer impendingTimer) {
102 this.impendingTimer = impendingTimer;
103 }
104
105 /**
106 * Gets impending timer task.
107 * @return impending timer task
108 */
109 public TimerTask impendingTask() {
110 return impendingTask;
111 }
112
113 /**
114 * Sets impending timer task.
115 * @param impendingTask impending timer task
116 */
117 public void setImpendingTask(TimerChainTask impendingTask) {
118 this.impendingTask = impendingTask;
119 }
120
121 /**
122 * Gets head of timer chain task queue.
123 * @return timer chain task
124 */
125 public TimerChainTask head() {
126 if (taskQueue.size() > 0) {
127 return taskQueue.peek();
128 } else {
129 return null;
130 }
131 }
132
133 /**
134 * Pops head of timer chain task queue.
135 * @return timer chain task
136 */
137 public TimerChainTask pop() {
138 if (taskQueue.size() > 0) {
139 return taskQueue.poll();
140 } else {
141 return null;
142 }
143 }
144
145 /**
146 * Class for timer chain task.
147 */
148 public static class TimerChainTask extends TimerTask implements Comparable<TimerChainTask> {
149
150 private final TimerChain timerchain;
151 private final Date date;
152 private final Runnable runnable;
153
154 /**
155 * Constructor of timer chain task.
156 * @param timerchain timer chain
157 * @param date date to be scheduled
158 * @param runnable runnable to be executed by timer
159 */
160 public TimerChainTask(TimerChain timerchain, Date date, Runnable runnable) {
161 this.timerchain = timerchain;
162 this.date = date;
163 this.runnable = runnable;
164 }
165
166 /**
167 * Gets date.
168 * @return date of timer chain task
169 */
170 public Date date() {
171 return this.date;
172 }
173
174 /**
175 * Gets runnable.
176 * @return runnable of timer chain task
177 */
178 public Runnable runnable() {
179 return this.runnable;
180 }
181
182 @Override
183 public void run() {
184
185 TimerChainTask nextTask;
186 synchronized (timerchain) {
187 if (timerchain.impendingTask() != this) {
188 System.out.println("Invalid impendingTask");
189 runnable().run();
190 return;
191 }
192
193 timerchain.implendingTimer().cancel();
194 timerchain.pop();
195
196 nextTask = timerchain.head();
197
198 if (nextTask != null) {
199 Timer nextTimer = new Timer();
200 this.timerchain.setImpendingTask(nextTask);
201 this.timerchain.setImpendingTimer(nextTimer);
202 nextTimer.schedule(nextTask, nextTask.date());
203 }
204 }
205
206 runnable().run();
207
208 }
209
210 @Override
211 public int compareTo(TimerChainTask target) {
212 return date().compareTo(target.date());
213 }
214
215 /**
216 * Copies timer chain task.
217 * @return timer chain task
218 */
219 public TimerChainTask copy() {
220 return new TimerChainTask(timerchain, date, runnable);
221 }
222 }
223}