Project

General

Profile

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

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

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

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

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

    
60
    }
61

    
62
///////////////////////////////////////////////////////////////////////////////////////////////////
63

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

    
73
        mList = new Vector<>();
74
        mThis = new EffectMessageSender();
75
        mThis.start();
76
        }
77
      }
78
    }
79

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

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

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

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

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

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

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

    
150
///////////////////////////////////////////////////////////////////////////////////////////////////
151

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

    
157
///////////////////////////////////////////////////////////////////////////////////////////////////
158

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

    
172
///////////////////////////////////////////////////////////////////////////////////////////////////
173

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