package com.framsticks.communication; import java.util.List; import com.framsticks.communication.queries.RegistrationRequest; import com.framsticks.util.dispatching.Dispatcher; import com.framsticks.util.dispatching.Dispatching; import com.framsticks.util.dispatching.RunAt; import com.framsticks.util.FramsticksException; import com.framsticks.util.StateFunctor; import org.apache.log4j.Logger; /** * @author Piotr Sniegowski */ public class Subscription { private final static Logger log = Logger.getLogger(Subscription.class); private final ClientConnection connection; private final String path; private final String registeredPath; private final Dispatcher dispatcher; private EventCallback eventCallback; public Subscription(ClientConnection connection, String path, String registeredPath, Dispatcher dispatcher) { this.connection = connection; this.path = path; this.registeredPath = registeredPath; this.dispatcher = dispatcher; } public String getPath () { return path; } public String getRegisteredPath() { return registeredPath; } @Override public String toString() { return path + "(" + registeredPath + ")"; } public void unsubscribe(final StateFunctor stateFunctor) { //@todo remove that /cli/ prefix, when registeredPath will be a fully qualified path connection.send(new RegistrationRequest().register(false).path(registeredPath), new ResponseCallback() { @Override public void process(Response response) { if (!response.getOk()) { log.error("failed to unsunscribe " + this + ": " + response.getComment()); stateFunctor.handle(new FramsticksException().msg("unsubscription failed").arg("comment", response.getComment())); return; } assert response.hasFiles(); log.debug("unsunscribed " + this); stateFunctor.call(); } }); } public EventCallback getEventCallback() { return eventCallback; } public Dispatcher getDispatcher() { return dispatcher; } public void setEventCallback(EventCallback eventCallback) { this.eventCallback = eventCallback; } public void dispatchCall(final List files) { Dispatching.dispatchIfNotActive(dispatcher, new RunAt() { @Override public void run() { eventCallback.call(files); } }); } }