package lava.net.psyc.packages; import java.util.Enumeration; import java.util.Hashtable; import lava.net.common.UNL; import lava.net.common.Value; import lava.net.psyc.PSYCDeliveryException; import lava.net.psyc.PSYCMessageCenter; import lava.net.psyc.PSYCPackage; /** * **/ public class LinkServer extends Skeleton { /** * **/ public final static String PackageName = "LinkServer"; /** * **/ public final static int PackageMajorVersion = 1; /** * **/ public final static int PackageMinorVersion = 0; class DummyListener implements LinkServerListener { /** * **/ public void peerLinkRequest(UNL server, String body) { } /** * **/ public void peerUnlinked(UNL server, String body) { } } /** * **/ private LinkServerListener listener = new DummyListener(); /** * **/ private Hashtable linked = new Hashtable(); /** * **/ private final static Integer USER_DEFINED = new Integer(-1); /** * **/ private final static Integer LINK_REQUEST_RCVD = new Integer(0); /** * **/ private final static Integer LINKED = new Integer(1); /** * **/ public LinkServer(LinkServerListener listener) { super(); setPackageName(PackageName); addPackageMethod(LinkPeer.requestLinkTag,// LinkPeer.requestLinkAliases,true); addPackageMethod(LinkPeer.requestUnlinkTag,// LinkPeer.requestUnlinkAliases,true); addPackageMethod(LinkPeer.statusLinkedTag,// LinkPeer.statusLinkedAliases,false); addPackageMethod(LinkPeer.statusUnlinkedTag,// LinkPeer.statusUnlinkedAliases,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(LinkPeer.requestLinkTag.equals(method)) { setLinked(source,LINK_REQUEST_RCVD); listener.peerLinkRequest(source,body); } else if(LinkPeer.requestUnlinkTag.equals(method)) { if(isLinked(source)) { unlink(source); listener.peerUnlinked(source,body); } } } /** * **/ public void reset(UNL source) { if(isLinked(source)) { removeLinked(source); listener.peerUnlinked(source,null); } } /** * **/ public Enumeration getAllLinked() { return linked.keys(); } /** * **/ public boolean isLinked(UNL client) { return linked.get(client) != null; } /** * **/ public boolean wantsLink(UNL client) { return linked.get(client) == LINK_REQUEST_RCVD; } /** * **/ private void setLinked(UNL client, Integer state) { linked.put(client,state); } /** * **/ public void setLinked(UNL client) { linked.put(client,USER_DEFINED); } /** * **/ public void removeLinked(UNL client) { linked.remove(client); } /** * **/ public void link(UNL client, String body) { setLinked(client,LINKED); try { center.sendChecked(client,LinkPeer.PackageName,// LinkPeer.statusLinkedTag,body); } catch(PSYCDeliveryException e) { removeLinked(client); listener.peerUnlinked(client,null); } } /** * **/ public void link(UNL client) { link(client,null); } /** * **/ public void unlink(UNL client, String body) { removeLinked(client); try { center.sendChecked(client,LinkPeer.PackageName,// LinkPeer.statusUnlinkedTag,body); } catch(PSYCDeliveryException e) { } } /** * **/ public void unlink(UNL client) { unlink(client,null); } }