package lava.net.psyc.packages; import java.util.Hashtable; import lava.net.common.UNL; import lava.net.psyc.PSYCDeliveryException; import lava.net.psyc.PSYCMessageCenter; import lava.net.psyc.PSYCPackage; /** * **/ public class Notification extends Skeleton { /** * **/ public final static String PackageName = "Notification"; /** * **/ public final static String subscribeTag = // "_request_notification_subscribe"; /** * **/ protected final static String[] subscribeAliases = // {"request_subscribe_notification"}; /** * **/ public final static String unsubscribeTag = // "_request_notification_unsubscribe"; /** * **/ protected final static String[] unsubscribeAliases = // {"request_notification_unsubscribe"}; /** * **/ public final static String subscribedTag = // "_status_notification_subscribed"; /** * **/ protected final static String[] subscribedAliases = // {"status_notification_subscribed"}; /** * **/ public final static String refusedTag = "_error_notification_refused"; /** * **/ protected final static String[] refusedAliases = // {"error_notification_refused"}; /** * **/ public final static String unsubscribedTag = // "_status_notification_unsubscribed"; /** * **/ protected final static String[] unsubscribedAliases = // {"status_notification_unsubscribed"}; /** * **/ public final static String signonTag = "_notice_friend_present"; /** * **/ protected final static String[] signonAliases = { "_notify_signon", "_status_person_present" }; /** * **/ public final static String signoffTag = "_notice_friend_absent"; /** * **/ protected final static String[] signoffAliases = { "_notify_signoff", "_status_person_absent" }; class DummyListener implements NotificationListener { /** * **/ public void notificationSubscribed(UNL source, String body) { } /** * **/ public void notificationRefused(UNL source, String body) { } /** * **/ public void notificationUnsubscribed(UNL source, String body) { } /** * **/ public void notificationSignon(UNL source, String body) { } /** * **/ public void notificationSignoff(UNL source, String body) { } } /** * **/ private NotificationListener listener = new DummyListener(); /** * **/ private Hashtable requested = new Hashtable(); /** * **/ private Hashtable subscribed = new Hashtable(); /** * **/ private Hashtable online = new Hashtable(); /** * **/ public Notification(NotificationListener listener) { super(); setPackageName(PackageName); addPackageMethod(subscribeTag,subscribeAliases,false); addPackageMethod(unsubscribeTag,unsubscribeAliases,false); addPackageMethod(subscribedTag,subscribedAliases,true); addPackageMethod(refusedTag,refusedAliases,true); addPackageMethod(unsubscribedTag,unsubscribedAliases,true); addPackageMethod(signonTag,signonAliases,true); addPackageMethod(signoffTag,signoffAliases,true); if(listener != null) this.listener = listener; } /** * **/ public boolean understands() { return !(listener instanceof DummyListener); } /** * **/ public void received(UNL source, String method, String body) { if(method.equals(subscribedTag)) { if(!haveRequested(source)) return; removeRequested(source); addSubscribed(source); listener.notificationSubscribed(source,body); } else if(method.equals(refusedTag)) { if(!haveRequested(source)) return; removeRequested(source); removeSubscribed(source); listener.notificationRefused(source,body); } else if(method.equals(unsubscribedTag)) { if(!haveSubscribed(source)) return; removeSubscribed(source); listener.notificationUnsubscribed(source,body); } else if(method.equals(signonTag)) { if(!haveSubscribed(source)) return; if(isOnline(source)) return; addOnline(source); listener.notificationSignon(source,body); } else if(method.equals(signoffTag)) { if(!haveSubscribed(source)) return; if(!isOnline(source)) return; removeOnline(source); listener.notificationSignoff(source,body); } } /** * **/ public void addRequested(UNL source) { requested.put(source,source); } /** * **/ public void removeRequested(UNL source) { requested.remove(source); } /** * **/ public boolean haveRequested(UNL source) { return requested.get(source) != null; } /** * **/ public void addSubscribed(UNL source) { subscribed.put(source,source); } /** * **/ public void removeSubscribed(UNL source) { subscribed.remove(source); } /** * **/ public boolean haveSubscribed(UNL source) { return subscribed.get(source) != null; } /** * **/ public void addOnline(UNL source) { online.put(source,source); } /** * **/ public void removeOnline(UNL source) { online.remove(source); } /** * **/ public boolean isOnline(UNL source) { return online.get(source) != null; } /** * **/ public void subscribe(UNL target, String body) throws PSYCDeliveryException { if(haveRequested(target) || haveSubscribed(target)) return; center.sendChecked(target,NotificationServer.PackageName,// subscribeTag,body); addRequested(target); } /** * **/ public void subscribe(UNL target) throws PSYCDeliveryException { subscribe(target,null); } /** * **/ public void unsubscribe(UNL target, String body) // throws PSYCDeliveryException { if(!haveSubscribed(target)) return; center.sendChecked(target,NotificationServer.PackageName,// unsubscribeTag,body); } /** * **/ public void unsubscribe(UNL target) throws PSYCDeliveryException { unsubscribe(target,null); } }