]> git.somenet.org - pub/jan/sbc.git/blob - src/main/java/at/ac/tuwien/sbc/valesriegler/xvsm/SpaceListener.java
Make cook and waiter look around for work to do if they are idle
[pub/jan/sbc.git] / src / main / java / at / ac / tuwien / sbc / valesriegler / xvsm / SpaceListener.java
1 package at.ac.tuwien.sbc.valesriegler.xvsm;
2
3 import java.io.Serializable;
4 import java.util.Date;
5 import java.util.List;
6 import java.util.Timer;
7 import java.util.TimerTask;
8
9 import org.mozartspaces.notifications.Notification;
10 import org.mozartspaces.notifications.NotificationListener;
11 import org.mozartspaces.notifications.Operation;
12 import org.slf4j.Logger;
13 import org.slf4j.LoggerFactory;
14
15 /**
16  * 
17  * The SpaceListener provides the {@link NotificationListener} interface.
18  * <p />
19  * In {@link SpaceListener#getEntries()} a method for reading the entities, that are normally expected from the notification,
20  * must be supplied. Thus, it's possible in {@link SpaceListener#startHandlingAbsenceOfNotifications()} to start a timer and to
21  * execute the actions, which are normally triggered by the notification, with space objects read from {@link SpaceListener#getEntries()}.
22  * If {@link SpaceListener#lookAround} is true the timer periodically checks if the corresponding {@link AbstractXVSMConnector#timeOflastOperation} is old enough (= the agent is idle for some time)
23  * and possibly executes the action as a result.
24  * 
25  * @author Gregor Riegler <gregor DOT riegler AT gmail DOT com>
26  *
27  */
28 public abstract class SpaceListener implements NotificationListener {
29         private static final Logger log = LoggerFactory
30                         .getLogger(SpaceListener.class);
31         
32         protected boolean lookAround = true;
33
34         @Override
35         public void entryOperationFinished(Notification arg0, Operation arg1,
36                         List<? extends Serializable> entries) {
37                 synchronized (AbstractXVSMConnector.lockObject) {
38                         try {
39                                 onEntriesWritten(entries);
40                         } catch (Exception e) {
41 //                              e.printStackTrace();
42                         }
43                         AbstractXVSMConnector.timeOflastOperation = new Date().getTime();
44                 }
45         }
46
47         abstract List<? extends Serializable> getEntries() throws Exception;
48
49         abstract void onEntriesWritten(List<? extends Serializable> entries)
50                         throws Exception;
51
52         public void startHandlingAbsenceOfNotifications() {
53                 if(!lookAround) return;
54                 
55                 Timer timer = new Timer();
56
57                 timer.schedule(new SpaceTask(), 500, 3000);
58         }
59         
60         class SpaceTask extends TimerTask {
61
62                 @Override
63                 public void run() {
64                         long time = new Date().getTime();
65                         if(time-3000 <= AbstractXVSMConnector.timeOflastOperation) return;
66                         synchronized(AbstractXVSMConnector.lockObject) {
67                                 try {
68                                         
69                                                 List<? extends Serializable> entries = getEntries();
70                                                 if(entries.size() != 0) {
71                                                         onEntriesWritten(entries);
72                                                 }
73                                 } catch (Exception e) {
74                                         log.info(e.getMessage());
75 //                                      e.printStackTrace();
76                                 }
77                                 AbstractXVSMConnector.timeOflastOperation = new Date().getTime();
78                         }
79                         
80                 }
81         }
82                 
83 }
84         
85         
86