[77] | 1 | package com.framsticks.communication; |
---|
| 2 | |
---|
[85] | 3 | import java.util.List; |
---|
| 4 | |
---|
[77] | 5 | import com.framsticks.communication.queries.RegistrationRequest; |
---|
[84] | 6 | import com.framsticks.util.dispatching.Dispatcher; |
---|
[85] | 7 | import com.framsticks.util.dispatching.Dispatching; |
---|
| 8 | import com.framsticks.util.dispatching.RunAt; |
---|
[77] | 9 | import com.framsticks.util.StateFunctor; |
---|
| 10 | import org.apache.log4j.Logger; |
---|
| 11 | |
---|
| 12 | |
---|
| 13 | /** |
---|
| 14 | * @author Piotr Sniegowski |
---|
| 15 | */ |
---|
[85] | 16 | public class Subscription<C> { |
---|
[77] | 17 | |
---|
[85] | 18 | private final static Logger log = Logger.getLogger(Subscription.class); |
---|
[77] | 19 | |
---|
[85] | 20 | private final ClientConnection connection; |
---|
| 21 | private final String path; |
---|
[77] | 22 | private final String registeredPath; |
---|
[85] | 23 | private final Dispatcher<C> dispatcher; |
---|
[77] | 24 | |
---|
| 25 | private EventCallback eventCallback; |
---|
| 26 | |
---|
[85] | 27 | public Subscription(ClientConnection connection, String path, String registeredPath, Dispatcher<C> dispatcher) { |
---|
[77] | 28 | this.connection = connection; |
---|
[85] | 29 | this.path = path; |
---|
[77] | 30 | this.registeredPath = registeredPath; |
---|
[85] | 31 | this.dispatcher = dispatcher; |
---|
[77] | 32 | } |
---|
| 33 | |
---|
| 34 | public String getPath () { |
---|
| 35 | return path; |
---|
| 36 | } |
---|
| 37 | |
---|
| 38 | public String getRegisteredPath() { |
---|
| 39 | return registeredPath; |
---|
| 40 | } |
---|
| 41 | |
---|
| 42 | @Override |
---|
| 43 | public String toString() { |
---|
| 44 | return path + "(" + registeredPath + ")"; |
---|
| 45 | } |
---|
| 46 | |
---|
[85] | 47 | public void unsubscribe(final StateFunctor stateFunctor) { |
---|
| 48 | //@todo remove that /cli/ prefix, when registeredPath will be a fully qualified path |
---|
| 49 | connection.send(new RegistrationRequest().register(false).path(registeredPath), new ResponseCallback<Connection>() { |
---|
| 50 | @Override |
---|
| 51 | public void process(Response response) { |
---|
| 52 | if (!response.getOk()) { |
---|
| 53 | log.error("failed to unsunscribe " + this + ": " + response.getComment()); |
---|
| 54 | stateFunctor.call(new Exception(response.getComment())); |
---|
| 55 | return; |
---|
| 56 | } |
---|
| 57 | assert response.hasFiles(); |
---|
| 58 | log.debug("unsunscribed " + this); |
---|
| 59 | stateFunctor.call(null); |
---|
| 60 | } |
---|
| 61 | }); |
---|
| 62 | } |
---|
[77] | 63 | |
---|
[85] | 64 | public EventCallback getEventCallback() { |
---|
| 65 | return eventCallback; |
---|
| 66 | } |
---|
[77] | 67 | |
---|
[85] | 68 | public Dispatcher<C> getDispatcher() { |
---|
| 69 | return dispatcher; |
---|
| 70 | } |
---|
[77] | 71 | |
---|
[85] | 72 | public void setEventCallback(EventCallback eventCallback) { |
---|
| 73 | this.eventCallback = eventCallback; |
---|
| 74 | } |
---|
[77] | 75 | |
---|
[85] | 76 | public void dispatchCall(final List<File> files) { |
---|
| 77 | Dispatching.invokeLaterOrNow(dispatcher, new RunAt<C>() { |
---|
| 78 | @Override |
---|
| 79 | public void run() { |
---|
| 80 | eventCallback.call(files); |
---|
| 81 | } |
---|
| 82 | }); |
---|
| 83 | } |
---|
[77] | 84 | } |
---|