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

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

HIGHLIGHTS:

CHANGELOG:
Make ProcedureParam? hold only ValueParams?.

Use id instead of names when naming gui components internally.

Basic procedure calling in GUI.

The actual procedure call is currently only backed
by the ObjectInstance?.

Add UnimplementedException?.

Improve naming of various gui elements.

Allow easy navigating in FEST Swing testing.

Add optional explicit order attribute to FramsClassAnnotation?.

That's because java reflection does return declared members
in any specific order. That ordering is needed only for
classes that have no representation in framsticks and need
a deterministic ordering of params.

Add ControlOwner? interface.

Add test for procedure calling in Browser.

First version of ParamAnnotation? for procedures.

Development of ProcedureParam?.

Add draft version of ProcedureParam? implementation in ReflectionAccess?.

Allow viewing FramsClasses? in gui Browser.

Extract ResourceBuilder? from ModelBuilder?.

Remove internalId from Param.

It was currently completely not utilised. Whether it is still needed
after introduction of ParamAnnotation? is arguable.

Add remaining param attributes to ParamAnnotation?.

Change AutoBuilder? semantics.

AutoBuilder? returns list of objects that are to be appended
with methods @AutoAppendAnnotation?.

This allows to omit explicit addition of ModelPackage? to instance
if the instance uses ModelBuilder? (registration of ModelPackage? comes
from schema).

Fix params ordering problem in auto created FramsClasses?.

Improve ObjectInstance?.

Several fixes to ModelBuilder?.

Improve test for ObjectInstance? in Browser.

Make initialization of robot static.

With robot recreated for second browser test, the test hanged
deep in AWT.

Add base convenience base test for Browser tests.

More tests to ObjectInstance?.

Rename Dispatcher.invokeLater() to dispatch().

Add assertDispatch.

It allows assertions in other threads, than TestNGInvoker.
Assertions are gathered after each method invocation and rethrown.

Use timeOut annotation attribute for tests involving some waiting.

Remove firstTask method (merge with joinableStart).

Clean up leftovers.

Remove unused FavouritesXMLFactory (the reading part is already
completely done with generic XmlLoader?, and writing part will be done
based on the same approach if needed).
Move UserFavourite? to the com.framsticks.gui.configuration package.

Remove GenotypeBrowser? as to specific.

This functionality will be available in ObjectInstance?.

Add interface ParamsPackage?.

Package containing registration of Java classes meant to use with
ReflectionAccess? may be in Instance using configuration.

Minor changes.

Make Group immutable.

Add AutoBuilder? interface extending Builder - only those would
be used to automatically build from XML.

Fix groups in FramsClass?.

Minor naming cleanup in Registry.

Add ModelComponent? interface.

All class creating the Model are implementing that interface.

Extract Model.build into ModelBuilder?.

ModelBuilder? will be compatible with other builders
and allow using it from configuration.

Fix NeuroConnection?.

Add synchronous get operation for dispatchers.

Rename JoinableMonitor? to Monitor.

Add ObjectInstance?.

This class is mainly for demonstration
and testing purposes.

Improve FramsServer? runner.

  • improve ExternalProcess? runner,
  • runner can kill the server but also react properly, when the server exists on it's own,
  • set default path to search for framsticks server installation,
  • add LoggingOutputListener?.
File size: 4.0 KB
Line 
1package com.framsticks.util.dispatching;
2
3import org.apache.log4j.Logger;
4
5import com.framsticks.util.StateFunctor;
6
7/**
8 * @author Piotr Sniegowski
9 */
10public 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 dispatchIfNotActive(Dispatcher<C> dispatcher, RunAt<? extends C> runnable) {
18                if (dispatcher.isActive()) {
19                        runnable.run();
20                        return;
21                }
22                dispatcher.dispatch(runnable);
23        }
24
25        //TODO RunAt StateFunctor
26        public static <C> void dispatchOk(Dispatcher<C> dispatcher, final StateFunctor stateFunctor) {
27                dispatcher.dispatch(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.dispatch(new RunAt<P>() {
42                        @Override
43                        public void run() {
44                                finalDispatcher.dispatch(runnable);
45                        }
46                });
47        }
48
49        public static void sleep(double seconds) {
50                try {
51                        java.lang.Thread.sleep((long) (seconds * 1000));
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                        dispatchIfNotActive(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        public static void drop(final Joinable joinable, final JoinableParent owner) {
76                log.debug("droping " + joinable + " by " + owner);
77                if (joinable.drop(owner)) {
78                        log.debug("stoped " + joinable);
79                } else {
80                        log.debug("stop of " + joinable + " deferred");
81                }
82        }
83
84        public static void join(Joinable joinable) throws InterruptedException {
85                log.debug("joining " + joinable);
86                try {
87                        joinable.join();
88                } catch (InterruptedException e) {
89                        log.debug("failed to join " + joinable);
90                        throw e;
91                }
92                log.debug("joined " + joinable);
93        }
94
95        public static void childChangedState(final JoinableParent parent, final Joinable joinable, final JoinableState state) {
96                dispatcherGuardedInvoke(joinable, new RunAt<Object>() {
97                        @Override
98                        public void run() {
99                                log.debug("joinable " + joinable + " is notifying parent " + parent + " about change to " + state);
100                                parent.childChangedState(joinable, state);
101                        }
102                });
103        }
104
105        public static void wait(Object object, long millis) {
106                try {
107                        synchronized (object) {
108                                object.wait(millis);
109                        }
110                } catch (InterruptedException e) {
111                }
112        }
113
114        public static void joinAbsolutely(Joinable joinable) {
115                log.debug("joining absolutely " + joinable);
116                while (true) {
117                        try {
118                                Dispatching.join(joinable);
119                                return;
120                        } catch (InterruptedException e) {
121                                // throw new FramsticksException().msg("failed to join").arg("dispatcher", dispatcher).cause(e);
122                        }
123                        log.debug("waiting for " + joinable);
124                        wait(joinable, 500);
125                }
126        }
127
128        public interface Query<T> {
129                T get();
130        }
131
132        public static class QueryRunner<T, C> extends RunAt<C> {
133                protected final Query<T> query;
134                T result;
135                boolean ready = false;
136
137                /**
138                 * @param query
139                 */
140                public QueryRunner(Query<T> query) {
141                        this.query = query;
142                }
143
144                @Override
145                public void run() {
146                        result = query.get();
147                        synchronized (this) {
148                                ready = true;
149                                this.notifyAll();
150                        }
151                }
152
153                public T get() {
154                        synchronized (this) {
155                                while (!ready) {
156                                        try {
157                                                this.wait(100);
158                                        } catch (InterruptedException e) {
159                                        }
160                                }
161                        }
162                        return result;
163                }
164        }
165
166        public static <T, C> T get(Dispatcher<C> dispatcher, Query<T> query) {
167                QueryRunner<T, C> runner = new QueryRunner<T, C>(query);
168                dispatcher.dispatch(runner);
169                return runner.get();
170        }
171
172
173
174}
Note: See TracBrowser for help on using the repository browser.