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

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

HIGHLIGHTS:

  • improve tree side notes
  • improve GUI layout
  • add foldable list of occured events to EventControl?
  • improve automatic type conversion in proxy listeners
  • implement several Access functionalities as algorithms independent of Access type
  • introduce draft base classes for distributed experiments
  • automatically register dependant Java classes to FramsClass? registry
  • add testing prime experiment and configuration
  • simplify and improve task dispatching

CHANGELOG:
Improve task dispatching in RemoteTree?.

GUI no longer hangs on connection problems.

Make all dispatchers joinables.

Refactorize Thread dispatcher.

Remove Task and PeriodicTask?.

Use Java utilities in those situations.

Reworking tasks dispatching.

Fix bug in EventControl? listener dispatching.

Minor improvements.

Add testing configuration for ExternalProcess? in GUI.

More improvement to prime.

Support for USERREADONLY in GUI.

Add that flag to various params in Java classes.

Remove redundant register clauses from several FramsClassAnnotations?.

Automatically gather and register dependant classes.

Add configuration for prime.

Improve Simulator class.

Add prime.xml configuration.

Introduce draft Experiment and Simulator classes.

Add prime experiment tests.

Enclose typical map with listeners into SimpleUniqueList?.

Needfile works in GUI.

Improve needfile handling in Browser.

More improvement with NeedFile?.

Implementing needfile.

Update test.

Rename ChangeEvent? to TestChangeEvent?.

Automatic argument type search in RemoteTree? listeners.

MultiParamLoader? uses AccessProvider?. By default old implementation
enclosed in AccessStash? or Registry.

Minor changes.

Rename SourceInterface? to Source.

Also improve toString of File and ListSource?.

Remove unused SimpleSource? class.

Add clearing in HistoryControl?.

Show entries in table at EventControl?.

Improve EventControl?.

Add listeners registration to EventControl?.

Add foldable table to HistoryControl?.

Add control row to Procedure and Event controls.

Improve layout of controls.

Another minor change to gui layout.

Minor improvement in the SliderControl?.

Minor changes.

Move ReflectionAccess?.Backend to separate file.

It was to cluttered.

Cleanup in ReflectionAccess?.

Move setMin, setMax, setDef to AccessOperations?.

Extract loading operation into AccessOperations?.

Append Framsticks to name of UnsupportedOperationException?.

The java.lang.UnsupportedOperationException? was shadowing this class.

Rename params.Util to params.ParamsUtil?.

Several improvements.

Minor changes.

Implement revert functionality.

Improve local changes management.

Minor improvement.

Remove methods rendered superfluous after SideNoteKey? improvement.

Improve SideNoteKey?.

It is now generic type, so explicit type specification at
call site is no more needed.

Introduce SideNoteKey? interface.

Only Objects implementing that key may be used as side note keys.

Minor improvements.

Use strings instead of ValueControls? in several gui mappings.

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