1 | package com.framsticks.util.dispatching; |
---|
2 | |
---|
3 | import org.apache.log4j.Logger; |
---|
4 | |
---|
5 | import com.framsticks.util.StateFunctor; |
---|
6 | |
---|
7 | /** |
---|
8 | * @author Piotr Sniegowski |
---|
9 | */ |
---|
10 | public abstract class Dispatching { |
---|
11 | private static final Logger log = Logger.getLogger(Dispatching.class); |
---|
12 | |
---|
13 | public static boolean isThreadSafe() { |
---|
14 | return true; |
---|
15 | } |
---|
16 | |
---|
17 | public static <C> void invokeLaterOrNow(Dispatcher<C> dispatcher, RunAt<? extends C> runnable) { |
---|
18 | if (dispatcher.isActive()) { |
---|
19 | runnable.run(); |
---|
20 | return; |
---|
21 | } |
---|
22 | dispatcher.invokeLater(runnable); |
---|
23 | } |
---|
24 | |
---|
25 | //TODO RunAt StateFunctor |
---|
26 | public static <C> void dispatchOk(Dispatcher<C> dispatcher, final StateFunctor stateFunctor) { |
---|
27 | dispatcher.invokeLater(new RunAt<C>() { |
---|
28 | @Override |
---|
29 | public void run() { |
---|
30 | stateFunctor.call(null); |
---|
31 | } |
---|
32 | }); |
---|
33 | } |
---|
34 | |
---|
35 | // public static boolean assertInvokeLater(Dispatcher dispatcher, RunAt runnable) { |
---|
36 | // dispatcher.invokeLater(runnable); |
---|
37 | // return true; |
---|
38 | // } |
---|
39 | |
---|
40 | public static <P, C> void invokeDispatch(Dispatcher<P> dispatcher, final Dispatcher<C> finalDispatcher, final RunAt<C> runnable) { |
---|
41 | dispatcher.invokeLater(new RunAt<P>() { |
---|
42 | @Override |
---|
43 | public void run() { |
---|
44 | finalDispatcher.invokeLater(runnable); |
---|
45 | } |
---|
46 | }); |
---|
47 | } |
---|
48 | |
---|
49 | public static void sleep(int milliseconds) { |
---|
50 | try { |
---|
51 | java.lang.Thread.sleep(milliseconds, 0); |
---|
52 | } catch (InterruptedException e) { |
---|
53 | |
---|
54 | } |
---|
55 | } |
---|
56 | |
---|
57 | @SuppressWarnings("unchecked") |
---|
58 | public static void dispatcherGuardedInvoke(Joinable joinable, RunAt<?> runnable) { |
---|
59 | if (joinable instanceof Dispatcher) { |
---|
60 | invokeLaterOrNow(Dispatcher.class.cast(joinable), runnable); |
---|
61 | return; |
---|
62 | } |
---|
63 | runnable.run(); |
---|
64 | } |
---|
65 | |
---|
66 | public static void use(final Joinable joinable, final JoinableParent owner) { |
---|
67 | log.debug("using " + joinable + " by " + owner); |
---|
68 | if (joinable.use(owner)) { |
---|
69 | log.debug("started " + joinable); |
---|
70 | } else { |
---|
71 | log.debug("start of " + joinable + " already happened"); |
---|
72 | } |
---|
73 | } |
---|
74 | |
---|
75 | |
---|
76 | public static void drop(final Joinable joinable, final JoinableParent owner) { |
---|
77 | log.debug("droping " + joinable + " by " + owner); |
---|
78 | if (joinable.drop(owner)) { |
---|
79 | log.debug("stoped " + joinable); |
---|
80 | } else { |
---|
81 | log.debug("stop of " + joinable + " deferred"); |
---|
82 | } |
---|
83 | } |
---|
84 | |
---|
85 | public static void join(Joinable joinable) throws InterruptedException { |
---|
86 | log.debug("joining " + joinable); |
---|
87 | try { |
---|
88 | joinable.join(); |
---|
89 | } catch (InterruptedException e) { |
---|
90 | log.debug("failed to join " + joinable); |
---|
91 | throw e; |
---|
92 | } |
---|
93 | log.debug("joined " + joinable); |
---|
94 | } |
---|
95 | |
---|
96 | public static void childChangedState(final JoinableParent parent, final Joinable joinable, final JoinableState state) { |
---|
97 | dispatcherGuardedInvoke(joinable, new RunAt<Object>() { |
---|
98 | @Override |
---|
99 | public void run() { |
---|
100 | log.debug("joinable " + joinable + " is notifying parent " + parent + " about change to " + state); |
---|
101 | parent.childChangedState(joinable, state); |
---|
102 | } |
---|
103 | }); |
---|
104 | } |
---|
105 | |
---|
106 | public static void wait(Object object, long millis) { |
---|
107 | try { |
---|
108 | object.wait(millis); |
---|
109 | } catch (InterruptedException e) { |
---|
110 | } |
---|
111 | } |
---|
112 | |
---|
113 | public static void joinAbsolutely(Joinable joinable) { |
---|
114 | log.info("joining absolutely " + joinable); |
---|
115 | while (true) { |
---|
116 | try { |
---|
117 | Dispatching.join(joinable); |
---|
118 | return; |
---|
119 | } catch (InterruptedException e) { |
---|
120 | // throw new FramsticksException().msg("failed to join").arg("dispatcher", dispatcher).cause(e); |
---|
121 | } |
---|
122 | log.info("waiting for " + joinable); |
---|
123 | sleep(500); |
---|
124 | } |
---|
125 | } |
---|
126 | |
---|
127 | |
---|
128 | |
---|
129 | |
---|
130 | } |
---|