Project

General

Profile

« Previous | Next » 

Revision 33a4e28c

Added by Leszek Koltunski 3 days ago

message

View differences:

src/main/java/org/distorted/library/message/EffectListener.kt
18 18
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA                //
19 19
///////////////////////////////////////////////////////////////////////////////////////////////////
20 20

  
21
package org.distorted.library.message;
21
package org.distorted.library.message
22 22

  
23 23
///////////////////////////////////////////////////////////////////////////////////////////////////
24 24

  
25
import org.distorted.library.effect.Effect;
26

  
27 25
/**
28 26
 * This interface lets users of the DistortedLibrary library get notified when a given effect finishes.
29
 * To receive the notifications, we first have to register with a call to {@link Effect#notifyWhenFinished(EffectListener)}.
27
 * To receive the notifications, we first have to register with a call to [Effect.notifyWhenFinished].
30 28
 */
29
interface EffectListener
30
{
31
    /**
32
     * Gets called when Effect 'effectID' finishes execution (i.e. the Dynamic inside it reaches its final point).
33
     *
34
     * @param effectID ID of the finished effect, as returned by [org.distorted.library.effect.Effect.getID]
35
     */
36
    fun effectFinished(effectID: Long)
37
}
31 38

  
32
public interface EffectListener 
33
  {
34
/**
35
 * Gets called when Effect 'effectID' finishes execution (i.e. the Dynamic inside it reaches its final point).
36
 * 
37
 * @param effectID ID of the finished effect, as returned by {@link org.distorted.library.effect.Effect#getID() }
38
 */
39
   
40
  void effectFinished(final long effectID);
41
  }
42

  
43
///////////////////////////////////////////////////////////////////////////////////////////////////
src/main/java/org/distorted/library/message/EffectMessageSender.kt
18 18
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA                //
19 19
///////////////////////////////////////////////////////////////////////////////////////////////////
20 20

  
21
package org.distorted.library.message;
21
package org.distorted.library.message
22 22

  
23
import org.distorted.library.effect.Effect;
24

  
25
import java.util.Vector;
23
import org.distorted.library.effect.Effect
24
import java.util.Vector
25
import kotlin.concurrent.Volatile
26 26

  
27 27
///////////////////////////////////////////////////////////////////////////////////////////////////
28 28
/**
......
30 30
 *
31 31
 * @y.exclude
32 32
 */
33
public final class EffectMessageSender extends Thread
34
  {
35
  private static class Message
36
    {
37
    EffectListener mListener;
38
    long mEffectID;
39

  
40
    Message(EffectListener listener, long effectID)
41
      {
42
      mListener = listener;
43
      mEffectID = effectID;
44
      }
45
    }
33
class EffectMessageSender
34
private constructor() : Thread()
35
{
36
    private class Message (var mListener: EffectListener, var mEffectID: Long)
46 37

  
47
  private static final Object mLock        = new Object();
48
  private static Vector<Message> mList     = null;
49
  private static EffectMessageSender mThis = null;
50
  private static volatile boolean mNotify  = false;
51

  
52
  // debug only, to be removed later
53
  private static int mNumStarts = 0;
54
  private static long mStartTime, mStopTime;
55

  
56
///////////////////////////////////////////////////////////////////////////////////////////////////
57
   
58
  private EffectMessageSender() 
38
    override fun run()
59 39
    {
40
        var tmp: Message
60 41

  
61
    }
62

  
63
///////////////////////////////////////////////////////////////////////////////////////////////////
64

  
65
  public static void startSending()
66
    {
67
    synchronized(mLock)
68
      {
69
      if( mThis==null )
42
        while (mThis!=null)
70 43
        {
71
        mStartTime = System.currentTimeMillis();
72
        mNumStarts++;
73

  
74
        mList = new Vector<>();
75
        mThis = new EffectMessageSender();
76
        mThis.start();
44
            while (!mList!!.isEmpty())
45
            {
46
                tmp = mList!!.removeAt(0)
47
                tmp.mListener.effectFinished(tmp.mEffectID)
48
            }
49

  
50
            synchronized(mLock)
51
            {
52
                if (!mNotify)
53
                {
54
                    try { mLock.wait() }
55
                    catch (ignored: InterruptedException) { }
56
                }
57
                mNotify = false
58
            }
77 59
        }
78
      }
60

  
61
        mList!!.clear()
79 62
    }
80 63

  
81
///////////////////////////////////////////////////////////////////////////////////////////////////
82
  
83
  public static void stopSending()
64
    companion object
84 65
    {
85
    synchronized(mLock)
86
      {
87
      if( mThis!=null )
88
        {
89
        mStopTime = System.currentTimeMillis();
90
        mNumStarts--;
66
        private val mLock = Object()
67
        private var mList: Vector<Message>? = null
68
        private var mThis: EffectMessageSender? = null
91 69

  
92
        mThis=null;
93
        mLock.notify();
94
        }
95
      }
96
    }
97
  
98
///////////////////////////////////////////////////////////////////////////////////////////////////
99
  
100
  public void run()
101
    {
102
    Message tmp;  
103
     
104
    while(mThis!=null)
105
      {
106
      while( !mList.isEmpty() )
107
        {
108
        tmp = mList.remove(0);
109
        tmp.mListener.effectFinished(tmp.mEffectID);
110
        }
70
        @Volatile
71
        private var mNotify = false
111 72

  
112
      synchronized(mLock)
73
        // debug only, to be removed later
74
        private var mNumStarts       = 0
75
        private var mStartTime: Long = 0
76
        private var mStopTime: Long  = 0
77

  
78
        @JvmStatic fun startSending()
113 79
        {
114
        if (!mNotify)
115
          {
116
          try  { mLock.wait(); }
117
          catch(InterruptedException ignored) { }
118
          }
119
        mNotify = false;
80
            synchronized(mLock)
81
            {
82
                if (mThis==null)
83
                {
84
                    mStartTime = System.currentTimeMillis()
85
                    mNumStarts++
86

  
87
                    mList = Vector()
88
                    mThis = EffectMessageSender()
89
                    mThis!!.start()
90
                }
91
            }
120 92
        }
121
      }
122

  
123
    mList.clear();
124
    }
125
  
126
///////////////////////////////////////////////////////////////////////////////////////////////////
127
        
128
  public static void newMessage(Effect effect)
129
    {
130
    int numListeners = effect.getNumListeners();
131 93

  
132
    if( numListeners>0 )
133
      {
134
      long id = effect.getID();
135

  
136
      for(int i=0; i<numListeners; i++)
94
        @JvmStatic fun stopSending()
137 95
        {
138
        EffectListener listener = effect.removeFirstListener();
139
        Message msg = new Message(listener,id);
140
        mList.add(msg);
96
            synchronized(mLock)
97
            {
98
                if (mThis!=null)
99
                {
100
                    mStopTime = System.currentTimeMillis()
101
                    mNumStarts--
102

  
103
                    mThis = null
104
                    mLock.notify()
105
                }
106
            }
141 107
        }
142 108

  
143
      synchronized(mLock)
109
        @JvmStatic fun newMessage(effect: Effect)
144 110
        {
145
        mNotify = true;
146
        mLock.notify();
111
            val numListeners = effect.numListeners
112

  
113
            if (numListeners>0)
114
            {
115
                val id = effect.id
116

  
117
                for (i in 0 until numListeners)
118
                {
119
                    val listener = effect.removeFirstListener()
120
                    val msg = Message(listener, id)
121
                    mList!!.add(msg)
122
                }
123

  
124
                synchronized(mLock)
125
                {
126
                    mNotify = true
127
                    mLock.notify()
128
                }
129
            }
147 130
        }
148
      }
149
    }
150

  
151
///////////////////////////////////////////////////////////////////////////////////////////////////
152

  
153
  public static boolean isRunning()
154
    {
155
    return mThis!=null;
156
    }
157 131

  
158
///////////////////////////////////////////////////////////////////////////////////////////////////
132
        @JvmStatic val isRunning: Boolean
133
            get() = mThis!=null
159 134

  
160
  public static void restartThread()
161
    {
162
    synchronized(mLock)
163
      {
164
      if( mThis==null )
135
        @JvmStatic fun restartThread()
165 136
        {
166
        if( mList==null ) mList = new Vector<>();
167
        mThis = new EffectMessageSender();
168
        mThis.start();
137
            synchronized(mLock)
138
            {
139
                if (mThis==null)
140
                {
141
                    if (mList==null) mList = Vector()
142
                    mThis = EffectMessageSender()
143
                    mThis!!.start()
144
                }
145
            }
169 146
        }
170
      }
171
    }
172 147

  
173
///////////////////////////////////////////////////////////////////////////////////////////////////
174

  
175
  public static String reportState()
176
    {
177
    return "running "+(mThis!=null)+" notify="+mNotify+" elements="+mList.size()+
178
           " start="+mStartTime+" stop="+mStopTime+" numStarts="+mNumStarts;
148
        @JvmStatic fun reportState(): String
149
        {
150
            return "running "+(mThis!=null)+" notify="+mNotify+" elements="+mList!!.size+
151
                    " start="+mStartTime+" stop="+mStopTime+" numStarts="+mNumStarts
152
        }
179 153
    }
180
  }
154
}

Also available in: Unified diff