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 LinkPeer extends Skeleton { /** * **/ public final static String PackageName = "LinkPeer"; /** * **/ public final static String requestLinkTag = "_request_link"; /** * **/ protected final static String[] requestLinkAliases = // {"request_link"}; /** * **/ public final static String requestUnlinkTag = "_request_unlink"; /** * **/ protected final static String[] requestUnlinkAliases = // {"request_unlink"}; /** * **/ public final static String statusLinkedTag = "_status_linked"; /** * **/ protected final static String[] statusLinkedAliases = // {"status_linked"}; /** * **/ public final static String statusUnlinkedTag = "_status_unlinked"; /** * **/ protected final static String[] statusUnlinkedAliases = // {"status_unlinked"}; class DummyListener implements LinkPeerListener { /** * **/ public void serverLinked(UNL server, String body) { } /** * **/ public void serverUnlinked(UNL server, String body) { } } /** * **/ private LinkPeerListener listener = new DummyListener(); /** * **/ private Hashtable linked = new Hashtable(); /** * **/ public boolean link = true; /** * **/ // TODO: auto-relink (only if no unlink request sent) // -1 = no // 0 = unlimited // >0 = how often // public int relinkTries = -1; // private Hashtable relinkCounters = new Hashtable(); private final static Integer USER_DEFINED = new Integer(-1); /** * **/ private final static Integer LINK_REQUEST_SENT = new Integer(0); /** * **/ private final static Integer LINK_ACCEPTED = new Integer(1); /** * **/ private final static Integer UNLINK_REQUEST_SENT = new Integer(2); /** * **/ public LinkPeer(LinkPeerListener listener) { this (listener,true); } /** * **/ public LinkPeer(LinkPeerListener listener, boolean link) { super(); setPackageName(PackageName); addPackageMethod(requestLinkTag,requestLinkAliases,false); addPackageMethod(requestUnlinkTag,requestUnlinkAliases,false); addPackageMethod(statusLinkedTag,statusLinkedAliases,true); addPackageMethod(statusUnlinkedTag,statusUnlinkedAliases,true); this.link = link; if(listener != null) this.listener = listener; } /** * **/ public boolean understands() { return !(listener instanceof DummyListener); } /** * **/ public boolean wantsErrors() { return true; } /** * **/ public void registerCenter(PSYCMessageCenter center) { super.registerCenter(center); if(link) try { link(); } catch(PSYCDeliveryException e) { } } /** * **/ public void received(UNL source, String method, String body) { // We'll ever get the method name we've registered, // even if a submethod were called if(statusLinkedTag.equals(method)) { if(isLinked(source)) { addLinked(source,LINK_ACCEPTED); listener.serverLinked(source,body); } } else if(statusUnlinkedTag.equals(method)) { if(isLinked(source)) { removeLinked(source); listener.serverUnlinked(source,body); } } } /** * **/ public void reset(UNL source) { if(isLinked(source)) { removeLinked(source); listener.serverUnlinked(source,null); } } /** * **/ public boolean isLinked() { if(center.getIdentification(null) == null) return false; return isLinked(center.getIdentification(null)); } /** * **/ public boolean isLinked(UNL server) { return linked.get(server) != null; } /** * **/ private void addLinked(UNL server, Integer state) { linked.put(server,state); } /** * **/ public void addLinked(UNL server) { linked.put(server,USER_DEFINED); } /** * **/ public void removeLinked(UNL server) { linked.remove(server); } /** * **/ public void link(UNL server) throws PSYCDeliveryException { center.sendChecked(server,LinkServer.PackageName,// requestLinkTag,null); addLinked(server,LINK_REQUEST_SENT); } /** * **/ public void link() throws PSYCDeliveryException { if(center.getIdentification(null) == null) return; link(center.getIdentification(null)); } /** * **/ public void unlink(UNL server) throws PSYCDeliveryException { center.sendChecked(server,LinkServer.PackageName,// requestUnlinkTag,null); addLinked(server,UNLINK_REQUEST_SENT); } /** * **/ public void unlink() throws PSYCDeliveryException { if(center.getIdentification(null) == null) return; unlink(center.getIdentification(null)); } }