source: java/main/src/main/java/com/framsticks/util/dispatching/Dispatching.java @ 100

Last change on this file since 100 was 100, checked in by psniegowski, 11 years ago

HIGHLIGHTS:

  • add <include/> to configuration
  • add side notes to tree
    • used to store arbitrary information alongside the tree structure
  • migrate to log4j2
    • supports lazy string evaluation of passed arguments
  • improve GUI tree
    • it stays in synchronization with actual state (even in high load test scenario)
  • improve panel management in GUI
  • make loading objects in GUI more lazy
  • offload parsing to connection receiver thread
    • info parsing
    • first step of objects parsing
  • fix connection parsing bug (eof in long values)
  • support zero-arguments procedure in table view

CHANGELOG:
Implement procedure calls from table view.

Refactorization around procedures in tables.

Add table editor for buttons.

Render buttons in the the list view.

Further improve Columns.

Add Column class for TableModel?.

Accept also non-arguments ProcedureParams? in tableView.

Increase maximal TextAreaControl? size.

Add tooltip to ProcedureControl?.

Fix bug of interpreting eofs in long values by connection reader.

Further rework connection parsing.

Simplify client connection processing.

Test ListChange? modification.

Test ListChange? events with java server.

Add TestChild?.

Fix bug with fast deregistering when connecting to running server.

Another minor refactorization in TreeOperations?.

Fix bug in SimpleAbstractAccess? loading routine.

Another minor improvement.

Minor change.

Make reading of List objects two-phase.

Another minor change.

Dispatch parsing into receiver thread.

Another step.

Enclose passing value in ObjectParam? case in closure.

Minor step.

Minor change on way to offload parsing.

Temporarily comment out single ValueParam? get.

It will be generalized to multi ValueParam?.

Process info in receiver thread.

Add DispatchingExceptionHandler?.

Make waits in browser test longer.

Use FETCHED_MARK.

It is honored in GUI, where it used to decide whether to get values

after user action.

It is set in standard algorithm for processing fetched values.

Add remove operation to side notes.

Make loading more lazy.

Improve loading policy.

On node choose load itself, on node expansion, load children.

Minor improvement.

Fix bug with panel interleaving.

Minor improvements.

Improve panel management.

More cleaning around panels.

Reorganize panels.

Further improve tree.

Fix bug in TreeModel?.

Remove children from TreeNode?.

Implement TreeNode? hashCode and equals.

Make TreeNode? delegate equals and hashcode to internal reference.

Move listeners from TreeNode? to side notes.

Store path.textual as a side note.

Side note params instead of accesses for objects.

More refactorizations.

In TreeNode? bindAccess based on side notes.

Minor step.

Hide createAccess.

Rename AccessInterface? to Access.

Minor changes.

Several improvements in high load scenarios.

Change semantics of ArrayListAccess?.set(index, null);

It now removes the element, making list shorter
(it was set to null before).

Add path remove handler.

Handle exceptions in Connection.

Update .gitignore

Configure logging to file.

Move registration to TreeModel?.

Further refactorization.

Minor refactorization.

Minor improvements.

Use specialized event also for Modify action of ListChange?.

Use remove events.

Use the insertion events for tree.

Further improve tree refreshing.

Further improve reacting on events in GUI.

Fix problem with not adding objects on addition list change.

Migrate to log4j lazy String construction interface.

Migrate imports to log4j2.

Drop dependency on adapter to version 1.2.

Switch log4j implementation to log4j2.

Add dirty mark to the NodeAtFrame?.

Make selecting in AccessInterfaces? type safe.

Ignore containers size settings in Model and Genotype.

Use tree side notes to remember local changes and panels.

Add sideNotes to tree.

They will be used to store various accompanying information
right in the tree.

Use ReferenceIdentityMap? from apache in TreeNode?.

It suits the need perfectly (weak semantics on both key and value).

Make ArrayListParam? do not react size changes.

Guard in TableModel? before not yet loaded objects.

Add <include/> clause and AutoInjector?.

Extract common columns configuration to separate xml,
that can be included by other configurations.

File size: 6.4 KB
Line 
1package com.framsticks.util.dispatching;
2
3import org.apache.logging.log4j.Logger;
4import org.apache.logging.log4j.LogManager;
5
6import com.framsticks.util.FramsticksException;
7
8/**
9 * @author Piotr Sniegowski
10 */
11public abstract class Dispatching {
12        private static final Logger log = LogManager.getLogger(Dispatching.class);
13
14        public static boolean isThreadSafe() {
15                return true;
16        }
17
18        public static <C> void dispatchIfNotActive(Dispatcher<C> dispatcher, RunAt<? extends C> runnable) {
19                if (dispatcher.isActive()) {
20                        runnable.runAt();
21                        return;
22                }
23                dispatcher.dispatch(runnable);
24        }
25
26        // public static boolean assertInvokeLater(Dispatcher dispatcher, RunAt runnable) {
27        //      dispatcher.invokeLater(runnable);
28        //      return true;
29        // }
30
31        public static <P, C> void invokeDispatch(Dispatcher<P> dispatcher, final Dispatcher<C> finalDispatcher, final RunAt<C> runnable) {
32                dispatcher.dispatch(new RunAt<P>(runnable) {
33                        @Override
34                        protected void runAt() {
35                                finalDispatcher.dispatch(runnable);
36                        }
37                });
38        }
39
40        public static void sleep(double seconds) {
41                log.debug("sleeping");
42                try {
43                        java.lang.Thread.sleep((long) (seconds * 1000));
44                } catch (InterruptedException e) {
45
46                }
47                log.debug("slept");
48        }
49
50        @SuppressWarnings("unchecked")
51        public static void dispatcherGuardedInvoke(Joinable joinable, RunAt<?> runnable) {
52                if (joinable instanceof Dispatcher) {
53                        dispatchIfNotActive(Dispatcher.class.cast(joinable), runnable);
54                        return;
55                }
56                runnable.runAt();
57        }
58
59        public static void use(final Joinable joinable, final JoinableParent owner) {
60                log.debug("using {} by {}", joinable, owner);
61                if (joinable.use(owner)) {
62                        log.debug("started {}", joinable);
63                } else {
64                        log.debug("start of {} already happened", joinable);
65                }
66        }
67
68        public static void drop(final Joinable joinable, final JoinableParent owner) {
69                log.debug("droping {} by {}", joinable, owner);
70                if (joinable.drop(owner)) {
71                        log.debug("stoped {}", joinable);
72                } else {
73                        log.debug("stop of {} deferred", joinable);
74                }
75        }
76
77        public static void join(Joinable joinable) throws InterruptedException {
78                log.debug("joining {}", joinable);
79                try {
80                        joinable.join();
81                } catch (InterruptedException e) {
82                        log.debug("failed to join {}", joinable);
83                        throw e;
84                }
85                log.debug("joined {}", joinable);
86        }
87
88        public static void childChangedState(final JoinableParent parent, final Joinable joinable, final JoinableState state) {
89                if (state.ordinal() <= JoinableState.RUNNING.ordinal()) {
90                        return;
91                }
92                dispatcherGuardedInvoke(joinable, new RunAt<Object>(ThrowExceptionHandler.getInstance()) {
93                        @Override
94                        protected void runAt() {
95                                log.debug("joinable {} is notifying parent {} about change to {}", joinable, parent, state);
96                                parent.childChangedState(joinable, state);
97                        }
98                });
99        }
100
101        public static void wait(Object object, long millis) {
102                try {
103                        synchronized (object) {
104                                object.wait(millis);
105                        }
106                } catch (InterruptedException e) {
107                }
108        }
109
110        public static void joinAbsolutely(Joinable joinable) {
111                log.debug("joining absolutely {}", joinable);
112                while (true) {
113                        try {
114                                Dispatching.join(joinable);
115                                return;
116                        } catch (InterruptedException e) {
117                                // throw new FramsticksException().msg("failed to join").arg("dispatcher", dispatcher).cause(e);
118                        }
119                        log.debug("waiting for {}", joinable);
120                        wait(joinable, 500);
121                }
122        }
123
124        public interface Query<T> extends ExceptionResultHandler {
125                T get();
126        }
127
128        public static abstract class QueryHandler<T> implements Query<T> {
129                ExceptionResultHandler handler;
130
131                /**
132                 * @param handler
133                 */
134                public QueryHandler(ExceptionResultHandler handler) {
135                        this.handler = handler;
136                }
137
138                @Override
139                public void handle(FramsticksException exception) {
140                        handler.handle(exception);
141                }
142        }
143
144        public static class QueryRunner<T, C> extends RunAt<C> {
145                protected final Query<T> query;
146                T result;
147                boolean ready = false;
148
149                /**
150                 * @param query
151                 */
152                public QueryRunner(Query<T> query) {
153                        super(query);
154                        this.query = query;
155                }
156
157                @Override
158                protected void runAt() {
159                        result = query.get();
160                        synchronized (this) {
161                                ready = true;
162                                this.notifyAll();
163                        }
164                }
165
166                public T get() {
167                        synchronized (this) {
168                                while (!ready) {
169                                        try {
170                                                this.wait(100);
171                                        } catch (InterruptedException e) {
172                                        }
173                                }
174                        }
175                        return result;
176                }
177        }
178
179        public static <T, C> T get(Dispatcher<C> dispatcher, Query<T> query) {
180                QueryRunner<T, C> runner = new QueryRunner<T, C>(query);
181                dispatcher.dispatch(runner);
182                return runner.get();
183        }
184
185        public static class DispatcherWaiter<C, T extends Dispatcher<C> & Joinable> implements Dispatcher<C> {
186                // protected boolean done = false;
187                protected final T dispatcher;
188                protected RunAt<? extends C> runnable;
189
190                /**
191                 * @param joinable
192                 */
193                public DispatcherWaiter(T dispatcher) {
194                        this.dispatcher = dispatcher;
195                }
196
197                public synchronized void waitFor() {
198                        while ((runnable == null) && (dispatcher.getState().ordinal() <= JoinableState.RUNNING.ordinal())) {
199                                try {
200                                        this.wait();
201                                } catch (InterruptedException e) {
202                                }
203                        }
204                        if (runnable != null) {
205                                runnable.run();
206                        }
207
208                }
209
210                @Override
211                public boolean isActive() {
212                        return dispatcher.isActive();
213                }
214
215                @Override
216                public synchronized void dispatch(RunAt<? extends C> runnable) {
217                        this.runnable = runnable;
218                        this.notifyAll();
219                }
220
221        }
222
223        public static class Waiter {
224                protected boolean done = false;
225
226                protected final double timeOut;
227                protected final ExceptionResultHandler handler;
228
229                /**
230                 * @param timeOut
231                 */
232                public Waiter(double timeOut, ExceptionResultHandler handler) {
233                        this.timeOut = timeOut;
234                        this.handler = handler;
235                }
236
237                public synchronized void pass() {
238                        done = true;
239                        this.notify();
240                }
241
242                public synchronized void waitFor() {
243                        long end = System.currentTimeMillis() + (int)(timeOut * 1000);
244                        while ((!done) && System.currentTimeMillis() < end) {
245                                try {
246                                        this.wait(end - System.currentTimeMillis());
247                                } catch (InterruptedException e) {
248                                        break;
249                                }
250                        }
251                        if (!done) {
252                                handler.handle(new FramsticksException().msg("waiter timed out"));
253                        }
254                }
255
256                public <T> Future<T> passInFuture(Class<T> type) {
257                        return new FutureHandler<T>(handler) {
258                                @Override
259                                protected void result(T result) {
260                                        Waiter.this.pass();
261                                }
262                        };
263                }
264        }
265
266
267        public static <C> void synchronize(Dispatcher<C> dispatcher, double seconds) {
268                final Waiter waiter = new Waiter(seconds, ThrowExceptionHandler.getInstance());
269                dispatcher.dispatch(new RunAt<C>(ThrowExceptionHandler.getInstance()) {
270                        @Override
271                        protected void runAt() {
272                                waiter.pass();
273                        }
274                });
275                waiter.waitFor();
276        }
277
278}
Note: See TracBrowser for help on using the repository browser.