[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; |
---|
| 12 | import org.apache.log4j.Logger; |
---|
| 13 | |
---|
| 14 | import com.framsticks.util.FramsticksException; |
---|
| 15 | |
---|
| 16 | |
---|
| 17 | |
---|
| 18 | public 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 | } |
---|