Project

General

Profile

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

library / src / main / java / org / distorted / library / message / EffectMessageSender.java @ c337dd1c

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.Vector;
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 mLock        = new Object();
47
  private static Vector<Message> mList     = null;
48
  private static EffectMessageSender mThis = null;
49
  private static volatile boolean mNotify  = false;
50
  private static int mNumStarts            = 0;
51

    
52
  private static long mStartTime, mStopTime;
53

    
54
///////////////////////////////////////////////////////////////////////////////////////////////////
55
   
56
  private EffectMessageSender() 
57
    {
58

    
59
    }
60

    
61
///////////////////////////////////////////////////////////////////////////////////////////////////
62

    
63
  public static void startSending()
64
    {
65
    synchronized(mLock)
66
      {
67
      mStartTime = System.currentTimeMillis();
68
      mNumStarts++;
69

    
70
      if( mThis==null )
71
        {
72
        mList = new Vector<>();
73
        mThis = new EffectMessageSender();
74
        mThis.start();
75
        }
76
      }
77
    }
78

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

    
88
      if( mThis!=null )
89
        {
90
        mThis=null;
91
        mLock.notify();
92
        }
93
      }
94
    }
95
  
96
///////////////////////////////////////////////////////////////////////////////////////////////////
97
  
98
  public void run()
99
    {
100
    Message tmp;  
101
     
102
    while(mThis!=null)
103
      {
104
      while( mList.size()>0 )
105
        {
106
        tmp = mList.remove(0);
107
        tmp.mListener.effectFinished(tmp.mEffectID);
108
        }
109

    
110
      synchronized(mLock)
111
        {
112
        if (!mNotify)
113
          {
114
          try  { mLock.wait(); }
115
          catch(InterruptedException ignored) { }
116
          }
117
        mNotify = false;
118
        }
119
      }
120

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

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

    
134
      for(int i=0; i<numListeners; i++)
135
        {
136
        EffectListener listener = effect.removeFirstListener();
137
        Message msg = new Message(listener,id);
138
        mList.add(msg);
139
        }
140

    
141
      synchronized(mLock)
142
        {
143
        mNotify = true;
144
        mLock.notify();
145
        }
146
      }
147
    }
148

    
149
///////////////////////////////////////////////////////////////////////////////////////////////////
150

    
151
  public static boolean isRunning()
152
    {
153
    return mThis!=null;
154
    }
155

    
156
///////////////////////////////////////////////////////////////////////////////////////////////////
157

    
158
  public static void restartThread()
159
    {
160
    synchronized(mLock)
161
      {
162
      if( mThis==null )
163
        {
164
        if( mList==null ) mList = new Vector<>();
165
        mThis = new EffectMessageSender();
166
        mThis.start();
167
        }
168
      }
169
    }
170

    
171
///////////////////////////////////////////////////////////////////////////////////////////////////
172

    
173
  public static String reportState()
174
    {
175
    return "running "+(mThis!=null)+" notify="+mNotify+" elements="+mList.size()+
176
           " start="+mStartTime+" stop="+mStopTime+" numStarts="+mNumStarts;
177
    }
178
  }
(2-2/2)