source: java/main/src/main/java/com/framsticks/util/dispatching/JoinableCollection.java @ 96

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

HIGHLIGHTS:

  • cleanup Instance management
    • extract Instance interface
    • extract Instance common algorithms to InstanceUtils?
  • fix closing issues: Ctrl+C or window close button

properly shutdown whole program

by Java Framsticks framework

  • fix parsing and printing of all request types
  • hide exception passing in special handle method of closures
    • substantially improve readability of closures
    • basically enable use of exception in asynchronous closures

(thrown exception is transported back to the caller)

  • implement call request on both sides

CHANGELOG:
Further improve calling.

Improve instance calling.

Calling is working on both sides.

Improve exception handling in testing.

Waiters do not supercede other apllication exception being thrown.

Finished parsing and printing of all request types (with tests).

Move implementation and tests of request parsing to Request.

Add tests for Requests.

Improve waits in asynchronours tests.

Extract more algorithms to InstanceUtils?.

Extract Instance.resolve to InstanceUtils?.

Improve naming.

Improve passing exception in InstanceClient?.

Hide calling of passed functor in StateCallback?.

Hide Exception passing in asynchronous closures.

Hide exception passing in Future.

Make ResponseCallback? an abstract class.

Make Future an abstract class.

Minor change.

Move getPath to Path.to()

Move bindAccess to InstanceUtils?.

Extract common things to InstanceUtils?.

Fix synchronization bug in Connection.

Move resolve to InstanceUtils?.

Allow names of Joinable to be dynamic.

Add support for set request server side.

More fixes in communication.

Fix issues with parsing in connection.

Cut new line characters when reading.

More improvements.

Migrate closures to FramsticksException?.

Several changes.

Extract resolveAndFetch to InstanceUtils? algorithms.

Test resolving and fetching.

More fixes with function signature deduction.

Do not print default values in SimpleAbstractAccess?.

Add test of FramsClass? printing.

Improve FramsticksException? messages.

Add explicit dispatcher synchronization feature.

Rework assertions in tests.

Previous solution was not generic enough.

Allow addition of joinables to collection after start.

Extract SimulatorInstance? from RemoteInstance?.

Remove PrivateJoinableCollection?.

Improve connections.

Move shutdown hook to inside the Monitor.

It should work in TestNG tests, but it seems that
hooks are not called.

In ServerTest? client connects to testing server.

Move socket initialization to receiver thread.

Add proper closing on Ctrl+C (don't use signals).

Fix bugs with server accepting connections.

Merge Entity into Joinable.

Reworking ServerInstance?.

Extract more algorithm to InstanceUtils?.

Extract some common functionality from AbstractInstance?.

Functions were placed in InstanceUtils?.

Hide registry of Instance.

Use ValueParam? in Instance interface.

Minor change.

Extract Instance interface.

Old Instance is now AbstractInstance?.

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