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