1 | package com.framsticks.communication; |
---|
2 | |
---|
3 | import com.framsticks.communication.queries.RegistrationRequest; |
---|
4 | import com.framsticks.util.AtOnceDispatcher; |
---|
5 | import com.framsticks.util.Dispatcher; |
---|
6 | import com.framsticks.util.StateFunctor; |
---|
7 | import org.apache.log4j.Logger; |
---|
8 | |
---|
9 | |
---|
10 | /** |
---|
11 | * @author Piotr Sniegowski |
---|
12 | */ |
---|
13 | public class Subscription { |
---|
14 | |
---|
15 | private final static Logger LOGGER = Logger.getLogger(Subscription.class); |
---|
16 | |
---|
17 | private final ClientConnection connection; |
---|
18 | private final String path; |
---|
19 | private final String registeredPath; |
---|
20 | private Dispatcher dispatcher = AtOnceDispatcher.instance; |
---|
21 | |
---|
22 | private EventCallback eventCallback; |
---|
23 | |
---|
24 | public Subscription(ClientConnection connection, String path, String registeredPath) { |
---|
25 | this.connection = connection; |
---|
26 | this.path = path; |
---|
27 | this.registeredPath = registeredPath; |
---|
28 | } |
---|
29 | |
---|
30 | public String getPath () { |
---|
31 | return path; |
---|
32 | } |
---|
33 | |
---|
34 | public String getRegisteredPath() { |
---|
35 | return registeredPath; |
---|
36 | } |
---|
37 | |
---|
38 | @Override |
---|
39 | public String toString() { |
---|
40 | return path + "(" + registeredPath + ")"; |
---|
41 | } |
---|
42 | |
---|
43 | public void setDispatcher(Dispatcher dispatcher) { |
---|
44 | this.dispatcher = dispatcher; |
---|
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).setPath(registeredPath), new ResponseCallback() { |
---|
50 | @Override |
---|
51 | public void process(Response response) { |
---|
52 | if (!response.getOk()) { |
---|
53 | LOGGER.error("failed to unsunscribe " + this + ": " + response.getComment()); |
---|
54 | stateFunctor.call(new Exception(response.getComment())); |
---|
55 | return; |
---|
56 | } |
---|
57 | assert response.hasFiles(); |
---|
58 | LOGGER.debug("unsunscribed " + this); |
---|
59 | stateFunctor.call(null); |
---|
60 | } |
---|
61 | }); |
---|
62 | } |
---|
63 | |
---|
64 | public EventCallback getEventCallback() { |
---|
65 | return eventCallback; |
---|
66 | } |
---|
67 | |
---|
68 | public Dispatcher getDispatcher() { |
---|
69 | return dispatcher; |
---|
70 | } |
---|
71 | |
---|
72 | public void setEventCallback(EventCallback eventCallback) { |
---|
73 | this.eventCallback = eventCallback; |
---|
74 | } |
---|
75 | } |
---|