1 | package com.framsticks.remote; |
---|
2 | |
---|
3 | import com.framsticks.core.Node; |
---|
4 | import com.framsticks.core.Path; |
---|
5 | import com.framsticks.params.AccessInterface; |
---|
6 | import com.framsticks.params.CompositeParam; |
---|
7 | import com.framsticks.params.FramsClass; |
---|
8 | import com.framsticks.core.Instance; |
---|
9 | import com.framsticks.util.dispatching.Future; |
---|
10 | import com.framsticks.util.StateFunctor; |
---|
11 | import com.framsticks.util.Stopwatch; |
---|
12 | import org.apache.log4j.Logger; |
---|
13 | import static com.framsticks.util.lang.Containers.filterInstanceof; |
---|
14 | import com.framsticks.util.dispatching.RunAt; |
---|
15 | |
---|
16 | /** |
---|
17 | * @author Piotr Sniegowski |
---|
18 | */ |
---|
19 | public class RecursiveFetcher { |
---|
20 | |
---|
21 | private final static Logger log = Logger.getLogger(RecursiveFetcher.class.getName()); |
---|
22 | |
---|
23 | protected final Instance instance; |
---|
24 | protected final StateFunctor stateFunctor; |
---|
25 | protected int dispatched; |
---|
26 | protected final Stopwatch stopwatch = new Stopwatch(); |
---|
27 | |
---|
28 | public RecursiveFetcher(Instance instance, final Path path, StateFunctor stateFunctor) { |
---|
29 | this.instance = instance; |
---|
30 | this.stateFunctor = stateFunctor; |
---|
31 | dispatched = 1; |
---|
32 | process(path); |
---|
33 | } |
---|
34 | |
---|
35 | protected void finished() { |
---|
36 | assert instance.isActive(); |
---|
37 | log.info("recursively fetched in " + stopwatch); |
---|
38 | stateFunctor.call(null); |
---|
39 | } |
---|
40 | |
---|
41 | protected void process(final Path path) { |
---|
42 | assert instance.isActive(); |
---|
43 | if (path == null || !path.isResolved()) { |
---|
44 | log.warn("path " + path + " is not resolved - skipping"); |
---|
45 | } else { |
---|
46 | AccessInterface access = instance.bindAccess(path); |
---|
47 | FramsClass framsClass = access.getFramsClass(); |
---|
48 | assert framsClass != null; |
---|
49 | for (CompositeParam p : filterInstanceof(access.getParams(), CompositeParam.class)) { |
---|
50 | Object child = access.get(p, Object.class); |
---|
51 | final Path childPath = path.appendNode(new Node(p, child)); |
---|
52 | if (childPath.isResolved() && instance.getInfoFromCache(childPath) != null) { |
---|
53 | ++dispatched; |
---|
54 | instance.invokeLater(new RunAt<Instance>() { |
---|
55 | @Override |
---|
56 | public void run() { |
---|
57 | fetch(childPath); |
---|
58 | } |
---|
59 | }); |
---|
60 | continue; |
---|
61 | } |
---|
62 | ++dispatched; |
---|
63 | instance.resolve(childPath, new Future<Path>() { |
---|
64 | @Override |
---|
65 | public void result(Path result, Exception e) { |
---|
66 | assert instance.isActive(); |
---|
67 | if (e != null) { |
---|
68 | log.error(e); |
---|
69 | return; |
---|
70 | } |
---|
71 | fetch(result); |
---|
72 | } |
---|
73 | }); |
---|
74 | } |
---|
75 | } |
---|
76 | --dispatched; |
---|
77 | if (dispatched == 0) { |
---|
78 | finished(); |
---|
79 | } |
---|
80 | } |
---|
81 | |
---|
82 | protected void fetch(final Path path) { |
---|
83 | instance.fetchValues(path, new StateFunctor() { |
---|
84 | @Override |
---|
85 | public void call(Exception e) { |
---|
86 | if (e != null) { |
---|
87 | log.error("failed to fetch values for " + path + ": " + e); |
---|
88 | process(null); |
---|
89 | return; |
---|
90 | } |
---|
91 | process(path); |
---|
92 | } |
---|
93 | }); |
---|
94 | } |
---|
95 | |
---|
96 | } |
---|