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