package lava.net.psyc.packages; import java.util.Enumeration; 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 NotificationServer extends Skeleton { /** * **/ public final static String PackageName = "NotificationServer"; class DummyListener implements NotificationServerListener { /** * **/ public void notificationRequestSubscribe(UNL source, String body) { try { subscribe(source); } catch(PSYCDeliveryException e) { } } /** * **/ public void notificationUnsubscribed(UNL source, String body) { } } /** * **/ private NotificationServerListener listener = new DummyListener(); /** * **/ private Hashtable subscribed = new Hashtable(); /** * **/ public NotificationServer() { this (null); } /** * **/ public NotificationServer(NotificationServerListener listener) { super(); setPackageName(PackageName); addPackageMethod(Notification.subscribeTag,// Notification.subscribeAliases,true); addPackageMethod(Notification.unsubscribeTag,// Notification.unsubscribeAliases,true); addPackageMethod(Notification.subscribedTag,// Notification.subscribedAliases,false); addPackageMethod(Notification.refusedTag,// Notification.refusedAliases,false); addPackageMethod(Notification.unsubscribedTag,// Notification.unsubscribedAliases,false); addPackageMethod(Notification.signonTag,// Notification.signonAliases,false); addPackageMethod(Notification.signoffTag,// Notification.signoffAliases,false); if(listener != null) this.listener = listener; } /** * **/ public boolean understands() { return !(listener instanceof DummyListener); } /** * **/ public boolean wantsErrors() { return true; } /** * **/ public void received(UNL source, String method, String body) { if(method.equals(Notification.subscribeTag)) { listener.notificationRequestSubscribe(source,body); } else if(method.equals(Notification.unsubscribeTag)) { try { unsubscribe(source,body); } catch(PSYCDeliveryException e) { } } } /** * **/ public void reset(UNL source) { removeSubscribed(source); } /** * **/ public void addSubscribed(UNL source) { subscribed.put(source,source); } /** * **/ public void removeSubscribed(UNL source) { subscribed.remove(source); } /** * **/ public boolean isSubscribed(UNL source) { return subscribed.get(source) != null; } /** * **/ public Enumeration getSubscribed() { return subscribed.keys(); } /** * **/ public void subscribe(UNL target, String body) throws PSYCDeliveryException { center.sendChecked(target,Notification.PackageName,// Notification.subscribedTag,body); addSubscribed(target); } /** * **/ public void subscribe(UNL target) throws PSYCDeliveryException { subscribe(target,null); } /** * **/ public void refuse(UNL target, String body) throws PSYCDeliveryException { removeSubscribed(target); center.sendChecked(target,Notification.PackageName,// Notification.refusedTag,body); } /** * **/ public void refuse(UNL target) throws PSYCDeliveryException { refuse(target,null); } /** * **/ public void unsubscribe(UNL target, String body) // throws PSYCDeliveryException { if(!isSubscribed(target)) return; removeSubscribed(target); PSYCDeliveryException ex = null; try { center.sendChecked(target,Notification.PackageName,// Notification.unsubscribedTag,body); } catch(PSYCDeliveryException e) { ex = e; } listener.notificationUnsubscribed(target,body); if(ex != null) throw ex; } /** * **/ public void unsubscribe(UNL target) throws PSYCDeliveryException { unsubscribe(target,null); } /** * **/ public void signon(UNL target, String body) throws PSYCDeliveryException { center.sendChecked(target,Notification.PackageName,// Notification.signonTag,body); } /** * **/ public void signon(UNL target) throws PSYCDeliveryException { signon(target,null); } /** * **/ public void signon(String body) { UNL target = null; for(Enumeration e = getSubscribed();e.hasMoreElements();) try { signon(target = (UNL)e.nextElement(),body); } catch(PSYCDeliveryException ex) { try { unsubscribe(target,body); } catch(PSYCDeliveryException exx) { } } } /** * **/ public void signon() { signon((String)null); } /** * **/ public void signoff(UNL target, String body) throws PSYCDeliveryException { center.sendChecked(target,Notification.PackageName,// Notification.signoffTag,body); } /** * **/ public void signoff(UNL target) throws PSYCDeliveryException { signoff(target,null); } /** * **/ public void signoff(String body) { UNL target = null; for(Enumeration e = getSubscribed();e.hasMoreElements();) try { signoff(target = (UNL)e.nextElement(),body); } catch(PSYCDeliveryException ex) { try { unsubscribe(target,body); } catch(PSYCDeliveryException exx) { } } } /** * **/ public void signoff() { signoff((String)null); } }