source: java/main/src/main/java/com/framsticks/util/dispatching/AbstractJoinable.java @ 88

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

HIGHLIGHTS:

  • loading f0 schema with XmlLoader?
  • use XmlLoader? to load configuration
  • introduce unified fork-join model of various entities

(Instances, Connections, GUI Frames, etc.),
all those entities clean up gracefully on
shutdown, which may be initialized by user
or by some entity

  • basing on above, simplify several organizing classes

(Observer, main class)

(to host native frams server process from Java level)

CHANGELOG:
Remove redundant Observer class.

Clean up in AbstractJoinable?.

Update ExternalProcess? class to changes in joining model.

Another sweep through code with FindBugs?.

Find bug with not joining RemoteInstance?.

Joining almost works.

Much improved joining model.

More improvement to joining model.

Add logging messages around joinable operations.

Rename methods in AbstractJoinable?.

Improve Joinable.

Rewrite of entity structure.

More simplifications with entities.

Further improve joinables.

Let Frame compose from JFrame instead of inheriting.

Add join classes.

Improvements of closing.

Add Builder interface.

Add FramsServerTest?.xml

FramsServer? may be configured through xml.

Make Framsticks main class an Observer of Entities.

Make Observer a generic type.

Remove variables regarding to removed endpoint.

Simplify observer (remove endpoints).

More changes to Observer and Endpoint.

Minor improvements.

Add OutputListener? to ExternalProcess?.

Improve testing of ExternalProcess?.

Add ExternalProcess? runner.

Rename the Program class to Framsticks.

Migrate Program to use XmlLoader? configuration.

First steps with configuration using XmlLoader?.

Fix several bugs.

Move all f0 classes to apriopriate package.

XmlLoader? is able to load Schema.

XmlLoader? is loading classes and props.

Add GroupBuilder?.

File size: 4.2 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
16
17
18public abstract class AbstractJoinable implements Joinable {
19        private static final Logger log = Logger.getLogger(AbstractJoinable.class);
20
21
22        protected final Set<JoinableParent> owners = new HashSet<JoinableParent>();
23        protected final Set<JoinableParent> joinableListeners = new HashSet<JoinableParent>();
24
25        protected static final Set<AbstractJoinable> joinablesRegistry = Collections.synchronizedSet(new HashSet<AbstractJoinable>());
26
27        JoinableState state = JoinableState.INITILIAZED;
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                synchronized (this) {
56                        log.debug(this + " is notifying " + joinableListeners.size() + " parents");
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                }
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                        return this.start();
147                }
148                return false;
149        }
150
151        public final boolean drop(@Nonnull JoinableParent owner) {
152                boolean stop = false;
153                synchronized (this) {
154                        if (!owners.contains(owner)) {
155                                return false;
156                                // throw new FramsticksException().msg("object is not owning that joinable").arg("joinable", this).arg("object", owner);
157                        }
158                        // assert owners.containsKey(owner);
159                        log.debug(owner + " is droping " + this);
160                        owners.remove(owner);
161                        stop = owners.isEmpty();
162                }
163                if (stop) {
164                        Dispatching.dispatcherGuardedInvoke(this, new RunAt<Object>() {
165                                @Override
166                                public void run() {
167                                        interrupt();
168                                }
169                        });
170                        return true;
171                }
172                return stop;
173        }
174
175        /**
176         * @return the state
177         */
178        public JoinableState getState() {
179                return state;
180        }
181
182        protected boolean isInState(JoinableState state) {
183                return this.state.equals(state);
184        }
185
186
187}
Note: See TracBrowser for help on using the repository browser.