1
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
2
|
// Copyright 2016 Leszek Koltunski leszek@koltunski.pl //
|
3
|
// //
|
4
|
// This file is part of Distorted. //
|
5
|
// //
|
6
|
// This library is free software; you can redistribute it and/or //
|
7
|
// modify it under the terms of the GNU Lesser General Public //
|
8
|
// License as published by the Free Software Foundation; either //
|
9
|
// version 2.1 of the License, or (at your option) any later version. //
|
10
|
// //
|
11
|
// This library is distributed in the hope that it will be useful, //
|
12
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of //
|
13
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU //
|
14
|
// Lesser General Public License for more details. //
|
15
|
// //
|
16
|
// You should have received a copy of the GNU Lesser General Public //
|
17
|
// License along with this library; if not, write to the Free Software //
|
18
|
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA //
|
19
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
20
|
|
21
|
package org.distorted.library.message;
|
22
|
|
23
|
import org.distorted.library.effect.Effect;
|
24
|
|
25
|
import java.util.Vector;
|
26
|
|
27
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
28
|
/**
|
29
|
* Not part of public API, do not document (public only because has to be used in Meshes)
|
30
|
*
|
31
|
* @y.exclude
|
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
|
}
|
46
|
|
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()
|
59
|
{
|
60
|
|
61
|
}
|
62
|
|
63
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
64
|
|
65
|
public static void startSending()
|
66
|
{
|
67
|
synchronized(mLock)
|
68
|
{
|
69
|
if( mThis==null )
|
70
|
{
|
71
|
mStartTime = System.currentTimeMillis();
|
72
|
mNumStarts++;
|
73
|
|
74
|
mList = new Vector<>();
|
75
|
mThis = new EffectMessageSender();
|
76
|
mThis.start();
|
77
|
}
|
78
|
}
|
79
|
}
|
80
|
|
81
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
82
|
|
83
|
public static void stopSending()
|
84
|
{
|
85
|
synchronized(mLock)
|
86
|
{
|
87
|
if( mThis!=null )
|
88
|
{
|
89
|
mStopTime = System.currentTimeMillis();
|
90
|
mNumStarts--;
|
91
|
|
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.size()>0 )
|
107
|
{
|
108
|
tmp = mList.remove(0);
|
109
|
tmp.mListener.effectFinished(tmp.mEffectID);
|
110
|
}
|
111
|
|
112
|
synchronized(mLock)
|
113
|
{
|
114
|
if (!mNotify)
|
115
|
{
|
116
|
try { mLock.wait(); }
|
117
|
catch(InterruptedException ignored) { }
|
118
|
}
|
119
|
mNotify = false;
|
120
|
}
|
121
|
}
|
122
|
|
123
|
mList.clear();
|
124
|
}
|
125
|
|
126
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
127
|
|
128
|
public static void newMessage(Effect effect)
|
129
|
{
|
130
|
int numListeners = effect.getNumListeners();
|
131
|
|
132
|
if( numListeners>0 )
|
133
|
{
|
134
|
long id = effect.getID();
|
135
|
|
136
|
for(int i=0; i<numListeners; i++)
|
137
|
{
|
138
|
EffectListener listener = effect.removeFirstListener();
|
139
|
Message msg = new Message(listener,id);
|
140
|
mList.add(msg);
|
141
|
}
|
142
|
|
143
|
synchronized(mLock)
|
144
|
{
|
145
|
mNotify = true;
|
146
|
mLock.notify();
|
147
|
}
|
148
|
}
|
149
|
}
|
150
|
|
151
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
152
|
|
153
|
public static boolean isRunning()
|
154
|
{
|
155
|
return mThis!=null;
|
156
|
}
|
157
|
|
158
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
159
|
|
160
|
public static void restartThread()
|
161
|
{
|
162
|
synchronized(mLock)
|
163
|
{
|
164
|
if( mThis==null )
|
165
|
{
|
166
|
if( mList==null ) mList = new Vector<>();
|
167
|
mThis = new EffectMessageSender();
|
168
|
mThis.start();
|
169
|
}
|
170
|
}
|
171
|
}
|
172
|
|
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;
|
179
|
}
|
180
|
}
|