source: java/main/src/main/java/com/framsticks/util/dispatching/AbstractJoinable.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: 4.8 KB
Line 
1package com.framsticks.util.dispatching;
2
3import java.util.Collections;
4import java.util.HashSet;
5import java.util.LinkedList;
6import java.util.List;
7import java.util.Set;
8
9import javax.annotation.Nonnull;
10
11import org.apache.commons.collections.CollectionUtils;
12import org.apache.logging.log4j.Logger;
13import org.apache.logging.log4j.LogManager;
14
15import com.framsticks.util.FramsticksException;
16
17public abstract class AbstractJoinable implements Joinable {
18
19        private static final Logger log = LogManager.getLogger(AbstractJoinable.class);
20
21        protected final Set<JoinableParent> owners = new HashSet<JoinableParent>();
22        protected final Set<JoinableParent> joinableListeners = new HashSet<JoinableParent>();
23
24        protected static final Set<AbstractJoinable> joinablesRegistry = Collections.synchronizedSet(new HashSet<AbstractJoinable>());
25
26        protected JoinableState state = JoinableState.INITILIAZED;
27        protected JoinableParent parent;
28        protected Monitor monitor;
29
30        /**
31         *
32         */
33        public AbstractJoinable() {
34                joinablesRegistry.add(this);
35        }
36
37        public static void report() {
38                StringBuilder b = new StringBuilder();
39                synchronized (joinablesRegistry) {
40                        for (AbstractJoinable j : joinablesRegistry) {
41                                b.append("\n").append(j.getState()).append(" : ").append(j).append(" - ").append(j.getClass());
42                        }
43                }
44                log.debug("state: {}", b);
45        }
46
47        private synchronized boolean changeState(JoinableState state) {
48                if (this.state.ordinal() >= state.ordinal()) {
49                        return false;
50                }
51                if (this.state.ordinal() + 1 != state.ordinal()) {
52                        throw new FramsticksException().msg("failed to change state").arg("from", this.state).arg("to", state).arg("joinable", this);
53                }
54                this.state = state;
55
56                log.debug("{} is notifying {} parents", this, joinableListeners.size());
57
58                List<JoinableParent> parents = new LinkedList<>();
59                CollectionUtils.addAll(parents, joinableListeners.iterator());
60
61                for (JoinableParent p : parents) {
62                        if (p != null) {
63                                Dispatching.childChangedState(p, this, state);
64                        }
65                }
66                this.notifyAll();
67
68                report();
69
70                return true;
71        }
72
73
74
75        protected final boolean startJoinable() {
76                if (changeState(JoinableState.RUNNING)) {
77                        joinableStart();
78                        return true;
79                }
80                return false;
81        }
82
83        protected final boolean interruptJoinable() {
84                if (changeState(JoinableState.FINISHING)) {
85                        joinableInterrupt();
86                        return true;
87                }
88                return false;
89        }
90
91        protected final boolean finishJoinable() {
92                if (changeState(JoinableState.JOINABLE)) {
93                        joinableFinish();
94                        return true;
95                }
96                return false;
97        }
98
99
100
101        @Override
102        public final void join() throws InterruptedException {
103                synchronized (this) {
104                        if (this.state.equals(JoinableState.JOINED)) {
105                                return;
106                        }
107                        if (!this.state.equals(JoinableState.JOINABLE)) {
108                                throw new InterruptedException();
109                        }
110                }
111                joinableJoin();
112                synchronized (this) {
113                        this.state = JoinableState.JOINED;
114                }
115        }
116
117        protected final boolean proceedToState(JoinableState state) {
118                switch (state) {
119                        case RUNNING:
120                                return startJoinable();
121                        case FINISHING:
122                                return interruptJoinable();
123                        case JOINABLE:
124                                return finishJoinable();
125                        default: return false;
126                }
127        }
128
129        protected abstract void joinableStart();
130
131        protected abstract void joinableInterrupt();
132
133        protected abstract void joinableFinish();
134
135        protected abstract void joinableJoin() throws InterruptedException;
136
137        public final boolean use(@Nonnull JoinableParent owner) {
138                boolean start = false;
139                synchronized (this) {
140                        if (owners.contains(owner)) {
141                                throw new FramsticksException().msg("owner is already using that joinable").arg("joinable", this).arg("owner", owner);
142                        }
143                        start = owners.isEmpty();
144                        log.debug("{} is using {}", owner, this);
145                        owners.add(owner);
146                        joinableListeners.add(owner);
147                }
148                if (start) {
149                        assert monitor == null;
150                        monitor = owner.getMonitor();
151                        return this.startJoinable();
152                }
153                return false;
154        }
155
156        public final boolean drop(@Nonnull JoinableParent owner) {
157                boolean stop = false;
158                synchronized (this) {
159                        if (!owners.contains(owner)) {
160                                return false;
161                                // throw new FramsticksException().msg("object is not owning that joinable").arg("joinable", this).arg("object", owner);
162                        }
163                        // assert owners.containsKey(owner);
164                        log.debug("{} is droping {}", owner, this);
165                        owners.remove(owner);
166                        stop = owners.isEmpty();
167                }
168                if (stop) {
169                        Dispatching.dispatcherGuardedInvoke(this, new RunAt<Object>(ThrowExceptionHandler.getInstance()) {
170                                @Override
171                                protected void runAt() {
172                                        interruptJoinable();
173                                }
174                        });
175                        return true;
176                }
177                return stop;
178        }
179
180        /**
181         * @return the state
182         */
183        public JoinableState getState() {
184                return state;
185        }
186
187        protected boolean isInState(JoinableState state) {
188                return this.state.equals(state);
189        }
190
191        protected boolean isRunning() {
192                return state.equals(JoinableState.RUNNING);
193        }
194
195        @Override
196        public String toString() {
197                return getName();
198        }
199
200        // @Override
201        public Monitor getMonitor() {
202                return monitor;
203        }
204
205
206}
Note: See TracBrowser for help on using the repository browser.