source: java/main/src/main/java/com/framsticks/util/dispatching/JoinableCollection.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.2 KB
Line 
1package com.framsticks.util.dispatching;
2
3import java.util.AbstractCollection;
4import java.util.Collection;
5import java.util.Collections;
6import java.util.HashSet;
7import java.util.Iterator;
8import java.util.Set;
9
10import com.framsticks.params.annotations.AutoAppendAnnotation;
11import com.framsticks.params.annotations.FramsClassAnnotation;
12import com.framsticks.util.FramsticksException;
13import com.framsticks.util.Misc;
14
15
16@FramsClassAnnotation
17public class JoinableCollection<T extends Joinable> extends AbstractJoinable implements JoinableParent, Iterable<T> {
18
19        protected final Set<T> joinables = new HashSet<T>();
20
21        protected boolean finishIfOne;
22
23        protected String observableName;
24
25        public JoinableCollection() {
26                this(false);
27        }
28
29        public JoinableCollection(boolean finishIfOne) {
30                this.finishIfOne = finishIfOne;
31        }
32
33        @AutoAppendAnnotation
34        public synchronized void add(T joinable) {
35                if (this.state.ordinal() > JoinableState.RUNNING.ordinal()) {
36                        throw new FramsticksException().msg("failed to add joinable - collection is passed running state").arg("joinable", joinable).arg("collection", this);
37                }
38
39                if (joinables.contains(joinable)) {
40                        throw new FramsticksException().msg("joinable is already observed").arg("joinable", joinable).arg("in", this);
41                }
42                joinables.add(joinable);
43
44                if (this.state.equals(JoinableState.RUNNING)) {
45                        Dispatching.use(joinable, this);
46                }
47        }
48
49        public synchronized void remove(T joinable) {
50                if (this.state.ordinal() > JoinableState.RUNNING.ordinal()) {
51                        throw new FramsticksException().msg("failed to remote joinable - collection is passed running state").arg("joinable", joinable).arg("collection", this);
52                }
53                if (!joinables.contains(joinable)) {
54                        throw new FramsticksException().msg("joinable is not observed").arg("joinable", joinable).arg("in", this);
55                }
56
57                joinables.remove(joinable);
58
59                if (this.state.equals(JoinableState.RUNNING)) {
60                        Dispatching.drop(joinable, this);
61                }
62        }
63
64        @Override
65        protected void joinableStart() {
66                for (T j : joinables) {
67                        Dispatching.use(j, this);
68                }
69        }
70
71        @Override
72        protected void joinableInterrupt() {
73                if (joinables.isEmpty()) {
74                        finish();
75                        return;
76                }
77
78                for (T j : joinables) {
79                        Dispatching.drop(j, this);
80                }
81        }
82
83        @Override
84        protected void joinableFinish() {
85        }
86
87        @Override
88        protected void joinableJoin() throws InterruptedException {
89                for (T j : joinables) {
90                        Dispatching.join(j);
91                }
92        }
93
94        protected JoinableState getNextState() {
95                if (joinables.isEmpty()) {
96                        return state;
97                }
98                JoinableState result = finishIfOne ? JoinableState.INITILIAZED : JoinableState.JOINED;
99                for (Joinable j : joinables) {
100                        JoinableState s = j.getState();
101                        if (finishIfOne) {
102                                if (s.ordinal() > result.ordinal()) {
103                                        result = s;
104                                }
105                        } else {
106                                if (s.ordinal() < result.ordinal()) {
107                                        result = s;
108                                }
109                        }
110                }
111                return result;
112        }
113
114        @Override
115        public void childChangedState(Joinable joinable, JoinableState state) {
116                proceedToState(getNextState());
117        }
118
119        @Override
120        public Iterator<T> iterator() {
121                return Collections.unmodifiableSet(joinables).iterator();
122        }
123
124        @Override
125        public String toString() {
126                return Misc.returnNotNull(observableName, "collection");
127        }
128
129        /**
130         * @param observableName the observableName to set
131         */
132        public JoinableCollection<T> setObservableName(String observableName) {
133                this.observableName = observableName;
134                return this;
135        }
136
137        public T get(String name) {
138                for (T j : joinables) {
139                        if (j.getName().equals(name)) {
140                                return j;
141                        }
142                }
143                return null;
144        }
145
146        public int size() {
147                return joinables.size();
148        }
149
150        public boolean contains(T joinable) {
151                return joinables.contains(joinable);
152        }
153
154        @Override
155        public String getName() {
156                return observableName;
157        }
158
159        public Collection<T> asCollection() {
160                return new AbstractCollection<T>() {
161
162                        @Override
163                        public Iterator<T> iterator() {
164                                return JoinableCollection.this.iterator();
165                        }
166
167                        @Override
168                        public int size() {
169                                return JoinableCollection.this.size();
170                        }
171
172                        @Override
173                        public boolean add(T joinable) {
174                                JoinableCollection.this.add(joinable);
175                                return true;
176                        }
177
178                        @SuppressWarnings("unchecked")
179                        @Override
180                        public boolean remove(Object joinable) {
181                                JoinableCollection.this.remove((T) joinable);
182                                return true;
183                        }
184                };
185        }
186
187}
Note: See TracBrowser for help on using the repository browser.