📄 scheduler.java
字号:
* <code>RemoveConditionListener</code>, * <code>AgentDeleteRequest</code>. * <p> * Note: management of <code>AgentDeleteRequest</code> notifications has been * removed to conform to the behaviour expected by the configurator agent. * This should be fixed. * * @param from agent sending notification * @param not notification to react to */ public void react(AgentId from, Notification not) throws Exception { if (logger.isLoggable(BasicLevel.DEBUG)) logger.log(BasicLevel.DEBUG, "Scheduler.react(" + from + ',' + not + ')'); if (not instanceof ScheduleEvent) { doReact(from, (ScheduleEvent) not); } else if (not instanceof ScheduleNotification) { doReact(from, (ScheduleNotification) not); } else if (not instanceof AddConditionListener) { doReact(from, (AddConditionListener) not); } else if (not instanceof RemoveConditionListener) { doReact(from, (RemoveConditionListener) not); // } else if (not instanceof AgentDeleteRequest) { // doReact(from, (AgentDeleteRequest) not); } else { super.react(from, not); } } /** * Reacts to <code>ScheduleEvent</code> notifications. * Calls <code>insertItem</code>. * * @param from agent sending notification * @param not notification to react to */ protected void doReact(AgentId from, ScheduleEvent not) throws Exception { // inserts event in list insertItem(not); // checks for ripe items checkItems(false); } /** * Reacts to <code>ScheduleNotification</code> notifications. * Calls <code>checkItems</code>. * * @param from agent sending notification * @param not notification to react to */ protected void doReact(AgentId from, ScheduleNotification not) throws Exception { // checks for ripe items checkItems(false); } /** * Reacts to <code>AddConditionListener</code> notifications. * Calls <code>addConditionListener</code>. * * @param from agent sending notification * @param not notification to react to */ protected void doReact(AgentId from, AddConditionListener not) throws Exception { addConditionListener(not.name, from); } /** * Reacts to <code>RemoveConditionListener</code> notifications. * Calls <code>removeConditionListener</code>. * * @param from agent sending notification * @param not notification to react to */ protected void doReact(AgentId from, RemoveConditionListener not) throws Exception { removeConditionListener(not.name, from); } /** * Reacts to <code>AgentDeleteRequest</code> notifications. * Calls <code>delete</code>. * * @param from agent sending notification * @param not notification to react to */ // protected void doReact(AgentId from, AgentDeleteRequest not) throws Exception { // delete(); // } /** * Reacts to <code>ScheduleEvent</code> notifications. * * @param not notification to react to */ protected void insertItem(ScheduleEvent not) throws Exception { Date now = new Date(); ScheduleItem newItem = new ScheduleItem(not); // finds next scheduling date of event newItem.date = not.nextDate(now); // checks for an outdated scheduling if (newItem.date == null) { if (! not.outdatedRestart) return; newItem.date = now; } insertItem(newItem); } /** * Inserts an item in the list ordered by date. * Inserts at list head a null dated item. * * @param newItem item to insert */ protected void insertItem(ScheduleItem newItem) throws Exception { if (newItem.date == null) return; if (items == null) { items = newItem; } else { ScheduleItem prev = null; for (ScheduleItem item = items; item != null; item = item.next) { if (!newItem.date.after(item.date)) break; prev = item; } if (prev == null) { if (items != null) { newItem.next = items; items.prev = newItem; } items = newItem; } else { newItem.next = prev.next; newItem.prev = prev; prev.next = newItem; if (newItem.next != null) newItem.next.prev = newItem; } } } /** * Checks for ripe events. * Sends <code>Condition</code> notification and reschedule event. * * @param restart <code>true</code> if called on restart */ protected void checkItems(boolean restart) throws Exception { if (logger.isLoggable(BasicLevel.DEBUG)) logger.log(BasicLevel.DEBUG, "Scheduler.checkItems(" + restart + ')'); Date now = new Date(); checkLoop: for (ScheduleItem item = items; item != null;) { if (item.date != null && item.date.after(now)) break checkLoop; ScheduleItem nextItem = item.next; if (! restart || item.event.outdatedRestart || item.status || // closes event (item.event.duration > 0 && new Date(item.date.getTime() + (item.event.duration * 1000)).after(now)) ) { // signals event item.status = ! item.status; signalEvent(item); } // reschedules event if (item.status == true) { if (item.event.duration > 0) { // finds event end date item.date.setTime(item.date.getTime() + (item.event.duration * 1000)); } else { // sends no false condition when duration is null item.status = ! item.status; } } if (item.status == false) { item.date = item.event.nextDate(now); // this call must return a date later than now // for fear of an infinite loop if ((item.date != null) && ! item.date.after(now)) item.date = null; } if (item.date == null) { // removes event from list removeItem(item); item = nextItem; continue checkLoop; } // checks if event needs to be changed in the list if (nextItem == null || ! item.date.after(nextItem.date)) { continue checkLoop; } // removes event from list removeItem(item); // and inserts it at its new place insertItem(item); item = nextItem; } // sets next wake-up time if (items != null) { alarm.setTime(items.date.getTime() - now.getTime()); } } /** * Removes an item from the list * * @param item item to remove */ void removeItem(ScheduleItem item) { if (item.next != null) item.next.prev = item.prev; if (item.prev == null) items = item.next; else item.prev.next = item.next; item.prev = item.next = null; // checks if condition may be deleted ConditionItem citem = findCondition(item.event.name); if (citem == null) return; Enumeration listeners = citem.listeners.getListeners(); if (listeners != null && listeners.hasMoreElements()) return; for (ScheduleItem sitem = items; sitem != null; sitem = sitem.next) { if (sitem.event.name.equals(item.event.name)) return; } removeCondition(item.event.name); } /** * Adds an agent to the list of listeners for an event. * * @param condition condition name of events to register to * @param listener registering agent */ protected void addConditionListener(String condition, AgentId listener) throws Exception { ConditionItem citem = addCondition(condition); citem.listeners.addListener(listener); for (ScheduleItem item = items; item != null; item = item.next) { if (! item.event.name.equals(condition)) continue; if (item.status) sendTo(listener, new Condition(item.event.name, item.status)); } } /** * Removes an agent from the list of listeners for an event. * * @param condition condition name of events to unregister to * @param listener unregistering agent */ protected void removeConditionListener(String condition, AgentId listener) throws Exception { ConditionItem citem = findCondition(condition); if (citem == null) return; citem.listeners.removeListener(listener); // checks if condition may be deleted Enumeration listeners = citem.listeners.getListeners(); if (listeners != null && listeners.hasMoreElements()) return; for (ScheduleItem item = items; item != null; item = item.next) { if (item.event.name.equals(condition)) return; } removeCondition(condition); } /** * Signals a condition to registered agents. * * @param item event to signal */ protected void signalEvent(ScheduleItem item) { ConditionItem condition = findCondition(item.event.name); if (condition == null) return; sendTo(condition.listeners, new Condition(item.event.name, item.status)); } /** * Overrides the ProxyAgent behavior in order not * to react to DriverDone notifications. * Useless for the Scheduler. * Moreover it may stop the scheduler when restarting * the agent server after a stop. */ protected void driverDone(DriverDone not) throws IOException { }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -