1 | package com.framsticks.communication; |
---|
2 | |
---|
3 | import java.util.List; |
---|
4 | |
---|
5 | import com.framsticks.communication.queries.RegistrationRequest; |
---|
6 | import com.framsticks.util.dispatching.Dispatcher; |
---|
7 | import com.framsticks.util.dispatching.Dispatching; |
---|
8 | import com.framsticks.util.dispatching.RunAt; |
---|
9 | import com.framsticks.util.StateFunctor; |
---|
10 | import org.apache.log4j.Logger; |
---|
11 | |
---|
12 | |
---|
13 | /** |
---|
14 | * @author Piotr Sniegowski |
---|
15 | */ |
---|
16 | public class Subscription<C> { |
---|
17 | |
---|
18 | private final static Logger log = Logger.getLogger(Subscription.class); |
---|
19 | |
---|
20 | private final ClientConnection connection; |
---|
21 | private final String path; |
---|
22 | private final String registeredPath; |
---|
23 | private final Dispatcher<C> dispatcher; |
---|
24 | |
---|
25 | private EventCallback eventCallback; |
---|
26 | |
---|
27 | public Subscription(ClientConnection connection, String path, String registeredPath, Dispatcher<C> dispatcher) { |
---|
28 | this.connection = connection; |
---|
29 | this.path = path; |
---|
30 | this.registeredPath = registeredPath; |
---|
31 | this.dispatcher = dispatcher; |
---|
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 | |
---|
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 | } |
---|
63 | |
---|
64 | public EventCallback getEventCallback() { |
---|
65 | return eventCallback; |
---|
66 | } |
---|
67 | |
---|
68 | public Dispatcher<C> getDispatcher() { |
---|
69 | return dispatcher; |
---|
70 | } |
---|
71 | |
---|
72 | public void setEventCallback(EventCallback eventCallback) { |
---|
73 | this.eventCallback = eventCallback; |
---|
74 | } |
---|
75 | |
---|
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 | } |
---|
84 | } |
---|