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