1 package at.ac.tuwien.sbc.valesriegler.xvsm.spacehelpers;
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;
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;
19 * The SpaceListener provides the {@link NotificationListener} interface.
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.
27 * @author Gregor Riegler <gregor DOT riegler AT gmail DOT com>
30 public abstract class SpaceListener implements NotificationListener {
31 private static final Logger log = LoggerFactory
32 .getLogger(SpaceListener.class);
34 protected boolean lookAround = true;
35 protected long timeout = 3000;
36 protected SpaceAction spaceAction;
40 public void entryOperationFinished(Notification arg0, Operation arg1,
41 List<? extends Serializable> entries) {
42 synchronized (AbstractXVSMConnector.lockObject) {
43 AbstractXVSMConnector.timeOflastOperation.set(new Date().getTime());
45 // log.info("I am running a notification now!");
46 spaceAction.inNotification.set(true);
47 spaceAction.onEntriesWritten(entries);
48 } catch (Exception e) {
49 // e.printStackTrace();
55 abstract List<? extends Serializable> getEntries() throws Exception;
57 public void startHandlingAbsenceOfNotifications() {
58 if(!lookAround || Util.runSimulation) return;
59 AbstractXVSMConnector.timeOflastOperation.set(new Date().getTime() + 3500);
61 Timer timer = new Timer();
63 timer.schedule(new SpaceTask(), 3000, 3000);
66 class SpaceTask extends TimerTask {
70 if(new Date().getTime()-timeout <= AbstractXVSMConnector.timeOflastOperation.get()) {
73 synchronized(AbstractXVSMConnector.lockObject) {
75 AbstractXVSMConnector.timeOflastOperation.set(new Date().getTime());
77 List<? extends Serializable> entries = getEntries();
78 if(entries.size() != 0) {
79 log.info("Start '{}' task", name);
80 log.info("{} entries in timer", entries.size());
81 spaceAction.inNotification.set(false);
82 spaceAction.onEntriesWritten(entries);
84 } catch (Exception e) {
85 log.info(e.getMessage());
86 // e.printStackTrace();