package lava.net.psyc.packages; import lava.net.common.UNL; import lava.net.psyc.PSYCDeliveryException; import lava.net.psyc.PSYCMessageCenter; import lava.net.psyc.PSYCPackage; /** * **/ public class Echo extends Skeleton { /** * **/ public final static String PackageName = "Echo"; /** * **/ public final static String requestEchoTag = "_request_echo"; /** * **/ private final static String[] requestEchoAliases = {"request_echo"}; /** * **/ public final static String echoTag = "_echo_request"; /** * **/ private final static String[] echoAliases = {"echo_request"}; class DummyListener implements EchoListener { /** * **/ public void echoPing(UNL source, String body) { try { Echo.this.pong(source,body); } catch(PSYCDeliveryException e) { } } /** * **/ public void echoPong(UNL source, String body) { } } /** * **/ private EchoListener listener = new DummyListener(); /** * **/ public Echo() { this (null); } /** * **/ public Echo(EchoListener listener) { super(); setPackageName(PackageName); addPackageMethod(requestEchoTag,requestEchoAliases,true); addPackageMethod(echoTag,echoAliases,true); if(listener != null) this.listener = listener; } /** * **/ public boolean understands() { return !(listener instanceof DummyListener); } /** * **/ public void received(UNL source, String method, String body) { if(requestEchoTag.equals(method)) listener.echoPing(source,body); else if(echoTag.equals(method)) listener.echoPong(source,body); } /** * **/ public void ping(UNL target, String body) throws PSYCDeliveryException { center.sendChecked(target,PackageName,requestEchoTag,body); } /** * **/ public void ping(UNL target) throws PSYCDeliveryException { ping(target,null); } /** * **/ public void pong(UNL target, String body) throws PSYCDeliveryException { center.sendChecked(target,PackageName,echoTag,body); } /** * **/ public void pong(UNL target) throws PSYCDeliveryException { pong(target,null); } }