source: java/main/src/main/java/com/framsticks/util/dispatching/AbstractJoinable.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: 4.5 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                        assert !owners.contains(owner);
140                        start = owners.isEmpty();
141                        log.debug(owner + " is using " + this);
142                        owners.add(owner);
143                        joinableListeners.add(owner);
144                }
145                if (start) {
146                        assert monitor == null;
147                        monitor = owner.getMonitor();
148                        return this.start();
149                }
150                return false;
151        }
152
153        public final boolean drop(@Nonnull JoinableParent owner) {
154                boolean stop = false;
155                synchronized (this) {
156                        if (!owners.contains(owner)) {
157                                return false;
158                                // throw new FramsticksException().msg("object is not owning that joinable").arg("joinable", this).arg("object", owner);
159                        }
160                        // assert owners.containsKey(owner);
161                        log.debug(owner + " is droping " + this);
162                        owners.remove(owner);
163                        stop = owners.isEmpty();
164                }
165                if (stop) {
166                        Dispatching.dispatcherGuardedInvoke(this, new RunAt<Object>() {
167                                @Override
168                                public void run() {
169                                        interrupt();
170                                }
171                        });
172                        return true;
173                }
174                return stop;
175        }
176
177        /**
178         * @return the state
179         */
180        public JoinableState getState() {
181                return state;
182        }
183
184        protected boolean isInState(JoinableState state) {
185                return this.state.equals(state);
186        }
187
188        protected boolean isRunning() {
189                return state.equals(JoinableState.RUNNING);
190        }
191
192        @Override
193        public String toString() {
194                return getName();
195        }
196
197        // @Override
198        public Monitor getMonitor() {
199                return monitor;
200        }
201
202
203}
Note: See TracBrowser for help on using the repository browser.