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

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

HIGHLIGHTS:

  • add proper exception passing between communication sides:

if exception occur during handling client request, it is
automatically passed as comment to error response.

it may be used to snoop communication between peers

  • fix algorithm choosing text controls in GUI
  • allow GUI testing in virtual frame buffer (xvfb)

FEST had some problem with xvfb but workaround was found

supports tab-completion based on requests history

CHANGELOG:
Further improve handling of exceptions in GUI.

Add StatusBar? implementing ExceptionResultHandler?.

Make completion processing asynchronous.

Minor changes.

Improve completion in console.

Improve history in InteractiveConsole?.

First working version of DirectConsole?.

Minor changes.

Make Connection.address non final.

It is more suitable to use in configuration.

Improvement of consoles.

Improve PopupMenu? and closing of FrameJoinable?.

Fix BrowserTest?.

Found bug with FEST running under xvfb.

JButtonFixture.click() is not working under xvfb.
GuiTest? has wrapper which uses JButton.doClick() directly.

Store CompositeParam? param in TreeNode?.

Simplify ClientSideManagedConnection? connecting.

There is now connectedFunctor needed, ApplicationRequests? can be
send right after creation. They are buffered until the version
and features are negotiated.

Narow down interface of ClientSideManagedConnection?.

Allow that connection specialization send only
ApplicationRequests?.

Improve policy of text control choosing.

Change name of Genotype in BrowserTest?.

Make BrowserTest? change name of Genotype.

Minor change.

First working draft of TrackConsole?.

Simplify Consoles.

More improvements with gui joinables.

Unify initialization on gui joinables.

More rework of Frame based entities.

Refactorize structure of JFrames based entities.

Extract GuiTest? from BrowserBaseTest?.

Reorganize Console classes structure.

Add Collection view to JoinableCollection?.

Configure timeout in testing.

Minor changes.

Rework connections hierarchy.

Add Mode to the get operation.

Make get and set in Tree take PrimitiveParam?.

Unify naming of operations.

Make RunAt? use the given ExceptionHandler?.

It wraps the virtual runAt() method call with
try-catch passing exception to handler.

Force RunAt? to include ExceptionHandler?.

Improve ClientAtServer?.

Minor change.

Another sweep with FindBugs?.

Rename Instance to Tree.

Minor changes.

Minor changes.

Further clarify semantics of Futures.

Add FutureHandler?.

FutureHandler? is refinement of Future, that proxifies
exception handling to ExceptionResultHandler? given
at construction time.

Remove StateFunctor? (use Future<Void> instead).

Make Connection use Future<Void>.

Unparametrize *ResponseFuture?.

Remove StateCallback? not needed anymore.

Distinguish between sides of ResponseFuture?.

Base ResponseCallback? on Future (now ResponseFuture?).

Make asynchronous store taking Future for flags.

Implement storeValue in ObjectInstance?.

File size: 4.9 KB
Line 
1package com.framsticks.util.dispatching;
2
3import org.apache.log4j.Logger;
4
5import com.framsticks.util.FramsticksException;
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.runAt();
20                        return;
21                }
22                dispatcher.dispatch(runnable);
23        }
24
25        // public static boolean assertInvokeLater(Dispatcher dispatcher, RunAt runnable) {
26        //      dispatcher.invokeLater(runnable);
27        //      return true;
28        // }
29
30        public static <P, C> void invokeDispatch(Dispatcher<P> dispatcher, final Dispatcher<C> finalDispatcher, final RunAt<C> runnable) {
31                dispatcher.dispatch(new RunAt<P>(runnable) {
32                        @Override
33                        protected void runAt() {
34                                finalDispatcher.dispatch(runnable);
35                        }
36                });
37        }
38
39        public static void sleep(double seconds) {
40                log.debug("sleeping");
41                try {
42                        java.lang.Thread.sleep((long) (seconds * 1000));
43                } catch (InterruptedException e) {
44
45                }
46                log.debug("slept");
47        }
48
49        @SuppressWarnings("unchecked")
50        public static void dispatcherGuardedInvoke(Joinable joinable, RunAt<?> runnable) {
51                if (joinable instanceof Dispatcher) {
52                        dispatchIfNotActive(Dispatcher.class.cast(joinable), runnable);
53                        return;
54                }
55                runnable.runAt();
56        }
57
58        public static void use(final Joinable joinable, final JoinableParent owner) {
59                log.debug("using " + joinable + " by " + owner);
60                if (joinable.use(owner)) {
61                        log.debug("started " + joinable);
62                } else {
63                        log.debug("start of " + joinable + " already happened");
64                }
65        }
66
67        public static void drop(final Joinable joinable, final JoinableParent owner) {
68                log.debug("droping " + joinable + " by " + owner);
69                if (joinable.drop(owner)) {
70                        log.debug("stoped " + joinable);
71                } else {
72                        log.debug("stop of " + joinable + " deferred");
73                }
74        }
75
76        public static void join(Joinable joinable) throws InterruptedException {
77                log.debug("joining " + joinable);
78                try {
79                        joinable.join();
80                } catch (InterruptedException e) {
81                        log.debug("failed to join " + joinable);
82                        throw e;
83                }
84                log.debug("joined " + joinable);
85        }
86
87        public static void childChangedState(final JoinableParent parent, final Joinable joinable, final JoinableState state) {
88                dispatcherGuardedInvoke(joinable, new RunAt<Object>(ThrowExceptionHandler.getInstance()) {
89                        @Override
90                        protected void runAt() {
91                                log.debug("joinable " + joinable + " is notifying parent " + parent + " about change to " + state);
92                                parent.childChangedState(joinable, state);
93                        }
94                });
95        }
96
97        public static void wait(Object object, long millis) {
98                try {
99                        synchronized (object) {
100                                object.wait(millis);
101                        }
102                } catch (InterruptedException e) {
103                }
104        }
105
106        public static void joinAbsolutely(Joinable joinable) {
107                log.debug("joining absolutely " + joinable);
108                while (true) {
109                        try {
110                                Dispatching.join(joinable);
111                                return;
112                        } catch (InterruptedException e) {
113                                // throw new FramsticksException().msg("failed to join").arg("dispatcher", dispatcher).cause(e);
114                        }
115                        log.debug("waiting for " + joinable);
116                        wait(joinable, 500);
117                }
118        }
119
120        public interface Query<T> {
121                T get();
122        }
123
124        public static class QueryRunner<T, C> extends RunAt<C> {
125                protected final Query<T> query;
126                T result;
127                boolean ready = false;
128
129                /**
130                 * @param query
131                 */
132                public QueryRunner(Query<T> query) {
133                        //TODO TEH
134                        super(ThrowExceptionHandler.getInstance());
135                        this.query = query;
136                }
137
138                @Override
139                protected void runAt() {
140                        result = query.get();
141                        synchronized (this) {
142                                ready = true;
143                                this.notifyAll();
144                        }
145                }
146
147                public T get() {
148                        synchronized (this) {
149                                while (!ready) {
150                                        try {
151                                                this.wait(100);
152                                        } catch (InterruptedException e) {
153                                        }
154                                }
155                        }
156                        return result;
157                }
158        }
159
160        public static <T, C> T get(Dispatcher<C> dispatcher, Query<T> query) {
161                QueryRunner<T, C> runner = new QueryRunner<T, C>(query);
162                dispatcher.dispatch(runner);
163                return runner.get();
164        }
165
166        public static class Waiter {
167                protected boolean done = false;
168
169                protected final double timeOut;
170
171                /**
172                 * @param timeOut
173                 */
174                public Waiter(double timeOut) {
175                        this.timeOut = timeOut;
176                }
177
178                public synchronized void pass() {
179                        done = true;
180                        this.notify();
181                }
182
183                public synchronized void waitFor() {
184                        long end = System.currentTimeMillis() + (int)(timeOut * 1000);
185                        while ((!done) && System.currentTimeMillis() < end) {
186                                try {
187                                        this.wait(end - System.currentTimeMillis());
188                                } catch (InterruptedException e) {
189                                        break;
190                                }
191                        }
192                        if (!done) {
193                                throw new FramsticksException().msg("waiter timed out");
194                        }
195                }
196        }
197
198
199        public static <C> void synchronize(Dispatcher<C> dispatcher, double seconds) {
200                final Waiter waiter = new Waiter(seconds);
201                dispatcher.dispatch(new RunAt<C>(ThrowExceptionHandler.getInstance()) {
202                        @Override
203                        protected void runAt() {
204                                waiter.pass();
205                        }
206                });
207                waiter.waitFor();
208        }
209
210}
Note: See TracBrowser for help on using the repository browser.