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

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

HIGHLIGHTS:

  • loading f0 schema with XmlLoader?
  • use XmlLoader? to load configuration
  • introduce unified fork-join model of various entities

(Instances, Connections, GUI Frames, etc.),
all those entities clean up gracefully on
shutdown, which may be initialized by user
or by some entity

  • basing on above, simplify several organizing classes

(Observer, main class)

(to host native frams server process from Java level)

CHANGELOG:
Remove redundant Observer class.

Clean up in AbstractJoinable?.

Update ExternalProcess? class to changes in joining model.

Another sweep through code with FindBugs?.

Find bug with not joining RemoteInstance?.

Joining almost works.

Much improved joining model.

More improvement to joining model.

Add logging messages around joinable operations.

Rename methods in AbstractJoinable?.

Improve Joinable.

Rewrite of entity structure.

More simplifications with entities.

Further improve joinables.

Let Frame compose from JFrame instead of inheriting.

Add join classes.

Improvements of closing.

Add Builder interface.

Add FramsServerTest?.xml

FramsServer? may be configured through xml.

Make Framsticks main class an Observer of Entities.

Make Observer a generic type.

Remove variables regarding to removed endpoint.

Simplify observer (remove endpoints).

More changes to Observer and Endpoint.

Minor improvements.

Add OutputListener? to ExternalProcess?.

Improve testing of ExternalProcess?.

Add ExternalProcess? runner.

Rename the Program class to Framsticks.

Migrate Program to use XmlLoader? configuration.

First steps with configuration using XmlLoader?.

Fix several bugs.

Move all f0 classes to apriopriate package.

XmlLoader? is able to load Schema.

XmlLoader? is loading classes and props.

Add GroupBuilder?.

File size: 3.2 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 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}
Note: See TracBrowser for help on using the repository browser.