Project

General

Profile

Download (5.71 KB) Statistics
| Branch: | Revision:

library / src / main / java / org / distorted / library / message / EffectMessageSender.java @ 2f40484b

1
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2016 Leszek Koltunski                                                               //
3
//                                                                                               //
4
// This file is part of Distorted.                                                               //
5
//                                                                                               //
6
// Distorted is free software: you can redistribute it and/or modify                             //
7
// it under the terms of the GNU General Public License as published by                          //
8
// the Free Software Foundation, either version 2 of the License, or                             //
9
// (at your option) any later version.                                                           //
10
//                                                                                               //
11
// Distorted 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                                 //
14
// GNU General Public License for more details.                                                  //
15
//                                                                                               //
16
// You should have received a copy of the GNU General Public License                             //
17
// along with Distorted.  If not, see <http://www.gnu.org/licenses/>.                            //
18
///////////////////////////////////////////////////////////////////////////////////////////////////
19

    
20
package org.distorted.library.message;
21

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

    
24
import java.util.ArrayList;
25

    
26
///////////////////////////////////////////////////////////////////////////////////////////////////
27
/**
28
 * Not part of public API, do not document (public only because has to be used in Meshes)
29
 *
30
 * @y.exclude
31
 */
32
public final class EffectMessageSender extends Thread
33
  {
34
  private static class Message
35
    {
36
    EffectListener mListener;
37
    long mEffectID;
38

    
39
    Message(EffectListener listener, long effectID)
40
      {
41
      mListener = listener;
42
      mEffectID = effectID;
43
      }
44
    }
45

    
46
  private static final Object mArrayLock   = new Object();
47
  private static final Object mLock        = new Object();
48
  private static ArrayList<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
      mStartTime = System.currentTimeMillis();
70
      mNumStarts++;
71

    
72
      if( mThis==null )
73
        {
74
        mList = new ArrayList<>();
75
        mThis = new EffectMessageSender();
76
        mThis.start();
77
        }
78
      }
79
    }
80

    
81
///////////////////////////////////////////////////////////////////////////////////////////////////
82
  
83
  public static void stopSending()
84
    {
85
    synchronized(mLock)
86
      {
87
      mStopTime = System.currentTimeMillis();
88
      mNumStarts--;
89

    
90
      if( mThis!=null )
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
      synchronized(mArrayLock)
107
        {
108
        int size = mList.size();
109

    
110
        for(int i=0; i<size; i++)
111
          {
112
          tmp = mList.remove(0);
113
          tmp.mListener.effectFinished(tmp.mEffectID);
114
          }
115
        }
116

    
117
      synchronized(mLock)
118
        {
119
        if (!mNotify)
120
          {
121
          try  { mLock.wait(); }
122
          catch(InterruptedException ignored) { }
123
          }
124
        mNotify = false;
125
        }
126
      }
127

    
128
    mList.clear();
129
    }
130
  
131
///////////////////////////////////////////////////////////////////////////////////////////////////
132
        
133
  public static void newMessage(Effect effect)
134
    {
135
    int numListeners = effect.getNumListeners();
136

    
137
    if( numListeners>0 )
138
      {
139
      long id = effect.getID();
140

    
141
      synchronized(mArrayLock)
142
        {
143
        for(int i=0; i<numListeners; i++)
144
          {
145
          EffectListener listener = effect.removeFirstListener();
146
          Message msg = new Message(listener,id);
147
          mList.add(msg);
148
          }
149
        }
150

    
151
      synchronized(mLock)
152
        {
153
        mNotify = true;
154
        mLock.notify();
155
        }
156
      }
157
    }
158

    
159
///////////////////////////////////////////////////////////////////////////////////////////////////
160

    
161
  public static boolean isRunning()
162
    {
163
    return mThis!=null;
164
    }
165

    
166
///////////////////////////////////////////////////////////////////////////////////////////////////
167

    
168
  public static void restartThread()
169
    {
170
    synchronized(mLock)
171
      {
172
      if( mThis==null )
173
        {
174
        if( mList==null ) mList = new ArrayList<>();
175
        mThis = new EffectMessageSender();
176
        mThis.start();
177
        }
178
      }
179
    }
180

    
181
///////////////////////////////////////////////////////////////////////////////////////////////////
182

    
183
  public static String reportState()
184
    {
185
    return "running "+(mThis!=null)+" notify="+mNotify+" elements="+mList.size()+
186
           " start="+mStartTime+" stop="+mStopTime+" numStarts="+mNumStarts;
187
    }
188
  }
(2-2/2)